Skip to content

Commit 93b3210

Browse files
committed
add FCW to #[should_panic] when applied to non #[test] or #[bench] fns
1 parent 7e0838f commit 93b3210

File tree

18 files changed

+242
-145
lines changed

18 files changed

+242
-145
lines changed

compiler/rustc_attr_parsing/src/attributes/codegen_attrs.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,7 @@ impl<S: Stage> AttributeParser<S> for NakedParser {
286286
// FIXME(#82232, #143834): temporarily renamed to mitigate `#[align]` nameres ambiguity
287287
sym::rustc_align,
288288
sym::rustc_align_static,
289+
sym::rustc_test_dummy,
289290
// obviously compatible with self
290291
sym::naked,
291292
// documentation

compiler/rustc_attr_parsing/src/attributes/rustc_internal.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,17 @@ impl<S: Stage> SingleAttributeParser<S> for RustcScalableVectorParser {
306306
}
307307
}
308308

309+
pub(crate) struct RustcTestDummy;
310+
311+
impl<S: Stage> NoArgsAttributeParser<S> for RustcTestDummy {
312+
const PATH: &[Symbol] = &[sym::rustc_test_dummy];
313+
314+
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Ignore;
315+
316+
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[Allow(Target::Fn)]);
317+
318+
const CREATE: fn(Span) -> AttributeKind = |_| AttributeKind::RustcTestDummy;
319+
}
309320
pub(crate) struct RustcTestMarker;
310321

