Skip to content

Commit 796d6cf

Browse files
Auto merge of #146850 - joshtriplett:macro-reduce-legacy-bang-more, r=<try>
Convert a few builtin macros from `LegacyBang` to `Bang`
2 parents f6092f2 + 2ede1b3 commit 796d6cf

File tree

5 files changed

+63
-45
lines changed

5 files changed

+63
-45
lines changed

compiler/rustc_builtin_macros/src/lib.rs

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -64,50 +64,58 @@ rustc_fluent_macro::fluent_messages! { "../messages.ftl" }
6464

6565
pub fn register_builtin_macros(resolver: &mut dyn ResolverExpand) {
6666
let mut register = |name, kind| resolver.register_builtin_macro(name, kind);
67-
macro register_bang($($name:ident: $f:expr,)*) {
67+
macro register_legacy_bang($($name:ident: $f:expr,)*) {
6868
$(register(sym::$name, SyntaxExtensionKind::LegacyBang(Arc::new($f as MacroExpanderFn)));)*
6969
}
70+
macro register_bang($($name:ident: $f:expr,)*) {
71+
$(register(sym::$name, SyntaxExtensionKind::Bang(Arc::new($f)));)*
72+
}
7073
macro register_attr($($name:ident: $f:expr,)*) {
7174
$(register(sym::$name, SyntaxExtensionKind::LegacyAttr(Arc::new($f)));)*
7275
}
7376
macro register_derive($($name:ident: $f:expr,)*) {
7477
$(register(sym::$name, SyntaxExtensionKind::LegacyDerive(Arc::new(BuiltinDerive($f))));)*
7578
}
7679

77-
register_bang! {
80+
register_legacy_bang! {
7881
// tidy-alphabetical-start
7982
asm: asm::expand_asm,
8083
assert: assert::expand_assert,
8184
cfg: cfg::expand_cfg,
8285
cfg_select: cfg_select::expand_cfg_select,
83-
column: source_util::expand_column,
8486
compile_error: compile_error::expand_compile_error,
8587
concat: concat::expand_concat,
8688
concat_bytes: concat_bytes::expand_concat_bytes,
8789
const_format_args: format::expand_format_args,
8890
core_panic: edition_panic::expand_panic,
8991
env: env::expand_env,
90-
file: source_util::expand_file,
9192
format_args: format::expand_format_args,
9293
format_args_nl: format::expand_format_args_nl,
9394
global_asm: asm::expand_global_asm,
9495
include: source_util::expand_include,
9596
include_bytes: source_util::expand_include_bytes,
9697
include_str: source_util::expand_include_str,
9798
iter: iter::expand,
98-
line: source_util::expand_line,
9999
log_syntax: log_syntax::expand_log_syntax,
100-
module_path: source_util::expand_mod,
101100
naked_asm: asm::expand_naked_asm,
102101
option_env: env::expand_option_env,
103102
pattern_type: pattern_type::expand,
104103
std_panic: edition_panic::expand_panic,
105-
stringify: source_util::expand_stringify,
106104
trace_macros: trace_macros::expand_trace_macros,
107105
unreachable: edition_panic::expand_unreachable,
108106
// tidy-alphabetical-end
109107
}
110108

109+
register_bang! {
110+
// tidy-alphabetical-start
111+
column: source_util::expand_column,
112+
file: source_util::expand_file,
113+
line: source_util::expand_line,
114+
module_path: source_util::expand_mod,
115+
stringify: source_util::expand_stringify,
116+
// tidy-alphabetical-end
117+
}
118+
111119
register_attr! {
112120
// tidy-alphabetical-start
113121
alloc_error_handler: alloc_error_handler::expand,

compiler/rustc_builtin_macros/src/source_util.rs

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use std::rc::Rc;
55
use std::sync::Arc;
66

77
use rustc_ast as ast;
8+
use rustc_ast::token::TokenKind;
89
use rustc_ast::tokenstream::TokenStream;
910
use rustc_ast::{join_path_idents, token};
1011
use rustc_ast_pretty::pprust;
@@ -18,7 +19,7 @@ use rustc_parse::{new_parser_from_file, unwrap_or_emit_fatal, utf8_error};
1819
use rustc_session::lint::builtin::INCOMPLETE_INCLUDE;
1920
use rustc_session::parse::ParseSess;
2021
use rustc_span::source_map::SourceMap;
21-
use rustc_span::{ByteSymbol, Pos, Span, Symbol};
22+
use rustc_span::{ByteSymbol, ErrorGuaranteed, Pos, Span, Symbol, sym};
2223
use smallvec::SmallVec;
2324

2425
use crate::errors;
@@ -31,37 +32,43 @@ pub(crate) fn expand_line(
3132
cx: &mut ExtCtxt<'_>,
3233
sp: Span,
3334
tts: TokenStream,
34-
) -> MacroExpanderResult<'static> {
35+
) -> Result<TokenStream, ErrorGuaranteed> {
3536
let sp = cx.with_def_site_ctxt(sp);
3637
check_zero_tts(cx, sp, tts, "line!");
3738

3839
let topmost = cx.expansion_cause().unwrap_or(sp);
3940
let loc = cx.source_map().lookup_char_pos(topmost.lo());
4041

41-
ExpandResult::Ready(MacEager::expr(cx.expr_u32(topmost, loc.line as u32)))
42+
Ok(TokenStream::token_alone(
43+
TokenKind::lit(token::Integer, sym::integer(loc.line), Some(sym::u32)),
44+
sp,
45+
))
4246
}
4347

4448
/// Expand `column!()` to the current column number.
4549
pub(crate) fn expand_column(
4650
cx: &mut ExtCtxt<'_>,
4751
sp: Span,
4852
tts: TokenStream,
49-
) -> MacroExpanderResult<'static> {
53+
) -> Result<TokenStream, ErrorGuaranteed> {
5054
let sp = cx.with_def_site_ctxt(sp);
5155
check_zero_tts(cx, sp, tts, "column!");
5256

5357
let topmost = cx.expansion_cause().unwrap_or(sp);
5458
let loc = cx.source_map().lookup_char_pos(topmost.lo());
5559

56-
ExpandResult::Ready(MacEager::expr(cx.expr_u32(topmost, loc.col.to_usize() as u32 + 1)))
60+
Ok(TokenStream::token_alone(
61+
TokenKind::lit(token::Integer, sym::integer(loc.col.to_usize() + 1), Some(sym::u32)),
62+
sp,
63+
))
5764
}
5865

5966
/// Expand `file!()` to the current filename.
6067
pub(crate) fn expand_file(
6168
cx: &mut ExtCtxt<'_>,
6269
sp: Span,
6370
tts: TokenStream,
64-
) -> MacroExpanderResult<'static> {
71+
) -> Result<TokenStream, ErrorGuaranteed> {
6572
let sp = cx.with_def_site_ctxt(sp);
6673
check_zero_tts(cx, sp, tts, "file!");
6774

@@ -70,37 +77,43 @@ pub(crate) fn expand_file(
7077

7178
use rustc_session::RemapFileNameExt;
7279
use rustc_session::config::RemapPathScopeComponents;
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(),
80+
Ok(TokenStream::token_alone(
81+
TokenKind::lit(
82+
token::RawStr,
83+
Symbol::intern(
84+
&loc.file
85+
.name
86+
.for_scope(cx.sess, RemapPathScopeComponents::MACRO)
87+
.to_string_lossy(),
88+
),
89+
None,
7790
),
78-
)))
91+
sp,
92+
))
7993
}
8094

