Skip to content

Commit f1d4ebf

Browse files
committed
Remove unnecessary expansions created by #[test_case/test/bench]
The expansions were created to allow unstable things inside `#[test_case/test/bench]`, but that's not a proper way to do that. Put the required `allow_internal_unstable`s into the macros' properties instead.
1 parent 16918a8 commit f1d4ebf

File tree

4 files changed

+24
-31
lines changed

4 files changed

+24
-31
lines changed

src/libcore/macros.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1244,19 +1244,22 @@ mod builtin {
12441244

12451245
/// Attribute macro applied to a function to turn it into a unit test.
12461246
#[stable(feature = "rust1", since = "1.0.0")]
1247+
#[allow_internal_unstable(test, rustc_attrs)]
12471248
#[rustc_builtin_macro]
12481249
#[rustc_macro_transparency = "semitransparent"]
12491250
pub macro test($item:item) { /* compiler built-in */ }
12501251

12511252
/// Attribute macro applied to a function to turn it into a benchmark test.
12521253
#[stable(feature = "rust1", since = "1.0.0")]
1254+
#[allow_internal_unstable(test, rustc_attrs)]
12531255
#[rustc_builtin_macro]
12541256
#[rustc_macro_transparency = "semitransparent"]
12551257
pub macro bench($item:item) { /* compiler built-in */ }
12561258

12571259
/// An implementation detail of the `#[test]` and `#[bench]` macros.
12581260
#[unstable(feature = "custom_test_frameworks", issue = "50297",
12591261
reason = "custom test frameworks are an unstable feature")]
1262+
#[allow_internal_unstable(test, rustc_attrs)]
12601263
#[rustc_builtin_macro]
12611264
#[rustc_macro_transparency = "semitransparent"]
12621265
pub macro test_case($item:item) { /* compiler built-in */ }

src/libsyntax_ext/lib.rs

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ pub fn register_builtins(resolver: &mut dyn syntax::ext::base::Resolver,
7272
let mut register = |name, ext| {
7373
resolver.add_builtin(ast::Ident::with_empty_ctxt(name), Lrc::new(ext));
7474
};
75+
7576
macro_rules! register {
7677
($( $name:ident: $f:expr, )*) => { $(
7778
register(sym::$name, SyntaxExtension::default(
@@ -125,24 +126,31 @@ pub fn register_builtins(resolver: &mut dyn syntax::ext::base::Resolver,
125126
trace_macros: trace_macros::expand_trace_macros,
126127
}
127128

129+
let allow_internal_unstable = Some([sym::test, sym::rustc_attrs][..].into());
128130
register(sym::test_case, SyntaxExtension {
129131
stability: Some(Stability::unstable(
130132
sym::custom_test_frameworks,
131133
Some(Symbol::intern(EXPLAIN_CUSTOM_TEST_FRAMEWORKS)),
132134
50297,
133135
)),
136+
allow_internal_unstable: allow_internal_unstable.clone(),
134137
..SyntaxExtension::default(
135138
SyntaxExtensionKind::LegacyAttr(Box::new(test_case::expand)), edition
136139
)
137140
});
138-
register(sym::test, SyntaxExtension::default(
139-
SyntaxExtensionKind::LegacyAttr(Box::new(test::expand_test)), edition
140-
));
141-
register(sym::bench, SyntaxExtension::default(
142-
SyntaxExtensionKind::LegacyAttr(Box::new(test::expand_bench)), edition
143-
));
144-
145-
// format_args uses `unstable` things internally.
141+
register(sym::test, SyntaxExtension {
142+
allow_internal_unstable: allow_internal_unstable.clone(),
143+
..SyntaxExtension::default(
144+
SyntaxExtensionKind::LegacyAttr(Box::new(test::expand_test)), edition
145+
)
146+
});
147+
register(sym::bench, SyntaxExtension {
148+
allow_internal_unstable,
149+
..SyntaxExtension::default(
150+
SyntaxExtensionKind::LegacyAttr(Box::new(test::expand_bench)), edition
151+
)
152+
});
153+
146154
let allow_internal_unstable = Some([sym::fmt_internals][..].into());
147155
register(sym::format_args, SyntaxExtension {
148156
allow_internal_unstable: allow_internal_unstable.clone(),

src/libsyntax_ext/test.rs

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,12 @@
33
44
use syntax::ext::base::*;
55
use syntax::ext::build::AstBuilder;
6-
use syntax::ext::hygiene::{Mark, SyntaxContext};
6+
use syntax::ext::hygiene::SyntaxContext;
77
use syntax::attr;
88
use syntax::ast;
99
use syntax::print::pprust;
1010
use syntax::symbol::{Symbol, sym};
1111
use syntax_pos::Span;
12-
use syntax::source_map::{ExpnInfo, ExpnKind};
1312
use std::iter;
1413

1514
pub fn expand_test(
@@ -60,15 +59,8 @@ pub fn expand_test_or_bench(
6059
return vec![Annotatable::Item(item)];
6160
}
6261

63-
let (sp, attr_sp) = {
64-
let mark = Mark::fresh(Mark::root());
65-
mark.set_expn_info(ExpnInfo::with_unstable(
66-
ExpnKind::MacroAttribute(sym::test), attr_sp, cx.parse_sess.edition,
67-
&[sym::rustc_attrs, sym::test],
68-
));
69-
(item.span.with_ctxt(SyntaxContext::empty().apply_mark(mark)),
70-
attr_sp.with_ctxt(SyntaxContext::empty().apply_mark(mark)))
71-
};
62+
let ctxt = SyntaxContext::empty().apply_mark(cx.current_expansion.mark);
63+
let (sp, attr_sp) = (item.span.with_ctxt(ctxt), attr_sp.with_ctxt(ctxt));
7264

7365
// Gensym "test" so we can extern crate without conflicting with any local names
7466
let test_id = cx.ident_of("test").gensym();

src/libsyntax_ext/test_case.rs

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,11 @@
1111

1212
use syntax::ext::base::*;
1313
use syntax::ext::build::AstBuilder;
14-
use syntax::ext::hygiene::{Mark, SyntaxContext};
14+
use syntax::ext::hygiene::SyntaxContext;
1515
use syntax::ast;
1616
use syntax::source_map::respan;
1717
use syntax::symbol::sym;
1818
use syntax_pos::Span;
19-
use syntax::source_map::{ExpnInfo, ExpnKind};
2019

2120
pub fn expand(
2221
ecx: &mut ExtCtxt<'_>,
@@ -26,17 +25,8 @@ pub fn expand(
2625
) -> Vec<Annotatable> {
2726
if !ecx.ecfg.should_test { return vec![]; }
2827

29-
let sp = {
30-
let mark = Mark::fresh(Mark::root());
31-
mark.set_expn_info(ExpnInfo::with_unstable(
32-
ExpnKind::MacroAttribute(sym::test_case), attr_sp, ecx.parse_sess.edition,
33-
&[sym::test, sym::rustc_attrs],
34-
));
35-
attr_sp.with_ctxt(SyntaxContext::empty().apply_mark(mark))
36-
};
37-
28+
let sp = attr_sp.with_ctxt(SyntaxContext::empty().apply_mark(ecx.current_expansion.mark));
3829
let mut item = anno_item.expect_item();
39-
4030
item = item.map(|mut item| {
4131
item.vis = respan(item.vis.span, ast::VisibilityKind::Public);
4232
item.ident = item.ident.gensym();

0 commit comments

Comments
 (0)