aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir/src/ty
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2019-05-30 12:05:35 +0100
committerAleksey Kladov <[email protected]>2019-05-30 12:06:28 +0100
commite6545cc647829091dd5aae5753bd4f5302becab6 (patch)
tree640de902fede2421a299ba22609fbd9db5b07231 /crates/ra_hir/src/ty
parent6b88735fe6cd3b259816c7c90a2675ee057c9e4c (diff)
add ModuleDef::BuiltInType
Diffstat (limited to 'crates/ra_hir/src/ty')
-rw-r--r--crates/ra_hir/src/ty/infer.rs3
-rw-r--r--crates/ra_hir/src/ty/lower.rs36
-rw-r--r--crates/ra_hir/src/ty/primitive.rs12
3 files changed, 45 insertions, 6 deletions
diff --git a/crates/ra_hir/src/ty/infer.rs b/crates/ra_hir/src/ty/infer.rs
index 7d8250292..e8ae33ead 100644
--- a/crates/ra_hir/src/ty/infer.rs
+++ b/crates/ra_hir/src/ty/infer.rs
@@ -649,7 +649,8 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
649 | TypableDef::Function(_) 649 | TypableDef::Function(_)
650 | TypableDef::Enum(_) 650 | TypableDef::Enum(_)
651 | TypableDef::Const(_) 651 | TypableDef::Const(_)
652 | TypableDef::Static(_) => (Ty::Unknown, None), 652 | TypableDef::Static(_)
653 | TypableDef::BuiltinType(_) => (Ty::Unknown, None),
653 } 654 }
654 } 655 }
655 656
diff --git a/crates/ra_hir/src/ty/lower.rs b/crates/ra_hir/src/ty/lower.rs
index 7defa7a9b..c4cef2d7c 100644
--- a/crates/ra_hir/src/ty/lower.rs
+++ b/crates/ra_hir/src/ty/lower.rs
@@ -10,7 +10,7 @@ use std::iter;
10 10
11use crate::{ 11use crate::{
12 Function, Struct, Union, StructField, Enum, EnumVariant, Path, ModuleDef, TypeAlias, Const, Static, 12 Function, Struct, Union, StructField, Enum, EnumVariant, Path, ModuleDef, TypeAlias, Const, Static,
13 HirDatabase, 13 HirDatabase, BuiltinType,
14 type_ref::TypeRef, 14 type_ref::TypeRef,
15 name::KnownName, 15 name::KnownName,
16 nameres::Namespace, 16 nameres::Namespace,
@@ -66,7 +66,7 @@ impl Ty {
66 66
67 pub(crate) fn from_hir_path(db: &impl HirDatabase, resolver: &Resolver, path: &Path) -> Self { 67 pub(crate) fn from_hir_path(db: &impl HirDatabase, resolver: &Resolver, path: &Path) -> Self {
68 if let Some(name) = path.as_ident() { 68 if let Some(name) = path.as_ident() {
69 // FIXME handle primitive type names in resolver as well? 69 // TODO: remove this
70 if let Some(int_ty) = primitive::IntTy::from_type_name(name) { 70 if let Some(int_ty) = primitive::IntTy::from_type_name(name) {
71 return Ty::simple(TypeCtor::Int(primitive::UncertainIntTy::Known(int_ty))); 71 return Ty::simple(TypeCtor::Int(primitive::UncertainIntTy::Known(int_ty)));
72 } else if let Some(float_ty) = primitive::FloatTy::from_type_name(name) { 72 } else if let Some(float_ty) = primitive::FloatTy::from_type_name(name) {
@@ -128,7 +128,7 @@ impl Ty {
128 TypableDef::Enum(e) => Some(e.into()), 128 TypableDef::Enum(e) => Some(e.into()),
129 TypableDef::EnumVariant(var) => Some(var.parent_enum(db).into()), 129 TypableDef::EnumVariant(var) => Some(var.parent_enum(db).into()),
130 TypableDef::TypeAlias(t) => Some(t.into()), 130 TypableDef::TypeAlias(t) => Some(t.into()),
131 TypableDef::Const(_) | TypableDef::Static(_) => None, 131 TypableDef::Const(_) | TypableDef::Static(_) | TypableDef::BuiltinType(_) => None,
132 }; 132 };
133 substs_from_path_segment(db, resolver, segment, def_generic, false) 133 substs_from_path_segment(db, resolver, segment, def_generic, false)
134 } 134 }
@@ -149,7 +149,8 @@ impl Ty {
149 | TypableDef::Enum(_) 149 | TypableDef::Enum(_)
150 | TypableDef::Const(_) 150 | TypableDef::Const(_)
151 | TypableDef::Static(_) 151 | TypableDef::Static(_)
152 | TypableDef::TypeAlias(_) => last, 152 | TypableDef::TypeAlias(_)
153 | TypableDef::BuiltinType(_) => last,
153 TypableDef::EnumVariant(_) => { 154 TypableDef::EnumVariant(_) => {
154 // the generic args for an enum variant may be either specified 155 // the generic args for an enum variant may be either specified
155 // on the segment referring to the enum, or on the segment 156 // on the segment referring to the enum, or on the segment
@@ -299,6 +300,7 @@ pub(crate) fn type_for_def(db: &impl HirDatabase, def: TypableDef, ns: Namespace
299 (TypableDef::TypeAlias(t), Namespace::Types) => type_for_type_alias(db, t), 300 (TypableDef::TypeAlias(t), Namespace::Types) => type_for_type_alias(db, t),
300 (TypableDef::Const(c), Namespace::Values) => type_for_const(db, c), 301 (TypableDef::Const(c), Namespace::Values) => type_for_const(db, c),
301 (TypableDef::Static(c), Namespace::Values) => type_for_static(db, c), 302 (TypableDef::Static(c), Namespace::Values) => type_for_static(db, c),
303 (TypableDef::BuiltinType(t), Namespace::Types) => type_for_builtin(t),
302 304
303 // 'error' cases: 305 // 'error' cases:
304 (TypableDef::Function(_), Namespace::Types) => Ty::Unknown, 306 (TypableDef::Function(_), Namespace::Types) => Ty::Unknown,
@@ -308,6 +310,7 @@ pub(crate) fn type_for_def(db: &impl HirDatabase, def: TypableDef, ns: Namespace
308 (TypableDef::TypeAlias(_), Namespace::Values) => Ty::Unknown, 310 (TypableDef::TypeAlias(_), Namespace::Values) => Ty::Unknown,
309 (TypableDef::Const(_), Namespace::Types) => Ty::Unknown, 311 (TypableDef::Const(_), Namespace::Types) => Ty::Unknown,
310 (TypableDef::Static(_), Namespace::Types) => Ty::Unknown, 312 (TypableDef::Static(_), Namespace::Types) => Ty::Unknown,
313 (TypableDef::BuiltinType(_), Namespace::Values) => Ty::Unknown,
311 } 314 }
312} 315}
313 316
@@ -399,6 +402,17 @@ fn type_for_static(db: &impl HirDatabase, def: Static) -> Ty {
399 Ty::from_hir(db, &resolver, signature.type_ref()) 402 Ty::from_hir(db, &resolver, signature.type_ref())
400} 403}
401 404
405/// Build the declared type of a static.
406fn type_for_builtin(def: BuiltinType) -> Ty {
407 Ty::simple(match def {
408 BuiltinType::Char => TypeCtor::Char,
409 BuiltinType::Bool => TypeCtor::Bool,
410 BuiltinType::Str => TypeCtor::Str,
411 BuiltinType::Int(ty) => TypeCtor::Int(ty.into()),
412 BuiltinType::Float(ty) => TypeCtor::Float(ty.into()),
413 })
414}
415
402fn fn_sig_for_struct_constructor(db: &impl HirDatabase, def: Struct) -> FnSig { 416fn fn_sig_for_struct_constructor(db: &impl HirDatabase, def: Struct) -> FnSig {
403 let var_data = def.variant_data(db); 417 let var_data = def.variant_data(db);
404 let fields = match var_data.fields() { 418 let fields = match var_data.fields() {
@@ -477,8 +491,19 @@ pub enum TypableDef {
477 TypeAlias(TypeAlias), 491 TypeAlias(TypeAlias),
478 Const(Const), 492 Const(Const),
479 Static(Static), 493 Static(Static),
494 BuiltinType(BuiltinType),
480} 495}
481impl_froms!(TypableDef: Function, Struct, Union, Enum, EnumVariant, TypeAlias, Const, Static); 496impl_froms!(
497 TypableDef: Function,
498 Struct,
499 Union,
500 Enum,
501 EnumVariant,
502 TypeAlias,
503 Const,
504 Static,
505 BuiltinType
506);
482 507
483impl From<ModuleDef> for Option<TypableDef> { 508impl From<ModuleDef> for Option<TypableDef> {
484 fn from(def: ModuleDef) -> Option<TypableDef> { 509 fn from(def: ModuleDef) -> Option<TypableDef> {
@@ -491,6 +516,7 @@ impl From<ModuleDef> for Option<TypableDef> {
491 ModuleDef::TypeAlias(t) => t.into(), 516 ModuleDef::TypeAlias(t) => t.into(),
492 ModuleDef::Const(v) => v.into(), 517 ModuleDef::Const(v) => v.into(),
493 ModuleDef::Static(v) => v.into(), 518 ModuleDef::Static(v) => v.into(),
519 ModuleDef::BuiltinType(t) => t.into(),
494 ModuleDef::Module(_) | ModuleDef::Trait(_) => return None, 520 ModuleDef::Module(_) | ModuleDef::Trait(_) => return None,
495 }; 521 };
496 Some(res) 522 Some(res)
diff --git a/crates/ra_hir/src/ty/primitive.rs b/crates/ra_hir/src/ty/primitive.rs
index b37326db7..e1ab16a6f 100644
--- a/crates/ra_hir/src/ty/primitive.rs
+++ b/crates/ra_hir/src/ty/primitive.rs
@@ -30,6 +30,12 @@ pub enum UncertainIntTy {
30 Known(IntTy), 30 Known(IntTy),
31} 31}
32 32
33impl From<IntTy> for UncertainIntTy {
34 fn from(ty: IntTy) -> Self {
35 UncertainIntTy::Known(ty)
36 }
37}
38
33impl fmt::Display for UncertainIntTy { 39impl fmt::Display for UncertainIntTy {
34 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { 40 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
35 match *self { 41 match *self {
@@ -45,6 +51,12 @@ pub enum UncertainFloatTy {
45 Known(FloatTy), 51 Known(FloatTy),
46} 52}
47 53
54impl From<FloatTy> for UncertainFloatTy {
55 fn from(ty: FloatTy) -> Self {
56 UncertainFloatTy::Known(ty)
57 }
58}
59
48impl fmt::Display for UncertainFloatTy { 60impl fmt::Display for UncertainFloatTy {
49 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { 61 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
50 match *self { 62 match *self {