8195
/// Expand `stringify!($input)`.
8296
pub(crate) fn expand_stringify(
8397
cx: &mut ExtCtxt<'_>,
8498
sp: Span,
8599
tts: TokenStream,
86-
) -> MacroExpanderResult<'static> {
100+
) -> Result<TokenStream, ErrorGuaranteed> {
87101
let sp = cx.with_def_site_ctxt(sp);
88102
let s = pprust::tts_to_string(&tts);
89-
ExpandResult::Ready(MacEager::expr(cx.expr_str(sp, Symbol::intern(&s))))
103+
Ok(TokenStream::token_alone(TokenKind::lit(token::RawStr, Symbol::intern(&s), None), sp))
90104
}
91105

92106
/// Expand `module_path!()` to (a textual representation of) the current module path.
93107
pub(crate) fn expand_mod(
94108
cx: &mut ExtCtxt<'_>,
95109
sp: Span,
96110
tts: TokenStream,
97-
) -> MacroExpanderResult<'static> {
111+
) -> Result<TokenStream, ErrorGuaranteed> {
98112
let sp = cx.with_def_site_ctxt(sp);
99113
check_zero_tts(cx, sp, tts, "module_path!");
100114
let mod_path = &cx.current_expansion.module.mod_path;
101115
let string = join_path_idents(mod_path);
102-
103-
ExpandResult::Ready(MacEager::expr(cx.expr_str(sp, Symbol::intern(&string))))
116+
Ok(TokenStream::token_alone(TokenKind::lit(token::RawStr, Symbol::intern(&string), None), sp))
104117
}
105118

106119
/// Expand `include!($input)`.

