From e38cb972dcfc0fdab44270257eac3405a39bd996 Mon Sep 17 00:00:00 2001 From: John Clements Date: Mon, 30 Jun 2014 18:02:14 -0700 Subject: Simplify PatIdent to contain an Ident rather than a Path Rationale: for what appear to be historical reasons only, the PatIdent contains a Path rather than an Ident. This means that there are many places in the code where an ident is artificially promoted to a path, and---much more problematically--- a bunch of elements from a path are simply thrown away, which seems like an invitation to some really nasty bugs. This commit replaces the Path in a PatIdent with a SpannedIdent, which just contains an ident and a span. --- src/libsyntax/parse/mod.rs | 53 +++++++++++++++---------------------------- src/libsyntax/parse/parser.rs | 23 +++++++++++-------- 2 files changed, 32 insertions(+), 44 deletions(-) (limited to 'src/libsyntax/parse') diff --git a/src/libsyntax/parse/mod.rs b/src/libsyntax/parse/mod.rs index 331a49c83be..4b5252bfba3 100644 --- a/src/libsyntax/parse/mod.rs +++ b/src/libsyntax/parse/mod.rs @@ -594,23 +594,15 @@ mod test { #[test] fn parse_ident_pat () { let sess = new_parse_sess(); let mut parser = string_to_parser(&sess, "b".to_string()); - assert!(parser.parse_pat() == - box(GC) ast::Pat{id: ast::DUMMY_NODE_ID, - node: ast::PatIdent( - ast::BindByValue(ast::MutImmutable), - ast::Path { - span:sp(0,1), - global:false, - segments: vec!( - ast::PathSegment { - identifier: str_to_ident("b"), - lifetimes: Vec::new(), - types: OwnedSlice::empty(), - } - ), - }, - None /* no idea */), - span: sp(0,1)}); + assert!(parser.parse_pat() + == box(GC) ast::Pat{ + id: ast::DUMMY_NODE_ID, + node: ast::PatIdent(ast::BindByValue(ast::MutImmutable), + Spanned{ span:sp(0, 1), + node: str_to_ident("b") + }, + None), + span: sp(0,1)}); parser_done(parser); } @@ -643,24 +635,15 @@ mod test { id: ast::DUMMY_NODE_ID, node: ast::PatIdent( ast::BindByValue(ast::MutImmutable), - ast::Path { - span:sp(6,7), - global:false, - segments: vec!( - ast::PathSegment { - identifier: - str_to_ident("b"), - lifetimes: Vec::new(), - types: OwnedSlice::empty(), - } - ), - }, - None // no idea - ), - span: sp(6,7) - }, - id: ast::DUMMY_NODE_ID - }), + Spanned{ + span: sp(6,7), + node: str_to_ident("b")}, + None + ), + span: sp(6,7) + }, + id: ast::DUMMY_NODE_ID + }), output: ast::P(ast::Ty{id: ast::DUMMY_NODE_ID, node: ast::TyNil, span:sp(15,15)}), // not sure diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs index 0fd5a7086b7..2aa2da3ba4b 100644 --- a/src/libsyntax/parse/parser.rs +++ b/src/libsyntax/parse/parser.rs @@ -58,7 +58,7 @@ use ast::{UnsafeFn, ViewItem, ViewItem_, ViewItemExternCrate, ViewItemUse}; use ast::{ViewPath, ViewPathGlob, ViewPathList, ViewPathSimple}; use ast::Visibility; use ast; -use ast_util::{as_prec, lit_is_str, operator_prec}; +use ast_util::{as_prec, ident_to_path, lit_is_str, operator_prec}; use ast_util; use codemap::{Span, BytePos, Spanned, spanned, mk_sp}; use codemap; @@ -2854,8 +2854,7 @@ impl<'a> Parser<'a> { self.bump(); self.parse_pat() } else { - let fieldpath = ast_util::ident_to_path(self.last_span, - fieldname); + let fieldpath = codemap::Spanned{span:self.last_span, node: fieldname}; box(GC) ast::Pat { id: ast::DUMMY_NODE_ID, node: PatIdent(bind_type, fieldpath, None), @@ -2961,6 +2960,7 @@ impl<'a> Parser<'a> { } _ => {} } + // at this point, token != _, ~, &, &&, (, [ if (!is_ident_or_path(&self.token) && self.token != token::MOD_SEP) || self.is_keyword(keywords::True) @@ -3017,7 +3017,9 @@ impl<'a> Parser<'a> { let end = self.parse_expr_res(RESTRICT_NO_BAR_OP); pat = PatRange(start, end); } else if is_plain_ident(&self.token) && !can_be_enum_or_struct { - let name = self.parse_path(NoTypesAllowed).path; + let id = self.parse_ident(); + let id_span = self.last_span; + let pth1 = codemap::Spanned{span:id_span, node: id}; if self.eat(&token::NOT) { // macro invocation let ket = token::close_delimiter_for(&self.token) @@ -3028,7 +3030,7 @@ impl<'a> Parser<'a> { seq_sep_none(), |p| p.parse_token_tree()); - let mac = MacInvocTT(name, tts, EMPTY_CTXT); + let mac = MacInvocTT(ident_to_path(id_span,id), tts, EMPTY_CTXT); pat = ast::PatMac(codemap::Spanned {node: mac, span: self.span}); } else { let sub = if self.eat(&token::AT) { @@ -3038,7 +3040,7 @@ impl<'a> Parser<'a> { // or just foo None }; - pat = PatIdent(BindByValue(MutImmutable), name, sub); + pat = PatIdent(BindByValue(MutImmutable), pth1, sub); } } else { // parse an enum pat @@ -3084,8 +3086,11 @@ impl<'a> Parser<'a> { // or an identifier pattern, resolve // will sort it out: pat = PatIdent(BindByValue(MutImmutable), - enum_path, - None); + codemap::Spanned{ + span: enum_path.span, + node: enum_path.segments.get(0) + .identifier}, + None); } else { pat = PatEnum(enum_path, Some(args)); } @@ -3115,7 +3120,7 @@ impl<'a> Parser<'a> { "expected identifier, found path"); } // why a path here, and not just an identifier? - let name = self.parse_path(NoTypesAllowed).path; + let name = codemap::Spanned{span: self.last_span, node: self.parse_ident()}; let sub = if self.eat(&token::AT) { Some(self.parse_pat()) } else { -- cgit 1.4.1-3-g733a5 From b81905eedbf56e026e8144a32056fd5dd0265d3b Mon Sep 17 00:00:00 2001 From: Kevin Ballard Date: Tue, 1 Jul 2014 22:11:47 -0700 Subject: Fix ICE with nested macro_rules!-style macros Fixes #10536. --- src/libsyntax/parse/parser.rs | 4 ++-- src/test/compile-fail/issue-10536.rs | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 2 deletions(-) create mode 100644 src/test/compile-fail/issue-10536.rs (limited to 'src/libsyntax/parse') diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs index 2aa2da3ba4b..f3789e25bc8 100644 --- a/src/libsyntax/parse/parser.rs +++ b/src/libsyntax/parse/parser.rs @@ -3248,7 +3248,7 @@ impl<'a> Parser<'a> { None => { // we only expect an ident if we didn't parse one // above. - let ident_str = if id == token::special_idents::invalid { + let ident_str = if id.name == token::special_idents::invalid.name { "identifier, " } else { "" @@ -3268,7 +3268,7 @@ impl<'a> Parser<'a> { ); let hi = self.span.hi; - if id == token::special_idents::invalid { + if id.name == token::special_idents::invalid.name { return box(GC) spanned(lo, hi, StmtMac( spanned(lo, hi, MacInvocTT(pth, tts, EMPTY_CTXT)), false)); } else { diff --git a/src/test/compile-fail/issue-10536.rs b/src/test/compile-fail/issue-10536.rs new file mode 100644 index 00000000000..36afc729de9 --- /dev/null +++ b/src/test/compile-fail/issue-10536.rs @@ -0,0 +1,34 @@ +// Copyright 2014 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. + +// We only want to assert that this doesn't ICE, we don't particularly care +// about whether it nor it fails to compile. + +// error-pattern: + +#![feature(macro_rules)] + +macro_rules! foo{ + () => {{ + macro_rules! bar{() => (())} + 1 + }} +} + +pub fn main() { + foo!(); + + assert!({one! two()}); + + // regardless of whether nested macro_rules works, the following should at + // least throw a conventional error. + assert!({one! two}); +} + -- cgit 1.4.1-3-g733a5