Skip to content

Commit

Permalink
Rollup merge of rust-lang#33841 - LeoTestard:macro-sequence-lhs, r=pn…
Browse files Browse the repository at this point in the history
…kfelix

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 rust-lang#33689.

r? @pnkfelix

Note: this can break code that defines such macros but does not use them.
  • Loading branch information
Manishearth committed Jun 1, 2016
2 parents cbfe74c + 864b3c8 commit 4721f3a
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 30 deletions.
13 changes: 4 additions & 9 deletions src/libsyntax/ext/tt/macro_rules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,15 +179,15 @@ fn generic_extension<'cx>(cx: &'cx ExtCtxt,
for (i, lhs) in lhses.iter().enumerate() { // try each arm's matchers
let lhs_tt = match *lhs {
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) {
Success(named_matches) => {
let rhs = match rhses[i] {
// ignore delimiters
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)
let trncbr = new_tt_reader(&cx.parse_sess().span_diagnostic,
Expand Down Expand Up @@ -326,19 +326,14 @@ pub fn compile<'cx>(cx: &'cx mut ExtCtxt,
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 {
// 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.
match lhs {
&TokenTree::Delimited(_, ref tts) => check_matcher(cx, &tts.tts),
tt @ &TokenTree::Sequence(..) => check_matcher(cx, ref_slice(tt)),
_ => {
cx.span_err(lhs.get_span(),
"invalid macro matcher; matchers must be contained \
in balanced delimiters or a repetition indicator");
cx.span_err(lhs.get_span(), "invalid macro matcher; matchers must \
be contained in balanced delimiters");
false
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/test/compile-fail/malformed_macro_lhs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
// except according to those terms.

macro_rules! my_precioooous {
$($t:tt)* => (1);
$($t:tt)* => (1); //~ ERROR invalid macro matcher
}

fn main() {
my_precioooous!(); //~ ERROR malformed macro lhs
my_precioooous!();
}
19 changes: 0 additions & 19 deletions src/test/run-pass/issue-21350.rs

This file was deleted.

0 comments on commit 4721f3a

Please sign in to comment.