Skip to content

Commit b8d5301

Browse files
Port #[should_panic] to the new attribute parsing infrastructure
Signed-off-by: Jonathan Brouwer <[email protected]>
1 parent cccf075 commit b8d5301

File tree

8 files changed

+72
-38
lines changed

8 files changed

+72
-38
lines changed

compiler/rustc_attr_data_structures/src/attributes.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,9 @@ pub enum AttributeKind {
361361
/// Represents `#[rustc_object_lifetime_default]`.
362362
RustcObjectLifetimeDefault,
363363

364+
/// Represents `#[should_panic]`
365+
ShouldPanic { reason: Option<Symbol>, span: Span },
366+
364367
/// Represents `#[rustc_skip_during_method_dispatch]`.
365368
SkipDuringMethodDispatch { array: bool, boxed_slice: bool, span: Span },
366369

compiler/rustc_attr_data_structures/src/encode_cross_crate.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ impl AttributeKind {
6161
RustcLayoutScalarValidRangeEnd(..) => Yes,
6262
RustcLayoutScalarValidRangeStart(..) => Yes,
6363
RustcObjectLifetimeDefault => No,
64+
ShouldPanic { .. } => No,
6465
SkipDuringMethodDispatch { .. } => No,
6566
SpecializationTrait(..) => No,
6667
Stability { .. } => Yes,

compiler/rustc_attr_parsing/src/attributes/test_attrs.rs

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,55 @@ impl<S: Stage> SingleAttributeParser<S> for IgnoreParser {
4444
})
4545
}
4646
}
47+
48+
pub(crate) struct ShouldPanicParser;
49+
50+
impl<S: Stage> SingleAttributeParser<S> for ShouldPanicParser {
51+
const PATH: &[Symbol] = &[sym::should_panic];
52+
const ATTRIBUTE_ORDER: AttributeOrder = AttributeOrder::KeepOutermost;
53+
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::WarnButFutureError;
54+
const TEMPLATE: AttributeTemplate =
55+
template!(Word, List: r#"expected = "reason""#, NameValueStr: "reason");
56+
57+
fn convert(cx: &mut AcceptContext<'_, '_, S>, args: &ArgParser<'_>) -> Option<AttributeKind> {
58+
Some(AttributeKind::ShouldPanic {
59+
span: cx.attr_span,
60+
reason: match args {
61+
ArgParser::NoArgs => None,
62+
ArgParser::NameValue(name_value) => {
63+
let Some(str_value) = name_value.value_as_str() else {
64+
cx.expected_string_literal(
65+
name_value.value_span,
66+
Some(name_value.value_as_lit()),
67+
);
68+
return None;
69+
};
70+
Some(str_value)
71+
}
72+
ArgParser::List(list) => {
73+
let Some(single) = list.single() else {
74+
cx.expected_single_argument(list.span);
75+
return None;
76+
};
77+
let Some(single) = single.meta_item() else {
78+
cx.expected_name_value(single.span(), Some(sym::expected));
79+
return None;
80+
};
81+
if !single.path().word_is(sym::expected) {
82+
cx.expected_specific_argument_strings(list.span, vec!["expected"]);
83+
return None;
84+
}
85+
let Some(nv) = single.args().name_value() else {
86+
cx.expected_name_value(single.span(), Some(sym::expected));
87+
return None;
88+
};
89+
let Some(expected) = nv.value_as_str() else {
90+
cx.expected_string_literal(nv.value_span, Some(nv.value_as_lit()));
91+
return None;
92+
};
93+
Some(expected)
94+
}
95+
},
96+
})
97+
}
98+
}

compiler/rustc_attr_parsing/src/context.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ use crate::attributes::semantics::MayDangleParser;
4444
use crate::attributes::stability::{
4545
BodyStabilityParser, ConstStabilityIndirectParser, ConstStabilityParser, StabilityParser,
4646
};
47-
use crate::attributes::test_attrs::IgnoreParser;
47+
use crate::attributes::test_attrs::{IgnoreParser, ShouldPanicParser};
4848
use crate::attributes::traits::{
4949
AllowIncoherentImplParser, CoherenceIsCoreParser, CoinductiveParser, ConstTraitParser,
5050
DenyExplicitImplParser, DoNotImplementViaObjectParser, FundamentalParser, MarkerParser,
@@ -152,6 +152,7 @@ attribute_parsers!(
152152
Single<RustcLayoutScalarValidRangeEnd>,
153153
Single<RustcLayoutScalarValidRangeStart>,
154154
Single<RustcObjectLifetimeDefaultParser>,
155+
Single<ShouldPanicParser>,
155156
Single<SkipDuringMethodDispatchParser>,
156157
Single<TransparencyParser>,
157158
Single<WithoutArgs<AllowIncoherentImplParser>>,

compiler/rustc_builtin_macros/src/test.rs

Lines changed: 10 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,13 @@ use std::assert_matches::assert_matches;
55
use std::iter;
66

77
use rustc_ast::ptr::P;
8-
use rustc_ast::{self as ast, GenericParamKind, attr};
8+
use rustc_ast::{self as ast, GenericParamKind, HasNodeId, attr};
99
use rustc_ast_pretty::pprust;
10+
use rustc_attr_data_structures::AttributeKind;
11+
use rustc_attr_parsing::AttributeParser;
1012
use rustc_errors::{Applicability, Diag, Level};
1113
use rustc_expand::base::*;
14+
use rustc_hir::Attribute;
1215
use rustc_span::{ErrorGuaranteed, FileNameDisplayPreference, Ident, Span, Symbol, sym};
1316
use thin_vec::{ThinVec, thin_vec};
1417
use tracing::debug;
@@ -478,39 +481,12 @@ fn should_ignore_message(i: &ast::Item) -> Option<Symbol> {
478481
}
479482

480483
fn should_panic(cx: &ExtCtxt<'_>, i: &ast::Item) -> ShouldPanic {
481-
match attr::find_by_name(&i.attrs, sym::should_panic) {
482-
Some(attr) => {
483-
match attr.meta_item_list() {
484-
// Handle #[should_panic(expected = "foo")]
485-
Some(list) => {
486-
let msg = list
487-
.iter()
488-
.find(|mi| mi.has_name(sym::expected))
489-
.and_then(|mi| mi.meta_item())
490-
.and_then(|mi| mi.value_str());
491-
if list.len() != 1 || msg.is_none() {
492-
cx.dcx()
493-
.struct_span_warn(
494-
attr.span,
495-
"argument must be of the form: \
496-
`expected = \"error message\"`",
497-
)
498-
.with_note(
499-
"errors in this attribute were erroneously \
500-
allowed and will become a hard error in a \
501-
future release",
502-
)
503-
.emit();
504-
ShouldPanic::Yes(None)
505-
} else {
506-
ShouldPanic::Yes(msg)
507-
}
508-
}
509-
// Handle #[should_panic] and #[should_panic = "expected"]
510-
None => ShouldPanic::Yes(attr.value_str()),
511-
}
512-
}
513-
None => ShouldPanic::No,
484+
if let Some(Attribute::Parsed(AttributeKind::ShouldPanic { reason, .. })) =
485+
AttributeParser::parse_limited(cx.sess, &i.attrs, sym::should_panic, i.span, i.node_id())
486+
{
487+
ShouldPanic::Yes(reason)
488+
} else {
489+
ShouldPanic::No
514490
}
515491
}
516492

compiler/rustc_hir/src/hir.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1304,6 +1304,7 @@ impl AttributeExt for Attribute {
13041304
Attribute::Parsed(AttributeKind::DocComment { span, .. }) => *span,
13051305
Attribute::Parsed(AttributeKind::MayDangle(span)) => *span,
13061306
Attribute::Parsed(AttributeKind::Ignore { span, .. }) => *span,
1307+
Attribute::Parsed(AttributeKind::ShouldPanic { span, .. }) => *span,
13071308
Attribute::Parsed(AttributeKind::AutomaticallyDerived(span)) => *span,
13081309
a => panic!("can't get the span of an arbitrary parsed attribute: {a:?}"),
13091310
}

compiler/rustc_parse/src/validate_attr.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,7 @@ pub fn check_builtin_meta_item(
308308
| sym::path
309309
| sym::ignore
310310
| sym::must_use
311+
| sym::should_panic
311312
| sym::track_caller
312313
| sym::link_name
313314
| sym::link_ordinal

compiler/rustc_passes/src/check_attr.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,8 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
276276
Attribute::Parsed(AttributeKind::Used { span: attr_span, .. }) => {
277277
self.check_used(*attr_span, target, span);
278278
}
279+
Attribute::Parsed(AttributeKind::ShouldPanic { span: attr_span, .. }) => self
280+
.check_generic_attr(hir_id, sym::should_panic, *attr_span, target, Target::Fn),
279281
&Attribute::Parsed(AttributeKind::PassByValue(attr_span)) => {
280282
self.check_pass_by_value(attr_span, span, target)
281283
}
@@ -346,9 +348,6 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
346348
}
347349
[sym::path, ..] => self.check_generic_attr_unparsed(hir_id, attr, target, Target::Mod),
348350
[sym::macro_export, ..] => self.check_macro_export(hir_id, attr, target),
349-
[sym::should_panic, ..] => {
350-
self.check_generic_attr_unparsed(hir_id, attr, target, Target::Fn)
351-
}
352351
[sym::proc_macro, ..] => {
353352
self.check_proc_macro(hir_id, target, ProcMacroKind::FunctionLike)
354353
}

0 commit comments

Comments
 (0)