From 16a7d8cc850002b427fdc8d21ccde81caaed7902 Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Tue, 13 Aug 2019 23:09:08 +0200 Subject: Add `impl Trait` and `dyn Trait` types - refactor bounds handling in the AST a bit - add HIR for bounds - add `Ty::Dyn` and `Ty::Opaque` variants and lower `dyn Trait` / `impl Trait` syntax to them --- crates/ra_syntax/src/ast.rs | 2 +- crates/ra_syntax/src/ast/extensions.rs | 26 ++++++++++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) (limited to 'crates/ra_syntax/src') diff --git a/crates/ra_syntax/src/ast.rs b/crates/ra_syntax/src/ast.rs index 6f0489617..afdfca66e 100644 --- a/crates/ra_syntax/src/ast.rs +++ b/crates/ra_syntax/src/ast.rs @@ -15,7 +15,7 @@ use crate::{ pub use self::{ expr_extensions::{ArrayExprKind, BinOp, ElseBranch, LiteralKind, PrefixOp}, - extensions::{FieldKind, PathSegmentKind, SelfParamKind, StructKind}, + extensions::{FieldKind, PathSegmentKind, SelfParamKind, StructKind, TypeBoundKind}, generated::*, tokens::*, traits::*, diff --git a/crates/ra_syntax/src/ast/extensions.rs b/crates/ra_syntax/src/ast/extensions.rs index efe261fc2..8de979ca2 100644 --- a/crates/ra_syntax/src/ast/extensions.rs +++ b/crates/ra_syntax/src/ast/extensions.rs @@ -399,3 +399,29 @@ 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 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') 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