aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir/src/type_ref.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_hir/src/type_ref.rs')
-rw-r--r--crates/ra_hir/src/type_ref.rs49
1 files changed, 44 insertions, 5 deletions
diff --git a/crates/ra_hir/src/type_ref.rs b/crates/ra_hir/src/type_ref.rs
index b92a0b55a..fa91bfb22 100644
--- a/crates/ra_hir/src/type_ref.rs
+++ b/crates/ra_hir/src/type_ref.rs
@@ -1,7 +1,7 @@
1//! HIR for references to types. Paths in these are not yet resolved. They can 1//! HIR for references to types. Paths in these are not yet resolved. They can
2//! be directly created from an ast::TypeRef, without further queries. 2//! be directly created from an ast::TypeRef, without further queries.
3 3
4use ra_syntax::ast::{self, TypeAscriptionOwner}; 4use ra_syntax::ast::{self, TypeAscriptionOwner, TypeBoundsOwner};
5 5
6use crate::Path; 6use crate::Path;
7 7
@@ -49,8 +49,16 @@ pub enum TypeRef {
49 /// A fn pointer. Last element of the vector is the return type. 49 /// A fn pointer. Last element of the vector is the return type.
50 Fn(Vec<TypeRef>), 50 Fn(Vec<TypeRef>),
51 // For 51 // For
52 // ImplTrait, 52 ImplTrait(Vec<TypeBound>),
53 // DynTrait, 53 DynTrait(Vec<TypeBound>),
54 Error,
55}
56
57#[derive(Clone, PartialEq, Eq, Hash, Debug)]
58pub enum TypeBound {
59 Path(Path),
60 // also for<> bounds
61 // also Lifetimes
54 Error, 62 Error,
55} 63}
56 64
@@ -95,8 +103,12 @@ impl TypeRef {
95 } 103 }
96 // for types are close enough for our purposes to the inner type for now... 104 // for types are close enough for our purposes to the inner type for now...
97 ast::TypeRef::ForType(inner) => TypeRef::from_ast_opt(inner.type_ref()), 105 ast::TypeRef::ForType(inner) => TypeRef::from_ast_opt(inner.type_ref()),
98 ast::TypeRef::ImplTraitType(_inner) => TypeRef::Error, 106 ast::TypeRef::ImplTraitType(inner) => {
99 ast::TypeRef::DynTraitType(_inner) => TypeRef::Error, 107 TypeRef::ImplTrait(type_bounds_from_ast(inner.type_bound_list()))
108 }
109 ast::TypeRef::DynTraitType(inner) => {
110 TypeRef::DynTrait(type_bounds_from_ast(inner.type_bound_list()))
111 }
100 } 112 }
101 } 113 }
102 114
@@ -112,3 +124,30 @@ impl TypeRef {
112 TypeRef::Tuple(Vec::new()) 124 TypeRef::Tuple(Vec::new())
113 } 125 }
114} 126}
127
128pub(crate) fn type_bounds_from_ast(type_bounds_opt: Option<ast::TypeBoundList>) -> Vec<TypeBound> {
129 if let Some(type_bounds) = type_bounds_opt {
130 type_bounds.bounds().map(TypeBound::from_ast).collect()
131 } else {
132 vec![]
133 }
134}
135
136impl TypeBound {
137 pub(crate) fn from_ast(node: ast::TypeBound) -> Self {
138 match node.kind() {
139 ast::TypeBoundKind::PathType(path_type) => {
140 let path = match path_type.path() {
141 Some(p) => p,
142 None => return TypeBound::Error,
143 };
144 let path = match Path::from_ast(path) {
145 Some(p) => p,
146 None => return TypeBound::Error,
147 };
148 TypeBound::Path(path)
149 }
150 ast::TypeBoundKind::ForType(_) | ast::TypeBoundKind::Lifetime(_) => TypeBound::Error,
151 }
152 }
153}