Skip to content

Commit 66388cc

Browse files
Auto merge of #150727 - Bryntet:test_err_should_panic, r=<try>
[DONT MERGE] crater run for FCW on `#[should_panic]`
2 parents bca37a2 + b96b9f2 commit 66388cc

27 files changed

+407
-210
lines changed

‎compiler/rustc_ast_pretty/src/pprust/state.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -655,7 +655,7 @@ pub trait PrintState<'a>: std::ops::Deref<Target = pp::Printer> + std::ops::Dere
655655
}
656656

657657
fn print_attribute_inline(&mut self, attr: &ast::Attribute, is_inline: bool) -> bool {
658-
if attr.has_name(sym::cfg_trace) || attr.has_name(sym::cfg_attr_trace) {
658+
if attr.has_any_name(&[sym::cfg_trace, sym::cfg_attr_trace, sym::test_trace]) {
659659
// It's not a valid identifier, so avoid printing it
660660
// to keep the printed code reasonably parse-able.
661661
return false;

‎compiler/rustc_attr_parsing/src/attributes/codegen_attrs.rs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,7 @@ impl<S: Stage> AttributeParser<S> for NakedParser {
264264
sym::cfg_attr_trace,
265265
// testing (allowed here so better errors can be generated in `rustc_builtin_macros::test`)
266266
sym::test,
267+
sym::test_trace,
267268
sym::ignore,
268269
sym::should_panic,
269270
sym::bench,

‎compiler/rustc_attr_parsing/src/attributes/mod.rs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ pub(crate) mod rustc_internal;
6060
pub(crate) mod semantics;
6161
pub(crate) mod stability;
6262
pub(crate) mod test_attrs;
63+
pub(crate) mod trace;
6364
pub(crate) mod traits;
6465
pub(crate) mod transparency;
6566
pub(crate) mod util;
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
use super::prelude::*;
2+
3+
pub(crate) struct TestTraceParser;
4+
5+
impl<S: Stage> NoArgsAttributeParser<S> for TestTraceParser {
6+
const PATH: &[Symbol] = &[sym::test_trace];
7+
8+
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Ignore;
9+
10+
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[Allow(Target::Fn)]);
11+
12+
const CREATE: fn(Span) -> AttributeKind = |_| AttributeKind::TestTrace;
13+
}

‎compiler/rustc_attr_parsing/src/context.rs‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ use crate::attributes::stability::{
7575
BodyStabilityParser, ConstStabilityIndirectParser, ConstStabilityParser, StabilityParser,
7676
};
7777
use crate::attributes::test_attrs::{IgnoreParser, ShouldPanicParser};
78+
use crate::attributes::trace::TestTraceParser;
7879
use crate::attributes::traits::{
7980
AllowIncoherentImplParser, CoinductiveParser, DenyExplicitImplParser,
8081
DoNotImplementViaObjectParser, FundamentalParser, MarkerParser, ParenSugarParser,
@@ -275,6 +276,7 @@ attribute_parsers!(
275276
Single<WithoutArgs<RustcShouldNotBeCalledOnConstItems>>,
276277
Single<WithoutArgs<SpecializationTraitParser>>,
277278
Single<WithoutArgs<StdInternalSymbolParser>>,
279+
Single<WithoutArgs<TestTraceParser>>,
278280
Single<WithoutArgs<ThreadLocalParser>>,
279281
Single<WithoutArgs<TrackCallerParser>>,
280282
Single<WithoutArgs<TypeConstParser>>,

‎compiler/rustc_attr_parsing/src/validate_attr.rs‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ use rustc_span::{Span, Symbol, sym};
2222
use crate::{AttributeParser, Late, session_diagnostics as errors};
2323

2424
pub fn check_attr(psess: &ParseSess, attr: &Attribute) {
25-
if attr.is_doc_comment() || attr.has_name(sym::cfg_trace) || attr.has_name(sym::cfg_attr_trace)
25+
if attr.is_doc_comment()
26+
|| attr.has_any_name(&[sym::cfg_trace, sym::cfg_attr_trace, sym::test_trace])
2627
{
2728
return;
2829
}

‎compiler/rustc_builtin_macros/src/test.rs‎

Lines changed: 6 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,11 @@ pub(crate) fn expand_test_or_bench(
136136
return vec![];
137137
}
138138

139+
// Add a trace to the originating test function, so that we can
140+
// check if attributes that have `#[test]` or `#[bench]` as a requirement
141+
// actually are annotated with said attributes
142+
item.attrs.push(cx.attr_word(sym::test_trace, cx.with_def_site_ctxt(attr_sp)));
143+
139144
if let Some(attr) = attr::find_by_name(&item.attrs, sym::naked) {
140145
cx.dcx().emit_err(errors::NakedFunctionTestingAttribute {
141146
testing_span: attr_sp,

‎compiler/rustc_feature/src/builtin_attrs.rs‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1140,6 +1140,10 @@ pub static BUILTIN_ATTRIBUTES: &[BuiltinAttribute] = &[
11401140
cfg_attr_trace, Normal, template!(Word /* irrelevant */), DuplicatesOk,
11411141
EncodeCrossCrate::No
11421142
),
1143+
ungated!(
1144+
test_trace, Normal, template!(Word), ErrorFollowing,
1145+
EncodeCrossCrate::No
1146+
),
11431147

11441148
// ==========================================================================
11451149
// Internal attributes, Diagnostics related:

‎compiler/rustc_hir/src/attrs/data_structures.rs‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -958,6 +958,9 @@ pub enum AttributeKind {
958958
/// `#[unsafe(force_target_feature(enable = "...")]`.
959959
TargetFeature { features: ThinVec<(Symbol, Span)>, attr_span: Span, was_forced: bool },
960960

961+
/// Represents `#[<test_trace>]`
962+
TestTrace,
963+
961964
/// Represents `#[thread_local]`
962965
ThreadLocal,
963966

‎compiler/rustc_hir/src/attrs/encode_cross_crate.rs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ impl AttributeKind {
119119
Stability { .. } => Yes,
120120
StdInternalSymbol(..) => No,
121121
TargetFeature { .. } => No,
122+
TestTrace => No,
122123
ThreadLocal => No,
123124
TrackCaller(..) => Yes,
124125
TypeConst(..) => Yes,

0 commit comments

Comments
 (0)