mirror of
https://github.com/rust-lang/rust-analyzer
synced 2024-12-26 04:53:34 +00:00
Merge #730
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:
commit
581c97a5c3
2 changed files with 34 additions and 1 deletions
|
@ -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 ;");
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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())
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue