Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[RFC-3086] Add a new concat metavar expr #111930

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 33 additions & 2 deletions compiler/rustc_expand/src/mbe/metavar_expr.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use rustc_ast::token::{self, Delimiter};
use rustc_ast::token::{self, Delimiter, TokenKind};
use rustc_ast::tokenstream::{RefTokenTreeCursor, TokenStream, TokenTree};
use rustc_ast::{LitIntType, LitKind};
use rustc_ast_pretty::pprust;
Expand All @@ -10,6 +10,8 @@ use rustc_span::Span;
/// A meta-variable expression, for expansions based on properties of meta-variables.
#[derive(Debug, Clone, PartialEq, Encodable, Decodable)]
pub(crate) enum MetaVarExpr {
Concat(Ident, Ident),

/// The number of repetitions of an identifier, optionally limited to a number
/// of outer-most repetition depths. If the depth limit is `None` then the depth is unlimited.
Count(Ident, Option<usize>),
Expand Down Expand Up @@ -42,6 +44,14 @@ impl MetaVarExpr {
check_trailing_token(&mut tts, sess)?;
let mut iter = args.trees();
let rslt = match ident.as_str() {
"concat" => {
let lhs = parse_ident_or_literal_as_ident(&mut iter, sess, ident.span)?;
if !try_eat_comma(&mut iter) {
return Err(sess.span_diagnostic.struct_span_err(ident.span, "expected comma"));
}
let rhs = parse_ident_or_literal_as_ident(&mut iter, sess, ident.span)?;
MetaVarExpr::Concat(lhs, rhs)
}
"count" => parse_count(&mut iter, sess, ident.span)?,
"ignore" => MetaVarExpr::Ignore(parse_ident(&mut iter, sess, ident.span)?),
"index" => MetaVarExpr::Index(parse_depth(&mut iter, sess, ident.span)?),
Expand All @@ -65,7 +75,7 @@ impl MetaVarExpr {
pub(crate) fn ident(&self) -> Option<Ident> {
match *self {
MetaVarExpr::Count(ident, _) | MetaVarExpr::Ignore(ident) => Some(ident),
MetaVarExpr::Index(..) | MetaVarExpr::Length(..) => None,
MetaVarExpr::Concat(..) | MetaVarExpr::Index(..) | MetaVarExpr::Length(..) => None,
}
}
}
Expand Down Expand Up @@ -150,6 +160,27 @@ fn parse_ident<'sess>(
Err(sess.span_diagnostic.struct_span_err(span, "expected identifier"))
}

fn parse_ident_or_literal_as_ident<'sess>(
iter: &mut RefTokenTreeCursor<'_>,
sess: &'sess ParseSess,
span: Span,
) -> PResult<'sess, Ident> {
if let Some(tt) = iter.look_ahead(0)
&& let TokenTree::Token(token, _) = tt
&& let TokenKind::Literal(lit) = token.kind
{
let ident = Ident::new(lit.symbol, token.span);
let _ = iter.next();
Ok(ident)
}
else if let Ok(ident) = parse_ident(iter, sess, span) {
Ok(ident)
}
else {
Err(sess.span_diagnostic.struct_span_err(span, "expected identifier or literal"))
}
}

/// 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 {
Expand Down
24 changes: 22 additions & 2 deletions compiler/rustc_expand/src/mbe/transcribe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ use crate::errors::{
use crate::mbe::macro_parser::{MatchedNonterminal, MatchedSeq, MatchedTokenTree, NamedMatch};
use crate::mbe::{self, MetaVarExpr};
use rustc_ast::mut_visit::{self, MutVisitor};
use rustc_ast::token::{self, Delimiter, Token, TokenKind};
use rustc_ast::token::{self, Delimiter, Nonterminal, Token, TokenKind};
use rustc_ast::tokenstream::{DelimSpan, Spacing, TokenStream, TokenTree};
use rustc_data_structures::fx::FxHashMap;
use rustc_errors::{pluralize, PResult};
use rustc_errors::{DiagnosticBuilder, ErrorGuaranteed};
use rustc_span::hygiene::{LocalExpnId, Transparency};
use rustc_span::symbol::{sym, Ident, MacroRulesNormalizedIdent};
use rustc_span::Span;
use rustc_span::{Span, Symbol};

use smallvec::{smallvec, SmallVec};
use std::mem;
Expand Down Expand Up @@ -528,6 +528,26 @@ fn transcribe_metavar_expr<'a>(
span
};
match *expr {
MetaVarExpr::Concat(lhs, rhs) => {
let string = |ident| {
let mrni = MacroRulesNormalizedIdent::new(ident);
if let Some(nm) = lookup_cur_matched(mrni, interp, &repeats)
&& let MatchedNonterminal(nt) = nm
&& let Nonterminal::NtIdent(nt_ident, _) = &**nt
{
nt_ident.to_string()
} else {
ident.to_string()
}
};
let symbol_span = lhs.span.to(rhs.span);
let mut symbol_string = string(lhs);
symbol_string.push_str(&string(rhs));
result.push(TokenTree::Token(
Token::from_ast_ident(Ident::new(Symbol::intern(&symbol_string), symbol_span)),
Spacing::Alone,
));
}
MetaVarExpr::Count(original_ident, depth_opt) => {
let matched = matched_from_ident(cx, original_ident, interp)?;
let count = count_repetitions(cx, depth_opt, matched, &repeats, sp)?;
Expand Down
39 changes: 39 additions & 0 deletions tests/ui/macros/rfc-3086-metavar-expr/concat.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// run-pass

#![allow(dead_code, non_camel_case_types)]
#![feature(macro_metavar_expr)]

macro_rules! simple_ident {
( $lhs:ident, $rhs:ident ) => { ${concat(lhs, rhs)} };
}

macro_rules! create_things {
( $lhs:ident ) => {
struct ${concat(lhs, _separated_idents_in_a_struct)} {
foo: i32,
${concat(lhs, _separated_idents_in_a_field)}: i32,
}

mod ${concat(lhs, _separated_idents_in_a_module)} {
pub const FOO: () = ();
}

fn ${concat(lhs, _separated_idents_in_a_fn)}() {}
};
}

create_things!(look_ma);

fn main() {
let abcdef = 1;
let _another = simple_ident!(abc, def);

look_ma_separated_idents_in_a_fn();

let _ = look_ma_separated_idents_in_a_module::FOO;

let _ = look_ma_separated_idents_in_a_struct {
foo: 1,
look_ma_separated_idents_in_a_field: 2,
};
}