Skip to content

Commit f00c54d

Browse files
committed
Port two panic attrs to the attr parser.
Ports #![panic_runtime] and #![needs_panic_runtime]
1 parent cdff12b commit f00c54d

7 files changed

Lines changed: 38 additions & 14 deletions

File tree

compiler/rustc_attr_parsing/src/attributes/crate_level.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,3 +184,21 @@ impl<S: Stage> SingleAttributeParser<S> for WindowsSubsystemParser {
184184
Some(AttributeKind::WindowsSubsystem(kind, cx.attr_span))
185185
}
186186
}
187+
188+
pub(crate) struct PanicRuntimeParser;
189+
190+
impl<S: Stage> NoArgsAttributeParser<S> for PanicRuntimeParser {
191+
const PATH: &[Symbol] = &[sym::panic_runtime];
192+
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Warn;
193+
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[Allow(Target::Crate)]);
194+
const CREATE: fn(Span) -> AttributeKind = |_| AttributeKind::PanicRuntime;
195+
}
196+
197+
pub(crate) struct NeedsPanicRuntimeParser;
198+
199+
impl<S: Stage> NoArgsAttributeParser<S> for NeedsPanicRuntimeParser {
200+
const PATH: &[Symbol] = &[sym::needs_panic_runtime];
201+
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Warn;
202+
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[Allow(Target::Crate)]);
203+
const CREATE: fn(Span) -> AttributeKind = |_| AttributeKind::NeedsPanicRuntime;
204+
}

