Skip to content

Commit 581c97a

Browse files
bors[bot]jrmuizel
andcommitted
Merge #730
730: Make sure we match the entire pattern r=matklad a=jrmuizel Co-authored-by: Jeff Muizelaar <[email protected]>
2 parents f370393 + dbc3074 commit 581c97a

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

crates/ra_mbe/src/lib.rs

+31-1
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ impl_froms!(TokenTree: Leaf, Subtree);
176176
}
177177

178178
#[test]
179-
fn test_fail_match_pattern_by_token() {
179+
fn test_fail_match_pattern_by_first_token() {
180180
let macro_definition = r#"
181181
macro_rules! foo {
182182
($ i:ident) => (
@@ -206,4 +206,34 @@ impl_froms!(TokenTree: Leaf, Subtree);
206206
assert_expansion(&rules, "foo! { + Baz }", "struct Baz ;");
207207
}
208208

209+
#[test]
210+
fn test_fail_match_pattern_by_last_token() {
211+
let macro_definition = r#"
212+
macro_rules! foo {
213+
($ i:ident) => (
214+
mod $ i {}
215+
);
216+
($ i:ident =) => (
217+
fn $ i() {}
218+
);
219+
($ i:ident +) => (
220+
struct $ i;
221+
)
222+
}
223+
"#;
224+
225+
let source_file = ast::SourceFile::parse(macro_definition);
226+
let macro_definition = source_file
227+
.syntax()
228+
.descendants()
229+
.find_map(ast::MacroCall::cast)
230+
.unwrap();
231+
232+
let definition_tt = ast_to_token_tree(macro_definition.token_tree().unwrap()).unwrap();
233+
let rules = crate::MacroRules::parse(&definition_tt).unwrap();
234+
235+
assert_expansion(&rules, "foo! { foo }", "mod foo {}");
236+
assert_expansion(&rules, "foo! { bar = }", "fn bar () {}");
237+
assert_expansion(&rules, "foo! { Baz + }", "struct Baz ;");
238+
}
209239
}

crates/ra_mbe/src/mbe_expander.rs

+3
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ pub(crate) fn exapnd(rules: &crate::MacroRules, input: &tt::Subtree) -> Option<t
1313
fn expand_rule(rule: &crate::Rule, input: &tt::Subtree) -> Option<tt::Subtree> {
1414
let mut input = TtCursor::new(input);
1515
let bindings = match_lhs(&rule.lhs, &mut input)?;
16+
if !input.is_eof() {
17+
return None;
18+
}
1619
expand_subtree(&rule.rhs, &bindings, &mut Vec::new())
1720
}
1821

0 commit comments

Comments
 (0)