730: Make sure we match the entire pattern r=matklad a=jrmuizel



Co-authored-by: Jeff Muizelaar <jrmuizel@gmail.com>
This commit is contained in:
bors[bot] 2019-02-03 15:12:07 +00:00
commit 581c97a5c3
2 changed files with 34 additions and 1 deletions

View file

@ -176,7 +176,7 @@ impl_froms!(TokenTree: Leaf, Subtree);
}
#[test]
fn test_fail_match_pattern_by_token() {
fn test_fail_match_pattern_by_first_token() {
let macro_definition = r#"
macro_rules! foo {
($ i:ident) => (
@ -206,4 +206,34 @@ impl_froms!(TokenTree: Leaf, Subtree);
assert_expansion(&rules, "foo! { + Baz }", "struct Baz ;");
}
#[test]
fn test_fail_match_pattern_by_last_token() {
let macro_definition = r#"
macro_rules! foo {
($ i:ident) => (
mod $ i {}
);
($ i:ident =) => (
fn $ i() {}
);
($ i:ident +) => (
struct $ i;
)
}
"#;
let source_file = ast::SourceFile::parse(macro_definition);
let macro_definition = source_file
.syntax()
.descendants()
.find_map(ast::MacroCall::cast)
.unwrap();
let definition_tt = ast_to_token_tree(macro_definition.token_tree().unwrap()).unwrap();
let rules = crate::MacroRules::parse(&definition_tt).unwrap();
assert_expansion(&rules, "foo! { foo }", "mod foo {}");
assert_expansion(&rules, "foo! { bar = }", "fn bar () {}");
assert_expansion(&rules, "foo! { Baz + }", "struct Baz ;");
}
}

View file

@ -13,6 +13,9 @@ pub(crate) fn exapnd(rules: &crate::MacroRules, input: &tt::Subtree) -> Option<t
fn expand_rule(rule: &crate::Rule, input: &tt::Subtree) -> Option<tt::Subtree> {
let mut input = TtCursor::new(input);
let bindings = match_lhs(&rule.lhs, &mut input)?;
if !input.is_eof() {
return None;
}
expand_subtree(&rule.rhs, &bindings, &mut Vec::new())
}