Skip to content

Commit ca590a9

Browse files
committed
Convert some macros from LegacyBang to Bang.
Specifically: `cfg!`, `column!`, `file!`, `line!`, `module_path!`. This requires changing `BangProcMacro for F` to use the `ecx` and `span` arguments, instead of ignoring them.
1 parent 982c9c1 commit ca590a9

File tree

6 files changed

+67
-55
lines changed

6 files changed

+67
-55
lines changed

compiler/rustc_builtin_macros/src/cfg.rs

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,31 +8,34 @@ use rustc_ast::token;
88
use rustc_ast::tokenstream::TokenStream;
99
use rustc_attr as attr;
1010
use rustc_errors::PResult;
11-
use rustc_expand::base::{DummyResult, ExpandResult, ExtCtxt, MacEager, MacroExpanderResult};
11+
use rustc_expand::base::ExtCtxt;
12+
use rustc_span::symbol::kw;
1213
use rustc_span::Span;
1314

14-
pub(crate) fn expand_cfg(
15-
cx: &mut ExtCtxt<'_>,
16-
sp: Span,
17-
tts: TokenStream,
18-
) -> MacroExpanderResult<'static> {
15+
pub(crate) fn expand_cfg(cx: &mut ExtCtxt<'_>, sp: Span, tts: TokenStream) -> TokenStream {
1916
let sp = cx.with_def_site_ctxt(sp);
2017

21-
ExpandResult::Ready(match parse_cfg(cx, sp, tts) {
18+
let kind = match parse_cfg(cx, sp, tts) {
2219
Ok(cfg) => {
2320
let matches_cfg = attr::cfg_matches(
2421
&cfg,
2522
&cx.sess,
2623
cx.current_expansion.lint_node_id,
2724
Some(cx.ecfg.features),
2825
);
29-
MacEager::expr(cx.expr_bool(sp, matches_cfg))
26+
let sym = if matches_cfg { kw::True } else { kw::False };
27+
token::TokenKind::Ident(sym, token::IdentIsRaw::No)
3028
}
3129
Err(err) => {
3230
let guar = err.emit();
33-
DummyResult::any(sp, guar)
31+
token::TokenKind::lit(
32+
token::LitKind::Err(guar), // njn: ?
33+
kw::Empty,
34+
None,
35+
)
3436
}
35-
})
37+
};
38+
TokenStream::token_alone(kind, sp)
3639
}
3740

3841
fn parse_cfg<'a>(cx: &ExtCtxt<'a>, span: Span, tts: TokenStream) -> PResult<'a, ast::MetaItem> {

