Skip to content

Commit e30e67d

Browse files
Port #[panic_handler] to the new attribute parsers
1 parent aa710d5 commit e30e67d

10 files changed

Lines changed: 25 additions & 64 deletions

File tree

compiler/rustc_attr_parsing/src/attributes/rustc_internal.rs

Lines changed: 10 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -633,29 +633,7 @@ impl<S: Stage> SingleAttributeParser<S> for LangParser {
633633
const PATH: &[Symbol] = &[sym::lang];
634634
const ATTRIBUTE_ORDER: AttributeOrder = AttributeOrder::KeepInnermost;
635635
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Error;
636-
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[
637-
Allow(Target::Trait),
638-
Allow(Target::Struct),
639-
Allow(Target::Enum),
640-
Allow(Target::Variant),
641-
Allow(Target::Union),
642-
Allow(Target::MacroDef),
643-
Allow(Target::TyAlias),
644-
Allow(Target::AssocTy),
645-
Allow(Target::AssocConst),
646-
Allow(Target::Fn),
647-
Allow(Target::ForeignFn),
648-
Allow(Target::Const),
649-
Allow(Target::Mod),
650-
Allow(Target::Impl { of_trait: false }),
651-
Allow(Target::Method(MethodKind::Inherent)),
652-
Allow(Target::Method(MethodKind::Trait { body: false })),
653-
Allow(Target::Method(MethodKind::Trait { body: true })),
654-
Allow(Target::Method(MethodKind::TraitImpl)),
655-
Allow(Target::Crate),
656-
Allow(Target::Static),
657-
Allow(Target::ForeignStatic),
658-
]);
636+
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(ALL_TARGETS); // Targets are checked per lang item in `rustc_passes`
659637
const TEMPLATE: AttributeTemplate = template!(NameValueStr: "name");
660638

661639
fn convert(cx: &mut AcceptContext<'_, '_, S>, args: &ArgParser) -> Option<AttributeKind> {
@@ -675,6 +653,15 @@ impl<S: Stage> SingleAttributeParser<S> for LangParser {
675653
}
676654
}
677655

656+
pub(crate) struct PanicHandlerParser;
657+
658+
impl<S: Stage> NoArgsAttributeParser<S> for PanicHandlerParser {
659+
const PATH: &[Symbol] = &[sym::panic_handler];
660+
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Error;
661+
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(ALL_TARGETS); // Targets are checked per lang item in `rustc_passes`
662+
const CREATE: fn(Span) -> AttributeKind = |_| AttributeKind::Lang(LangItem::PanicImpl);
663+
}
664+
678665
pub(crate) struct RustcHasIncoherentInherentImplsParser;
679666

