Skip to content

Commit 739bcfe

Browse files
committed
[experiment] Use unhygienic token matching for identifying macro variables
1 parent b1f395d commit 739bcfe

File tree

4 files changed

+17
-17
lines changed

4 files changed

+17
-17
lines changed

src/librustc_expand/mbe/macro_check.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ use rustc_session::lint::builtin::META_VARIABLE_MISUSE;
111111
use rustc_session::parse::ParseSess;
112112
use rustc_span::symbol::kw;
113113
use rustc_span::{symbol::Ident, MultiSpan, Span};
114-
use syntax::ast::NodeId;
114+
use syntax::ast::{Name, NodeId};
115115
use syntax::token::{DelimToken, Token, TokenKind};
116116

117117
use smallvec::SmallVec;
@@ -179,7 +179,7 @@ struct BinderInfo {
179179
}
180180

181181
/// An environment of meta-variables to their binder information.
182-
type Binders = FxHashMap<Ident, BinderInfo>;
182+
type Binders = FxHashMap<Name, BinderInfo>;
183183

184184
/// The state at which we entered a macro definition in the RHS of another macro definition.
185185
struct MacroState<'a> {
@@ -246,14 +246,14 @@ fn check_binders(
246246
sess.span_diagnostic.span_bug(span, "unexpected MetaVar in lhs");
247247
}
248248
// There are 3 possibilities:
249-
if let Some(prev_info) = binders.get(&name) {
249+
if let Some(prev_info) = binders.get(&name.name) {
250250
// 1. The meta-variable is already bound in the current LHS: This is an error.
251251
let mut span = MultiSpan::from_span(span);
252252
span.push_span_label(prev_info.span, "previous declaration".into());
253253
buffer_lint(sess, span, node_id, "duplicate matcher binding");
254254
} else if get_binder_info(macros, binders, name).is_none() {
255255
// 2. The meta-variable is free: This is a binder.
256-
binders.insert(name, BinderInfo { span, ops: ops.into() });
256+
binders.insert(name.name, BinderInfo { span, ops: ops.into() });
257257
} else {
258258
// 3. The meta-variable is bound: This is an occurrence.
259259
check_occurrences(sess, node_id, lhs, macros, binders, ops, valid);
@@ -274,7 +274,7 @@ fn check_binders(
274274
.emit();
275275
*valid = false;
276276
} else {
277-
binders.insert(name, BinderInfo { span, ops: ops.into() });
277+
binders.insert(name.name, BinderInfo { span, ops: ops.into() });
278278
}
279279
}
280280
TokenTree::Delimited(_, ref del) => {
@@ -302,7 +302,7 @@ fn get_binder_info<'a>(
302302
binders: &'a Binders,
303303
name: Ident,
304304
) -> Option<&'a BinderInfo> {
305-
binders.get(&name).or_else(|| macros.find_map(|state| state.binders.get(&name)))
305+
binders.get(&name.name).or_else(|| macros.find_map(|state| state.binders.get(&name.name)))
306306
}
307307

