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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 5 additions & 5 deletions
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

Lines changed: 3 additions & 3 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 16 additions & 7 deletions
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

Lines changed: 22 additions & 3 deletions
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

0 commit comments

Comments
 (0)