compiler/rustc_builtin_macros/src/lib.rs

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -59,38 +59,46 @@ rustc_fluent_macro::fluent_messages! { "../messages.ftl" }
5959
pub fn register_builtin_macros(resolver: &mut dyn ResolverExpand) {
6060
let mut register = |name, kind| resolver.register_builtin_macro(name, kind);
6161
macro register_bang($($name:ident: $f:expr,)*) {
62+
$(register(sym::$name, SyntaxExtensionKind::Bang(Box::new($f)));)*
63+
}
64+
macro register_legacy_bang($($name:ident: $f:expr,)*) {
6265
$(register(sym::$name, SyntaxExtensionKind::LegacyBang(Box::new($f as MacroExpanderFn)));)*
6366
}
64-
macro register_attr($($name:ident: $f:expr,)*) {
67+
macro register_legacy_attr($($name:ident: $f:expr,)*) {
6568
$(register(sym::$name, SyntaxExtensionKind::LegacyAttr(Box::new($f)));)*
6669
}
67-
macro register_derive($($name:ident: $f:expr,)*) {
70+
macro register_legacy_derive($($name:ident: $f:expr,)*) {
6871
$(register(sym::$name, SyntaxExtensionKind::LegacyDerive(Box::new(BuiltinDerive($f))));)*
6972
}
7073

7174
register_bang! {
7275
// tidy-alphabetical-start
73-
asm: asm::expand_asm,
74-
assert: assert::expand_assert,
7576
cfg: cfg::expand_cfg,
7677
column: source_util::expand_column,
78+
file: source_util::expand_file,
79+
line: source_util::expand_line,
80+
module_path: source_util::expand_module_path,
81+
// tidy-alphabetical-end
82+
}
83+
84+
register_legacy_bang! {
85+
// tidy-alphabetical-start
86+
asm: asm::expand_asm,
87+
assert: assert::expand_assert,
7788
compile_error: compile_error::expand_compile_error,
7889
concat: concat::expand_concat,
7990
concat_bytes: concat_bytes::expand_concat_bytes,
8091
concat_idents: concat_idents::expand_concat_idents,
8192
const_format_args: format::expand_format_args,
8293
core_panic: edition_panic::expand_panic,
8394
env: env::expand_env,
84-
file: source_util::expand_file,
8595
format_args: format::expand_format_args,
8696
format_args_nl: format::expand_format_args_nl,
8797
global_asm: asm::expand_global_asm,
8898
include: source_util::expand_include,
8999
include_bytes: source_util::expand_include_bytes,
90100
include_str: source_util::expand_include_str,
91-
line: source_util::expand_line,
92101
log_syntax: log_syntax::expand_log_syntax,
93-
module_path: source_util::expand_mod,
94102
option_env: env::expand_option_env,
95103
pattern_type: pattern_type::expand,
96104
std_panic: edition_panic::expand_panic,
@@ -100,7 +108,7 @@ pub fn register_builtin_macros(resolver: &mut dyn ResolverExpand) {
100108
// tidy-alphabetical-end
101109
}
102110

103-
register_attr! {
111+
register_legacy_attr! {
104112
alloc_error_handler: alloc_error_handler::expand,
105113
bench: test::expand_bench,
106114
cfg_accessible: cfg_accessible::Expander,
@@ -112,7 +120,7 @@ pub fn register_builtin_macros(resolver: &mut dyn ResolverExpand) {
112120
test_case: test::expand_test_case,
113121
}
114122

115-
register_derive! {
123+
register_legacy_derive! {
116124
Clone: clone::expand_deriving_clone,
117125
Copy: bounds::expand_deriving_copy,
118126
ConstParamTy: bounds::expand_deriving_const_param_ty,

compiler/rustc_builtin_macros/src/source_util.rs

Lines changed: 26 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use rustc_parse::new_parser_from_file;
1515
use rustc_parse::parser::{ForceCollect, Parser};
1616
use rustc_session::lint::builtin::INCOMPLETE_INCLUDE;
1717
use rustc_span::source_map::SourceMap;
18+
use rustc_span::sym;
1819
use rustc_span::symbol::Symbol;
1920
use rustc_span::{Pos, Span};
2021
use smallvec::SmallVec;
@@ -26,56 +27,54 @@ use std::rc::Rc;
2627
// a given file into the current one.
2728

2829
/// line!(): expands to the current line number
29-
pub(crate) fn expand_line(
30-
cx: &mut ExtCtxt<'_>,
31-
sp: Span,
32-
tts: TokenStream,
33-
) -> MacroExpanderResult<'static> {
30+
pub(crate) fn expand_line(cx: &mut ExtCtxt<'_>, sp: Span, tts: TokenStream) -> TokenStream {
3431
let sp = cx.with_def_site_ctxt(sp);
3532
check_zero_tts(cx, sp, tts, "line!");
3633

3734
let topmost = cx.expansion_cause().unwrap_or(sp);
3835
let loc = cx.source_map().lookup_char_pos(topmost.lo());
3936

40-
ExpandResult::Ready(MacEager::expr(cx.expr_u32(topmost, loc.line as u32)))
37+
let kind = token::TokenKind::lit(
38+
token::LitKind::Integer,
39+
sym::integer(loc.line as u32),
40+
Some(sym::u32),
41+
);
42+
TokenStream::token_alone(kind, topmost)
4143
}
4244

4345
/* column!(): expands to the current column number */
44-
pub(crate) fn expand_column(
45-
cx: &mut ExtCtxt<'_>,
46-
sp: Span,
47-
tts: TokenStream,
48-
) -> MacroExpanderResult<'static> {
46+
pub(crate) fn expand_column(cx: &mut ExtCtxt<'_>, sp: Span, tts: TokenStream) -> TokenStream {
4947
let sp = cx.with_def_site_ctxt(sp);
5048
check_zero_tts(cx, sp, tts, "column!");
5149

5250
let topmost = cx.expansion_cause().unwrap_or(sp);
5351
let loc = cx.source_map().lookup_char_pos(topmost.lo());
5452

55-
ExpandResult::Ready(MacEager::expr(cx.expr_u32(topmost, loc.col.to_usize() as u32 + 1)))
53+
let kind = token::TokenKind::lit(
54+
token::LitKind::Integer,
55+
sym::integer(loc.col.to_usize() as u32 + 1),
56+
Some(sym::u32),
57+
);
58+
TokenStream::token_alone(kind, topmost)
5659
}
5760

5861
/// file!(): expands to the current filename */
5962
/// The source_file (`loc.file`) contains a bunch more information we could spit
6063
/// out if we wanted.
61-
pub(crate) fn expand_file(
62-
cx: &mut ExtCtxt<'_>,
63-
sp: Span,
64-
tts: TokenStream,
65-
) -> MacroExpanderResult<'static> {
64+
pub(crate) fn expand_file(cx: &mut ExtCtxt<'_>, sp: Span, tts: TokenStream) -> TokenStream {
6665
let sp = cx.with_def_site_ctxt(sp);
6766
check_zero_tts(cx, sp, tts, "file!");
6867

6968
let topmost = cx.expansion_cause().unwrap_or(sp);
7069
let loc = cx.source_map().lookup_char_pos(topmost.lo());
7170

7271
use rustc_session::{config::RemapPathScopeComponents, RemapFileNameExt};
73-
ExpandResult::Ready(MacEager::expr(cx.expr_str(
74-
topmost,
75-
Symbol::intern(
76-
&loc.file.name.for_scope(cx.sess, RemapPathScopeComponents::MACRO).to_string_lossy(),
77-
),
78-
)))
72+
73+
let symbol = Symbol::intern(
74+
&loc.file.name.for_scope(cx.sess, RemapPathScopeComponents::MACRO).to_string_lossy(),
75+
);
76+
let kind = token::TokenKind::lit(token::LitKind::Str, symbol, None);
77+
TokenStream::token_alone(kind, topmost)
7978
}
8079

8180
pub(crate) fn expand_stringify(
@@ -88,17 +87,15 @@ pub(crate) fn expand_stringify(
8887
ExpandResult::Ready(MacEager::expr(cx.expr_str(sp, Symbol::intern(&s))))
8988
}
9089

91-
pub(crate) fn expand_mod(
92-
cx: &mut ExtCtxt<'_>,
93-
sp: Span,
94-
tts: TokenStream,
95-
) -> MacroExpanderResult<'static> {
90+
pub(crate) fn expand_module_path(cx: &mut ExtCtxt<'_>, sp: Span, tts: TokenStream) -> TokenStream {
9691
let sp = cx.with_def_site_ctxt(sp);
9792
check_zero_tts(cx, sp, tts, "module_path!");
9893
let mod_path = &cx.current_expansion.module.mod_path;
9994
let string = mod_path.iter().map(|x| x.to_string()).collect::<Vec<String>>().join("::");
10095

101-
ExpandResult::Ready(MacEager::expr(cx.expr_str(sp, Symbol::intern(&string))))
96+
let symbol = Symbol::intern(&string);
97+
let kind = token::TokenKind::lit(token::LitKind::Str, symbol, None);
98+
TokenStream::token_alone(kind, sp)
10299
}
103100

104101
/// include! : parse the given file as an expr

compiler/rustc_expand/src/base.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -290,16 +290,15 @@ pub trait BangProcMacro {
290290

291291
impl<F> BangProcMacro for F
292292
where
293-
F: Fn(TokenStream) -> TokenStream,
293+
F: Fn(&mut ExtCtxt<'_>, Span, TokenStream) -> TokenStream,
294294
{
295295
fn expand<'cx>(
296296
&self,
297-
_ecx: &'cx mut ExtCtxt<'_>,
298-
_span: Span,
297+
ecx: &'cx mut ExtCtxt<'_>,
298+
span: Span,
299299
ts: TokenStream,
300300
) -> Result<TokenStream, ErrorGuaranteed> {
301-
// FIXME setup implicit context in TLS before calling self.
302-
Ok(self(ts))
301+
Ok(self(ecx, span, ts))
303302
}
304303
}
305304

tests/ui/macros/macro-error.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@ macro_rules! foo {
55
fn main() {
66
foo!(0); // Check that we report errors at macro definition, not expansion.
77

8-
let _: cfg!(FALSE) = (); //~ ERROR non-type macro in type position
8+
let _: cfg!(FALSE) = (); //~ ERROR expected type, found keyword `false`
99
}

tests/ui/macros/macro-error.stderr

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,16 @@ error: macro rhs must be delimited
44
LL | ($a:expr) => a;
55
| ^
66

7-
error: non-type macro in type position: cfg
7+
error: expected type, found keyword `false`
88
--> $DIR/macro-error.rs:8:12
99
|
1010
LL | let _: cfg!(FALSE) = ();
1111
| ^^^^^^^^^^^
12+
| |
13+
| expected type
14+
| this macro call doesn't expand to a type
15+
|
16+
= note: this error originates in the macro `cfg` (in Nightly builds, run with -Z macro-backtrace for more info)
1217

1318
error: aborting due to 2 previous errors
1419

0 commit comments

Comments
 (0)