3230: ra_mbe: convert_literal now works with malformed tokens r=edwin0cheng a=Veetaha

Fixes: #3226

Co-authored-by: Veetaha <gerzoh1@gmail.com>
Co-authored-by: Veetaha <veetaha2@gmail.com>
This commit is contained in:
bors[bot] 2020-02-22 11:17:23 +00:00 committed by GitHub
commit 62ddf617e2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 23 additions and 4 deletions

View file

@ -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<tt::DelimiterKind>, 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],

View file

@ -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<tt::Subtree, ExpandError> {
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(&macro_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 {
@ -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);
}