Skip to content

Commit fffdd21

Browse files
committed
Add support for literals
1 parent a0f01c3 commit fffdd21

File tree

5 files changed

+64
-42
lines changed

5 files changed

+64
-42
lines changed

compiler/rustc_expand/src/mbe/metavar_expr.rs

+40-15
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, Token};
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 token::TokenKind::Literal(token::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

@@ -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,6 +216,20 @@ 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"));
226+
};
227+
let TokenTree::Token(token, _) = tt else {
228+
return Err(psess.dcx().struct_span_err(tt.span(), "expected identifier"));
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 {

compiler/rustc_expand/src/mbe/transcribe.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -680,8 +680,9 @@ fn transcribe_metavar_expr<'a>(
680680
let mut concatenated = String::new();
681681
for element in elements.into_iter() {
682682
let string = match element {
683-
MetaVarExprConcatElem::Ident(ident) => ident.to_string(),
684-
MetaVarExprConcatElem::Var(ident) => extract_ident(dcx, *ident, interp)?,
683+
MetaVarExprConcatElem::Ident(elem) => elem.to_string(),
684+
MetaVarExprConcatElem::Literal(elem) => elem.as_str().into(),
685+
MetaVarExprConcatElem::Var(elem) => extract_ident(dcx, *elem, interp)?,
685686
};
686687
concatenated.push_str(&string);
687688
}

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+
($var:ident) => {{
42+
let ${concat(_a, 'b')}: () = ();
43+
let ${concat(_a, 1)}: () = ();
44+
let ${concat(_a, "b")}: () = ();
45+
46+
let ${concat($var, 'b')}: () = ();
47+
let ${concat($var, 1)}: () = ();
48+
let ${concat($var, "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

-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

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

+7-19
Original file line numberDiff line numberDiff line change
@@ -16,53 +16,41 @@ error: expected identifier
1616
LL | ${concat(aaaa,)}
1717
| ^^^^^^^^^^^^^^^
1818

19-
error: expected identifier, found `1`
20-
--> $DIR/syntax-errors.rs:14:24
21-
|
22-
LL | ${concat(aaaa, 1)}
23-
| ^ help: try removing `1`
24-
2519
error: expected comma
26-
--> $DIR/syntax-errors.rs:19:10
20+
--> $DIR/syntax-errors.rs:16:10
2721
|
2822
LL | ${concat(aaaa aaaa)}
2923
| ^^^^^^^^^^^^^^^^^^^
3024

3125
error: `concat` must have at least two elements
32-
--> $DIR/syntax-errors.rs:22:11
26+
--> $DIR/syntax-errors.rs:19:11
3327
|
3428
LL | ${concat($ex)}
3529
| ^^^^^^
3630

3731
error: expected comma
38-
--> $DIR/syntax-errors.rs:28:10
32+
--> $DIR/syntax-errors.rs:25:10
3933
|
4034
LL | ${concat($ex, aaaa 123)}
4135
| ^^^^^^^^^^^^^^^^^^^^^^^
4236

4337
error: expected identifier
44-
--> $DIR/syntax-errors.rs:31:10
38+
--> $DIR/syntax-errors.rs:28:10
4539
|
4640
LL | ${concat($ex, aaaa,)}
4741
| ^^^^^^^^^^^^^^^^^^^^
4842

49-
error: expected identifier, found `123`
50-
--> $DIR/syntax-errors.rs:34:29
51-
|
52-
LL | ${concat($ex, aaaa, 123)}
53-
| ^^^ help: try removing `123`
54-
5543
error: `${concat(..)}` currently only accepts identifiers or meta-variables as parameters
56-
--> $DIR/syntax-errors.rs:25:19
44+
--> $DIR/syntax-errors.rs:22:19
5745
|
5846
LL | ${concat($ex, aaaa)}
5947
| ^^
6048

6149
error: variable `foo` is not recognized in meta-variable expression
62-
--> $DIR/syntax-errors.rs:41:30
50+
--> $DIR/syntax-errors.rs:35:30
6351
|
6452
LL | const ${concat(FOO, $foo)}: i32 = 2;
6553
| ^^^
6654

67-
error: aborting due to 11 previous errors
55+
error: aborting due to 9 previous errors
6856

0 commit comments

Comments
 (0)