compiler/rustc_attr_parsing/src/context.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ use crate::attributes::codegen_attrs::{
2828
};
2929
use crate::attributes::confusables::ConfusablesParser;
3030
use crate::attributes::crate_level::{
31-
CrateNameParser, MoveSizeLimitParser, NoCoreParser, NoMainParser, NoStdParser,
32-
PatternComplexityLimitParser, RecursionLimitParser, RustcCoherenceIsCoreParser,
33-
TypeLengthLimitParser, WindowsSubsystemParser,
31+
CrateNameParser, MoveSizeLimitParser, NeedsPanicRuntimeParser, NoCoreParser, NoMainParser,
32+
NoStdParser, PanicRuntimeParser, PatternComplexityLimitParser, RecursionLimitParser,
33+
RustcCoherenceIsCoreParser, TypeLengthLimitParser, WindowsSubsystemParser,
3434
};
3535
use crate::attributes::debugger::DebuggerViualizerParser;
3636
use crate::attributes::deprecation::DeprecationParser;
@@ -267,13 +267,15 @@ attribute_parsers!(
267267
Single<WithoutArgs<MarkerParser>>,
268268
Single<WithoutArgs<MayDangleParser>>,
269269
Single<WithoutArgs<NeedsAllocatorParser>>,
270+
Single<WithoutArgs<NeedsPanicRuntimeParser>>,
270271
Single<WithoutArgs<NoCoreParser>>,
271272
Single<WithoutArgs<NoImplicitPreludeParser>>,
272273
Single<WithoutArgs<NoLinkParser>>,
273274
Single<WithoutArgs<NoMainParser>>,
274275
Single<WithoutArgs<NoMangleParser>>,
275276
Single<WithoutArgs<NoStdParser>>,
276277
Single<WithoutArgs<NonExhaustiveParser>>,
278+
Single<WithoutArgs<PanicRuntimeParser>>,
277279
Single<WithoutArgs<ParenSugarParser>>,
278280
Single<WithoutArgs<PassByValueParser>>,
279281
Single<WithoutArgs<PinV2Parser>>,

compiler/rustc_hir/src/attrs/data_structures.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -846,6 +846,9 @@ pub enum AttributeKind {
846846
/// Represents `#[needs_allocator]`
847847
NeedsAllocator,
848848

849+
/// Represents `#[needs_panic_runtime]`
850+
NeedsPanicRuntime,
851+
849852
/// Represents `#[no_core]`
850853
NoCore(Span),
851854

@@ -876,6 +879,9 @@ pub enum AttributeKind {
876879
/// Represents `#[optimize(size|speed)]`
877880
Optimize(OptimizeAttr, Span),
878881

882+
/// Represents `#[panic_runtime]`
883+
PanicRuntime,
884+
879885
/// Represents `#[rustc_paren_sugar]`.
880886
ParenSugar(Span),
881887

compiler/rustc_hir/src/attrs/encode_cross_crate.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ impl AttributeKind {
7777
MustUse { .. } => Yes,
7878
Naked(..) => No,
7979
NeedsAllocator => No,
80+
NeedsPanicRuntime => No,
8081
NoCore(..) => No,
8182
NoImplicitPrelude(..) => No,
8283
NoLink => No,
@@ -87,6 +88,7 @@ impl AttributeKind {
8788
ObjcClass { .. } => No,
8889
ObjcSelector { .. } => No,
8990
Optimize(..) => No,
91+
PanicRuntime => No,
9092
ParenSugar(..) => No,
9193
PassByValue(..) => Yes,
9294
Path(..) => No,

compiler/rustc_metadata/src/rmeta/encoder.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -743,9 +743,9 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
743743
debugger_visualizers,
744744
compiler_builtins: find_attr!(attrs, AttributeKind::CompilerBuiltins),
745745
needs_allocator: find_attr!(attrs, AttributeKind::NeedsAllocator),
746-
needs_panic_runtime: ast::attr::contains_name(attrs, sym::needs_panic_runtime),
746+
needs_panic_runtime: find_attr!(attrs, AttributeKind::NeedsPanicRuntime),
747747
no_builtins: ast::attr::contains_name(attrs, sym::no_builtins),
748-
panic_runtime: ast::attr::contains_name(attrs, sym::panic_runtime),
748+
panic_runtime: find_attr!(attrs, AttributeKind::PanicRuntime),
749749
profiler_runtime: ast::attr::contains_name(attrs, sym::profiler_runtime),
750750
symbol_mangling_version: tcx.sess.opts.get_symbol_mangling_version(),
751751

compiler/rustc_middle/src/ty/context.rs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ use rustc_hir::definitions::{DefPathData, Definitions, DisambiguatorState};
3838
use rustc_hir::intravisit::VisitorExt;
3939
use rustc_hir::lang_items::LangItem;
4040
use rustc_hir::limit::Limit;
41-
use rustc_hir::{self as hir, Attribute, HirId, Node, TraitCandidate, find_attr};
41+
use rustc_hir::{self as hir, HirId, Node, TraitCandidate, find_attr};
4242
use rustc_index::IndexVec;
4343
use rustc_query_system::cache::WithDepNode;
4444
use rustc_query_system::dep_graph::DepNodeIndex;
@@ -49,7 +49,7 @@ use rustc_session::config::CrateType;
4949
use rustc_session::cstore::{CrateStoreDyn, Untracked};
5050
use rustc_session::lint::Lint;
5151
use rustc_span::def_id::{CRATE_DEF_ID, DefPathHash, StableCrateId};
52-
use rustc_span::{DUMMY_SP, Ident, Span, Symbol, kw, sym};
52+
use rustc_span::{DUMMY_SP, Ident, Span, Symbol, kw};
5353
use rustc_type_ir::TyKind::*;
5454
use rustc_type_ir::lang_items::{SolverAdtLangItem, SolverLangItem, SolverTraitLangItem};
5555
pub use rustc_type_ir::lift::Lift;
@@ -3564,7 +3564,7 @@ impl<'tcx> TyCtxt<'tcx> {
35643564

35653565
pub fn provide(providers: &mut Providers) {
35663566
providers.is_panic_runtime =
3567-
|tcx, LocalCrate| contains_name(tcx.hir_krate_attrs(), sym::panic_runtime);
3567+
|tcx, LocalCrate| find_attr!(tcx.hir_krate_attrs(), AttributeKind::PanicRuntime);
35683568
providers.is_compiler_builtins =
35693569
|tcx, LocalCrate| find_attr!(tcx.hir_krate_attrs(), AttributeKind::CompilerBuiltins);
35703570
providers.has_panic_handler = |tcx, LocalCrate| {
@@ -3573,7 +3573,3 @@ pub fn provide(providers: &mut Providers) {
35733573
};
35743574
providers.source_span = |tcx, def_id| tcx.untracked.source_span.get(def_id).unwrap_or(DUMMY_SP);
35753575
}
3576-
3577-
pub fn contains_name(attrs: &[Attribute], name: Symbol) -> bool {
3578-
attrs.iter().any(|x| x.has_name(name))
3579-
}

compiler/rustc_passes/src/check_attr.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,8 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
324324
| AttributeKind::RustcNounwind
325325
| AttributeKind::RustcOffloadKernel
326326
| AttributeKind::CompilerBuiltins
327+
| AttributeKind::PanicRuntime
328+
| AttributeKind::NeedsPanicRuntime
327329
) => { /* do nothing */ }
328330
Attribute::Unparsed(attr_item) => {
329331
style = Some(attr_item.style);
@@ -401,8 +403,6 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
401403
| sym::no_builtins
402404
| sym::crate_type
403405
| sym::profiler_runtime
404-
| sym::needs_panic_runtime
405-
| sym::panic_runtime
406406
| sym::rustc_preserve_ub_checks,
407407
..
408408
] => {}

0 commit comments

Comments
 (0)