From d101971ab10ebc95ad0664b704b730ce792d8fc5 Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Wed, 17 Apr 2024 12:17:29 +0200 Subject: [PATCH] weak lang items are not allowed to be #[track_caller] --- compiler/rustc_passes/messages.ftl | 13 +++++++++-- compiler/rustc_passes/src/check_attr.rs | 23 +++++++++++++++++-- compiler/rustc_passes/src/errors.rs | 10 ++++++++ .../panic-handler-with-target-feature.rs | 2 +- .../panic-handler-with-target-feature.stderr | 4 ++-- .../panic-handler-with-track-caller.rs | 14 +++++++++++ .../panic-handler-with-track-caller.stderr | 11 +++++++++ 7 files changed, 70 insertions(+), 7 deletions(-) create mode 100644 tests/ui/panic-handler/panic-handler-with-track-caller.rs create mode 100644 tests/ui/panic-handler/panic-handler-with-track-caller.stderr diff --git a/compiler/rustc_passes/messages.ftl b/compiler/rustc_passes/messages.ftl index 7fc523ffe0d..8cd744925f5 100644 --- a/compiler/rustc_passes/messages.ftl +++ b/compiler/rustc_passes/messages.ftl @@ -394,9 +394,18 @@ passes_invalid_macro_export_arguments = `{$name}` isn't a valid `#[macro_export] passes_invalid_macro_export_arguments_too_many_items = `#[macro_export]` can only take 1 or 0 arguments +passes_lang_item_fn = {$name -> + [panic_impl] `#[panic_handler]` + *[other] `{$name}` language item +} function + passes_lang_item_fn_with_target_feature = - `{$name}` language item function is not allowed to have `#[target_feature]` - .label = `{$name}` language item function is not allowed to have `#[target_feature]` + {passes_lang_item_fn} is not allowed to have `#[target_feature]` + .label = {passes_lang_item_fn} is not allowed to have `#[target_feature]` + +passes_lang_item_fn_with_track_caller = + {passes_lang_item_fn} is not allowed to have `#[track_caller]` + .label = {passes_lang_item_fn} is not allowed to have `#[target_feature]` passes_lang_item_on_incorrect_target = `{$name}` language item must be applied to a {$expected_target} diff --git a/compiler/rustc_passes/src/check_attr.rs b/compiler/rustc_passes/src/check_attr.rs index 1254ae8cfc8..ca41c5165b2 100644 --- a/compiler/rustc_passes/src/check_attr.rs +++ b/compiler/rustc_passes/src/check_attr.rs @@ -11,9 +11,9 @@ use rustc_data_structures::fx::FxHashMap; use rustc_errors::StashKey; use rustc_errors::{Applicability, DiagCtxt, IntoDiagArg, MultiSpan}; use rustc_feature::{AttributeDuplicates, AttributeType, BuiltinAttribute, BUILTIN_ATTRIBUTE_MAP}; -use rustc_hir as hir; use rustc_hir::def_id::LocalModDefId; use rustc_hir::intravisit::{self, Visitor}; +use rustc_hir::{self as hir}; use rustc_hir::{ self, FnSig, ForeignItem, HirId, Item, ItemKind, TraitItem, CRATE_HIR_ID, CRATE_OWNER_ID, }; @@ -519,7 +519,26 @@ impl<'tcx> CheckAttrVisitor<'tcx> { self.dcx().emit_err(errors::NakedTrackedCaller { attr_span }); false } - Target::Fn | Target::Method(..) | Target::ForeignFn | Target::Closure => true, + Target::Fn => { + // `#[track_caller]` is not valid on weak lang items because they are called via + // `extern` declarations and `#[track_caller]` would alter their ABI. + if let Some((lang_item, _)) = hir::lang_items::extract(attrs) + && let Some(item) = hir::LangItem::from_name(lang_item) + && item.is_weak() + { + let sig = self.tcx.hir_node(hir_id).fn_sig().unwrap(); + + self.dcx().emit_err(errors::LangItemWithTrackCaller { + attr_span, + name: lang_item, + sig_span: sig.span, + }); + false + } else { + true + } + } + Target::Method(..) | Target::ForeignFn | Target::Closure => true, // FIXME(#80564): We permit struct fields, match arms and macro defs to have an // `#[track_caller]` attribute with just a lint, because we previously // erroneously allowed it and some crates used it accidentally, to be compatible diff --git a/compiler/rustc_passes/src/errors.rs b/compiler/rustc_passes/src/errors.rs index 3f26ea4507d..19b5e5495d6 100644 --- a/compiler/rustc_passes/src/errors.rs +++ b/compiler/rustc_passes/src/errors.rs @@ -819,6 +819,16 @@ pub struct MissingLangItem { pub name: Symbol, } +#[derive(Diagnostic)] +#[diag(passes_lang_item_fn_with_track_caller)] +pub struct LangItemWithTrackCaller { + #[primary_span] + pub attr_span: Span, + pub name: Symbol, + #[label] + pub sig_span: Span, +} + #[derive(Diagnostic)] #[diag(passes_lang_item_fn_with_target_feature)] pub struct LangItemWithTargetFeature { diff --git a/tests/ui/panic-handler/panic-handler-with-target-feature.rs b/tests/ui/panic-handler/panic-handler-with-target-feature.rs index 3dfdd2847bf..8d5ea0703af 100644 --- a/tests/ui/panic-handler/panic-handler-with-target-feature.rs +++ b/tests/ui/panic-handler/panic-handler-with-target-feature.rs @@ -9,7 +9,7 @@ use core::panic::PanicInfo; #[panic_handler] #[target_feature(enable = "avx2")] -//~^ ERROR `panic_impl` language item function is not allowed to have `#[target_feature]` +//~^ ERROR `#[panic_handler]` function is not allowed to have `#[target_feature]` fn panic(info: &PanicInfo) -> ! { unimplemented!(); } diff --git a/tests/ui/panic-handler/panic-handler-with-target-feature.stderr b/tests/ui/panic-handler/panic-handler-with-target-feature.stderr index c38feab49c3..cb17da3a4ef 100644 --- a/tests/ui/panic-handler/panic-handler-with-target-feature.stderr +++ b/tests/ui/panic-handler/panic-handler-with-target-feature.stderr @@ -1,11 +1,11 @@ -error: `panic_impl` language item function is not allowed to have `#[target_feature]` +error: `#[panic_handler]` function is not allowed to have `#[target_feature]` --> $DIR/panic-handler-with-target-feature.rs:11:1 | LL | #[target_feature(enable = "avx2")] | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ LL | LL | fn panic(info: &PanicInfo) -> ! { - | ------------------------------- `panic_impl` language item function is not allowed to have `#[target_feature]` + | ------------------------------- `#[panic_handler]` function is not allowed to have `#[target_feature]` error: aborting due to 1 previous error diff --git a/tests/ui/panic-handler/panic-handler-with-track-caller.rs b/tests/ui/panic-handler/panic-handler-with-track-caller.rs new file mode 100644 index 00000000000..09c94139e16 --- /dev/null +++ b/tests/ui/panic-handler/panic-handler-with-track-caller.rs @@ -0,0 +1,14 @@ +//@ compile-flags:-C panic=abort +//@ only-x86_64 + +#![no_std] +#![no_main] + +use core::panic::PanicInfo; + +#[panic_handler] +#[track_caller] +//~^ ERROR `#[panic_handler]` function is not allowed to have `#[track_caller]` +fn panic(info: &PanicInfo) -> ! { + unimplemented!(); +} diff --git a/tests/ui/panic-handler/panic-handler-with-track-caller.stderr b/tests/ui/panic-handler/panic-handler-with-track-caller.stderr new file mode 100644 index 00000000000..9ed387fc8d1 --- /dev/null +++ b/tests/ui/panic-handler/panic-handler-with-track-caller.stderr @@ -0,0 +1,11 @@ +error: `#[panic_handler]` function is not allowed to have `#[track_caller]` + --> $DIR/panic-handler-with-track-caller.rs:10:1 + | +LL | #[track_caller] + | ^^^^^^^^^^^^^^^ +LL | +LL | fn panic(info: &PanicInfo) -> ! { + | ------------------------------- `#[panic_handler]` function is not allowed to have `#[target_feature]` + +error: aborting due to 1 previous error +