Skip to content

Commit ad1f7cc

Browse files
committed
Add a new concat metavar expr
1 parent 02c7a59 commit ad1f7cc

File tree

13 files changed

+387
-30
lines changed

13 files changed

+387
-30
lines changed

compiler/rustc_expand/src/mbe/metavar_expr.rs

+70-20
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,11 @@ use rustc_span::symbol::Ident;
99
use rustc_span::Span;
1010

1111
/// A meta-variable expression, for expansions based on properties of meta-variables.
12-
#[derive(Debug, Clone, PartialEq, Encodable, Decodable)]
12+
#[derive(Debug, PartialEq, Encodable, Decodable)]
1313
pub(crate) enum MetaVarExpr {
14+
/// Unification of two or more identifiers.
15+
Concat(Box<[MetaVarExprConcatElem]>),
16+
1417
/// The number of repetitions of an identifier.
1518
Count(Ident, usize),
1619

@@ -42,6 +45,31 @@ impl MetaVarExpr {
4245
check_trailing_token(&mut tts, psess)?;
4346
let mut iter = args.trees();
4447
let rslt = match ident.as_str() {
48+
"concat" => {
49+
let mut result = Vec::new();
50+
loop {
51+
let is_var = try_eat_dollar(&mut iter);
52+
let element_ident = parse_ident(&mut iter, psess, outer_span)?;
53+
let element = if is_var {
54+
MetaVarExprConcatElem::Var(element_ident)
55+
} else {
56+
MetaVarExprConcatElem::Ident(element_ident)
57+
};
58+
result.push(element);
59+
if iter.look_ahead(0).is_none() {
60+
break;
61+
}
62+
if !try_eat_comma(&mut iter) {
63+
return Err(psess.dcx.struct_span_err(outer_span, "expected comma"));
64+
}
65+
}
66+
if result.len() < 2 {
67+
return Err(psess
68+
.dcx
69+
.struct_span_err(ident.span, "`concat` must have at least two elements"));
70+
}
71+
MetaVarExpr::Concat(result.into())
72+
}
4573
"count" => parse_count(&mut iter, psess, ident.span)?,
4674
"ignore" => {
4775
eat_dollar(&mut iter, psess, ident.span)?;
@@ -68,11 +96,21 @@ impl MetaVarExpr {
6896
pub(crate) fn ident(&self) -> Option<Ident> {
6997
match *self {
7098
MetaVarExpr::Count(ident, _) | MetaVarExpr::Ignore(ident) => Some(ident),
71-
MetaVarExpr::Index(..) | MetaVarExpr::Len(..) => None,
99+
MetaVarExpr::Concat { .. } | MetaVarExpr::Index(..) | MetaVarExpr::Len(..) => None,
72100
}
73101
}
74102
}
75103

104+
#[derive(Debug, Decodable, Encodable, PartialEq)]
105+
pub(crate) enum MetaVarExprConcatElem {
106+
/// There is NO preceding dollar sign, which means that this identifier should be interpreted
107+
/// as a literal.
108+
Ident(Ident),
109+
/// There is a preceding dollar sign, which means that this identifier should be expanded
110+
/// and interpreted as a variable.
111+
Var(Ident),
112+
}
113+
76114
// Checks if there are any remaining tokens. For example, `${ignore(ident ... a b c ...)}`
77115
fn check_trailing_token<'psess>(
78116
iter: &mut RefTokenTreeCursor<'_>,
@@ -138,26 +176,27 @@ fn parse_depth<'psess>(
138176
fn parse_ident<'psess>(
139177
iter: &mut RefTokenTreeCursor<'_>,
140178
psess: &'psess ParseSess,
141-
span: Span,
179+
fallback_span: Span,
142180
) -> PResult<'psess, Ident> {
143-
if let Some(tt) = iter.next()
144-
&& let TokenTree::Token(token, _) = tt
145-
{
146-
if let Some((elem, IdentIsRaw::No)) = token.ident() {
147-
return Ok(elem);
148-
}
149-
let token_str = pprust::token_to_string(token);
150-
let mut err =
151-
psess.dcx.struct_span_err(span, format!("expected identifier, found `{}`", &token_str));
152-
err.span_suggestion(
153-
token.span,
154-
format!("try removing `{}`", &token_str),
155-
"",
156-
Applicability::MaybeIncorrect,
157-
);
158-
return Err(err);
181+
let Some(tt) = iter.next() else {
182+
return Err(psess.dcx.struct_span_err(fallback_span, "expected identifier"));
183+
};
184+
let TokenTree::Token(token, _) = tt else {
185+
return Err(psess.dcx.struct_span_err(tt.span(), "expected identifier"));
186+
};
187+
if let Some((elem, IdentIsRaw::No)) = token.ident() {
188+
return Ok(elem);
159189
}
160-
Err(psess.dcx.struct_span_err(span, "expected identifier"))
190+
let token_str = pprust::token_to_string(token);
191+
let mut err =
192+
psess.dcx.struct_span_err(token.span, format!("expected identifier, found `{token_str}`"));
193+
err.span_suggestion(
194+
token.span,
195+
format!("try removing `{token_str}`"),
196+
"",
197+
Applicability::MaybeIncorrect,
198+
);
199+
Err(err)
161200
}
162201

163202
/// Tries to move the iterator forward returning `true` if there is a comma. If not, then the
@@ -170,6 +209,17 @@ fn try_eat_comma(iter: &mut RefTokenTreeCursor<'_>) -> bool {
170209
false
171210
}
172211

212+
/// Tries to move the iterator forward returning `true` if there is a dollar sign. If not, then the
213+
/// iterator is not modified and the result is `false`.
214+
fn try_eat_dollar(iter: &mut RefTokenTreeCursor<'_>) -> bool {
215+
if let Some(TokenTree::Token(token::Token { kind: token::Dollar, .. }, _)) = iter.look_ahead(0)
216+
{
217+
let _ = iter.next();
218+
return true;
219+
}
220+
false
221+
}
222+
173223
/// Expects that the next item is a dollar sign.
174224
fn eat_dollar<'psess>(
175225
iter: &mut RefTokenTreeCursor<'_>,

compiler/rustc_expand/src/mbe/quoted.rs

+20-5
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,13 @@ fn maybe_emit_macro_metavar_expr_feature(features: &Features, sess: &Session, sp
155155
}
156156
}
157157

158+
fn maybe_emit_macro_metavar_expr_concat_feature(features: &Features, sess: &Session, span: Span) {
159+
if !features.macro_metavar_expr_concat {
160+
let msg = "the `concat` meta-variable expression is unstable";
161+
feature_err(sess, sym::macro_metavar_expr_concat, span, msg).emit();
162+
}
163+
}
164+
158165
/// Takes a `tokenstream::TokenTree` and returns a `self::TokenTree`. Specifically, this takes a
159166
/// generic `TokenTree`, such as is used in the rest of the compiler, and returns a `TokenTree`
160167
/// for use in parsing a macro.
@@ -217,11 +224,19 @@ fn parse_tree<'a>(
217224
return TokenTree::token(token::Dollar, dollar_span);
218225
}
219226
Ok(elem) => {
220-
maybe_emit_macro_metavar_expr_feature(
221-
features,
222-
sess,
223-
delim_span.entire(),
224-
);
227+
if let MetaVarExpr::Concat(_) = elem {
228+
maybe_emit_macro_metavar_expr_concat_feature(
229+
features,
230+
sess,
231+
delim_span.entire(),
232+
);
233+
} else {
234+
maybe_emit_macro_metavar_expr_feature(
235+
features,
236+
sess,
237+
delim_span.entire(),
238+
);
239+
}
225240
return TokenTree::MetaVarExpr(delim_span, elem);
226241
}
227242
}

