Skip to content

Commit 36316df

Browse files
authored
Rollup merge of rust-lang#124067 - RalfJung:weak-lang-items, r=davidtwco
weak lang items are not allowed to be #[track_caller] For instance the panic handler will be called via this import ```rust extern "Rust" { #[lang = "panic_impl"] fn panic_impl(pi: &PanicInfo<'_>) -> !; } ``` A `#[track_caller]` would add an extra argument and thus make this the wrong signature. The 2nd commit is a consistency rename; based on the docs [here](https://doc.rust-lang.org/unstable-book/language-features/lang-items.html) and [here](https://rustc-dev-guide.rust-lang.org/lang-items.html) I figured "lang item" is more widely used. (In the compiler output, "lang item" and "language item" seem to be pretty even.)
2 parents 72e8fb4 + 8b35be7 commit 36316df

37 files changed

+141
-78
lines changed

compiler/rustc_error_codes/src/error_codes/E0522.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ Erroneous code example:
66
#![feature(lang_items)]
77
88
#[lang = "cookie"]
9-
fn cookie() -> ! { // error: definition of an unknown language item: `cookie`
9+
fn cookie() -> ! { // error: definition of an unknown lang item: `cookie`
1010
loop {}
1111
}
1212
```

compiler/rustc_feature/src/builtin_attrs.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -798,7 +798,7 @@ pub const BUILTIN_ATTRIBUTES: &[BuiltinAttribute] = &[
798798
// ==========================================================================
799799
gated!(
800800
lang, Normal, template!(NameValueStr: "name"), DuplicatesOk, EncodeCrossCrate::No, lang_items,
801-
"language items are subject to change",
801+
"lang items are subject to change",
802802
),
803803
rustc_attr!(
804804
rustc_pass_by_value, Normal, template!(Word), ErrorFollowing,

compiler/rustc_hir/src/lang_items.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//! Defines language items.
1+
//! Defines lang items.
22
//!
33
//! Language items are items that represent concepts intrinsic to the language
44
//! itself. Examples are:
@@ -16,7 +16,7 @@ use rustc_macros::HashStable_Generic;
1616
use rustc_span::symbol::{kw, sym, Symbol};
1717
use rustc_span::Span;
1818

19-
/// All of the language items, defined or not.
19+
/// All of the lang items, defined or not.
2020
/// Defined lang items can come from the current crate or its dependencies.
2121
#[derive(HashStable_Generic, Debug)]
2222
pub struct LanguageItems {
@@ -57,7 +57,7 @@ macro_rules! language_item_table {
5757
) => {
5858

5959
enum_from_u32! {
60-
/// A representation of all the valid language items in Rust.
60+
/// A representation of all the valid lang items in Rust.
6161
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, Encodable, Decodable)]
6262
pub enum LangItem {
6363
$(
@@ -177,7 +177,7 @@ language_item_table! {
177177
CoerceUnsized, sym::coerce_unsized, coerce_unsized_trait, Target::Trait, GenericRequirement::Minimum(1);
178178
DispatchFromDyn, sym::dispatch_from_dyn, dispatch_from_dyn_trait, Target::Trait, GenericRequirement::Minimum(1);
179179

180-
// language items relating to transmutability
180+
// lang items relating to transmutability
181181
TransmuteOpts, sym::transmute_opts, transmute_opts, Target::Struct, GenericRequirement::Exact(0);
182182
TransmuteTrait, sym::transmute_trait, transmute_trait, Target::Trait, GenericRequirement::Exact(2);
183183

@@ -304,7 +304,7 @@ language_item_table! {
304304
OwnedBox, sym::owned_box, owned_box, Target::Struct, GenericRequirement::Minimum(1);
305305
GlobalAlloc, sym::global_alloc_ty, global_alloc_ty, Target::Struct, GenericRequirement::None;
306306

307-
// Experimental language item for Miri
307+
// Experimental lang item for Miri
308308
PtrUnique, sym::ptr_unique, ptr_unique, Target::Struct, GenericRequirement::Exact(1);
309309

310310
PhantomData, sym::phantom_data, phantom_data, Target::Struct, GenericRequirement::Exact(1);

compiler/rustc_hir_analysis/src/check/intrinsic.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -535,20 +535,20 @@ pub fn check_intrinsic_type(
535535

536536
sym::va_start | sym::va_end => match mk_va_list_ty(hir::Mutability::Mut) {
537537
Some((va_list_ref_ty, _)) => (0, 0, vec![va_list_ref_ty], Ty::new_unit(tcx)),
538-
None => bug!("`va_list` language item needed for C-variadic intrinsics"),
538+
None => bug!("`va_list` lang item needed for C-variadic intrinsics"),
539539
},
540540

541541
sym::va_copy => match mk_va_list_ty(hir::Mutability::Not) {
542542
Some((va_list_ref_ty, va_list_ty)) => {
543543
let va_list_ptr_ty = Ty::new_mut_ptr(tcx, va_list_ty);
544544
(0, 0, vec![va_list_ptr_ty, va_list_ref_ty], Ty::new_unit(tcx))
545545
}
546-
None => bug!("`va_list` language item needed for C-variadic intrinsics"),
546+
None => bug!("`va_list` lang item needed for C-variadic intrinsics"),
547547
},
548548

549549
sym::va_arg => match mk_va_list_ty(hir::Mutability::Mut) {
550550
Some((va_list_ref_ty, _)) => (1, 0, vec![va_list_ref_ty], param(0)),
551-
None => bug!("`va_list` language item needed for C-variadic intrinsics"),
551+
None => bug!("`va_list` lang item needed for C-variadic intrinsics"),
552552
},
553553

554554
sym::nontemporal_store => {

compiler/rustc_metadata/src/rmeta/decoder.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1208,7 +1208,7 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
12081208
tcx.arena.alloc_from_iter(self.root.stability_implications.decode(self))
12091209
}
12101210

1211-
/// Iterates over the language items in the given crate.
1211+
/// Iterates over the lang items in the given crate.
12121212
fn get_lang_items(self, tcx: TyCtxt<'tcx>) -> &'tcx [(DefId, LangItem)] {
12131213
tcx.arena.alloc_from_iter(
12141214
self.root

compiler/rustc_middle/src/middle/lang_items.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//! Detecting language items.
1+
//! Detecting lang items.
22
//!
33
//! Language items are items that represent concepts intrinsic to the language
44
//! itself. Examples are:

compiler/rustc_mir_transform/src/deduce_param_attrs.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ pub fn deduced_param_attrs<'tcx>(
160160
return &[];
161161
}
162162

163-
// If the Freeze language item isn't present, then don't bother.
163+
// If the Freeze lang item isn't present, then don't bother.
164164
if tcx.lang_items().freeze_trait().is_none() {
165165
return &[];
166166
}

compiler/rustc_mir_transform/src/lower_slice_len.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ impl<'tcx> MirPass<'tcx> for LowerSliceLenCalls {
2121
pub fn lower_slice_len_calls<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
2222
let language_items = tcx.lang_items();
2323
let Some(slice_len_fn_item_def_id) = language_items.slice_len_fn() else {
24-
// there is no language item to compare to :)
24+
// there is no lang item to compare to :)
2525
return;
2626
};
2727

compiler/rustc_passes/messages.ftl

+16-7
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,7 @@ passes_incorrect_meta_item = expected a quoted string literal
353353
passes_incorrect_meta_item_suggestion = consider surrounding this with quotes
354354
355355
passes_incorrect_target =
356-
`{$name}` language item must be applied to a {$kind} with {$at_least ->
356+
`{$name}` lang item must be applied to a {$kind} with {$at_least ->
357357
[true] at least {$num}
358358
*[false] {$num}
359359
} generic {$num ->
@@ -394,12 +394,21 @@ passes_invalid_macro_export_arguments = `{$name}` isn't a valid `#[macro_export]
394394
395395
passes_invalid_macro_export_arguments_too_many_items = `#[macro_export]` can only take 1 or 0 arguments
396396
397+
passes_lang_item_fn = {$name ->
398+
[panic_impl] `#[panic_handler]`
399+
*[other] `{$name}` lang item
400+
} function
401+
397402
passes_lang_item_fn_with_target_feature =
398-
`{$name}` language item function is not allowed to have `#[target_feature]`
399-
.label = `{$name}` language item function is not allowed to have `#[target_feature]`
403+
{passes_lang_item_fn} is not allowed to have `#[target_feature]`
404+
.label = {passes_lang_item_fn} is not allowed to have `#[target_feature]`
405+
406+
passes_lang_item_fn_with_track_caller =
407+
{passes_lang_item_fn} is not allowed to have `#[track_caller]`
408+
.label = {passes_lang_item_fn} is not allowed to have `#[target_feature]`
400409
401410
passes_lang_item_on_incorrect_target =
402-
`{$name}` language item must be applied to a {$expected_target}
411+
`{$name}` lang item must be applied to a {$expected_target}
403412
.label = attribute should be applied to a {$expected_target}, not a {$actual_target}
404413
405414
passes_layout_abi =
@@ -455,7 +464,7 @@ passes_missing_const_stab_attr =
455464
{$descr} has missing const stability attribute
456465
457466
passes_missing_lang_item =
458-
language item required, but not found: `{$name}`
467+
lang item required, but not found: `{$name}`
459468
.note = this can occur when a binary crate with `#![no_std]` is compiled for a target where `{$name}` is defined in the standard library
460469
.help = you may be able to compile for a target that doesn't need `{$name}`, specify a target with `--target` or in `.cargo/config`
461470
@@ -696,8 +705,8 @@ passes_unknown_feature =
696705
unknown feature `{$feature}`
697706
698707
passes_unknown_lang_item =
699-
definition of an unknown language item: `{$name}`
700-
.label = definition of unknown language item `{$name}`
708+
definition of an unknown lang item: `{$name}`
709+
.label = definition of unknown lang item `{$name}`
701710
702711
passes_unlabeled_cf_in_while_condition =
703712
`break` or `continue` with no label in the condition of a `while` loop

compiler/rustc_passes/src/check_attr.rs

+22-3
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ use rustc_data_structures::fx::FxHashMap;
1111
use rustc_errors::StashKey;
1212
use rustc_errors::{Applicability, DiagCtxt, IntoDiagArg, MultiSpan};
1313
use rustc_feature::{AttributeDuplicates, AttributeType, BuiltinAttribute, BUILTIN_ATTRIBUTE_MAP};
14-
use rustc_hir as hir;
1514
use rustc_hir::def_id::LocalModDefId;
1615
use rustc_hir::intravisit::{self, Visitor};
16+
use rustc_hir::{self as hir};
1717
use rustc_hir::{
1818
self, FnSig, ForeignItem, HirId, Item, ItemKind, TraitItem, CRATE_HIR_ID, CRATE_OWNER_ID,
1919
};
@@ -519,7 +519,26 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
519519
self.dcx().emit_err(errors::NakedTrackedCaller { attr_span });
520520
false
521521
}
522-
Target::Fn | Target::Method(..) | Target::ForeignFn | Target::Closure => true,
522+
Target::Fn => {
523+
// `#[track_caller]` is not valid on weak lang items because they are called via
524+
// `extern` declarations and `#[track_caller]` would alter their ABI.
525+
if let Some((lang_item, _)) = hir::lang_items::extract(attrs)
526+
&& let Some(item) = hir::LangItem::from_name(lang_item)
527+
&& item.is_weak()
528+
{
529+
let sig = self.tcx.hir_node(hir_id).fn_sig().unwrap();
530+
531+
self.dcx().emit_err(errors::LangItemWithTrackCaller {
532+
attr_span,
533+
name: lang_item,
534+
sig_span: sig.span,
535+
});
536+
false
537+
} else {
538+
true
539+
}
540+
}
541+
Target::Method(..) | Target::ForeignFn | Target::Closure => true,
523542
// FIXME(#80564): We permit struct fields, match arms and macro defs to have an
524543
// `#[track_caller]` attribute with just a lint, because we previously
525544
// erroneously allowed it and some crates used it accidentally, to be compatible
@@ -602,7 +621,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
602621
) -> bool {
603622
match target {
604623
Target::Fn => {
605-
// `#[target_feature]` is not allowed in language items.
624+
// `#[target_feature]` is not allowed in lang items.
606625
if let Some((lang_item, _)) = hir::lang_items::extract(attrs)
607626
// Calling functions with `#[target_feature]` is
608627
// not unsafe on WASM, see #84988

compiler/rustc_passes/src/errors.rs

+10
Original file line numberDiff line numberDiff line change
@@ -819,6 +819,16 @@ pub struct MissingLangItem {
819819
pub name: Symbol,
820820
}
821821

822+
#[derive(Diagnostic)]
823+
#[diag(passes_lang_item_fn_with_track_caller)]
824+
pub struct LangItemWithTrackCaller {
825+
#[primary_span]
826+
pub attr_span: Span,
827+
pub name: Symbol,
828+
#[label]
829+
pub sig_span: Span,
830+
}
831+
822832
#[derive(Diagnostic)]
823833
#[diag(passes_lang_item_fn_with_target_feature)]
824834
pub struct LangItemWithTargetFeature {

compiler/rustc_passes/src/lang_items.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//! Detecting language items.
1+
//! Detecting lang items.
22
//!
33
//! Language items are items that represent concepts intrinsic to the language
44
//! itself. Examples are:

compiler/rustc_passes/src/weak_lang_items.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use crate::errors::{
1414
};
1515

1616
/// Checks the crate for usage of weak lang items, returning a vector of all the
17-
/// language items required by this crate, but not defined yet.
17+
/// lang items required by this crate, but not defined yet.
1818
pub fn check_crate(tcx: TyCtxt<'_>, items: &mut lang_items::LanguageItems, krate: &ast::Crate) {
1919
// These are never called by user code, they're generated by the compiler.
2020
// They will never implicitly be added to the `missing` array unless we do

compiler/rustc_resolve/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1042,7 +1042,7 @@ pub struct Resolver<'a, 'tcx> {
10421042
block_map: NodeMap<Module<'a>>,
10431043
/// A fake module that contains no definition and no prelude. Used so that
10441044
/// some AST passes can generate identifiers that only resolve to local or
1045-
/// language items.
1045+
/// lang items.
10461046
empty_module: Module<'a>,
10471047
module_map: FxHashMap<DefId, Module<'a>>,
10481048
binding_parent_modules: FxHashMap<NameBinding<'a>, Module<'a>>,

tests/ui/assoc-lang-items.stderr

+8-8
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,26 @@
1-
error[E0522]: definition of an unknown language item: `dummy_lang_item_1`
1+
error[E0522]: definition of an unknown lang item: `dummy_lang_item_1`
22
--> $DIR/assoc-lang-items.rs:4:5
33
|
44
LL | #[lang = "dummy_lang_item_1"]
5-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ definition of unknown language item `dummy_lang_item_1`
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ definition of unknown lang item `dummy_lang_item_1`
66

7-
error[E0522]: definition of an unknown language item: `dummy_lang_item_2`
7+
error[E0522]: definition of an unknown lang item: `dummy_lang_item_2`
88
--> $DIR/assoc-lang-items.rs:7:5
99
|
1010
LL | #[lang = "dummy_lang_item_2"]
11-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ definition of unknown language item `dummy_lang_item_2`
11+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ definition of unknown lang item `dummy_lang_item_2`
1212

13-
error[E0522]: definition of an unknown language item: `dummy_lang_item_3`
13+
error[E0522]: definition of an unknown lang item: `dummy_lang_item_3`
1414
--> $DIR/assoc-lang-items.rs:10:5
1515
|
1616
LL | #[lang = "dummy_lang_item_3"]
17-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ definition of unknown language item `dummy_lang_item_3`
17+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ definition of unknown lang item `dummy_lang_item_3`
1818

19-
error[E0522]: definition of an unknown language item: `dummy_lang_item_4`
19+
error[E0522]: definition of an unknown lang item: `dummy_lang_item_4`
2020
--> $DIR/assoc-lang-items.rs:17:5
2121
|
2222
LL | #[lang = "dummy_lang_item_4"]
23-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ definition of unknown language item `dummy_lang_item_4`
23+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ definition of unknown lang item `dummy_lang_item_4`
2424

2525
error: aborting due to 4 previous errors
2626

tests/ui/error-codes/E0522.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
#[lang = "cookie"]
44
fn cookie() -> ! {
5-
//~^^ ERROR definition of an unknown language item: `cookie` [E0522]
5+
//~^^ ERROR definition of an unknown lang item: `cookie` [E0522]
66
loop {}
77
}
88

tests/ui/error-codes/E0522.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
error[E0522]: definition of an unknown language item: `cookie`
1+
error[E0522]: definition of an unknown lang item: `cookie`
22
--> $DIR/E0522.rs:3:1
33
|
44
LL | #[lang = "cookie"]
5-
| ^^^^^^^^^^^^^^^^^^ definition of unknown language item `cookie`
5+
| ^^^^^^^^^^^^^^^^^^ definition of unknown lang item `cookie`
66

77
error: aborting due to 1 previous error
88

tests/ui/error-codes/E0718.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#![feature(lang_items)]
22

33
// Box is expected to be a struct, so this will error.
4-
#[lang = "owned_box"] //~ ERROR language item must be applied to a struct
4+
#[lang = "owned_box"] //~ ERROR lang item must be applied to a struct
55
static X: u32 = 42;
66

77
fn main() {}

tests/ui/error-codes/E0718.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error[E0718]: `owned_box` language item must be applied to a struct
1+
error[E0718]: `owned_box` lang item must be applied to a struct
22
--> $DIR/E0718.rs:4:1
33
|
44
LL | #[lang = "owned_box"]
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
#[lang = "foo"] //~ ERROR language items are subject to change
2-
//~^ ERROR definition of an unknown language item: `foo`
1+
#[lang = "foo"] //~ ERROR lang items are subject to change
2+
//~^ ERROR definition of an unknown lang item: `foo`
33
trait Foo {}
44

55
fn main() {}

tests/ui/feature-gates/feature-gate-lang-items.stderr

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error[E0658]: language items are subject to change
1+
error[E0658]: lang items are subject to change
22
--> $DIR/feature-gate-lang-items.rs:1:1
33
|
44
LL | #[lang = "foo"]
@@ -7,11 +7,11 @@ LL | #[lang = "foo"]
77
= help: add `#![feature(lang_items)]` to the crate attributes to enable
88
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
99

10-
error[E0522]: definition of an unknown language item: `foo`
10+
error[E0522]: definition of an unknown lang item: `foo`
1111
--> $DIR/feature-gate-lang-items.rs:1:1
1212
|
1313
LL | #[lang = "foo"]
14-
| ^^^^^^^^^^^^^^^ definition of unknown language item `foo`
14+
| ^^^^^^^^^^^^^^^ definition of unknown lang item `foo`
1515

1616
error: aborting due to 2 previous errors
1717

tests/ui/lang-items/issue-83471.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@
55
#![no_core]
66

77
#[lang = "sized"]
8-
//~^ ERROR: language items are subject to change [E0658]
8+
//~^ ERROR: lang items are subject to change [E0658]
99
trait Sized {}
1010

1111
#[lang = "fn"]
12-
//~^ ERROR: language items are subject to change [E0658]
13-
//~| ERROR: `fn` language item must be applied to a trait with 1 generic argument
12+
//~^ ERROR: lang items are subject to change [E0658]
13+
//~| ERROR: `fn` lang item must be applied to a trait with 1 generic argument
1414
trait Fn {
1515
fn call(export_name);
1616
//~^ ERROR: expected type

0 commit comments

Comments
 (0)