From 93a19bda173cdd9ef80aeb1b647eb4e9fd4f7955 Mon Sep 17 00:00:00 2001 From: Veetaha Date: Wed, 19 Feb 2020 09:49:26 +0200 Subject: ra_mbe: convert_literal now works with malformed tokens --- crates/ra_mbe/src/subtree_source.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/crates/ra_mbe/src/subtree_source.rs b/crates/ra_mbe/src/subtree_source.rs index dacca8279..91e324db9 100644 --- a/crates/ra_mbe/src/subtree_source.rs +++ b/crates/ra_mbe/src/subtree_source.rs @@ -1,7 +1,7 @@ //! FIXME: write short doc here use ra_parser::{Token, TokenSource}; -use ra_syntax::{lex_single_valid_syntax_kind, SmolStr, SyntaxKind, SyntaxKind::*, T}; +use ra_syntax::{lex_single_syntax_kind, SmolStr, SyntaxKind, SyntaxKind::*, T}; use std::cell::{Cell, Ref, RefCell}; use tt::buffer::{Cursor, TokenBuffer}; @@ -129,7 +129,8 @@ fn convert_delim(d: Option, closing: bool) -> TtToken { } fn convert_literal(l: &tt::Literal) -> TtToken { - let kind = lex_single_valid_syntax_kind(&l.text) + let kind = lex_single_syntax_kind(&l.text) + .map(|(kind, _error)| kind) .filter(|kind| kind.is_literal()) .unwrap_or_else(|| match l.text.as_ref() { "true" => T![true], -- cgit v1.2.3 From 28bdb654074f63d6ce1a2a3fe328b1a2a1867954 Mon Sep 17 00:00:00 2001 From: Veetaha Date: Thu, 20 Feb 2020 23:42:24 +0200 Subject: 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. --- crates/ra_mbe/src/tests.rs | 24 +++++++++++++++++++++--- 1 file 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 { impl MacroFixture { pub(crate) fn expand_tt(&self, invocation: &str) -> tt::Subtree { - let source_file = ast::SourceFile::parse(invocation).ok().unwrap(); + self.try_expand_tt(invocation).unwrap() + } + + fn try_expand_tt(&self, invocation: &str) -> Result { + let source_file = ast::SourceFile::parse(invocation).tree(); let macro_invocation = source_file.syntax().descendants().find_map(ast::MacroCall::cast).unwrap(); let (invocation_tt, _) = ast_to_token_tree(¯o_invocation.token_tree().unwrap()).unwrap(); - self.rules.expand(&invocation_tt).unwrap() + self.rules.expand(&invocation_tt) + } + + fn assert_expand_err(&self, invocation: &str, err: &ExpandError) { + assert_eq!(self.try_expand_tt(invocation).as_ref(), Err(err)); } fn expand_items(&self, invocation: &str) -> SyntaxNode { @@ -1448,7 +1456,7 @@ impl MacroFixture { pub(crate) fn parse_macro(macro_definition: &str) -> MacroFixture { let source_file = ast::SourceFile::parse(macro_definition).ok().unwrap(); - let macro_definition = + let macro_definition: ast::MacroCall = source_file.syntax().descendants().find_map(ast::MacroCall::cast).unwrap(); let (definition_tt, _) = ast_to_token_tree(¯o_definition.token_tree().unwrap()).unwrap(); @@ -1539,3 +1547,13 @@ fn test_repeat_bad_var() { ) .assert_expand_items("foo!(b0 b1);", "b0 b1"); } + +#[test] +fn test_expand_bad_literal() { + parse_macro( + r#" + macro_rules! foo { ($i:literal) => {}; } + "#, + ) + .assert_expand_err(r#"foo!(&k");"#, &ExpandError::NoMatchingRule); +} -- cgit v1.2.3 From 5f15e3aeb00a8f7379916106fc203f230c5bb7e8 Mon Sep 17 00:00:00 2001 From: Veetaha Date: Sat, 22 Feb 2020 13:14:46 +0200 Subject: ra_mbe: Remove explicit type annotation --- crates/ra_mbe/src/tests.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/ra_mbe/src/tests.rs b/crates/ra_mbe/src/tests.rs index ef34dde5c..cb228702f 100644 --- a/crates/ra_mbe/src/tests.rs +++ b/crates/ra_mbe/src/tests.rs @@ -1456,7 +1456,7 @@ impl MacroFixture { pub(crate) fn parse_macro(macro_definition: &str) -> MacroFixture { let source_file = ast::SourceFile::parse(macro_definition).ok().unwrap(); - let macro_definition: ast::MacroCall = + let macro_definition = source_file.syntax().descendants().find_map(ast::MacroCall::cast).unwrap(); let (definition_tt, _) = ast_to_token_tree(¯o_definition.token_tree().unwrap()).unwrap(); -- cgit v1.2.3