Skip to content

Commit 914b037

Browse files
committed
Auto merge of #152425 - Ozzy1423:test-runner, r=<try>
Port #![test_runner] to the attribute parser try-job: dist-x86_64-linux-alt
2 parents d34f1f9 + d1f11fd commit 914b037

9 files changed

Lines changed: 63 additions & 35 deletions

File tree

compiler/rustc_attr_parsing/src/attributes/test_attrs.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,3 +228,32 @@ impl<S: Stage> NoArgsAttributeParser<S> for RustcOutlivesParser {
228228
]);
229229
const CREATE: fn(Span) -> AttributeKind = |_| AttributeKind::RustcOutlives;
230230
}
231+
232+
pub(crate) struct TestRunnerParser;
233+
234+
impl<S: Stage> SingleAttributeParser<S> for TestRunnerParser {
235+
const PATH: &[Symbol] = &[sym::test_runner];
236+
const ATTRIBUTE_ORDER: AttributeOrder = AttributeOrder::KeepOutermost;
237+
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Error;
238+
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[Allow(Target::Crate)]);
239+
const TEMPLATE: AttributeTemplate = template!(List: &["path"]);
240+
241+
fn convert(cx: &mut AcceptContext<'_, '_, S>, args: &ArgParser) -> Option<AttributeKind> {
242+
let Some(list) = args.list() else {
243+
cx.expected_list(cx.attr_span, args);
244+
return None;
245+
};
246+
247+
let Some(single) = list.single() else {
248+
cx.expected_single_argument(list.span);
249+
return None;
250+
};
251+
252+
let Some(meta) = single.meta_item() else {
253+
cx.unexpected_literal(single.span());
254+
return None;
255+
};
256+
257+
Some(AttributeKind::TestRunner(meta.path().0.clone()))
258+
}
259+
}