680667
impl<S: Stage> NoArgsAttributeParser<S> for RustcHasIncoherentInherentImplsParser {

compiler/rustc_attr_parsing/src/context.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,7 @@ attribute_parsers!(
254254
Single<WithoutArgs<NoMangleParser>>,
255255
Single<WithoutArgs<NoStdParser>>,
256256
Single<WithoutArgs<NonExhaustiveParser>>,
257+
Single<WithoutArgs<PanicHandlerParser>>,
257258
Single<WithoutArgs<PanicRuntimeParser>>,
258259
Single<WithoutArgs<ParenSugarParser>>,
259260
Single<WithoutArgs<PassByValueParser>>,

compiler/rustc_codegen_ssa/src/codegen_attrs.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use rustc_hir::attrs::{
88
};
99
use rustc_hir::def::DefKind;
1010
use rustc_hir::def_id::{DefId, LOCAL_CRATE, LocalDefId};
11-
use rustc_hir::{self as hir, Attribute, find_attr, lang_items};
11+
use rustc_hir::{self as hir, Attribute, find_attr};
1212
use rustc_middle::middle::codegen_fn_attrs::{
1313
CodegenFnAttrFlags, CodegenFnAttrs, PatchableFunctionEntry, SanitizerFnAttrs,
1414
};
@@ -504,7 +504,7 @@ fn handle_lang_items(
504504
attrs: &[Attribute],
505505
codegen_fn_attrs: &mut CodegenFnAttrs,
506506
) {
507-
let lang_item = lang_items::extract_hir(attrs);
507+
let lang_item = find_attr!(attrs, AttributeKind::Lang(lang) => lang);
508508

509509
// Weak lang items have the same semantics as "std internal" symbols in the
510510
// sense that they're preserved through all our LTO passes and only

compiler/rustc_hir/src/attrs/encode_cross_crate.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ impl AttributeKind {
5050
Ignore { .. } => No,
5151
Inline(..) => No,
5252
InstructionSet(..) => No,
53-
Lang(..) => No,
53+
Lang(..) => Yes,
5454
Link(..) => No,
5555
LinkName { .. } => Yes, // Needed for rustdoc
5656
LinkOrdinal { .. } => No,

compiler/rustc_hir/src/lang_items.rs

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,12 @@
1010
use rustc_ast::ast;
1111
use rustc_data_structures::fx::FxIndexMap;
1212
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
13-
use rustc_hir::find_attr;
1413
use rustc_macros::{BlobDecodable, Encodable, HashStable_Generic, PrintAttribute};
1514
use rustc_span::{Span, Symbol, kw, sym};
1615

17-
use crate::attrs::{AttributeKind, PrintAttribute};
16+
use crate::attrs::PrintAttribute;
1817
use crate::def_id::DefId;
19-
use crate::{MethodKind, Target, hir};
18+
use crate::{MethodKind, Target};
2019

2120
/// All of the lang items, defined or not.
2221
/// Defined lang items can come from the current crate or its dependencies.
@@ -154,18 +153,8 @@ impl<CTX> HashStable<CTX> for LangItem {
154153

155154
/// Extracts the first `lang = "$name"` out of a list of attributes.
156155
/// The `#[panic_handler]` attribute is also extracted out when found.
157-
pub fn extract_hir(attrs: &[hir::Attribute]) -> Option<LangItem> {
158-
if let Some(lang) = find_attr!(attrs, AttributeKind::Lang(lang) => lang) {
159-
Some(*lang)
160-
} else if let Some(_) = attrs.iter().find(|attr| attr.has_name(sym::panic_handler)) {
161-
Some(LangItem::PanicImpl)
162-
} else {
163-
None
164-
}
165-
}
166-
167-
/// Extracts the first `lang = "$name"` out of a list of attributes.
168-
/// The `#[panic_handler]` attribute is also extracted out when found.
156+
///
157+
/// This function is used for `ast::Attribute`, for `hir::Attribute` use the `find_attr!` macro with `AttributeKind::Lang`
169158
pub fn extract_ast(attrs: &[ast::Attribute]) -> Option<(Symbol, Span)> {
170159
attrs.iter().find_map(|attr| {
171160
Some(match attr {

compiler/rustc_passes/src/check_attr.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -396,7 +396,6 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
396396
| sym::deprecated_safe // FIXME(deprecated_safe)
397397
// internal
398398
| sym::prelude_import
399-
| sym::panic_handler
400399
| sym::default_lib_allocator
401400
| sym::rustc_diagnostic_item
402401
| sym::rustc_nonnull_optimization_guaranteed
@@ -857,7 +856,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
857856
Target::Method(MethodKind::Trait { body: true } | MethodKind::Inherent)
858857
| Target::Fn => {
859858
// `#[target_feature]` is not allowed in lang items.
860-
if let Some(lang_item) = hir::lang_items::extract_hir(attrs)
859+
if let Some(lang_item) = find_attr!(attrs, AttributeKind::Lang(lang) => lang)
861860
// Calling functions with `#[target_feature]` is
862861
// not unsafe on WASM, see #84988
863862
&& !self.tcx.sess.target.is_like_wasm

compiler/rustc_passes/src/dead.rs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use rustc_middle::ty::{self, AssocTag, TyCtxt};
2222
use rustc_middle::{bug, span_bug};
2323
use rustc_session::lint::builtin::DEAD_CODE;
2424
use rustc_session::lint::{self, LintExpectationId};
25-
use rustc_span::{Symbol, kw, sym};
25+
use rustc_span::{Symbol, kw};
2626

2727
use crate::errors::{
2828
ChangeFields, IgnoredDerivedImpls, MultipleDeadCodes, ParentInfo, UselessAssignment,
@@ -706,12 +706,6 @@ fn has_allow_dead_code_or_lang_attr(
706706
tcx: TyCtxt<'_>,
707707
def_id: LocalDefId,
708708
) -> Option<ComesFromAllowExpect> {
709-
fn has_lang_attr(tcx: TyCtxt<'_>, def_id: LocalDefId) -> bool {
710-
find_attr!(tcx.get_all_attrs(def_id), AttributeKind::Lang(..))
711-
// Stable attribute for #[lang = "panic_impl"]
712-
|| tcx.has_attr(def_id, sym::panic_handler)
713-
}
714-
715709
fn has_allow_expect_dead_code(tcx: TyCtxt<'_>, def_id: LocalDefId) -> bool {
716710
let hir_id = tcx.local_def_id_to_hir_id(def_id);
717711
let lint_level = tcx.lint_level_at_node(lint::builtin::DEAD_CODE, hir_id).level;
@@ -732,7 +726,9 @@ fn has_allow_dead_code_or_lang_attr(
732726

733727
if has_allow_expect_dead_code(tcx, def_id) {
734728
Some(ComesFromAllowExpect::Yes)
735-
} else if has_used_like_attr(tcx, def_id) || has_lang_attr(tcx, def_id) {
729+
} else if has_used_like_attr(tcx, def_id)
730+
|| find_attr!(tcx.get_all_attrs(def_id), AttributeKind::Lang(..))
731+
{
736732
Some(ComesFromAllowExpect::No)
737733
} else {
738734
None

compiler/rustc_passes/src/weak_lang_items.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,7 @@ use rustc_middle::ty::TyCtxt;
1010
use rustc_session::config::CrateType;
1111
use rustc_target::spec::Os;
1212

13-
use crate::errors::{
14-
MissingLangItem, MissingPanicHandler, PanicUnwindWithoutStd,
15-
};
13+
use crate::errors::{MissingLangItem, MissingPanicHandler, PanicUnwindWithoutStd};
1614

1715
/// Checks the crate for usage of weak lang items, returning a vector of all the
1816
/// lang items required by this crate, but not defined yet.

tests/ui/lowering/issue-96847.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ fn main() {
1010
for _ in [1,2,3] {
1111
#![lang="foo"]
1212
//~^ ERROR definition of an unknown lang item: `foo`
13-
//~| ERROR attribute cannot be used on expressions
1413
println!("foo");
1514
}
1615
}

tests/ui/lowering/issue-96847.stderr

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,6 @@ error[E0522]: definition of an unknown lang item: `foo`
44
LL | #![lang="foo"]
55
| ^^^^^^^^^^^^^^ definition of unknown lang item `foo`
66

7-
error: `#[lang]` attribute cannot be used on expressions
8-
--> $DIR/issue-96847.rs:11:9
9-
|
10-
LL | #![lang="foo"]
11-
| ^^^^^^^^^^^^^^
12-
|
13-
= help: `#[lang]` can be applied to associated consts, associated types, constants, crates, data types, enum variants, foreign statics, functions, inherent impl blocks, macro defs, modules, statics, traits, and type aliases
14-
15-
error: aborting due to 2 previous errors
7+
error: aborting due to 1 previous error
168

179
For more information about this error, try `rustc --explain E0522`.

0 commit comments

Comments
 (0)