diff options
author | Florian Diebold <[email protected]> | 2019-08-22 16:43:09 +0100 |
---|---|---|
committer | Florian Diebold <[email protected]> | 2019-08-22 20:58:29 +0100 |
commit | 4768f5e7177159b894d65a50b1e4492cb4048ac3 (patch) | |
tree | de5d4388124443eb51847a390f259acd44ef1a65 /crates/ra_syntax | |
parent | b1a40042e8f595af0486cf1cc70b63be1ff302b3 (diff) |
Improve/fix type bound lowering
Diffstat (limited to 'crates/ra_syntax')
-rw-r--r-- | crates/ra_syntax/src/ast/extensions.rs | 55 |
1 files changed, 29 insertions, 26 deletions
diff --git a/crates/ra_syntax/src/ast/extensions.rs b/crates/ra_syntax/src/ast/extensions.rs index 8de979ca2..e0ea3e5ab 100644 --- a/crates/ra_syntax/src/ast/extensions.rs +++ b/crates/ra_syntax/src/ast/extensions.rs | |||
@@ -382,7 +382,36 @@ impl ast::WherePred { | |||
382 | } | 382 | } |
383 | } | 383 | } |
384 | 384 | ||
385 | #[derive(Clone, Debug, PartialEq, Eq, Hash)] | ||
386 | pub enum TypeBoundKind { | ||
387 | /// Trait | ||
388 | PathType(ast::PathType), | ||
389 | /// for<'a> ... | ||
390 | ForType(ast::ForType), | ||
391 | /// 'a | ||
392 | Lifetime(ast::SyntaxToken), | ||
393 | } | ||
394 | |||
385 | impl ast::TypeBound { | 395 | impl ast::TypeBound { |
396 | pub fn kind(&self) -> TypeBoundKind { | ||
397 | if let Some(path_type) = children(self).next() { | ||
398 | TypeBoundKind::PathType(path_type) | ||
399 | } else if let Some(for_type) = children(self).next() { | ||
400 | TypeBoundKind::ForType(for_type) | ||
401 | } else if let Some(lifetime) = self.lifetime() { | ||
402 | TypeBoundKind::Lifetime(lifetime) | ||
403 | } else { | ||
404 | unreachable!() | ||
405 | } | ||
406 | } | ||
407 | |||
408 | fn lifetime(&self) -> Option<SyntaxToken> { | ||
409 | self.syntax() | ||
410 | .children_with_tokens() | ||
411 | .filter_map(|it| it.into_token()) | ||
412 | .find(|it| it.kind() == LIFETIME) | ||
413 | } | ||
414 | |||
386 | pub fn question_mark_token(&self) -> Option<SyntaxToken> { | 415 | pub fn question_mark_token(&self) -> Option<SyntaxToken> { |
387 | self.syntax() | 416 | self.syntax() |
388 | .children_with_tokens() | 417 | .children_with_tokens() |
@@ -399,29 +428,3 @@ impl ast::TraitDef { | |||
399 | self.syntax().children_with_tokens().any(|t| t.kind() == T![auto]) | 428 | self.syntax().children_with_tokens().any(|t| t.kind() == T![auto]) |
400 | } | 429 | } |
401 | } | 430 | } |
402 | |||
403 | #[derive(Clone, Debug, PartialEq, Eq, Hash)] | ||
404 | pub enum TypeBoundKind { | ||
405 | /// Trait | ||
406 | PathType(ast::PathType), | ||
407 | /// for<'a> ... | ||
408 | ForType(ast::ForType), | ||
409 | /// 'a | ||
410 | Lifetime(ast::SyntaxToken), | ||
411 | } | ||
412 | |||
413 | impl ast::TypeBound { | ||
414 | pub fn kind(&self) -> Option<TypeBoundKind> { | ||
415 | let child = self.syntax.first_child_or_token()?; | ||
416 | match child.kind() { | ||
417 | PATH_TYPE => Some(TypeBoundKind::PathType( | ||
418 | ast::PathType::cast(child.into_node().unwrap()).unwrap(), | ||
419 | )), | ||
420 | FOR_TYPE => Some(TypeBoundKind::ForType( | ||
421 | ast::ForType::cast(child.into_node().unwrap()).unwrap(), | ||
422 | )), | ||
423 | LIFETIME => Some(TypeBoundKind::Lifetime(child.into_token().unwrap())), | ||
424 | _ => unreachable!(), | ||
425 | } | ||
426 | } | ||
427 | } | ||