diff options
author | bors[bot] <bors[bot]@users.noreply.github.com> | 2019-06-16 08:58:15 +0100 |
---|---|---|
committer | bors[bot] <bors[bot]@users.noreply.github.com> | 2019-06-16 08:58:15 +0100 |
commit | ce9ea0939a1ae94a83d56ddafc7aeb757dcda776 (patch) | |
tree | 3a82994adcb0cadcfc1da9ea651879f734b14144 /crates/ra_syntax/src | |
parent | 41c56c8a0d01d395e49cd199e6050b02a91cff1d (diff) | |
parent | 774537fb01a8839d3cb933017067541bd0fd542b (diff) |
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](https://github.com/rust-lang/rust/blob/9ebf47851a357faa4cd97f4b1dc7835f6376e639/src/test/run-pass/proc-macro/attr-stmt-expr.rs#L22-L25).
Co-authored-by: Shotaro Yamada <[email protected]>
Diffstat (limited to 'crates/ra_syntax/src')
-rw-r--r-- | crates/ra_syntax/src/ast/expr_extensions.rs | 15 |
1 files changed, 13 insertions, 2 deletions
diff --git a/crates/ra_syntax/src/ast/expr_extensions.rs b/crates/ra_syntax/src/ast/expr_extensions.rs index 17763809d..d88671d45 100644 --- a/crates/ra_syntax/src/ast/expr_extensions.rs +++ b/crates/ra_syntax/src/ast/expr_extensions.rs | |||
@@ -229,8 +229,12 @@ pub enum LiteralKind { | |||
229 | 229 | ||
230 | impl ast::Literal { | 230 | impl ast::Literal { |
231 | pub fn token(&self) -> SyntaxToken { | 231 | pub fn token(&self) -> SyntaxToken { |
232 | match self.syntax().first_child_or_token().unwrap() { | 232 | let elem = self |
233 | SyntaxElement::Token(token) => token, | 233 | .syntax() |
234 | .children_with_tokens() | ||
235 | .find(|e| e.kind() != ATTR && !e.kind().is_trivia()); | ||
236 | match elem { | ||
237 | Some(SyntaxElement::Token(token)) => token, | ||
234 | _ => unreachable!(), | 238 | _ => unreachable!(), |
235 | } | 239 | } |
236 | } | 240 | } |
@@ -268,6 +272,13 @@ impl ast::Literal { | |||
268 | } | 272 | } |
269 | } | 273 | } |
270 | 274 | ||
275 | #[test] | ||
276 | fn test_literal_with_attr() { | ||
277 | let parse = ast::SourceFile::parse(r#"const _: &str = { #[attr] "Hello" };"#); | ||
278 | let lit = parse.tree.syntax().descendants().find_map(ast::Literal::cast).unwrap(); | ||
279 | assert_eq!(lit.token().text(), r#""Hello""#); | ||
280 | } | ||
281 | |||
271 | impl ast::NamedField { | 282 | impl ast::NamedField { |
272 | pub fn parent_struct_lit(&self) -> &ast::StructLit { | 283 | pub fn parent_struct_lit(&self) -> &ast::StructLit { |
273 | self.syntax().ancestors().find_map(ast::StructLit::cast).unwrap() | 284 | self.syntax().ancestors().find_map(ast::StructLit::cast).unwrap() |