Skip to content

Fix "Cannot fill in a NT" ICE #30060

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

Merged
merged 2 commits into from
Nov 26, 2015
Merged
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: 23 additions & 12 deletions src/libsyntax/ext/tt/macro_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,18 +200,19 @@ pub enum NamedMatch {
}

pub fn nameize(p_s: &ParseSess, ms: &[TokenTree], res: &[Rc<NamedMatch>])
-> HashMap<Name, Rc<NamedMatch>> {
-> ParseResult<HashMap<Name, Rc<NamedMatch>>> {
fn n_rec(p_s: &ParseSess, m: &TokenTree, res: &[Rc<NamedMatch>],
ret_val: &mut HashMap<Name, Rc<NamedMatch>>, idx: &mut usize) {
ret_val: &mut HashMap<Name, Rc<NamedMatch>>, idx: &mut usize)
-> Result<(), (codemap::Span, String)> {
match *m {
TokenTree::Sequence(_, ref seq) => {
for next_m in &seq.tts {
n_rec(p_s, next_m, res, ret_val, idx)
try!(n_rec(p_s, next_m, res, ret_val, idx))
}
}
TokenTree::Delimited(_, ref delim) => {
for next_m in &delim.tts {
n_rec(p_s, next_m, res, ret_val, idx)
try!(n_rec(p_s, next_m, res, ret_val, idx));
}
}
TokenTree::Token(sp, MatchNt(bind_name, _, _, _)) => {
Expand All @@ -221,26 +222,36 @@ pub fn nameize(p_s: &ParseSess, ms: &[TokenTree], res: &[Rc<NamedMatch>])
*idx += 1;
}
Occupied(..) => {
panic!(p_s.span_diagnostic
.span_fatal(sp,
&format!("duplicated bind name: {}",
bind_name)))
return Err((sp, format!("duplicated bind name: {}", bind_name)))
}
}
}
TokenTree::Token(_, SubstNt(..)) => panic!("Cannot fill in a NT"),
TokenTree::Token(sp, SubstNt(..)) => {
return Err((sp, "missing fragment specifier".to_string()))
}
TokenTree::Token(_, _) => (),
}

Ok(())
}

let mut ret_val = HashMap::new();
let mut idx = 0;
for m in ms { n_rec(p_s, m, res, &mut ret_val, &mut idx) }
ret_val
for m in ms {
match n_rec(p_s, m, res, &mut ret_val, &mut idx) {
Ok(_) => {},
Err((sp, msg)) => return Error(sp, msg),
}
}

Success(ret_val)
}

pub enum ParseResult<T> {
Success(T),
/// Arm failed to match
Failure(codemap::Span, String),
/// Fatal error (malformed macro?). Abort compilation.
Error(codemap::Span, String)
}

Expand Down Expand Up @@ -429,7 +440,7 @@ pub fn parse(sess: &ParseSess,
for dv in &mut (&mut eof_eis[0]).matches {
v.push(dv.pop().unwrap());
}
return Success(nameize(sess, ms, &v[..]));
return nameize(sess, ms, &v[..]);
} else if eof_eis.len() > 1 {
return Error(sp, "ambiguity: multiple successful parses".to_string());
} else {
Expand Down
17 changes: 17 additions & 0 deletions src/test/compile-fail/macro-missing-fragment.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Copyright 2015 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 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

macro_rules! m {
( $( any_token $field_rust_type )* ) => {}; //~ ERROR missing fragment
}

fn main() {
m!();
}