Skip to content

Commit f4fdbf7

Browse files
Port #[should_panic] to the new attribute parsing infrastructure
Signed-off-by: Jonathan Brouwer <jonathantbrouwer@gmail.com>
1 parent f8f6997 commit f4fdbf7

File tree

8 files changed

+79
-38
lines changed

8 files changed

+79
-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
@@ -392,6 +392,9 @@ pub enum AttributeKind {
392392
/// Represents `#[rustc_object_lifetime_default]`.
393393
RustcObjectLifetimeDefault,
394394

395+
/// Represents `#[should_panic]`
396+
ShouldPanic { reason: Option<Symbol>, span: Span },
397+
395398
/// Represents `#[rustc_skip_during_method_dispatch]`.
396399
SkipDuringMethodDispatch { array: bool, boxed_slice: bool, span: Span },
397400

compiler/rustc_attr_data_structures/src/encode_cross_crate.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ impl AttributeKind {
6363
RustcLayoutScalarValidRangeEnd(..) => Yes,
6464
RustcLayoutScalarValidRangeStart(..) => Yes,
6565
RustcObjectLifetimeDefault => No,
66+
ShouldPanic { .. } => No,
6667
SkipDuringMethodDispatch { .. } => No,
6768
SpecializationTrait(..) => No,
6869
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
@@ -45,7 +45,7 @@ use crate::attributes::semantics::MayDangleParser;
4545
use crate::attributes::stability::{
4646
BodyStabilityParser, ConstStabilityIndirectParser, ConstStabilityParser, StabilityParser,
4747
};
48-
use crate::attributes::test_attrs::IgnoreParser;
48+
use crate::attributes::test_attrs::{IgnoreParser, ShouldPanicParser};
4949
use crate::attributes::traits::{
5050
AllowIncoherentImplParser, CoherenceIsCoreParser, CoinductiveParser, ConstTraitParser,
5151
DenyExplicitImplParser, DoNotImplementViaObjectParser, FundamentalParser, MarkerParser,
@@ -154,6 +154,7 @@ attribute_parsers!(
154154
Single<RustcLayoutScalarValidRangeEnd>,
155155
Single<RustcLayoutScalarValidRangeStart>,
156156
Single<RustcObjectLifetimeDefaultParser>,
157+
Single<ShouldPanicParser>,
157158
Single<SkipDuringMethodDispatchParser>,
158159
Single<TransparencyParser>,
159160
Single<WithoutArgs<AllowIncoherentImplParser>>,

compiler/rustc_builtin_macros/src/test.rs

Lines changed: 17 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, join_path_idents};
8+
use rustc_ast::{self as ast, GenericParamKind, HasNodeId, attr, join_path_idents};
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;
@@ -473,39 +476,19 @@ fn should_ignore_message(i: &ast::Item) -> Option<Symbol> {
473476
}
474477

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

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
@@ -282,6 +282,8 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
282282
Attribute::Parsed(AttributeKind::Used { span: attr_span, .. }) => {
283283
self.check_used(*attr_span, target, span);
284284
}
285+
Attribute::Parsed(AttributeKind::ShouldPanic { span: attr_span, .. }) => self
286+
.check_generic_attr(hir_id, sym::should_panic, *attr_span, target, Target::Fn),
285287
&Attribute::Parsed(AttributeKind::PassByValue(attr_span)) => {
286288
self.check_pass_by_value(attr_span, span, target)
287289
}
@@ -352,9 +354,6 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
352354
}
353355
[sym::path, ..] => self.check_generic_attr_unparsed(hir_id, attr, target, Target::Mod),
354356
[sym::macro_export, ..] => self.check_macro_export(hir_id, attr, target),
355-
[sym::should_panic, ..] => {
356-
self.check_generic_attr_unparsed(hir_id, attr, target, Target::Fn)
357-
}
358357
[sym::proc_macro, ..] => {
359358
self.check_proc_macro(hir_id, target, ProcMacroKind::FunctionLike)
360359
}

0 commit comments

Comments
 (0)