311322
impl<S: Stage> SingleAttributeParser<S> for RustcTestMarker {

compiler/rustc_attr_parsing/src/context.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ use crate::attributes::rustc_internal::{
6767
RustcLintUntrackedQueryInformationParser, RustcMainParser, RustcMustImplementOneOfParser,
6868
RustcNeverReturnsNullPointerParser, RustcNoImplicitAutorefsParser,
6969
RustcObjectLifetimeDefaultParser, RustcScalableVectorParser,
70-
RustcSimdMonomorphizeLaneLimitParser, RustcTestMarker,
70+
RustcSimdMonomorphizeLaneLimitParser, RustcTestDummy, RustcTestMarker,
7171
};
7272
use crate::attributes::semantics::MayDangleParser;
7373
use crate::attributes::stability::{
@@ -272,6 +272,7 @@ attribute_parsers!(
272272
Single<WithoutArgs<RustcNoImplicitAutorefsParser>>,
273273
Single<WithoutArgs<RustcPassIndirectlyInNonRusticAbisParser>>,
274274
Single<WithoutArgs<RustcShouldNotBeCalledOnConstItems>>,
275+
Single<WithoutArgs<RustcTestDummy>>,
275276
Single<WithoutArgs<SpecializationTraitParser>>,
276277
Single<WithoutArgs<StdInternalSymbolParser>>,
277278
Single<WithoutArgs<ThreadLocalParser>>,

compiler/rustc_builtin_macros/src/test.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ pub(crate) fn expand_test_or_bench(
113113
item: Annotatable,
114114
is_bench: bool,
115115
) -> Vec<Annotatable> {
116-
let (item, is_stmt) = match item {
116+
let (mut item, is_stmt) = match item {
117117
Annotatable::Item(i) => (i, false),
118118
Annotatable::Stmt(box ast::Stmt { kind: ast::StmtKind::Item(i), .. }) => (i, true),
119119
other => {
@@ -136,6 +136,8 @@ pub(crate) fn expand_test_or_bench(
136136
return vec![];
137137
}
138138

139+
item.attrs.push(cx.attr_word(sym::rustc_test_dummy, cx.with_def_site_ctxt(attr_sp)));
140+
139141
if let Some(attr) = attr::find_by_name(&item.attrs, sym::naked) {
140142
cx.dcx().emit_err(errors::NakedFunctionTestingAttribute {
141143
testing_span: attr_sp,

compiler/rustc_feature/src/builtin_attrs.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1377,6 +1377,12 @@ pub static BUILTIN_ATTRIBUTES: &[BuiltinAttribute] = &[
13771377
rustc_test_marker, Normal, template!(NameValueStr: "name"), WarnFollowing,
13781378
EncodeCrossCrate::No, "the `#[rustc_test_marker]` attribute is used internally to track tests",
13791379
),
1380+
1381+
rustc_attr!(
1382+
rustc_test_dummy, Normal, template!(Word), ErrorPreceding,
1383+
EncodeCrossCrate::No, "the `#[rustc_test_dummy]` attribute is used internally to track tests",
1384+
),
1385+
13801386
rustc_attr!(
13811387
rustc_unsafe_specialization_marker, Normal, template!(Word),
13821388
WarnFollowing, EncodeCrossCrate::No,

compiler/rustc_hir/src/attrs/data_structures.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -981,6 +981,9 @@ pub enum AttributeKind {
981981
/// Represents `#[rustc_simd_monomorphize_lane_limit = "N"]`.
982982
RustcSimdMonomorphizeLaneLimit(Limit),
983983

984+
/// Represents `#[rustc_test_dummy]`
985+
RustcTestDummy,
986+
984987
/// Represents `#[rustc_test_marker]`
985988
RustcTestMarker { fn_name: Symbol },
986989

compiler/rustc_hir/src/attrs/encode_cross_crate.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ impl AttributeKind {
109109
RustcScalableVector { .. } => Yes,
110110
RustcShouldNotBeCalledOnConstItems(..) => Yes,
111111
RustcSimdMonomorphizeLaneLimit(..) => Yes, // Affects layout computation, which needs to work cross-crate
112+
RustcTestDummy => No,
112113
RustcTestMarker { .. } => No,
113114
Sanitize { .. } => No,
114115
ShouldPanic { .. } => No,

compiler/rustc_passes/messages.ftl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -498,6 +498,9 @@ passes_sanitize_attribute_not_allowed =
498498
.no_body = function has no body
499499
.help = sanitize attribute can be applied to a function (with body), impl block, or module
500500
501+
passes_should_panic_must_be_applied_to_test = `#[should_panic]` should only be applied to `#[test]` functions
502+
.warn = {-passes_previously_accepted}
503+
501504
passes_trait_impl_const_stability_mismatch = const stability on the impl does not match the const stability on the trait
502505
passes_trait_impl_const_stability_mismatch_impl_stable = this impl is (implicitly) stable...
503506
passes_trait_impl_const_stability_mismatch_impl_unstable = this impl is unstable...

compiler/rustc_passes/src/check_attr.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,9 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
223223
Attribute::Parsed(AttributeKind::RustcMustImplementOneOf { attr_span, fn_names }) => {
224224
self.check_rustc_must_implement_one_of(*attr_span, fn_names, hir_id,target)
225225
},
226+
Attribute::Parsed(AttributeKind::ShouldPanic { span,.. }) => {
227+
self.check_should_panic(attrs, *span, target);
228+
},
226229
Attribute::Parsed(
227230
AttributeKind::EiiExternTarget { .. }
228231
| AttributeKind::EiiExternItem
@@ -272,6 +275,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
272275
| AttributeKind::RustcSimdMonomorphizeLaneLimit(..)
273276
| AttributeKind::RustcShouldNotBeCalledOnConstItems(..)
274277
| AttributeKind::RustcTestMarker { .. }
278+
| AttributeKind::RustcTestDummy
275279
| AttributeKind::ExportStable
276280
| AttributeKind::FfiConst(..)
277281
| AttributeKind::UnstableFeatureBound(..)
@@ -283,7 +287,6 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
283287
| AttributeKind::PassByValue (..)
284288
| AttributeKind::StdInternalSymbol (..)
285289
| AttributeKind::Coverage (..)
286-
| AttributeKind::ShouldPanic { .. }
287290
| AttributeKind::Coroutine(..)
288291
| AttributeKind::Linkage(..)
289292
| AttributeKind::MustUse { .. }
@@ -449,6 +452,15 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
449452
self.check_mix_no_mangle_export(hir_id, attrs);
450453
}
451454

455+
fn check_should_panic(&self, attrs: &[Attribute], attr_span: Span, target: Target) {
456+
// The error message only makes sense if it's actually being applied on a function
457+
if matches!(target, Target::Fn) {
458+
if !find_attr!(attrs, AttributeKind::RustcTestDummy) {
459+
self.dcx().emit_warn(errors::MustBeAppliedToTest { attr_span, warning: true });
460+
}
461+
}
462+
}
463+
452464
fn check_rustc_must_implement_one_of(
453465
&self,
454466
attr_span: Span,

compiler/rustc_passes/src/errors.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1360,3 +1360,12 @@ pub(crate) struct FunctionNamesDuplicated {
13601360
#[primary_span]
13611361
pub spans: Vec<Span>,
13621362
}
1363+
1364+
#[derive(Diagnostic)]
1365+
#[diag(passes_should_panic_must_be_applied_to_test)]
1366+
pub(crate) struct MustBeAppliedToTest {
1367+
#[primary_span]
1368+
pub attr_span: Span,
1369+
#[warning]
1370+
pub warning: bool,
1371+
}

0 commit comments

Comments
 (0)