From 4768f5e7177159b894d65a50b1e4492cb4048ac3 Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Thu, 22 Aug 2019 17:43:09 +0200 Subject: Improve/fix type bound lowering --- crates/ra_syntax/src/ast/extensions.rs | 55 ++++++++++++++++++---------------- 1 file changed, 29 insertions(+), 26 deletions(-) (limited to 'crates/ra_syntax/src/ast') 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 { } } +#[derive(Clone, Debug, PartialEq, Eq, Hash)] +pub enum TypeBoundKind { + /// Trait + PathType(ast::PathType), + /// for<'a> ... + ForType(ast::ForType), + /// 'a + Lifetime(ast::SyntaxToken), +} + impl ast::TypeBound { + pub fn kind(&self) -> TypeBoundKind { + if let Some(path_type) = children(self).next() { + TypeBoundKind::PathType(path_type) + } else if let Some(for_type) = children(self).next() { + TypeBoundKind::ForType(for_type) + } else if let Some(lifetime) = self.lifetime() { + TypeBoundKind::Lifetime(lifetime) + } else { + unreachable!() + } + } + + fn lifetime(&self) -> Option { + self.syntax() + .children_with_tokens() + .filter_map(|it| it.into_token()) + .find(|it| it.kind() == LIFETIME) + } + pub fn question_mark_token(&self) -> Option { self.syntax() .children_with_tokens() @@ -399,29 +428,3 @@ impl ast::TraitDef { self.syntax().children_with_tokens().any(|t| t.kind() == T![auto]) } } - -#[derive(Clone, Debug, PartialEq, Eq, Hash)] -pub enum TypeBoundKind { - /// Trait - PathType(ast::PathType), - /// for<'a> ... - ForType(ast::ForType), - /// 'a - Lifetime(ast::SyntaxToken), -} - -impl ast::TypeBound { - pub fn kind(&self) -> Option { - let child = self.syntax.first_child_or_token()?; - match child.kind() { - PATH_TYPE => Some(TypeBoundKind::PathType( - ast::PathType::cast(child.into_node().unwrap()).unwrap(), - )), - FOR_TYPE => Some(TypeBoundKind::ForType( - ast::ForType::cast(child.into_node().unwrap()).unwrap(), - )), - LIFETIME => Some(TypeBoundKind::Lifetime(child.into_token().unwrap())), - _ => unreachable!(), - } - } -} -- cgit v1.2.3