308308
/// Checks `rhs` as part of the RHS of a macro definition and sets `valid` to false in case of
@@ -560,7 +560,7 @@ fn check_ops_is_prefix(
560560
let mut acc: SmallVec<[&SmallVec<[KleeneToken; 1]>; 1]> = SmallVec::new();
561561
for state in &macros {
562562
acc.push(&state.ops);
563-
if let Some(binder) = state.binders.get(&name) {
563+
if let Some(binder) = state.binders.get(&name.name) {
564564
// This variable concatenates the stack of operators from the RHS of the LHS where the
565565
// meta-variable was defined to where it is used (in possibly nested macros). The
566566
// outermost operator is first.

src/librustc_expand/mbe/macro_parser.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ crate enum ParseResult<T> {
275275

276276
/// A `ParseResult` where the `Success` variant contains a mapping of `Ident`s to `NamedMatch`es.
277277
/// This represents the mapping of metavars to the token trees they bind to.
278-
crate type NamedParseResult = ParseResult<FxHashMap<Ident, NamedMatch>>;
278+
crate type NamedParseResult = ParseResult<FxHashMap<Name, NamedMatch>>;
279279

280280
/// Count how many metavars are named in the given matcher `ms`.
281281
pub(super) fn count_names(ms: &[TokenTree]) -> usize {
@@ -368,7 +368,7 @@ fn nameize<I: Iterator<Item = NamedMatch>>(
368368
sess: &ParseSess,
369369
m: &TokenTree,
370370
res: &mut I,
371-
ret_val: &mut FxHashMap<Ident, NamedMatch>,
371+
ret_val: &mut FxHashMap<Name, NamedMatch>,
372372
) -> Result<(), (rustc_span::Span, String)> {
373373
match *m {
374374
TokenTree::Sequence(_, ref seq) => {
@@ -386,7 +386,7 @@ fn nameize<I: Iterator<Item = NamedMatch>>(
386386
return Err((span, "missing fragment specifier".to_string()));
387387
}
388388
}
389-
TokenTree::MetaVarDecl(sp, bind_name, _) => match ret_val.entry(bind_name) {
389+
TokenTree::MetaVarDecl(sp, bind_name, _) => match ret_val.entry(bind_name.name) {
390390
Vacant(spot) => {
391391
spot.insert(res.next().unwrap());
392392
}

src/librustc_expand/mbe/macro_rules.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -411,7 +411,7 @@ pub fn compile_declarative_macro(
411411
let mut valid = true;
412412

413413
// Extract the arguments:
414-
let lhses = match argument_map[&lhs_nm] {
414+
let lhses = match argument_map[&lhs_nm.name] {
415415
MatchedSeq(ref s) => s
416416
.iter()
417417
.map(|m| {
@@ -428,7 +428,7 @@ pub fn compile_declarative_macro(
428428
_ => sess.span_diagnostic.span_bug(def.span, "wrong-structured lhs"),
429429
};
430430

431-
let rhses = match argument_map[&rhs_nm] {
431+
let rhses = match argument_map[&rhs_nm.name] {
432432
MatchedSeq(ref s) => s
433433
.iter()
434434
.map(|m| {

src/librustc_expand/mbe/transcribe.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use rustc_data_structures::sync::Lrc;
77
use rustc_errors::pluralize;
88
use rustc_span::hygiene::{ExpnId, Transparency};
99
use rustc_span::Span;
10-
use syntax::ast::{Ident, Mac};
10+
use syntax::ast::{Ident, Mac, Name};
1111
use syntax::mut_visit::{self, MutVisitor};
1212
use syntax::token::{self, NtTT, Token};
1313
use syntax::tokenstream::{DelimSpan, TokenStream, TokenTree, TreeAndJoint};
@@ -81,7 +81,7 @@ impl Iterator for Frame {
8181
/// Along the way, we do some additional error checking.
8282
pub(super) fn transcribe(
8383
cx: &ExtCtxt<'_>,
84-
interp: &FxHashMap<Ident, NamedMatch>,
84+
interp: &FxHashMap<Name, NamedMatch>,
8585
src: Vec<mbe::TokenTree>,
8686
transparency: Transparency,
8787
) -> TokenStream {
@@ -289,10 +289,10 @@ pub(super) fn transcribe(
289289
/// made a mistake, and we return `None`.
290290
fn lookup_cur_matched<'a>(
291291
ident: Ident,
292-
interpolations: &'a FxHashMap<Ident, NamedMatch>,
292+
interpolations: &'a FxHashMap<Name, NamedMatch>,
293293
repeats: &[(usize, usize)],
294294
) -> Option<&'a NamedMatch> {
295-
interpolations.get(&ident).map(|matched| {
295+
interpolations.get(&ident.name).map(|matched| {
296296
let mut matched = matched;
297297
for &(idx, _) in repeats {
298298
match matched {
@@ -361,7 +361,7 @@ impl LockstepIterSize {
361361
/// multiple nested matcher sequences.
362362
fn lockstep_iter_size(
363363
tree: &mbe::TokenTree,
364-
interpolations: &FxHashMap<Ident, NamedMatch>,
364+
interpolations: &FxHashMap<Name, NamedMatch>,
365365
repeats: &[(usize, usize)],
366366
) -> LockstepIterSize {
367367
use mbe::TokenTree;

0 commit comments

Comments
 (0)