aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_mbe
diff options
context:
space:
mode:
authorVeetaha <[email protected]>2020-02-20 21:42:24 +0000
committerVeetaha <[email protected]>2020-02-22 11:10:54 +0000
commit28bdb654074f63d6ce1a2a3fe328b1a2a1867954 (patch)
tree9983c53ae04fa6a82483339f2a383956c025021f /crates/ra_mbe
parent93a19bda173cdd9ef80aeb1b647eb4e9fd4f7955 (diff)
ra_mbe: added test for malformed token in macro invokation
There was a panic where lexer returned None on malformed tokens. But now we just ignore tokenization errors in mbe.
Diffstat (limited to 'crates/ra_mbe')
-rw-r--r--crates/ra_mbe/src/tests.rs24
1 files changed, 21 insertions, 3 deletions
diff --git a/crates/ra_mbe/src/tests.rs b/crates/ra_mbe/src/tests.rs
index e0d689704..ef34dde5c 100644
--- a/crates/ra_mbe/src/tests.rs
+++ b/crates/ra_mbe/src/tests.rs
@@ -1374,14 +1374,22 @@ pub(crate) struct MacroFixture {
1374 1374
1375impl MacroFixture { 1375impl MacroFixture {
1376 pub(crate) fn expand_tt(&self, invocation: &str) -> tt::Subtree { 1376 pub(crate) fn expand_tt(&self, invocation: &str) -> tt::Subtree {
1377 let source_file = ast::SourceFile::parse(invocation).ok().unwrap(); 1377 self.try_expand_tt(invocation).unwrap()
1378 }
1379
1380 fn try_expand_tt(&self, invocation: &str) -> Result<tt::Subtree, ExpandError> {
1381 let source_file = ast::SourceFile::parse(invocation).tree();
1378 let macro_invocation = 1382 let macro_invocation =
1379 source_file.syntax().descendants().find_map(ast::MacroCall::cast).unwrap(); 1383 source_file.syntax().descendants().find_map(ast::MacroCall::cast).unwrap();
1380 1384
1381 let (invocation_tt, _) = 1385 let (invocation_tt, _) =
1382 ast_to_token_tree(&macro_invocation.token_tree().unwrap()).unwrap(); 1386 ast_to_token_tree(&macro_invocation.token_tree().unwrap()).unwrap();
1383 1387
1384 self.rules.expand(&invocation_tt).unwrap() 1388 self.rules.expand(&invocation_tt)
1389 }
1390
1391 fn assert_expand_err(&self, invocation: &str, err: &ExpandError) {
1392 assert_eq!(self.try_expand_tt(invocation).as_ref(), Err(err));
1385 } 1393 }
1386 1394
1387 fn expand_items(&self, invocation: &str) -> SyntaxNode { 1395 fn expand_items(&self, invocation: &str) -> SyntaxNode {
@@ -1448,7 +1456,7 @@ impl MacroFixture {
1448 1456
1449pub(crate) fn parse_macro(macro_definition: &str) -> MacroFixture { 1457pub(crate) fn parse_macro(macro_definition: &str) -> MacroFixture {
1450 let source_file = ast::SourceFile::parse(macro_definition).ok().unwrap(); 1458 let source_file = ast::SourceFile::parse(macro_definition).ok().unwrap();
1451 let macro_definition = 1459 let macro_definition: ast::MacroCall =
1452 source_file.syntax().descendants().find_map(ast::MacroCall::cast).unwrap(); 1460 source_file.syntax().descendants().find_map(ast::MacroCall::cast).unwrap();
1453 1461
1454 let (definition_tt, _) = ast_to_token_tree(&macro_definition.token_tree().unwrap()).unwrap(); 1462 let (definition_tt, _) = ast_to_token_tree(&macro_definition.token_tree().unwrap()).unwrap();
@@ -1539,3 +1547,13 @@ fn test_repeat_bad_var() {
1539 ) 1547 )
1540 .assert_expand_items("foo!(b0 b1);", "b0 b1"); 1548 .assert_expand_items("foo!(b0 b1);", "b0 b1");
1541} 1549}
1550
1551#[test]
1552fn test_expand_bad_literal() {
1553 parse_macro(
1554 r#"
1555 macro_rules! foo { ($i:literal) => {}; }
1556 "#,
1557 )
1558 .assert_expand_err(r#"foo!(&k");"#, &ExpandError::NoMatchingRule);
1559}