Skip to content

Commit

Permalink
Add support for literals
Browse files Browse the repository at this point in the history
  • Loading branch information
c410-f3r committed Jun 29, 2024
1 parent a0f01c3 commit 4a9dcc6
Show file tree
Hide file tree
Showing 6 changed files with 170 additions and 53 deletions.
65 changes: 44 additions & 21 deletions compiler/rustc_expand/src/mbe/metavar_expr.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use rustc_ast::token::{self, Delimiter, IdentIsRaw};
use rustc_ast::token::{self, Delimiter, IdentIsRaw, Lit, Token, TokenKind};
use rustc_ast::tokenstream::{RefTokenTreeCursor, TokenStream, TokenTree};
use rustc_ast::{LitIntType, LitKind};
use rustc_ast_pretty::pprust;
use rustc_errors::{Applicability, PResult};
use rustc_macros::{Decodable, Encodable};
use rustc_session::parse::ParseSess;
use rustc_span::symbol::Ident;
use rustc_span::Span;
use rustc_span::{Span, Symbol};

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

Expand Down Expand Up @@ -51,11 +51,18 @@ impl MetaVarExpr {
let mut result = Vec::new();
loop {
let is_var = try_eat_dollar(&mut iter);
let element_ident = parse_ident(&mut iter, psess, outer_span)?;
let token = parse_token(&mut iter, psess, outer_span)?;
let element = if is_var {
MetaVarExprConcatElem::Var(element_ident)
MetaVarExprConcatElem::Var(parse_ident_from_token(psess, token)?)
} else if let TokenKind::Literal(Lit {
kind: token::LitKind::Char | token::LitKind::Integer | token::LitKind::Str,
symbol,
suffix: None,
}) = token.kind
{
MetaVarExprConcatElem::Literal(symbol)
} else {
MetaVarExprConcatElem::Ident(element_ident)
MetaVarExprConcatElem::Ident(parse_ident_from_token(psess, token)?)
};
result.push(element);
if iter.look_ahead(0).is_none() {
Expand Down Expand Up @@ -105,11 +112,13 @@ impl MetaVarExpr {

#[derive(Debug, Decodable, Encodable, PartialEq)]
pub(crate) enum MetaVarExprConcatElem {
/// There is NO preceding dollar sign, which means that this identifier should be interpreted
/// as a literal.
/// Identifier WITHOUT a preceding dollar sign, which means that this identifier should be
/// interpreted as a literal.
Ident(Ident),
/// There is a preceding dollar sign, which means that this identifier should be expanded
/// and interpreted as a variable.
/// For example, a number or a string.
Literal(Symbol),
/// Identifier WITH a preceding dollar sign, which means that this identifier should be
/// expanded and interpreted as a variable.
Var(Ident),
}

Expand Down Expand Up @@ -158,7 +167,7 @@ fn parse_depth<'psess>(
span: Span,
) -> PResult<'psess, usize> {
let Some(tt) = iter.next() else { return Ok(0) };
let TokenTree::Token(token::Token { kind: token::TokenKind::Literal(lit), .. }, _) = tt else {
let TokenTree::Token(Token { kind: TokenKind::Literal(lit), .. }, _) = tt else {
return Err(psess
.dcx()
.struct_span_err(span, "meta-variable expression depth must be a literal"));
Expand All @@ -180,12 +189,14 @@ fn parse_ident<'psess>(
psess: &'psess ParseSess,
fallback_span: Span,
) -> PResult<'psess, Ident> {
let Some(tt) = iter.next() else {
return Err(psess.dcx().struct_span_err(fallback_span, "expected identifier"));
};
let TokenTree::Token(token, _) = tt else {
return Err(psess.dcx().struct_span_err(tt.span(), "expected identifier"));
};
let token = parse_token(iter, psess, fallback_span)?;
parse_ident_from_token(psess, token)
}

fn parse_ident_from_token<'psess>(
psess: &'psess ParseSess,
token: &Token,
) -> PResult<'psess, Ident> {
if let Some((elem, is_raw)) = token.ident() {
if let IdentIsRaw::Yes = is_raw {
return Err(psess.dcx().struct_span_err(elem.span, RAW_IDENT_ERR));
Expand All @@ -205,10 +216,24 @@ fn parse_ident<'psess>(
Err(err)
}

fn parse_token<'psess, 't>(
iter: &mut RefTokenTreeCursor<'t>,
psess: &'psess ParseSess,
fallback_span: Span,
) -> PResult<'psess, &'t Token> {
let Some(tt) = iter.next() else {
return Err(psess.dcx().struct_span_err(fallback_span, "expected identifier or literal"));
};
let TokenTree::Token(token, _) = tt else {
return Err(psess.dcx().struct_span_err(tt.span(), "expected identifier or literal"));
};
Ok(token)
}

/// Tries to move the iterator forward returning `true` if there is a comma. If not, then the
/// iterator is not modified and the result is `false`.
fn try_eat_comma(iter: &mut RefTokenTreeCursor<'_>) -> bool {
if let Some(TokenTree::Token(token::Token { kind: token::Comma, .. }, _)) = iter.look_ahead(0) {
if let Some(TokenTree::Token(Token { kind: token::Comma, .. }, _)) = iter.look_ahead(0) {
let _ = iter.next();
return true;
}
Expand All @@ -218,8 +243,7 @@ fn try_eat_comma(iter: &mut RefTokenTreeCursor<'_>) -> bool {
/// Tries to move the iterator forward returning `true` if there is a dollar sign. If not, then the
/// iterator is not modified and the result is `false`.
fn try_eat_dollar(iter: &mut RefTokenTreeCursor<'_>) -> bool {
if let Some(TokenTree::Token(token::Token { kind: token::Dollar, .. }, _)) = iter.look_ahead(0)
{
if let Some(TokenTree::Token(Token { kind: token::Dollar, .. }, _)) = iter.look_ahead(0) {
let _ = iter.next();
return true;
}
Expand All @@ -232,8 +256,7 @@ fn eat_dollar<'psess>(
psess: &'psess ParseSess,
span: Span,
) -> PResult<'psess, ()> {
if let Some(TokenTree::Token(token::Token { kind: token::Dollar, .. }, _)) = iter.look_ahead(0)
{
if let Some(TokenTree::Token(Token { kind: token::Dollar, .. }, _)) = iter.look_ahead(0) {
let _ = iter.next();
return Ok(());
}
Expand Down
26 changes: 23 additions & 3 deletions compiler/rustc_expand/src/mbe/transcribe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -680,16 +680,36 @@ fn transcribe_metavar_expr<'a>(
let mut concatenated = String::new();
for element in elements.into_iter() {
let string = match element {
MetaVarExprConcatElem::Ident(ident) => ident.to_string(),
MetaVarExprConcatElem::Var(ident) => extract_ident(dcx, *ident, interp)?,
MetaVarExprConcatElem::Ident(elem) => elem.to_string(),
MetaVarExprConcatElem::Literal(elem) => elem.as_str().into(),
MetaVarExprConcatElem::Var(elem) => extract_ident(dcx, *elem, interp)?,
};
concatenated.push_str(&string);
}
let concatenated_span = visited_span();
let has_valid_ascii_ident = if let [first, rest @ ..] = concatenated.as_bytes() {
let first_is_valid = matches!(first, b'_' | b'a'..=b'z' | b'A'..=b'Z');
let rest_is_valid = rest
.iter()
.all(|b| matches!(b, b'_' | b'a'..=b'z' | b'A'..=b'Z' | b'0'..=b'9'));
first_is_valid && rest_is_valid
} else {
return Err(dcx.struct_span_err(
concatenated_span,
"`${concat(..)}` is generating an empty identifier",
));
};
if !has_valid_ascii_ident {
return Err(dcx.struct_span_err(
concatenated_span,
"`${concat(..)}` is not generating a valid identifier",
));
}
// The current implementation marks the span as coming from the macro regardless of
// contexts of the concatenated identifiers but this behavior may change in the
// future.
result.push(TokenTree::Token(
Token::from_ast_ident(Ident::new(Symbol::intern(&concatenated), visited_span())),
Token::from_ast_ident(Ident::new(Symbol::intern(&concatenated), concatenated_span)),
Spacing::Alone,
));
}
Expand Down
14 changes: 14 additions & 0 deletions tests/ui/macros/macro-metavar-expr-concat/allowed-operations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,18 @@ macro_rules! without_dollar_sign_is_an_ident {
};
}

macro_rules! literals {
($ident:ident) => {{
let ${concat(_a, 'b')}: () = ();
let ${concat(_a, 1)}: () = ();
let ${concat(_a, "b")}: () = ();

let ${concat($ident, 'b')}: () = ();
let ${concat($ident, 1)}: () = ();
let ${concat($ident, "b")}: () = ();
}};
}

fn main() {
create_things!(behold);
behold_separated_idents_in_a_fn();
Expand All @@ -55,4 +67,6 @@ fn main() {
without_dollar_sign_is_an_ident!(_123);
assert_eq!(VARident, 1);
assert_eq!(VAR_123, 2);

literals!(_hello);
}
40 changes: 34 additions & 6 deletions tests/ui/macros/macro-metavar-expr-concat/syntax-errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@ macro_rules! wrong_concat_declarations {
${concat(aaaa,)}
//~^ ERROR expected identifier

${concat(aaaa, 1)}
//~^ ERROR expected identifier

${concat(_, aaaa)}

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

${concat($ex, aaaa,)}
//~^ ERROR expected identifier

${concat($ex, aaaa, 123)}
//~^ ERROR expected identifier
};
}

Expand All @@ -43,8 +37,42 @@ macro_rules! dollar_sign_without_referenced_ident {
};
}

macro_rules! starting_number {
($ident:ident) => {{
let ${concat(1, $ident)}: () = ();
//~^ ERROR `${concat(..)}` is not generating a valid identifier
}};
}

macro_rules! starting_unicode {
($ident:ident) => {{
let ${concat("\u{342}", $ident)}: () = ();
//~^ ERROR `${concat(..)}` is not generating a valid identifier
}};
}

macro_rules! ending_unicode {
($ident:ident) => {{
let ${concat($ident, "\u{342}")}: () = ();
//~^ ERROR `${concat(..)}` is not generating a valid identifier
}};
}

macro_rules! empty {
() => {{
let ${concat("", "")}: () = ();
//~^ ERROR `${concat(..)}` is generating an empty identifier
}};
}


fn main() {
wrong_concat_declarations!(1);

dollar_sign_without_referenced_ident!(VAR);

starting_number!(_abc);
starting_unicode!(_abc);
ending_unicode!(_abc);
empty!();
}
76 changes: 54 additions & 22 deletions tests/ui/macros/macro-metavar-expr-concat/syntax-errors.stderr
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
error: expected identifier
error: expected identifier or literal
--> $DIR/syntax-errors.rs:5:10
|
LL | ${concat()}
Expand All @@ -10,59 +10,91 @@ error: `concat` must have at least two elements
LL | ${concat(aaaa)}
| ^^^^^^

error: expected identifier
error: expected identifier or literal
--> $DIR/syntax-errors.rs:11:10
|
LL | ${concat(aaaa,)}
| ^^^^^^^^^^^^^^^

error: expected identifier, found `1`
--> $DIR/syntax-errors.rs:14:24
|
LL | ${concat(aaaa, 1)}
| ^ help: try removing `1`

error: expected comma
--> $DIR/syntax-errors.rs:19:10
--> $DIR/syntax-errors.rs:16:10
|
LL | ${concat(aaaa aaaa)}
| ^^^^^^^^^^^^^^^^^^^

error: `concat` must have at least two elements
--> $DIR/syntax-errors.rs:22:11
--> $DIR/syntax-errors.rs:19:11
|
LL | ${concat($ex)}
| ^^^^^^

error: expected comma
--> $DIR/syntax-errors.rs:28:10
--> $DIR/syntax-errors.rs:25:10
|
LL | ${concat($ex, aaaa 123)}
| ^^^^^^^^^^^^^^^^^^^^^^^

error: expected identifier
--> $DIR/syntax-errors.rs:31:10
error: expected identifier or literal
--> $DIR/syntax-errors.rs:28:10
|
LL | ${concat($ex, aaaa,)}
| ^^^^^^^^^^^^^^^^^^^^

error: expected identifier, found `123`
--> $DIR/syntax-errors.rs:34:29
|
LL | ${concat($ex, aaaa, 123)}
| ^^^ help: try removing `123`

error: `${concat(..)}` currently only accepts identifiers or meta-variables as parameters
--> $DIR/syntax-errors.rs:25:19
--> $DIR/syntax-errors.rs:22:19
|
LL | ${concat($ex, aaaa)}
| ^^

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

error: aborting due to 11 previous errors
error: `${concat(..)}` is not generating a valid identifier
--> $DIR/syntax-errors.rs:42:14
|
LL | let ${concat(1, $ident)}: () = ();
| ^^^^^^^^^^^^^^^^^^^
...
LL | starting_number!(_abc);
| ---------------------- in this macro invocation
|
= note: this error originates in the macro `starting_number` (in Nightly builds, run with -Z macro-backtrace for more info)

error: `${concat(..)}` is not generating a valid identifier
--> $DIR/syntax-errors.rs:49:14
|
LL | let ${concat("\u{342}", $ident)}: () = ();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
...
LL | starting_unicode!(_abc);
| ----------------------- in this macro invocation
|
= note: this error originates in the macro `starting_unicode` (in Nightly builds, run with -Z macro-backtrace for more info)

error: `${concat(..)}` is not generating a valid identifier
--> $DIR/syntax-errors.rs:56:14
|
LL | let ${concat($ident, "\u{342}")}: () = ();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
...
LL | ending_unicode!(_abc);
| --------------------- in this macro invocation
|
= note: this error originates in the macro `ending_unicode` (in Nightly builds, run with -Z macro-backtrace for more info)

error: `${concat(..)}` is generating an empty identifier
--> $DIR/syntax-errors.rs:63:14
|
LL | let ${concat("", "")}: () = ();
| ^^^^^^^^^^^^^^^^
...
LL | empty!();
| -------- in this macro invocation
|
= note: this error originates in the macro `empty` (in Nightly builds, run with -Z macro-backtrace for more info)

error: aborting due to 13 previous errors

2 changes: 1 addition & 1 deletion tests/ui/macros/rfc-3086-metavar-expr/syntax-errors.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ error: unrecognized meta-variable expression
LL | ( $( $i:ident ),* ) => { ${ aaaaaaaaaaaaaa(i) } };
| ^^^^^^^^^^^^^^ help: supported expressions are count, ignore, index and len

error: expected identifier
error: expected identifier or literal
--> $DIR/syntax-errors.rs:118:33
|
LL | ( $( $i:ident ),* ) => { ${ {} } };
Expand Down

0 comments on commit 4a9dcc6

Please sign in to comment.