Skip to content

Commit 49e479a

Browse files
committed
Add support for literals
1 parent a0f01c3 commit 49e479a

File tree

6 files changed

+227
-53
lines changed

6 files changed

+227
-53
lines changed

compiler/rustc_expand/src/mbe/metavar_expr.rs

+44-21
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
use rustc_ast::token::{self, Delimiter, IdentIsRaw};
1+
use rustc_ast::token::{self, Delimiter, IdentIsRaw, Lit, Token, TokenKind};
22
use rustc_ast::tokenstream::{RefTokenTreeCursor, TokenStream, TokenTree};
33
use rustc_ast::{LitIntType, LitKind};
44
use rustc_ast_pretty::pprust;
55
use rustc_errors::{Applicability, PResult};
66
use rustc_macros::{Decodable, Encodable};
77
use rustc_session::parse::ParseSess;
88
use rustc_span::symbol::Ident;
9-
use rustc_span::Span;
9+
use rustc_span::{Span, Symbol};
1010

1111
pub(crate) const RAW_IDENT_ERR: &str = "`${concat(..)}` currently does not support raw identifiers";
1212

@@ -51,11 +51,18 @@ impl MetaVarExpr {
5151
let mut result = Vec::new();
5252
loop {
5353
let is_var = try_eat_dollar(&mut iter);
54-
let element_ident = parse_ident(&mut iter, psess, outer_span)?;
54+
let token = parse_token(&mut iter, psess, outer_span)?;
5555
let element = if is_var {
56-
MetaVarExprConcatElem::Var(element_ident)
56+
MetaVarExprConcatElem::Var(parse_ident_from_token(psess, token)?)
57+
} else if let TokenKind::Literal(Lit {
58+
kind: token::LitKind::Char | token::LitKind::Integer | token::LitKind::Str,
59+
symbol,
60+
suffix: None,
61+
}) = token.kind
62+
{
63+
MetaVarExprConcatElem::Literal(symbol)
5764
} else {
58-
MetaVarExprConcatElem::Ident(element_ident)
65+
MetaVarExprConcatElem::Ident(parse_ident_from_token(psess, token)?)
5966
};
6067
result.push(element);
6168
if iter.look_ahead(0).is_none() {
@@ -105,11 +112,13 @@ impl MetaVarExpr {
105112

106113
#[derive(Debug, Decodable, Encodable, PartialEq)]
107114
pub(crate) enum MetaVarExprConcatElem {
108-
/// There is NO preceding dollar sign, which means that this identifier should be interpreted
109-
/// as a literal.
115+
/// Identifier WITHOUT a preceding dollar sign, which means that this identifier should be
116+
/// interpreted as a literal.
110117
Ident(Ident),
111-
/// There is a preceding dollar sign, which means that this identifier should be expanded
112-
/// and interpreted as a variable.
118+
/// For example, a number or a string.
119+
Literal(Symbol),
120+
/// Identifier WITH a preceding dollar sign, which means that this identifier should be
121+
/// expanded and interpreted as a variable.
113122
Var(Ident),
114123
}
115124

@@ -158,7 +167,7 @@ fn parse_depth<'psess>(
158167
span: Span,
159168
) -> PResult<'psess, usize> {
160169
let Some(tt) = iter.next() else { return Ok(0) };
161-
let TokenTree::Token(token::Token { kind: token::TokenKind::Literal(lit), .. }, _) = tt else {
170+
let TokenTree::Token(Token { kind: TokenKind::Literal(lit), .. }, _) = tt else {
162171
return Err(psess
163172
.dcx()
164173
.struct_span_err(span, "meta-variable expression depth must be a literal"));
@@ -180,12 +189,14 @@ fn parse_ident<'psess>(
180189
psess: &'psess ParseSess,
181190
fallback_span: Span,
182191
) -> PResult<'psess, Ident> {
183-
let Some(tt) = iter.next() else {
184-
return Err(psess.dcx().struct_span_err(fallback_span, "expected identifier"));
185-
};
186-
let TokenTree::Token(token, _) = tt else {
187-
return Err(psess.dcx().struct_span_err(tt.span(), "expected identifier"));
188-
};
192+
let token = parse_token(iter, psess, fallback_span)?;
193+
parse_ident_from_token(psess, token)
194+
}
195+
196+
fn parse_ident_from_token<'psess>(
197+
psess: &'psess ParseSess,
198+
token: &Token,
199+
) -> PResult<'psess, Ident> {
189200
if let Some((elem, is_raw)) = token.ident() {
190201
if let IdentIsRaw::Yes = is_raw {
191202
return Err(psess.dcx().struct_span_err(elem.span, RAW_IDENT_ERR));
@@ -205,10 +216,24 @@ fn parse_ident<'psess>(
205216
Err(err)
206217
}
207218

219+
fn parse_token<'psess, 't>(
220+
iter: &mut RefTokenTreeCursor<'t>,
221+
psess: &'psess ParseSess,
222+
fallback_span: Span,
223+
) -> PResult<'psess, &'t Token> {
224+
let Some(tt) = iter.next() else {
225+
return Err(psess.dcx().struct_span_err(fallback_span, "expected identifier or literal"));
226+
};
227+
let TokenTree::Token(token, _) = tt else {
228+
return Err(psess.dcx().struct_span_err(tt.span(), "expected identifier or literal"));
229+
};
230+
Ok(token)
231+
}
232+
208233
/// Tries to move the iterator forward returning `true` if there is a comma. If not, then the
209234
/// iterator is not modified and the result is `false`.
210235
fn try_eat_comma(iter: &mut RefTokenTreeCursor<'_>) -> bool {
211-
if let Some(TokenTree::Token(token::Token { kind: token::Comma, .. }, _)) = iter.look_ahead(0) {
236+
if let Some(TokenTree::Token(Token { kind: token::Comma, .. }, _)) = iter.look_ahead(0) {
212237
let _ = iter.next();
213238
return true;
214239
}
@@ -218,8 +243,7 @@ fn try_eat_comma(iter: &mut RefTokenTreeCursor<'_>) -> bool {
218243
/// Tries to move the iterator forward returning `true` if there is a dollar sign. If not, then the
219244
/// iterator is not modified and the result is `false`.
220245
fn try_eat_dollar(iter: &mut RefTokenTreeCursor<'_>) -> bool {
221-
if let Some(TokenTree::Token(token::Token { kind: token::Dollar, .. }, _)) = iter.look_ahead(0)
222-
{
246+
if let Some(TokenTree::Token(Token { kind: token::Dollar, .. }, _)) = iter.look_ahead(0) {
223247
let _ = iter.next();
224248
return true;
225249
}
@@ -232,8 +256,7 @@ fn eat_dollar<'psess>(
232256
psess: &'psess ParseSess,
233257
span: Span,
234258
) -> PResult<'psess, ()> {
235-
if let Some(TokenTree::Token(token::Token { kind: token::Dollar, .. }, _)) = iter.look_ahead(0)
236-
{
259+
if let Some(TokenTree::Token(Token { kind: token::Dollar, .. }, _)) = iter.look_ahead(0) {
237260
let _ = iter.next();
238261
return Ok(());
239262
}

compiler/rustc_expand/src/mbe/transcribe.rs

+27-5
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,13 @@ use rustc_ast::token::{self, Delimiter, Token, TokenKind};
1111
use rustc_ast::tokenstream::{DelimSpacing, DelimSpan, Spacing, TokenStream, TokenTree};
1212
use rustc_data_structures::fx::FxHashMap;
1313
use rustc_errors::{pluralize, Diag, DiagCtxtHandle, PResult};
14+
use rustc_parse::lexer::nfc_normalize;
1415
use rustc_parse::parser::ParseNtResult;
1516
use rustc_session::parse::ParseSess;
17+
use rustc_session::parse::SymbolGallery;
1618
use rustc_span::hygiene::{LocalExpnId, Transparency};
1719
use rustc_span::symbol::{sym, Ident, MacroRulesNormalizedIdent};
18-
use rustc_span::{with_metavar_spans, Span, Symbol, SyntaxContext};
20+
use rustc_span::{with_metavar_spans, Span, SyntaxContext};
1921
use smallvec::{smallvec, SmallVec};
2022
use std::mem;
2123

@@ -312,7 +314,16 @@ pub(super) fn transcribe<'a>(
312314

313315
// Replace meta-variable expressions with the result of their expansion.
314316
mbe::TokenTree::MetaVarExpr(sp, expr) => {
315-
transcribe_metavar_expr(dcx, expr, interp, &mut marker, &repeats, &mut result, sp)?;
317+
transcribe_metavar_expr(
318+
dcx,
319+
expr,
320+
interp,
321+
&mut marker,
322+
&repeats,
323+
&mut result,
324+
sp,
325+
&psess.symbol_gallery,
326+
)?;
316327
}
317328

318329
// If we are entering a new delimiter, we push its contents to the `stack` to be
@@ -669,6 +680,7 @@ fn transcribe_metavar_expr<'a>(
669680
repeats: &[(usize, usize)],
670681
result: &mut Vec<TokenTree>,
671682
sp: &DelimSpan,
683+
symbol_gallery: &SymbolGallery,
672684
) -> PResult<'a, ()> {
673685
let mut visited_span = || {
674686
let mut span = sp.entire();
@@ -680,16 +692,26 @@ fn transcribe_metavar_expr<'a>(
680692
let mut concatenated = String::new();
681693
for element in elements.into_iter() {
682694
let string = match element {
683-
MetaVarExprConcatElem::Ident(ident) => ident.to_string(),
684-
MetaVarExprConcatElem::Var(ident) => extract_ident(dcx, *ident, interp)?,
695+
MetaVarExprConcatElem::Ident(elem) => elem.to_string(),
696+
MetaVarExprConcatElem::Literal(elem) => elem.as_str().into(),
697+
MetaVarExprConcatElem::Var(elem) => extract_ident(dcx, *elem, interp)?,
685698
};
686699
concatenated.push_str(&string);
687700
}
701+
let symbol = nfc_normalize(&concatenated);
702+
let concatenated_span = visited_span();
703+
if !rustc_lexer::is_ident(symbol.as_str()) {
704+
return Err(dcx.struct_span_err(
705+
concatenated_span,
706+
"`${concat(..)}` is not generating a valid identifier",
707+
));
708+
}
709+
symbol_gallery.insert(symbol, concatenated_span);
688710
// The current implementation marks the span as coming from the macro regardless of
689711
// contexts of the concatenated identifiers but this behavior may change in the
690712
// future.
691713
result.push(TokenTree::Token(
692-
Token::from_ast_ident(Ident::new(Symbol::intern(&concatenated), visited_span())),
714+
Token::from_ast_ident(Ident::new(symbol, concatenated_span)),
693715
Spacing::Alone,
694716
));
695717
}

tests/ui/macros/macro-metavar-expr-concat/allowed-operations.rs

+14
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,18 @@ macro_rules! without_dollar_sign_is_an_ident {
3737
};
3838
}
3939

40+
macro_rules! literals {
41+
($ident:ident) => {{
42+
let ${concat(_a, 'b')}: () = ();
43+
let ${concat(_a, 1)}: () = ();
44+
let ${concat(_a, "b")}: () = ();
45+
46+
let ${concat($ident, 'b')}: () = ();
47+
let ${concat($ident, 1)}: () = ();
48+
let ${concat($ident, "b")}: () = ();
49+
}};
50+
}
51+
4052
fn main() {
4153
create_things!(behold);
4254
behold_separated_idents_in_a_fn();
@@ -55,4 +67,6 @@ fn main() {
5567
without_dollar_sign_is_an_ident!(_123);
5668
assert_eq!(VARident, 1);
5769
assert_eq!(VAR_123, 2);
70+
71+
literals!(_hello);
5872
}

tests/ui/macros/macro-metavar-expr-concat/syntax-errors.rs

+61-6
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,6 @@ macro_rules! wrong_concat_declarations {
1111
${concat(aaaa,)}
1212
//~^ ERROR expected identifier
1313

14-
${concat(aaaa, 1)}
15-
//~^ ERROR expected identifier
16-
1714
${concat(_, aaaa)}
1815

1916
${concat(aaaa aaaa)}
@@ -30,9 +27,6 @@ macro_rules! wrong_concat_declarations {
3027

3128
${concat($ex, aaaa,)}
3229
//~^ ERROR expected identifier
33-
34-
${concat($ex, aaaa, 123)}
35-
//~^ ERROR expected identifier
3630
};
3731
}
3832

@@ -43,8 +37,69 @@ macro_rules! dollar_sign_without_referenced_ident {
4337
};
4438
}
4539

40+
macro_rules! starting_number {
41+
($ident:ident) => {{
42+
let ${concat(1, $ident)}: () = ();
43+
//~^ ERROR `${concat(..)}` is not generating a valid identifier
44+
}};
45+
}
46+
47+
macro_rules! starting_valid_unicode {
48+
($ident:ident) => {{
49+
let ${concat('Ý', $ident)}: () = ();
50+
}};
51+
}
52+
53+
macro_rules! starting_invalid_unicode {
54+
($ident:ident) => {{
55+
let ${concat("\u{999999}", $ident)}: () = ();
56+
//~^ ERROR invalid unicode character escape
57+
//~| ERROR expected identifier, found
58+
//~| ERROR expected pattern, found
59+
}};
60+
}
61+
62+
macro_rules! ending_number {
63+
($ident:ident) => {{
64+
let ${concat($ident, 1)}: () = ();
65+
}};
66+
}
67+
68+
macro_rules! ending_valid_unicode {
69+
($ident:ident) => {{
70+
let ${concat($ident, 'Ý')}: () = ();
71+
}};
72+
}
73+
74+
macro_rules! ending_invalid_unicode {
75+
($ident:ident) => {{
76+
let ${concat($ident, "\u{999999}")}: () = ();
77+
//~^ ERROR invalid unicode character escape
78+
//~| ERROR expected identifier, found
79+
//~| ERROR expected pattern, found
80+
}};
81+
}
82+
83+
macro_rules! empty {
84+
() => {{
85+
let ${concat("", "")}: () = ();
86+
//~^ ERROR `${concat(..)}` is not generating a valid identifier
87+
}};
88+
}
89+
90+
4691
fn main() {
4792
wrong_concat_declarations!(1);
4893

4994
dollar_sign_without_referenced_ident!(VAR);
95+
96+
starting_number!(_abc);
97+
starting_valid_unicode!(_abc);
98+
starting_invalid_unicode!(_abc);
99+
100+
ending_number!(_abc);
101+
ending_valid_unicode!(_abc);
102+
ending_invalid_unicode!(_abc);
103+
104+
empty!();
50105
}

0 commit comments

Comments
 (0)