From f9f99c5a9c08dda7c85f9d2f7e7d893282ee3a27 Mon Sep 17 00:00:00 2001 From: Jeffrey Seyfried Date: Sun, 7 Aug 2016 02:19:10 +0000 Subject: [PATCH 1/4] Make metavariables hygienic. --- src/libsyntax/ext/tt/macro_parser.rs | 10 +++++----- src/libsyntax/ext/tt/macro_rules.rs | 4 ++-- src/libsyntax/ext/tt/transcribe.rs | 10 +++++----- 3 files changed, 12 insertions(+), 12 deletions(-) diff --git a/src/libsyntax/ext/tt/macro_parser.rs b/src/libsyntax/ext/tt/macro_parser.rs index 813afb935762e..7db03e9a8634a 100644 --- a/src/libsyntax/ext/tt/macro_parser.rs +++ b/src/libsyntax/ext/tt/macro_parser.rs @@ -79,7 +79,7 @@ pub use self::ParseResult::*; use self::TokenTreeOrTokenTreeVec::*; use ast; -use ast::{Name, Ident}; +use ast::Ident; use syntax_pos::{self, BytePos, mk_sp, Span}; use codemap::Spanned; use errors::FatalError; @@ -202,9 +202,9 @@ pub enum NamedMatch { } pub fn nameize(p_s: &ParseSess, ms: &[TokenTree], res: &[Rc]) - -> ParseResult>> { + -> ParseResult>> { fn n_rec(p_s: &ParseSess, m: &TokenTree, res: &[Rc], - ret_val: &mut HashMap>, idx: &mut usize) + ret_val: &mut HashMap>, idx: &mut usize) -> Result<(), (syntax_pos::Span, String)> { match *m { TokenTree::Sequence(_, ref seq) => { @@ -218,7 +218,7 @@ pub fn nameize(p_s: &ParseSess, ms: &[TokenTree], res: &[Rc]) } } TokenTree::Token(sp, MatchNt(bind_name, _)) => { - match ret_val.entry(bind_name.name) { + match ret_val.entry(bind_name) { Vacant(spot) => { spot.insert(res[*idx].clone()); *idx += 1; @@ -257,7 +257,7 @@ pub enum ParseResult { Error(syntax_pos::Span, String) } -pub type NamedParseResult = ParseResult>>; +pub type NamedParseResult = ParseResult>>; pub type PositionalParseResult = ParseResult>>; /// Perform a token equality check, ignoring syntax context (that is, an diff --git a/src/libsyntax/ext/tt/macro_rules.rs b/src/libsyntax/ext/tt/macro_rules.rs index db12ef24f7149..d197741e9a367 100644 --- a/src/libsyntax/ext/tt/macro_rules.rs +++ b/src/libsyntax/ext/tt/macro_rules.rs @@ -302,7 +302,7 @@ pub fn compile<'cx>(cx: &'cx mut ExtCtxt, let mut valid = true; // Extract the arguments: - let lhses = match **argument_map.get(&lhs_nm.name).unwrap() { + let lhses = match **argument_map.get(&lhs_nm).unwrap() { MatchedSeq(ref s, _) => { s.iter().map(|m| match **m { MatchedNonterminal(NtTT(ref tt)) => { @@ -315,7 +315,7 @@ pub fn compile<'cx>(cx: &'cx mut ExtCtxt, _ => cx.span_bug(def.span, "wrong-structured lhs") }; - let rhses = match **argument_map.get(&rhs_nm.name).unwrap() { + let rhses = match **argument_map.get(&rhs_nm).unwrap() { MatchedSeq(ref s, _) => { s.iter().map(|m| match **m { MatchedNonterminal(NtTT(ref tt)) => (**tt).clone(), diff --git a/src/libsyntax/ext/tt/transcribe.rs b/src/libsyntax/ext/tt/transcribe.rs index 29a300b172e75..939425378def6 100644 --- a/src/libsyntax/ext/tt/transcribe.rs +++ b/src/libsyntax/ext/tt/transcribe.rs @@ -9,7 +9,7 @@ // except according to those terms. use self::LockstepIterSize::*; -use ast::{Ident, Name}; +use ast::Ident; use syntax_pos::{Span, DUMMY_SP}; use errors::{Handler, DiagnosticBuilder}; use ext::tt::macro_parser::{NamedMatch, MatchedSeq, MatchedNonterminal}; @@ -38,7 +38,7 @@ pub struct TtReader<'a> { /// the unzipped tree: stack: Vec, /* for MBE-style macro transcription */ - interpolations: HashMap>, + interpolations: HashMap>, imported_from: Option, // Some => return imported_from as the next token @@ -57,7 +57,7 @@ pub struct TtReader<'a> { /// `src` contains no `TokenTree::Sequence`s, `MatchNt`s or `SubstNt`s, `interp` can /// (and should) be None. pub fn new_tt_reader(sp_diag: &Handler, - interp: Option>>, + interp: Option>>, imported_from: Option, src: Vec) -> TtReader { @@ -71,7 +71,7 @@ pub fn new_tt_reader(sp_diag: &Handler, /// `src` contains no `TokenTree::Sequence`s, `MatchNt`s or `SubstNt`s, `interp` can /// (and should) be None. pub fn new_tt_reader_with_doc_flag(sp_diag: &Handler, - interp: Option>>, + interp: Option>>, imported_from: Option, src: Vec, desugar_doc_comments: bool) @@ -119,7 +119,7 @@ fn lookup_cur_matched_by_matched(r: &TtReader, start: Rc) -> Rc Option> { - let matched_opt = r.interpolations.get(&name.name).cloned(); + let matched_opt = r.interpolations.get(&name).cloned(); matched_opt.map(|s| lookup_cur_matched_by_matched(r, s)) } From 8f73fc83dda9cfbd4e78b709961a8cf0cb0f8300 Mon Sep 17 00:00:00 2001 From: Jeffrey Seyfried Date: Sun, 7 Aug 2016 06:09:19 +0000 Subject: [PATCH 2/4] Add regression test. --- src/test/compile-fail/issue-35450.rs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 src/test/compile-fail/issue-35450.rs diff --git a/src/test/compile-fail/issue-35450.rs b/src/test/compile-fail/issue-35450.rs new file mode 100644 index 0000000000000..d890d02a91047 --- /dev/null +++ b/src/test/compile-fail/issue-35450.rs @@ -0,0 +1,16 @@ +// Copyright 2016 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +macro_rules! m { ($t:tt) => { $t } } + +fn main() { + m!($t); //~ ERROR unknown macro variable + //~| ERROR expected expression +} From cdbfe9fce300b9a28e7f2106bcb85df38d72ae66 Mon Sep 17 00:00:00 2001 From: Jeffrey Seyfried Date: Sun, 7 Aug 2016 06:28:05 +0000 Subject: [PATCH 3/4] Add test for metavariable hygiene. --- src/test/compile-fail/macro-tt-matchers.rs | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/test/compile-fail/macro-tt-matchers.rs b/src/test/compile-fail/macro-tt-matchers.rs index f41da77ee9896..945490cefb95a 100644 --- a/src/test/compile-fail/macro-tt-matchers.rs +++ b/src/test/compile-fail/macro-tt-matchers.rs @@ -16,5 +16,16 @@ macro_rules! foo { foo!(Box); +macro_rules! bar { + ($x:tt) => { + macro_rules! baz { + ($x:tt, $y:tt) => { ($x, $y) } + } + } +} + #[rustc_error] -fn main() {} //~ ERROR compilation successful +fn main() { //~ ERROR compilation successful + bar!($y); + let _: (i8, i16) = baz!(0i8, 0i16); +} From 95b68aa5eacac7fb6340829289871da6e517e8b0 Mon Sep 17 00:00:00 2001 From: Jeffrey Seyfried Date: Fri, 12 Aug 2016 06:26:06 +0000 Subject: [PATCH 4/4] Fix fallout in tests. --- src/test/run-pass-fulldeps/auxiliary/procedural_mbe_matching.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/run-pass-fulldeps/auxiliary/procedural_mbe_matching.rs b/src/test/run-pass-fulldeps/auxiliary/procedural_mbe_matching.rs index 5b1e210b0b258..2b50c4fe11e93 100644 --- a/src/test/run-pass-fulldeps/auxiliary/procedural_mbe_matching.rs +++ b/src/test/run-pass-fulldeps/auxiliary/procedural_mbe_matching.rs @@ -36,7 +36,7 @@ fn expand_mbe_matches(cx: &mut ExtCtxt, sp: Span, args: &[TokenTree]) let mac_expr = match TokenTree::parse(cx, &mbe_matcher[..], args) { Success(map) => { - match (&*map[&str_to_ident("matched").name], &*map[&str_to_ident("pat").name]) { + match (&*map[&str_to_ident("matched")], &*map[&str_to_ident("pat")]) { (&MatchedNonterminal(NtExpr(ref matched_expr)), &MatchedSeq(ref pats, seq_sp)) => { let pats: Vec> = pats.iter().map(|pat_nt|