compiler/rustc_expand/src/base.rs

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -324,16 +324,16 @@ pub trait BangProcMacro {
324324

325325
impl<F> BangProcMacro for F
326326
where
327-
F: Fn(TokenStream) -> TokenStream,
327+
F: Fn(&mut ExtCtxt<'_>, Span, TokenStream) -> Result<TokenStream, ErrorGuaranteed>,
328328
{
329329
fn expand<'cx>(
330330
&self,
331-
_ecx: &'cx mut ExtCtxt<'_>,
332-
_span: Span,
331+
ecx: &'cx mut ExtCtxt<'_>,
332+
span: Span,
333333
ts: TokenStream,
334334
) -> Result<TokenStream, ErrorGuaranteed> {
335335
// FIXME setup implicit context in TLS before calling self.
336-
Ok(self(ts))
336+
self(ecx, span, ts)
337337
}
338338
}
339339

@@ -999,17 +999,14 @@ impl SyntaxExtension {
999999

10001000
/// A dummy bang macro `foo!()`.
10011001
pub fn dummy_bang(edition: Edition) -> SyntaxExtension {
1002-
fn expander<'cx>(
1003-
cx: &'cx mut ExtCtxt<'_>,
1002+
fn expand(
1003+
ecx: &mut ExtCtxt<'_>,
10041004
span: Span,
1005-
_: TokenStream,
1006-
) -> MacroExpanderResult<'cx> {
1007-
ExpandResult::Ready(DummyResult::any(
1008-
span,
1009-
cx.dcx().span_delayed_bug(span, "expanded a dummy bang macro"),
1010-
))
1005+
_ts: TokenStream,
1006+
) -> Result<TokenStream, ErrorGuaranteed> {
1007+
Err(ecx.dcx().span_delayed_bug(span, "expanded a dummy bang macro"))
10111008
}
1012-
SyntaxExtension::default(SyntaxExtensionKind::LegacyBang(Arc::new(expander)), edition)
1009+
SyntaxExtension::default(SyntaxExtensionKind::Bang(Arc::new(expand)), edition)
10131010
}
10141011

10151012
/// A dummy derive macro `#[derive(Foo)]`.

compiler/rustc_expand/src/expand.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -971,7 +971,7 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
971971
});
972972
}
973973
},
974-
SyntaxExtensionKind::LegacyBang(..) => {
974+
SyntaxExtensionKind::Bang(..) => {
975975
let msg = "expanded a dummy glob delegation";
976976
let guar = self.cx.dcx().span_delayed_bug(span, msg);
977977
return ExpandResult::Ready(fragment_kind.dummy(span, guar));

compiler/rustc_expand/src/mbe/macro_rules.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ use super::diagnostics::{FailedMacro, failed_to_match_macro};
3333
use super::macro_parser::{NamedMatches, NamedParseResult};
3434
use super::{SequenceRepetition, diagnostics};
3535
use crate::base::{
36-
AttrProcMacro, DummyResult, ExpandResult, ExtCtxt, MacResult, MacroExpanderResult,
37-
SyntaxExtension, SyntaxExtensionKind, TTMacroExpander,
36+
AttrProcMacro, BangProcMacro, DummyResult, ExpandResult, ExtCtxt, MacResult,
37+
MacroExpanderResult, SyntaxExtension, SyntaxExtensionKind, TTMacroExpander,
3838
};
3939
use crate::errors;
4040
use crate::expand::{AstFragment, AstFragmentKind, ensure_complete_parse, parse_ast_fragment};
@@ -267,16 +267,16 @@ impl AttrProcMacro for MacroRulesMacroExpander {
267267
}
268268
}
269269

270-
struct DummyExpander(ErrorGuaranteed);
270+
struct DummyBang(ErrorGuaranteed);
271271

272-
impl TTMacroExpander for DummyExpander {
272+
impl BangProcMacro for DummyBang {
273273
fn expand<'cx>(
274274
&self,
275275
_: &'cx mut ExtCtxt<'_>,
276-
span: Span,
276+
_: Span,
277277
_: TokenStream,
278-
) -> ExpandResult<Box<dyn MacResult + 'cx>, ()> {
279-
ExpandResult::Ready(DummyResult::any(span, self.0))
278+
) -> Result<TokenStream, ErrorGuaranteed> {
279+
Err(self.0)
280280
}
281281
}
282282

@@ -664,7 +664,7 @@ pub fn compile_declarative_macro(
664664
SyntaxExtension::new(sess, kind, span, Vec::new(), edition, ident.name, attrs, is_local)
665665
};
666666
let dummy_syn_ext =
667-
|guar| (mk_syn_ext(SyntaxExtensionKind::LegacyBang(Arc::new(DummyExpander(guar)))), 0);
667+
|guar| (mk_syn_ext(SyntaxExtensionKind::Bang(Arc::new(DummyBang(guar)))), 0);
668668

669669
let macro_rules = macro_def.macro_rules;
670670
let exp_sep = if macro_rules { exp!(Semi) } else { exp!(Comma) };

0 commit comments

Comments
 (0)