compiler/rustc_expand/src/mbe/transcribe.rs

+51-3
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,19 @@ use crate::errors::{
33
NoSyntaxVarsExprRepeat, VarStillRepeating,
44
};
55
use crate::mbe::macro_parser::{NamedMatch, NamedMatch::*};
6+
use crate::mbe::metavar_expr::MetaVarExprConcatElem;
67
use crate::mbe::{self, KleeneOp, MetaVarExpr};
78
use rustc_ast::mut_visit::{self, MutVisitor};
9+
use rustc_ast::token::IdentIsRaw;
810
use rustc_ast::token::{self, Delimiter, Token, TokenKind};
911
use rustc_ast::tokenstream::{DelimSpacing, DelimSpan, Spacing, TokenStream, TokenTree};
1012
use rustc_data_structures::fx::FxHashMap;
1113
use rustc_errors::{pluralize, Diag, DiagCtxt, PResult};
1214
use rustc_parse::parser::ParseNtResult;
15+
use rustc_session::parse::ParseSess;
1316
use rustc_span::hygiene::{LocalExpnId, Transparency};
1417
use rustc_span::symbol::{sym, Ident, MacroRulesNormalizedIdent};
15-
use rustc_span::{with_metavar_spans, Span, SyntaxContext};
16-
17-
use rustc_session::parse::ParseSess;
18+
use rustc_span::{with_metavar_spans, Span, Symbol, SyntaxContext};
1819
use smallvec::{smallvec, SmallVec};
1920
use std::mem;
2021

