Skip to content

Commit 2ede1b3

Browse files
committed
Convert a few builtin macros from LegacyBang to Bang
1 parent a1646bf commit 2ede1b3

File tree

2 files changed

+44
-23
lines changed

2 files changed

+44
-23
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)`.

0 commit comments

Comments
 (0)