Skip to content

Commit

Permalink
[experiment] Use unhygienic token matching for identifying macro vari…
Browse files Browse the repository at this point in the history
…ables
  • Loading branch information
petrochenkov committed Feb 23, 2020
1 parent b1f395d commit 739bcfe
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 17 deletions.
14 changes: 7 additions & 7 deletions src/librustc_expand/mbe/macro_check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ use rustc_session::lint::builtin::META_VARIABLE_MISUSE;
use rustc_session::parse::ParseSess;
use rustc_span::symbol::kw;
use rustc_span::{symbol::Ident, MultiSpan, Span};
use syntax::ast::NodeId;
use syntax::ast::{Name, NodeId};
use syntax::token::{DelimToken, Token, TokenKind};

use smallvec::SmallVec;
Expand Down Expand Up @@ -179,7 +179,7 @@ struct BinderInfo {
}

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

/// The state at which we entered a macro definition in the RHS of another macro definition.
struct MacroState<'a> {
Expand Down Expand Up @@ -246,14 +246,14 @@ fn check_binders(
sess.span_diagnostic.span_bug(span, "unexpected MetaVar in lhs");
}
// There are 3 possibilities:
if let Some(prev_info) = binders.get(&name) {
if let Some(prev_info) = binders.get(&name.name) {
// 1. The meta-variable is already bound in the current LHS: This is an error.
let mut span = MultiSpan::from_span(span);
span.push_span_label(prev_info.span, "previous declaration".into());
buffer_lint(sess, span, node_id, "duplicate matcher binding");
} else if get_binder_info(macros, binders, name).is_none() {
// 2. The meta-variable is free: This is a binder.
binders.insert(name, BinderInfo { span, ops: ops.into() });
binders.insert(name.name, BinderInfo { span, ops: ops.into() });
} else {
// 3. The meta-variable is bound: This is an occurrence.
check_occurrences(sess, node_id, lhs, macros, binders, ops, valid);
Expand All @@ -274,7 +274,7 @@ fn check_binders(
.emit();
*valid = false;
} else {
binders.insert(name, BinderInfo { span, ops: ops.into() });
binders.insert(name.name, BinderInfo { span, ops: ops.into() });
}
}
TokenTree::Delimited(_, ref del) => {
Expand Down Expand Up @@ -302,7 +302,7 @@ fn get_binder_info<'a>(
binders: &'a Binders,
name: Ident,
) -> Option<&'a BinderInfo> {
binders.get(&name).or_else(|| macros.find_map(|state| state.binders.get(&name)))
binders.get(&name.name).or_else(|| macros.find_map(|state| state.binders.get(&name.name)))
}

/// Checks `rhs` as part of the RHS of a macro definition and sets `valid` to false in case of
Expand Down Expand Up @@ -560,7 +560,7 @@ fn check_ops_is_prefix(
let mut acc: SmallVec<[&SmallVec<[KleeneToken; 1]>; 1]> = SmallVec::new();
for state in &macros {
acc.push(&state.ops);
if let Some(binder) = state.binders.get(&name) {
if let Some(binder) = state.binders.get(&name.name) {
// This variable concatenates the stack of operators from the RHS of the LHS where the
// meta-variable was defined to where it is used (in possibly nested macros). The
// outermost operator is first.
Expand Down
6 changes: 3 additions & 3 deletions src/librustc_expand/mbe/macro_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ crate enum ParseResult<T> {

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

/// Count how many metavars are named in the given matcher `ms`.
pub(super) fn count_names(ms: &[TokenTree]) -> usize {
Expand Down Expand Up @@ -368,7 +368,7 @@ fn nameize<I: Iterator<Item = NamedMatch>>(
sess: &ParseSess,
m: &TokenTree,
res: &mut I,
ret_val: &mut FxHashMap<Ident, NamedMatch>,
ret_val: &mut FxHashMap<Name, NamedMatch>,
) -> Result<(), (rustc_span::Span, String)> {
match *m {
TokenTree::Sequence(_, ref seq) => {
Expand All @@ -386,7 +386,7 @@ fn nameize<I: Iterator<Item = NamedMatch>>(
return Err((span, "missing fragment specifier".to_string()));
}
}
TokenTree::MetaVarDecl(sp, bind_name, _) => match ret_val.entry(bind_name) {
TokenTree::MetaVarDecl(sp, bind_name, _) => match ret_val.entry(bind_name.name) {
Vacant(spot) => {
spot.insert(res.next().unwrap());
}
Expand Down
4 changes: 2 additions & 2 deletions src/librustc_expand/mbe/macro_rules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,7 @@ pub fn compile_declarative_macro(
let mut valid = true;

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

let rhses = match argument_map[&rhs_nm] {
let rhses = match argument_map[&rhs_nm.name] {
MatchedSeq(ref s) => s
.iter()
.map(|m| {
Expand Down
10 changes: 5 additions & 5 deletions src/librustc_expand/mbe/transcribe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use rustc_data_structures::sync::Lrc;
use rustc_errors::pluralize;
use rustc_span::hygiene::{ExpnId, Transparency};
use rustc_span::Span;
use syntax::ast::{Ident, Mac};
use syntax::ast::{Ident, Mac, Name};
use syntax::mut_visit::{self, MutVisitor};
use syntax::token::{self, NtTT, Token};
use syntax::tokenstream::{DelimSpan, TokenStream, TokenTree, TreeAndJoint};
Expand Down Expand Up @@ -81,7 +81,7 @@ impl Iterator for Frame {
/// Along the way, we do some additional error checking.
pub(super) fn transcribe(
cx: &ExtCtxt<'_>,
interp: &FxHashMap<Ident, NamedMatch>,
interp: &FxHashMap<Name, NamedMatch>,
src: Vec<mbe::TokenTree>,
transparency: Transparency,
) -> TokenStream {
Expand Down Expand Up @@ -289,10 +289,10 @@ pub(super) fn transcribe(
/// made a mistake, and we return `None`.
fn lookup_cur_matched<'a>(
ident: Ident,
interpolations: &'a FxHashMap<Ident, NamedMatch>,
interpolations: &'a FxHashMap<Name, NamedMatch>,
repeats: &[(usize, usize)],
) -> Option<&'a NamedMatch> {
interpolations.get(&ident).map(|matched| {
interpolations.get(&ident.name).map(|matched| {
let mut matched = matched;
for &(idx, _) in repeats {
match matched {
Expand Down Expand Up @@ -361,7 +361,7 @@ impl LockstepIterSize {
/// multiple nested matcher sequences.
fn lockstep_iter_size(
tree: &mbe::TokenTree,
interpolations: &FxHashMap<Ident, NamedMatch>,
interpolations: &FxHashMap<Name, NamedMatch>,
repeats: &[(usize, usize)],
) -> LockstepIterSize {
use mbe::TokenTree;
Expand Down

0 comments on commit 739bcfe

Please sign in to comment.