@@ -675,6 +676,23 @@ fn transcribe_metavar_expr<'a>(
675676
span
676677
};
677678
match *expr {
679+
MetaVarExpr::Concat(ref elements) => {
680+
let mut concatenated = String::new();
681+
for element in elements.into_iter() {
682+
let string = match element {
683+
MetaVarExprConcatElem::Ident(ident) => ident.to_string(),
684+
MetaVarExprConcatElem::Var(ident) => extract_ident(dcx, *ident, interp)?,
685+
};
686+
concatenated.push_str(&string);
687+
}
688+
// The current implementation marks the span as coming from the macro regardless of
689+
// contexts of the concatenated identifiers but this behavior may change in the
690+
// future.
691+
result.push(TokenTree::Token(
692+
Token::from_ast_ident(Ident::new(Symbol::intern(&concatenated), visited_span())),
693+
Spacing::Alone,
694+
));
695+
}
678696
MetaVarExpr::Count(original_ident, depth) => {
679697
let matched = matched_from_ident(dcx, original_ident, interp)?;
680698
let count = count_repetitions(dcx, depth, matched, repeats, sp)?;
@@ -709,3 +727,33 @@ fn transcribe_metavar_expr<'a>(
709727
}
710728
Ok(())
711729
}
730+
731+
/// Extracts an identifier that can be originated from a `$var:ident` variable or from a token tree.
732+
fn extract_ident<'a>(
733+
dcx: &'a DiagCtxt,
734+
ident: Ident,
735+
interp: &FxHashMap<MacroRulesNormalizedIdent, NamedMatch>,
736+
) -> PResult<'a, String> {
737+
if let NamedMatch::MatchedSingle(pnr) = matched_from_ident(dcx, ident, interp)? {
738+
if let ParseNtResult::Ident(nt_ident, is_raw) = pnr {
739+
if let IdentIsRaw::Yes = is_raw {
740+
return Err(dcx.struct_span_err(
741+
ident.span,
742+
"`${concat(..)}` currently does not support raw identifiers",
743+
));
744+
}
745+
return Ok(nt_ident.to_string());
746+
}
747+
if let ParseNtResult::Tt(TokenTree::Token(
748+
Token { kind: TokenKind::Ident(token_ident, _), .. },
749+
_,
750+
)) = pnr
751+
{
752+
return Ok(token_ident.to_string());
753+
}
754+
}
755+
Err(dcx.struct_span_err(
756+
ident.span,
757+
"`${concat(..)}` currently only accepts identifiers or meta-variables as parameters",
758+
))
759+
}

compiler/rustc_feature/src/unstable.rs

