From ad9d2012ded90a33702efedabf8f8e2c8d992975 Mon Sep 17 00:00:00 2001 From: Edwin Cheng Date: Mon, 20 May 2019 18:29:02 +0800 Subject: [PATCH] Fix match literal --- crates/ra_mbe/src/mbe_expander.rs | 6 +++++- crates/ra_mbe/src/tests.rs | 14 ++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/crates/ra_mbe/src/mbe_expander.rs b/crates/ra_mbe/src/mbe_expander.rs index 3a4dbb5f53..7fff8deff3 100644 --- a/crates/ra_mbe/src/mbe_expander.rs +++ b/crates/ra_mbe/src/mbe_expander.rs @@ -281,7 +281,11 @@ fn match_lhs(pattern: &crate::Subtree, input: &mut TtCursor) -> Result return Err(ExpandError::UnexpectedToken), + crate::Leaf::Literal(literal) => { + if input.eat_literal().map(|i| &i.text) != Some(&literal.text) { + return Err(ExpandError::UnexpectedToken); + } + } }, crate::TokenTree::Repeat(crate::Repeat { subtree, kind, separator }) => { // Dirty hack to make macro-expansion terminate. diff --git a/crates/ra_mbe/src/tests.rs b/crates/ra_mbe/src/tests.rs index 004faf77e9..e3a5ceecfe 100644 --- a/crates/ra_mbe/src/tests.rs +++ b/crates/ra_mbe/src/tests.rs @@ -575,6 +575,20 @@ fn test_tt_to_stmts() { ); } +#[test] +fn test_match_literal() { + let rules = create_rules( + r#" + macro_rules! foo { + ('(') => { + fn foo() {} + } + } +"#, + ); + assert_expansion(MacroKind::Items, &rules, "foo! ['(']", "fn foo () {}"); +} + // The following tests are port from intellij-rust directly // https://github.com/intellij-rust/intellij-rust/blob/c4e9feee4ad46e7953b1948c112533360b6087bb/src/test/kotlin/org/rust/lang/core/macros/RsMacroExpansionTest.kt