1
Fork 0

weak lang items are not allowed to be #[track_caller]

This commit is contained in:
Ralf Jung 2024-04-17 12:17:29 +02:00
parent b7581490aa
commit d101971ab1
7 changed files with 70 additions and 7 deletions

View file

@ -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

View file

@ -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 {