+2
Original file line numberDiff line numberDiff line change
@@ -516,6 +516,8 @@ declare_features! (
516516
(unstable, lint_reasons, "1.31.0", Some(54503)),
517517
/// Give access to additional metadata about declarative macro meta-variables.
518518
(unstable, macro_metavar_expr, "1.61.0", Some(83527)),
519+
/// Provides a way to concatenate identifiers using metavariable expressions.
520+
(unstable, macro_metavar_expr_concat, "CURRENT_RUSTC_VERSION", Some(124225)),
519521
/// Allows `#[marker]` on certain traits allowing overlapping implementations.
520522
(unstable, marker_trait_attr, "1.30.0", Some(29864)),
521523
/// Allows exhaustive pattern matching on types that contain uninhabited types in cases that are

compiler/rustc_span/src/symbol.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1116,6 +1116,7 @@ symbols! {
11161116
macro_lifetime_matcher,
11171117
macro_literal_matcher,
11181118
macro_metavar_expr,
1119+
macro_metavar_expr_concat,
11191120
macro_reexport,
11201121
macro_use,
11211122
macro_vis_matcher,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
//@ run-pass
2+
3+
#![allow(dead_code, non_camel_case_types, non_upper_case_globals)]
4+
#![feature(macro_metavar_expr_concat)]
5+
6+
macro_rules! create_things {
7+
($lhs:ident) => {
8+
struct ${concat($lhs, _separated_idents_in_a_struct)} {
9+
foo: i32,
10+
${concat($lhs, _separated_idents_in_a_field)}: i32,
11+
}
12+
13+
mod ${concat($lhs, _separated_idents_in_a_module)} {
14+
pub const FOO: () = ();
15+
}
16+
17+
fn ${concat($lhs, _separated_idents_in_a_fn)}() {}
18+
};
19+
}
20+
21+
macro_rules! many_idents {
22+
($a:ident, $c:ident) => {
23+
const ${concat($a, B, $c, D)}: i32 = 1;
24+
};
25+
}
26+
27+
macro_rules! valid_tts {
28+
($_0:tt, $_1:tt) => {
29+
const ${concat($_0, $_1)}: i32 = 1;
30+
}
31+
}
32+
33+
macro_rules! without_dollar_sign_is_an_ident {
34+
($ident:ident) => {
35+
const ${concat(VAR, ident)}: i32 = 1;
36+
const ${concat(VAR, $ident)}: i32 = 2;
37+
};
38+
}
39+
40+
fn main() {
41+
create_things!(behold);
42+
behold_separated_idents_in_a_fn();
43+
let _ = behold_separated_idents_in_a_module::FOO;
44+
let _ = behold_separated_idents_in_a_struct {
45+
foo: 1,
46+
behold_separated_idents_in_a_field: 2,
47+
};
48+
49+
many_idents!(A, C);
50+
assert_eq!(ABCD, 1);
51+
52+
valid_tts!(X, YZ);
53+
assert_eq!(XYZ, 1);
54+
55+
without_dollar_sign_is_an_ident!(_123);
56+
assert_eq!(VARident, 1);
57+
assert_eq!(VAR_123, 2);
58+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#![feature(macro_metavar_expr_concat)]
2+
3+
macro_rules! join {
4+
($lhs:ident, $rhs:ident) => {
5+
${concat($lhs, $rhs)}
6+
//~^ ERROR cannot find value `abcdef` in this scope
7+
};
8+
}
9+
10+
fn main() {
11+
let abcdef = 1;
12+
let _another = join!(abc, def);
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
error[E0425]: cannot find value `abcdef` in this scope
2+
--> $DIR/hygiene.rs:5:10
3+
|
4+
LL | ${concat($lhs, $rhs)}
5+
| ^^^^^^^^^^^^^^^^^^^^ not found in this scope
6+
...
7+
LL | let _another = join!(abc, def);
8+
| --------------- in this macro invocation
9+
|
10+
= note: this error originates in the macro `join` (in Nightly builds, run with -Z macro-backtrace for more info)
11+
12+
error: aborting due to 1 previous error
13+
14+
For more information about this error, try `rustc --explain E0425`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#![feature(macro_metavar_expr_concat)]
2+
3+
macro_rules! join {
4+
($lhs:ident, $rhs:ident) => {
5+
let ${concat($lhs, $rhs)}: () = ();
6+
//~^ ERROR `${concat(..)}` currently does not support raw identifiers
7+
//~| ERROR `${concat(..)}` currently does not support raw identifiers
8+
//~| ERROR `${concat(..)}` currently does not support raw identifiers
9+
};
10+
}
11+
12+
fn main() {
13+
join!(r#_c, d);
14+
join!(_e, r#f);
15+
join!(r#_g, r#h);
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
error: `${concat(..)}` currently does not support raw identifiers
2+
--> $DIR/raw-identifiers.rs:5:23
3+
|
4+
LL | let ${concat($lhs, $rhs)}: () = ();
5+
| ^^^
6+
7+
error: `${concat(..)}` currently does not support raw identifiers
8+
--> $DIR/raw-identifiers.rs:5:29
9+
|
10+
LL | let ${concat($lhs, $rhs)}: () = ();
11+
| ^^^
12+
13+
error: `${concat(..)}` currently does not support raw identifiers
14+
--> $DIR/raw-identifiers.rs:5:23
15+
|
16+
LL | let ${concat($lhs, $rhs)}: () = ();
17+
| ^^^
18+
|
19+
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
20+
21+
error: aborting due to 3 previous errors
22+

0 commit comments

Comments
 (0)