Merge #1407
1407: Skip attrs in `ast::Literal::token` r=matklad a=sinkuu
`ast::Literal::token` panics on literals containing attributes. rust-analyzer crashed on the `rust-lang/rust` repository by parsing [a test containing this](9ebf47851a/src/test/run-pass/proc-macro/attr-stmt-expr.rs (L22-L25)
).
Co-authored-by: Shotaro Yamada <sinkuu@sinkuu.xyz>
This commit is contained in:
commit
ce9ea0939a
@ -229,8 +229,12 @@ pub enum LiteralKind {
|
||||
|
||||
impl ast::Literal {
|
||||
pub fn token(&self) -> SyntaxToken {
|
||||
match self.syntax().first_child_or_token().unwrap() {
|
||||
SyntaxElement::Token(token) => token,
|
||||
let elem = self
|
||||
.syntax()
|
||||
.children_with_tokens()
|
||||
.find(|e| e.kind() != ATTR && !e.kind().is_trivia());
|
||||
match elem {
|
||||
Some(SyntaxElement::Token(token)) => token,
|
||||
_ => unreachable!(),
|
||||
}
|
||||
}
|
||||
@ -268,6 +272,13 @@ impl ast::Literal {
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_literal_with_attr() {
|
||||
let parse = ast::SourceFile::parse(r#"const _: &str = { #[attr] "Hello" };"#);
|
||||
let lit = parse.tree.syntax().descendants().find_map(ast::Literal::cast).unwrap();
|
||||
assert_eq!(lit.token().text(), r#""Hello""#);
|
||||
}
|
||||
|
||||
impl ast::NamedField {
|
||||
pub fn parent_struct_lit(&self) -> &ast::StructLit {
|
||||
self.syntax().ancestors().find_map(ast::StructLit::cast).unwrap()
|
||||
|
Loading…
x
Reference in New Issue
Block a user