compiler/rustc_attr_parsing/src/context.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,7 @@ attribute_parsers!(
218218
Single<SanitizeParser>,
219219
Single<ShouldPanicParser>,
220220
Single<SkipDuringMethodDispatchParser>,
221+
Single<TestRunnerParser>,
221222
Single<TransparencyParser>,
222223
Single<TypeLengthLimitParser>,
223224
Single<WindowsSubsystemParser>,

compiler/rustc_builtin_macros/src/errors.rs

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1002,20 +1002,6 @@ pub(crate) struct AsmUnsupportedClobberAbi {
10021002
pub(crate) macro_name: &'static str,
10031003
}
10041004

1005-
#[derive(Diagnostic)]
1006-
#[diag("`test_runner` argument must be a path")]
1007-
pub(crate) struct TestRunnerInvalid {
1008-
#[primary_span]
1009-
pub(crate) span: Span,
1010-
}
1011-
1012-
#[derive(Diagnostic)]
1013-
#[diag("`#![test_runner(..)]` accepts exactly 1 argument")]
1014-
pub(crate) struct TestRunnerNargs {
1015-
#[primary_span]
1016-
pub(crate) span: Span,
1017-
}
1018-
10191005
#[derive(Diagnostic)]
10201006
#[diag("expected token: `,`")]
10211007
pub(crate) struct ExpectedCommaInList {

compiler/rustc_builtin_macros/src/test_harness.rs

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,11 @@ use rustc_ast::entry::EntryPointType;
88
use rustc_ast::mut_visit::*;
99
use rustc_ast::visit::Visitor;
1010
use rustc_ast::{ModKind, attr};
11-
use rustc_errors::DiagCtxtHandle;
11+
use rustc_attr_parsing::AttributeParser;
1212
use rustc_expand::base::{ExtCtxt, ResolverExpand};
1313
use rustc_expand::expand::{AstFragment, ExpansionConfig};
1414
use rustc_feature::Features;
15+
use rustc_hir::attrs::AttributeKind;
1516
use rustc_session::Session;
1617
use rustc_session::lint::builtin::UNNAMEABLE_TEST_ITEMS;
1718
use rustc_span::hygiene::{AstPass, SyntaxContext, Transparency};
@@ -60,7 +61,7 @@ pub fn inject(
6061

6162
// Do this here so that the test_runner crate attribute gets marked as used
6263
// even in non-test builds
63-
let test_runner = get_test_runner(dcx, krate);
64+
let test_runner = get_test_runner(sess, features, krate);
6465

6566
if sess.is_test_crate() {
6667
let panic_strategy = match (panic_strategy, sess.opts.unstable_opts.panic_abort_tests) {
@@ -386,20 +387,16 @@ fn get_test_name(i: &ast::Item) -> Option<Symbol> {
386387
attr::first_attr_value_str_by_name(&i.attrs, sym::rustc_test_marker)
387388
}
388389

389-
fn get_test_runner(dcx: DiagCtxtHandle<'_>, krate: &ast::Crate) -> Option<ast::Path> {
390-
let test_attr = attr::find_by_name(&krate.attrs, sym::test_runner)?;
391-
let meta_list = test_attr.meta_item_list()?;
392-
let span = test_attr.span;
393-
match &*meta_list {
394-
[single] => match single.meta_item() {
395-
Some(meta_item) if meta_item.is_word() => return Some(meta_item.path.clone()),
396-
_ => {
397-
dcx.emit_err(errors::TestRunnerInvalid { span });
398-
}
399-
},
400-
_ => {
401-
dcx.emit_err(errors::TestRunnerNargs { span });
402-
}
390+
fn get_test_runner(sess: &Session, features: &Features, krate: &ast::Crate) -> Option<ast::Path> {
391+
match AttributeParser::parse_limited(
392+
sess,
393+
&krate.attrs,
394+
sym::test_runner,
395+
krate.spans.inner_span,
396+
krate.id,
397+
Some(features),
398+
) {
399+
Some(rustc_hir::Attribute::Parsed(AttributeKind::TestRunner(path))) => Some(path),
400+
_ => None,
403401
}
404-
None
405402
}

compiler/rustc_hir/src/attrs/data_structures.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ pub use ReprAttr::*;
55
use rustc_abi::Align;
66
pub use rustc_ast::attr::data_structures::*;
77
use rustc_ast::token::DocFragmentKind;
8-
use rustc_ast::{AttrStyle, ast};
8+
use rustc_ast::{AttrStyle, Path, ast};
99
use rustc_data_structures::fx::FxIndexMap;
1010
use rustc_error_messages::{DiagArgValue, IntoDiagArg};
1111
use rustc_macros::{Decodable, Encodable, HashStable_Generic, PrintAttribute};
@@ -1367,6 +1367,9 @@ pub enum AttributeKind {
13671367
/// `#[unsafe(force_target_feature(enable = "...")]`.
13681368
TargetFeature { features: ThinVec<(Symbol, Span)>, attr_span: Span, was_forced: bool },
13691369

1370+
/// Represents `#![test_runner(path)]`
1371+
TestRunner(Path),
1372+
13701373
/// Represents `#[thread_local]`
13711374
ThreadLocal,
13721375

compiler/rustc_hir/src/attrs/encode_cross_crate.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,7 @@ impl AttributeKind {
176176
ShouldPanic { .. } => No,
177177
Stability { .. } => Yes,
178178
TargetFeature { .. } => No,
179+
TestRunner(..) => Yes,
179180
ThreadLocal => No,
180181
TrackCaller(..) => Yes,
181182
TypeLengthLimit { .. } => No,

compiler/rustc_hir/src/attrs/pretty_printing.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use std::ops::Deref;
33
use std::path::PathBuf;
44

55
use rustc_abi::Align;
6+
use rustc_ast::ast::{Path, join_path_idents};
67
use rustc_ast::attr::data_structures::CfgEntry;
78
use rustc_ast::attr::version::RustcVersion;
89
use rustc_ast::token::{CommentKind, DocFragmentKind};
@@ -106,6 +107,16 @@ impl PrintAttribute for PathBuf {
106107
p.word(self.display().to_string());
107108
}
108109
}
110+
impl PrintAttribute for Path {
111+
fn should_render(&self) -> bool {
112+
true
113+
}
114+
115+
fn print_attribute(&self, p: &mut Printer) {
116+
p.word(join_path_idents(self.segments.iter().map(|seg| seg.ident)));
117+
}
118+
}
119+
109120
macro_rules! print_skip {
110121
($($t: ty),* $(,)?) => {$(
111122
impl PrintAttribute for $t {

compiler/rustc_passes/src/check_attr.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
364364
| AttributeKind::RustcVariance
365365
| AttributeKind::RustcVarianceOfOpaques
366366
| AttributeKind::ShouldPanic { .. }
367+
| AttributeKind::TestRunner(..)
367368
| AttributeKind::ThreadLocal
368369
| AttributeKind::TypeLengthLimit { .. }
369370
| AttributeKind::UnstableFeatureBound(..)
@@ -411,9 +412,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
411412
| sym::rustc_mir
412413
// crate-level attrs, are checked below
413414
| sym::feature
414-
| sym::register_tool
415-
| sym::test_runner,
416-
..
415+
| sym::register_tool, ..
417416
] => {}
418417
[name, rest@..] => {
419418
match BUILTIN_ATTRIBUTE_MAP.get(name) {

src/librustdoc/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#![feature(rustc_private)]
1818
#![feature(test)]
1919
#![feature(trim_prefix_suffix)]
20+
#![recursion_limit = "256"]
2021
#![warn(rustc::internal)]
2122
// tidy-alphabetical-end
2223

0 commit comments

Comments
 (0)