From e6545cc647829091dd5aae5753bd4f5302becab6 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 30 May 2019 14:05:35 +0300 Subject: add ModuleDef::BuiltInType --- crates/ra_hir/src/code_model.rs | 15 +++++++++++++-- crates/ra_hir/src/lib.rs | 5 +++-- crates/ra_hir/src/ty/infer.rs | 3 ++- crates/ra_hir/src/ty/lower.rs | 36 +++++++++++++++++++++++++++++++----- crates/ra_hir/src/ty/primitive.rs | 12 ++++++++++++ 5 files changed, 61 insertions(+), 10 deletions(-) (limited to 'crates/ra_hir') diff --git a/crates/ra_hir/src/code_model.rs b/crates/ra_hir/src/code_model.rs index 69496b624..3f77850dd 100644 --- a/crates/ra_hir/src/code_model.rs +++ b/crates/ra_hir/src/code_model.rs @@ -9,7 +9,7 @@ use crate::{ type_ref::TypeRef, nameres::{ModuleScope, Namespace, ImportId, CrateModuleId}, expr::{Body, BodySourceMap, validation::ExprValidator}, - ty::{TraitRef, InferenceResult}, + ty::{TraitRef, InferenceResult, primitive::{IntTy, FloatTy}}, adt::{EnumVariantId, StructFieldId, VariantDef}, generics::HasGenericParams, docs::{Documentation, Docs, docs_from_ast}, @@ -75,6 +75,15 @@ pub struct Module { pub(crate) module_id: CrateModuleId, } +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] +pub enum BuiltinType { + Char, + Bool, + Str, + Int(IntTy), + Float(FloatTy), +} + /// The defs which can be visible in the module. #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] pub enum ModuleDef { @@ -89,6 +98,7 @@ pub enum ModuleDef { Static(Static), Trait(Trait), TypeAlias(TypeAlias), + BuiltinType(BuiltinType), } impl_froms!( ModuleDef: Module, @@ -100,7 +110,8 @@ impl_froms!( Const, Static, Trait, - TypeAlias + TypeAlias, + BuiltinType ); pub enum ModuleSource { diff --git a/crates/ra_hir/src/lib.rs b/crates/ra_hir/src/lib.rs index cb09c60f8..3e00eea26 100644 --- a/crates/ra_hir/src/lib.rs +++ b/crates/ra_hir/src/lib.rs @@ -6,7 +6,7 @@ //! applied. So, the relation between syntax and HIR is many-to-one. macro_rules! impl_froms { - ($e:ident: $($v:ident), *) => { + ($e:ident: $($v:ident),*) => { $( impl From<$v> for $e { fn from(it: $v) -> $e { @@ -80,5 +80,6 @@ pub use self::code_model::{ Function, FnSignature, StructField, FieldSource, Static, Const, ConstSignature, - Trait, TypeAlias, MacroDef, Container + Trait, TypeAlias, MacroDef, Container, + BuiltinType, }; 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> { | TypableDef::Function(_) | TypableDef::Enum(_) | TypableDef::Const(_) - | TypableDef::Static(_) => (Ty::Unknown, None), + | TypableDef::Static(_) + | TypableDef::BuiltinType(_) => (Ty::Unknown, None), } } 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; use crate::{ Function, Struct, Union, StructField, Enum, EnumVariant, Path, ModuleDef, TypeAlias, Const, Static, - HirDatabase, + HirDatabase, BuiltinType, type_ref::TypeRef, name::KnownName, nameres::Namespace, @@ -66,7 +66,7 @@ impl Ty { pub(crate) fn from_hir_path(db: &impl HirDatabase, resolver: &Resolver, path: &Path) -> Self { if let Some(name) = path.as_ident() { - // FIXME handle primitive type names in resolver as well? + // TODO: remove this if let Some(int_ty) = primitive::IntTy::from_type_name(name) { return Ty::simple(TypeCtor::Int(primitive::UncertainIntTy::Known(int_ty))); } else if let Some(float_ty) = primitive::FloatTy::from_type_name(name) { @@ -128,7 +128,7 @@ impl Ty { TypableDef::Enum(e) => Some(e.into()), TypableDef::EnumVariant(var) => Some(var.parent_enum(db).into()), TypableDef::TypeAlias(t) => Some(t.into()), - TypableDef::Const(_) | TypableDef::Static(_) => None, + TypableDef::Const(_) | TypableDef::Static(_) | TypableDef::BuiltinType(_) => None, }; substs_from_path_segment(db, resolver, segment, def_generic, false) } @@ -149,7 +149,8 @@ impl Ty { | TypableDef::Enum(_) | TypableDef::Const(_) | TypableDef::Static(_) - | TypableDef::TypeAlias(_) => last, + | TypableDef::TypeAlias(_) + | TypableDef::BuiltinType(_) => last, TypableDef::EnumVariant(_) => { // the generic args for an enum variant may be either specified // 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 (TypableDef::TypeAlias(t), Namespace::Types) => type_for_type_alias(db, t), (TypableDef::Const(c), Namespace::Values) => type_for_const(db, c), (TypableDef::Static(c), Namespace::Values) => type_for_static(db, c), + (TypableDef::BuiltinType(t), Namespace::Types) => type_for_builtin(t), // 'error' cases: (TypableDef::Function(_), Namespace::Types) => Ty::Unknown, @@ -308,6 +310,7 @@ pub(crate) fn type_for_def(db: &impl HirDatabase, def: TypableDef, ns: Namespace (TypableDef::TypeAlias(_), Namespace::Values) => Ty::Unknown, (TypableDef::Const(_), Namespace::Types) => Ty::Unknown, (TypableDef::Static(_), Namespace::Types) => Ty::Unknown, + (TypableDef::BuiltinType(_), Namespace::Values) => Ty::Unknown, } } @@ -399,6 +402,17 @@ fn type_for_static(db: &impl HirDatabase, def: Static) -> Ty { Ty::from_hir(db, &resolver, signature.type_ref()) } +/// Build the declared type of a static. +fn type_for_builtin(def: BuiltinType) -> Ty { + Ty::simple(match def { + BuiltinType::Char => TypeCtor::Char, + BuiltinType::Bool => TypeCtor::Bool, + BuiltinType::Str => TypeCtor::Str, + BuiltinType::Int(ty) => TypeCtor::Int(ty.into()), + BuiltinType::Float(ty) => TypeCtor::Float(ty.into()), + }) +} + fn fn_sig_for_struct_constructor(db: &impl HirDatabase, def: Struct) -> FnSig { let var_data = def.variant_data(db); let fields = match var_data.fields() { @@ -477,8 +491,19 @@ pub enum TypableDef { TypeAlias(TypeAlias), Const(Const), Static(Static), + BuiltinType(BuiltinType), } -impl_froms!(TypableDef: Function, Struct, Union, Enum, EnumVariant, TypeAlias, Const, Static); +impl_froms!( + TypableDef: Function, + Struct, + Union, + Enum, + EnumVariant, + TypeAlias, + Const, + Static, + BuiltinType +); impl From for Option { fn from(def: ModuleDef) -> Option { @@ -491,6 +516,7 @@ impl From for Option { ModuleDef::TypeAlias(t) => t.into(), ModuleDef::Const(v) => v.into(), ModuleDef::Static(v) => v.into(), + ModuleDef::BuiltinType(t) => t.into(), ModuleDef::Module(_) | ModuleDef::Trait(_) => return None, }; 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 { Known(IntTy), } +impl From for UncertainIntTy { + fn from(ty: IntTy) -> Self { + UncertainIntTy::Known(ty) + } +} + impl fmt::Display for UncertainIntTy { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { match *self { @@ -45,6 +51,12 @@ pub enum UncertainFloatTy { Known(FloatTy), } +impl From for UncertainFloatTy { + fn from(ty: FloatTy) -> Self { + UncertainFloatTy::Known(ty) + } +} + impl fmt::Display for UncertainFloatTy { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { match *self { -- cgit v1.2.3