Skip to content

Commit f93f9bd

Browse files
committed
Auto merge of #150727 - Bryntet:test_err_should_panic, r=<try>
[DONT MERGE] crater run for FCW on `#[should_panic]`
2 parents 2b112ef + 9a50d00 commit f93f9bd

File tree

25 files changed

+351
-152
lines changed

25 files changed

+351
-152
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
@@ -63,6 +63,7 @@ pub(crate) mod rustc_internal;
6363
pub(crate) mod semantics;
6464
pub(crate) mod stability;
6565
pub(crate) mod test_attrs;
66+
pub(crate) mod trace;
6667
pub(crate) mod traits;
6768
pub(crate) mod transparency;
6869
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
@@ -82,6 +82,7 @@ use crate::attributes::stability::{
8282
BodyStabilityParser, ConstStabilityIndirectParser, ConstStabilityParser, StabilityParser,
8383
};
8484
use crate::attributes::test_attrs::{IgnoreParser, ShouldPanicParser};
85+
use crate::attributes::trace::TestTraceParser;
8586
use crate::attributes::traits::{
8687
AllowIncoherentImplParser, CoinductiveParser, DenyExplicitImplParser,
8788
DoNotImplementViaObjectParser, FundamentalParser, MarkerParser, ParenSugarParser,
@@ -291,6 +292,7 @@ attribute_parsers!(
291292
Single<WithoutArgs<RustcShouldNotBeCalledOnConstItems>>,
292293
Single<WithoutArgs<SpecializationTraitParser>>,
293294
Single<WithoutArgs<StdInternalSymbolParser>>,
295+
Single<WithoutArgs<TestTraceParser>>,
294296
Single<WithoutArgs<ThreadLocalParser>>,
295297
Single<WithoutArgs<TrackCallerParser>>,
296298
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: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use rustc_ast_pretty::pprust;
99
use rustc_attr_parsing::AttributeParser;
1010
use rustc_errors::{Applicability, Diag, Level};
1111
use rustc_expand::base::*;
12+
use rustc_expand::config::attr_into_trace;
1213
use rustc_hir::Attribute;
1314
use rustc_hir::attrs::AttributeKind;
1415
use rustc_span::{ErrorGuaranteed, Ident, RemapPathScopeComponents, Span, Symbol, sym};
@@ -113,7 +114,7 @@ pub(crate) fn expand_test_or_bench(
113114
item: Annotatable,
114115
is_bench: bool,
115116
) -> Vec<Annotatable> {
116-
let (item, is_stmt) = match item {
117+
let (mut item, is_stmt) = match item {
117118
Annotatable::Item(i) => (i, false),
118119
Annotatable::Stmt(box ast::Stmt { kind: ast::StmtKind::Item(i), .. }) => (i, true),
119120
other => {
@@ -136,6 +137,14 @@ pub(crate) fn expand_test_or_bench(
136137
return vec![];
137138
}
138139

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

compiler/rustc_expand/src/config.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ pub fn pre_configure_attrs(sess: &Session, attrs: &[Attribute]) -> ast::AttrVec
169169
.collect()
170170
}
171171

172-
pub(crate) fn attr_into_trace(mut attr: Attribute, trace_name: Symbol) -> Attribute {
172+
pub fn attr_into_trace(mut attr: Attribute, trace_name: Symbol) -> Attribute {
173173
match &mut attr.kind {
174174
AttrKind::Normal(normal) => {
175175
let NormalAttr { item, tokens } = &mut **normal;

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
@@ -1021,6 +1021,9 @@ pub enum AttributeKind {
10211021
/// `#[unsafe(force_target_feature(enable = "...")]`.
10221022
TargetFeature { features: ThinVec<(Symbol, Span)>, attr_span: Span, was_forced: bool },
10231023

1024+
/// Represents `#[<test_trace>]`
1025+
TestTrace,
1026+
10241027
/// Represents `#[thread_local]`
10251028
ThreadLocal,
10261029

0 commit comments

Comments
 (0)