1
Fork 0

Rollup merge of #33841 - LeoTestard:macro-sequence-lhs, r=pnkfelix

Reject a LHS formed of a single sequence TT during `macro_rules!` checking.

This was already rejected during expansion. Encountering malformed LHS or RHS during expansion is now considered a bug.

Follow up to #33689.

r? @pnkfelix

Note: this can break code that defines such macros but does not use them.
This commit is contained in:
Manish Goregaokar 2016-06-01 12:57:41 +05:30
commit 4721f3a543
3 changed files with 6 additions and 30 deletions

View file

@ -179,7 +179,7 @@ fn generic_extension<'cx>(cx: &'cx ExtCtxt,
for (i, lhs) in lhses.iter().enumerate() { // try each arm's matchers for (i, lhs) in lhses.iter().enumerate() { // try each arm's matchers
let lhs_tt = match *lhs { let lhs_tt = match *lhs {
TokenTree::Delimited(_, ref delim) => &delim.tts[..], TokenTree::Delimited(_, ref delim) => &delim.tts[..],
_ => cx.span_fatal(sp, "malformed macro lhs") _ => cx.span_bug(sp, "malformed macro lhs")
}; };
match TokenTree::parse(cx, lhs_tt, arg) { match TokenTree::parse(cx, lhs_tt, arg) {
@ -187,7 +187,7 @@ fn generic_extension<'cx>(cx: &'cx ExtCtxt,
let rhs = match rhses[i] { let rhs = match rhses[i] {
// ignore delimiters // ignore delimiters
TokenTree::Delimited(_, ref delimed) => delimed.tts.clone(), TokenTree::Delimited(_, ref delimed) => delimed.tts.clone(),
_ => cx.span_fatal(sp, "malformed macro rhs"), _ => cx.span_bug(sp, "malformed macro rhs"),
}; };
// rhs has holes ( `$id` and `$(...)` that need filled) // rhs has holes ( `$id` and `$(...)` that need filled)
let trncbr = new_tt_reader(&cx.parse_sess().span_diagnostic, let trncbr = new_tt_reader(&cx.parse_sess().span_diagnostic,
@ -326,19 +326,14 @@ pub fn compile<'cx>(cx: &'cx mut ExtCtxt,
NormalTT(exp, Some(def.span), def.allow_internal_unstable) NormalTT(exp, Some(def.span), def.allow_internal_unstable)
} }
// why is this here? because of https://github.com/rust-lang/rust/issues/27774
fn ref_slice<A>(s: &A) -> &[A] { use std::slice::from_raw_parts; unsafe { from_raw_parts(s, 1) } }
fn check_lhs_nt_follows(cx: &mut ExtCtxt, lhs: &TokenTree) -> bool { fn check_lhs_nt_follows(cx: &mut ExtCtxt, lhs: &TokenTree) -> bool {
// lhs is going to be like TokenTree::Delimited(...), where the // lhs is going to be like TokenTree::Delimited(...), where the
// entire lhs is those tts. Or, it can be a "bare sequence", not wrapped in parens. // entire lhs is those tts. Or, it can be a "bare sequence", not wrapped in parens.
match lhs { match lhs {
&TokenTree::Delimited(_, ref tts) => check_matcher(cx, &tts.tts), &TokenTree::Delimited(_, ref tts) => check_matcher(cx, &tts.tts),
tt @ &TokenTree::Sequence(..) => check_matcher(cx, ref_slice(tt)),
_ => { _ => {
cx.span_err(lhs.get_span(), cx.span_err(lhs.get_span(), "invalid macro matcher; matchers must \
"invalid macro matcher; matchers must be contained \ be contained in balanced delimiters");
in balanced delimiters or a repetition indicator");
false false
} }
} }

View file

@ -9,9 +9,9 @@
// except according to those terms. // except according to those terms.
macro_rules! my_precioooous { macro_rules! my_precioooous {
$($t:tt)* => (1); $($t:tt)* => (1); //~ ERROR invalid macro matcher
} }
fn main() { fn main() {
my_precioooous!(); //~ ERROR malformed macro lhs my_precioooous!();
} }

View file

@ -1,19 +0,0 @@
// 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.
// Make sure that "bare sequences" don't ICE in follow checking
// pretty-expanded FIXME #23616
macro_rules! bare {
$($id:expr),+ => ( $($id)+ )
}
fn main() { }