Skip to content

Commit 47b1896

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

File tree

2 files changed

+24
-9
lines changed

2 files changed

+24
-9
lines changed

compiler/rustc_builtin_macros/src/lib.rs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,23 +64,25 @@ 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,
@@ -95,7 +97,6 @@ pub fn register_builtin_macros(resolver: &mut dyn ResolverExpand) {
9597
include_bytes: source_util::expand_include_bytes,
9698
include_str: source_util::expand_include_str,
9799
iter: iter::expand,
98-
line: source_util::expand_line,
99100
log_syntax: log_syntax::expand_log_syntax,
100101
module_path: source_util::expand_mod,
101102
naked_asm: asm::expand_naked_asm,
@@ -108,6 +109,13 @@ pub fn register_builtin_macros(resolver: &mut dyn ResolverExpand) {
108109
// tidy-alphabetical-end
109110
}
110111

112+
register_bang! {
113+
// tidy-alphabetical-start
114+
column: source_util::expand_column,
115+
line: source_util::expand_line,
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: 12 additions & 5 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,29 +32,35 @@ 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.

0 commit comments

Comments
 (0)