diff options
Diffstat (limited to 'crates/ra_hir/src/ty/lower.rs')
-rw-r--r-- | crates/ra_hir/src/ty/lower.rs | 40 |
1 files changed, 19 insertions, 21 deletions
diff --git a/crates/ra_hir/src/ty/lower.rs b/crates/ra_hir/src/ty/lower.rs index 389a2fc68..72b1234bf 100644 --- a/crates/ra_hir/src/ty/lower.rs +++ b/crates/ra_hir/src/ty/lower.rs | |||
@@ -6,8 +6,6 @@ | |||
6 | //! | 6 | //! |
7 | //! This usually involves resolving names, collecting generic arguments etc. | 7 | //! This usually involves resolving names, collecting generic arguments etc. |
8 | 8 | ||
9 | use std::sync::Arc; | ||
10 | |||
11 | use crate::{ | 9 | use crate::{ |
12 | Function, Struct, StructField, Enum, EnumVariant, Path, | 10 | Function, Struct, StructField, Enum, EnumVariant, Path, |
13 | ModuleDef, TypeAlias, | 11 | ModuleDef, TypeAlias, |
@@ -21,40 +19,40 @@ use crate::{ | |||
21 | generics::GenericParams, | 19 | generics::GenericParams, |
22 | adt::VariantDef, | 20 | adt::VariantDef, |
23 | }; | 21 | }; |
24 | use super::{Ty, primitive, FnSig, Substs}; | 22 | use super::{Ty, primitive, FnSig, Substs, TypeCtor}; |
25 | 23 | ||
26 | impl Ty { | 24 | impl Ty { |
27 | pub(crate) fn from_hir(db: &impl HirDatabase, resolver: &Resolver, type_ref: &TypeRef) -> Self { | 25 | pub(crate) fn from_hir(db: &impl HirDatabase, resolver: &Resolver, type_ref: &TypeRef) -> Self { |
28 | match type_ref { | 26 | match type_ref { |
29 | TypeRef::Never => Ty::Never, | 27 | TypeRef::Never => Ty::simple(TypeCtor::Never), |
30 | TypeRef::Tuple(inner) => { | 28 | TypeRef::Tuple(inner) => { |
31 | let inner_tys = | 29 | let inner_tys = |
32 | inner.iter().map(|tr| Ty::from_hir(db, resolver, tr)).collect::<Vec<_>>(); | 30 | inner.iter().map(|tr| Ty::from_hir(db, resolver, tr)).collect::<Vec<_>>(); |
33 | Ty::Tuple(inner_tys.into()) | 31 | Ty::apply(TypeCtor::Tuple, Substs(inner_tys.into())) |
34 | } | 32 | } |
35 | TypeRef::Path(path) => Ty::from_hir_path(db, resolver, path), | 33 | TypeRef::Path(path) => Ty::from_hir_path(db, resolver, path), |
36 | TypeRef::RawPtr(inner, mutability) => { | 34 | TypeRef::RawPtr(inner, mutability) => { |
37 | let inner_ty = Ty::from_hir(db, resolver, inner); | 35 | let inner_ty = Ty::from_hir(db, resolver, inner); |
38 | Ty::RawPtr(Arc::new(inner_ty), *mutability) | 36 | Ty::apply_one(TypeCtor::RawPtr(*mutability), inner_ty) |
39 | } | 37 | } |
40 | TypeRef::Array(inner) => { | 38 | TypeRef::Array(inner) => { |
41 | let inner_ty = Ty::from_hir(db, resolver, inner); | 39 | let inner_ty = Ty::from_hir(db, resolver, inner); |
42 | Ty::Array(Arc::new(inner_ty)) | 40 | Ty::apply_one(TypeCtor::Array, inner_ty) |
43 | } | 41 | } |
44 | TypeRef::Slice(inner) => { | 42 | TypeRef::Slice(inner) => { |
45 | let inner_ty = Ty::from_hir(db, resolver, inner); | 43 | let inner_ty = Ty::from_hir(db, resolver, inner); |
46 | Ty::Slice(Arc::new(inner_ty)) | 44 | Ty::apply_one(TypeCtor::Slice, inner_ty) |
47 | } | 45 | } |
48 | TypeRef::Reference(inner, mutability) => { | 46 | TypeRef::Reference(inner, mutability) => { |
49 | let inner_ty = Ty::from_hir(db, resolver, inner); | 47 | let inner_ty = Ty::from_hir(db, resolver, inner); |
50 | Ty::Ref(Arc::new(inner_ty), *mutability) | 48 | Ty::apply_one(TypeCtor::Ref(*mutability), inner_ty) |
51 | } | 49 | } |
52 | TypeRef::Placeholder => Ty::Unknown, | 50 | TypeRef::Placeholder => Ty::Unknown, |
53 | TypeRef::Fn(params) => { | 51 | TypeRef::Fn(params) => { |
54 | let inner_tys = | 52 | let inner_tys = |
55 | params.iter().map(|tr| Ty::from_hir(db, resolver, tr)).collect::<Vec<_>>(); | 53 | params.iter().map(|tr| Ty::from_hir(db, resolver, tr)).collect::<Vec<_>>(); |
56 | let sig = FnSig { params_and_return: inner_tys.into() }; | 54 | let sig = Substs(inner_tys.into()); |
57 | Ty::FnPtr(sig) | 55 | Ty::apply(TypeCtor::FnPtr, sig) |
58 | } | 56 | } |
59 | TypeRef::Error => Ty::Unknown, | 57 | TypeRef::Error => Ty::Unknown, |
60 | } | 58 | } |
@@ -64,14 +62,14 @@ impl Ty { | |||
64 | if let Some(name) = path.as_ident() { | 62 | if let Some(name) = path.as_ident() { |
65 | // TODO handle primitive type names in resolver as well? | 63 | // TODO handle primitive type names in resolver as well? |
66 | if let Some(int_ty) = primitive::UncertainIntTy::from_type_name(name) { | 64 | if let Some(int_ty) = primitive::UncertainIntTy::from_type_name(name) { |
67 | return Ty::Int(int_ty); | 65 | return Ty::simple(TypeCtor::Int(int_ty)); |
68 | } else if let Some(float_ty) = primitive::UncertainFloatTy::from_type_name(name) { | 66 | } else if let Some(float_ty) = primitive::UncertainFloatTy::from_type_name(name) { |
69 | return Ty::Float(float_ty); | 67 | return Ty::simple(TypeCtor::Float(float_ty)); |
70 | } else if let Some(known) = name.as_known_name() { | 68 | } else if let Some(known) = name.as_known_name() { |
71 | match known { | 69 | match known { |
72 | KnownName::Bool => return Ty::Bool, | 70 | KnownName::Bool => return Ty::simple(TypeCtor::Bool), |
73 | KnownName::Char => return Ty::Char, | 71 | KnownName::Char => return Ty::simple(TypeCtor::Char), |
74 | KnownName::Str => return Ty::Str, | 72 | KnownName::Str => return Ty::simple(TypeCtor::Str), |
75 | _ => {} | 73 | _ => {} |
76 | } | 74 | } |
77 | } | 75 | } |
@@ -247,7 +245,7 @@ fn fn_sig_for_fn(db: &impl HirDatabase, def: Function) -> FnSig { | |||
247 | fn type_for_fn(db: &impl HirDatabase, def: Function) -> Ty { | 245 | fn type_for_fn(db: &impl HirDatabase, def: Function) -> Ty { |
248 | let generics = def.generic_params(db); | 246 | let generics = def.generic_params(db); |
249 | let substs = make_substs(&generics); | 247 | let substs = make_substs(&generics); |
250 | Ty::FnDef { def: def.into(), substs } | 248 | Ty::apply(TypeCtor::FnDef(def.into()), substs) |
251 | } | 249 | } |
252 | 250 | ||
253 | /// Build the declared type of a const. | 251 | /// Build the declared type of a const. |
@@ -289,7 +287,7 @@ fn type_for_struct_constructor(db: &impl HirDatabase, def: Struct) -> Ty { | |||
289 | } | 287 | } |
290 | let generics = def.generic_params(db); | 288 | let generics = def.generic_params(db); |
291 | let substs = make_substs(&generics); | 289 | let substs = make_substs(&generics); |
292 | Ty::FnDef { def: def.into(), substs } | 290 | Ty::apply(TypeCtor::FnDef(def.into()), substs) |
293 | } | 291 | } |
294 | 292 | ||
295 | fn fn_sig_for_enum_variant_constructor(db: &impl HirDatabase, def: EnumVariant) -> FnSig { | 293 | fn fn_sig_for_enum_variant_constructor(db: &impl HirDatabase, def: EnumVariant) -> FnSig { |
@@ -317,7 +315,7 @@ fn type_for_enum_variant_constructor(db: &impl HirDatabase, def: EnumVariant) -> | |||
317 | } | 315 | } |
318 | let generics = def.parent_enum(db).generic_params(db); | 316 | let generics = def.parent_enum(db).generic_params(db); |
319 | let substs = make_substs(&generics); | 317 | let substs = make_substs(&generics); |
320 | Ty::FnDef { def: def.into(), substs } | 318 | Ty::apply(TypeCtor::FnDef(def.into()), substs) |
321 | } | 319 | } |
322 | 320 | ||
323 | fn make_substs(generics: &GenericParams) -> Substs { | 321 | fn make_substs(generics: &GenericParams) -> Substs { |
@@ -333,12 +331,12 @@ fn make_substs(generics: &GenericParams) -> Substs { | |||
333 | 331 | ||
334 | fn type_for_struct(db: &impl HirDatabase, s: Struct) -> Ty { | 332 | fn type_for_struct(db: &impl HirDatabase, s: Struct) -> Ty { |
335 | let generics = s.generic_params(db); | 333 | let generics = s.generic_params(db); |
336 | Ty::Adt { def_id: s.into(), substs: make_substs(&generics) } | 334 | Ty::apply(TypeCtor::Adt(s.into()), make_substs(&generics)) |
337 | } | 335 | } |
338 | 336 | ||
339 | fn type_for_enum(db: &impl HirDatabase, s: Enum) -> Ty { | 337 | fn type_for_enum(db: &impl HirDatabase, s: Enum) -> Ty { |
340 | let generics = s.generic_params(db); | 338 | let generics = s.generic_params(db); |
341 | Ty::Adt { def_id: s.into(), substs: make_substs(&generics) } | 339 | Ty::apply(TypeCtor::Adt(s.into()), make_substs(&generics)) |
342 | } | 340 | } |
343 | 341 | ||
344 | fn type_for_type_alias(db: &impl HirDatabase, t: TypeAlias) -> Ty { | 342 | fn type_for_type_alias(db: &impl HirDatabase, t: TypeAlias) -> Ty { |