diff options
author | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2020-02-22 11:17:23 +0000 |
---|---|---|
committer | GitHub <[email protected]> | 2020-02-22 11:17:23 +0000 |
commit | 62ddf617e2d2774744bd376dfb5a17ad4dc9cf19 (patch) | |
tree | d461a85607f0a10cfed39dc42fb9c4e64d677ea9 | |
parent | baf832d6d903afbc39e3a01c752a1aa5218c020e (diff) | |
parent | 5f15e3aeb00a8f7379916106fc203f230c5bb7e8 (diff) |
Merge #3230
3230: ra_mbe: convert_literal now works with malformed tokens r=edwin0cheng a=Veetaha
Fixes: #3226
Co-authored-by: Veetaha <[email protected]>
Co-authored-by: Veetaha <[email protected]>
-rw-r--r-- | crates/ra_mbe/src/subtree_source.rs | 5 | ||||
-rw-r--r-- | crates/ra_mbe/src/tests.rs | 22 |
2 files changed, 23 insertions, 4 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 @@ | |||
1 | //! FIXME: write short doc here | 1 | //! FIXME: write short doc here |
2 | 2 | ||
3 | use ra_parser::{Token, TokenSource}; | 3 | use ra_parser::{Token, TokenSource}; |
4 | use ra_syntax::{lex_single_valid_syntax_kind, SmolStr, SyntaxKind, SyntaxKind::*, T}; | 4 | use ra_syntax::{lex_single_syntax_kind, SmolStr, SyntaxKind, SyntaxKind::*, T}; |
5 | use std::cell::{Cell, Ref, RefCell}; | 5 | use std::cell::{Cell, Ref, RefCell}; |
6 | use tt::buffer::{Cursor, TokenBuffer}; | 6 | use tt::buffer::{Cursor, TokenBuffer}; |
7 | 7 | ||
@@ -129,7 +129,8 @@ fn convert_delim(d: Option<tt::DelimiterKind>, closing: bool) -> TtToken { | |||
129 | } | 129 | } |
130 | 130 | ||
131 | fn convert_literal(l: &tt::Literal) -> TtToken { | 131 | fn convert_literal(l: &tt::Literal) -> TtToken { |
132 | let kind = lex_single_valid_syntax_kind(&l.text) | 132 | let kind = lex_single_syntax_kind(&l.text) |
133 | .map(|(kind, _error)| kind) | ||
133 | .filter(|kind| kind.is_literal()) | 134 | .filter(|kind| kind.is_literal()) |
134 | .unwrap_or_else(|| match l.text.as_ref() { | 135 | .unwrap_or_else(|| match l.text.as_ref() { |
135 | "true" => T![true], | 136 | "true" => T![true], |
diff --git a/crates/ra_mbe/src/tests.rs b/crates/ra_mbe/src/tests.rs index e0d689704..cb228702f 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 | ||
1375 | impl MacroFixture { | 1375 | impl 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(¯o_invocation.token_tree().unwrap()).unwrap(); | 1386 | ast_to_token_tree(¯o_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 { |
@@ -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] | ||
1552 | fn 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 | } | ||