From 216dc856c5b48de7d9cdbe6259b2d0cd3c4e5042 Mon Sep 17 00:00:00 2001 From: Jonas Schievink Date: Thu, 11 Feb 2021 19:52:33 +0100 Subject: Wrap `BuiltinType` in code model --- crates/hir/src/code_model.rs | 20 ++++++++++++++++++-- crates/hir/src/from_id.rs | 21 +++++++++++++++++---- crates/hir/src/semantics.rs | 2 +- crates/hir/src/source_analyzer.rs | 9 +++++---- 4 files changed, 41 insertions(+), 11 deletions(-) (limited to 'crates/hir/src') diff --git a/crates/hir/src/code_model.rs b/crates/hir/src/code_model.rs index e9bb4f541..35b532602 100644 --- a/crates/hir/src/code_model.rs +++ b/crates/hir/src/code_model.rs @@ -6,7 +6,6 @@ use base_db::{CrateDisplayName, CrateId, Edition, FileId}; use either::Either; use hir_def::{ adt::{ReprKind, StructKind, VariantData}, - builtin_type::BuiltinType, expr::{BindingAnnotation, LabelId, Pat, PatId}, import_map, item_tree::ItemTreeNode, @@ -245,7 +244,7 @@ impl ModuleDef { ModuleDef::Const(it) => it.name(db), ModuleDef::Static(it) => it.name(db), - ModuleDef::BuiltinType(it) => Some(it.as_name()), + ModuleDef::BuiltinType(it) => Some(it.name()), } } @@ -994,6 +993,23 @@ impl HasVisibility for TypeAlias { } } +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] +pub struct BuiltinType { + pub(crate) inner: hir_def::builtin_type::BuiltinType, +} + +impl BuiltinType { + pub fn ty(self, db: &dyn HirDatabase, module: Module) -> Type { + let resolver = module.id.resolver(db.upcast()); + Type::new_with_resolver(db, &resolver, Ty::builtin(self.inner)) + .expect("crate not present in resolver") + } + + pub fn name(self) -> Name { + self.inner.as_name() + } +} + #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] pub struct MacroDef { pub(crate) id: MacroDefId, diff --git a/crates/hir/src/from_id.rs b/crates/hir/src/from_id.rs index c8c5fecd7..b5814da11 100644 --- a/crates/hir/src/from_id.rs +++ b/crates/hir/src/from_id.rs @@ -11,8 +11,9 @@ use hir_def::{ }; use crate::{ - code_model::GenericParam, Adt, AssocItem, DefWithBody, Field, GenericDef, Label, Local, - MacroDef, ModuleDef, Variant, VariantDef, + code_model::{BuiltinType, GenericParam}, + Adt, AssocItem, DefWithBody, Field, GenericDef, Label, Local, MacroDef, ModuleDef, Variant, + VariantDef, }; macro_rules! from_id { @@ -111,7 +112,7 @@ impl From for ModuleDef { ModuleDefId::StaticId(it) => ModuleDef::Static(it.into()), ModuleDefId::TraitId(it) => ModuleDef::Trait(it.into()), ModuleDefId::TypeAliasId(it) => ModuleDef::TypeAlias(it.into()), - ModuleDefId::BuiltinType(it) => ModuleDef::BuiltinType(it), + ModuleDefId::BuiltinType(it) => ModuleDef::BuiltinType(it.into()), } } } @@ -127,7 +128,7 @@ impl From for ModuleDefId { ModuleDef::Static(it) => ModuleDefId::StaticId(it.into()), ModuleDef::Trait(it) => ModuleDefId::TraitId(it.into()), ModuleDef::TypeAlias(it) => ModuleDefId::TypeAliasId(it.into()), - ModuleDef::BuiltinType(it) => ModuleDefId::BuiltinType(it), + ModuleDef::BuiltinType(it) => ModuleDefId::BuiltinType(it.into()), } } } @@ -274,3 +275,15 @@ impl From for ItemInNs { } } } + +impl From for BuiltinType { + fn from(inner: hir_def::builtin_type::BuiltinType) -> Self { + Self { inner } + } +} + +impl From for hir_def::builtin_type::BuiltinType { + fn from(it: BuiltinType) -> Self { + it.inner + } +} diff --git a/crates/hir/src/semantics.rs b/crates/hir/src/semantics.rs index 0a30b4f5b..59292d5a2 100644 --- a/crates/hir/src/semantics.rs +++ b/crates/hir/src/semantics.rs @@ -49,7 +49,7 @@ impl PathResolution { match self { PathResolution::Def(ModuleDef::Adt(adt)) => Some(TypeNs::AdtId((*adt).into())), PathResolution::Def(ModuleDef::BuiltinType(builtin)) => { - Some(TypeNs::BuiltinType(*builtin)) + Some(TypeNs::BuiltinType((*builtin).into())) } PathResolution::Def(ModuleDef::Const(_)) | PathResolution::Def(ModuleDef::Variant(_)) diff --git a/crates/hir/src/source_analyzer.rs b/crates/hir/src/source_analyzer.rs index bed3fa50f..dc21f6051 100644 --- a/crates/hir/src/source_analyzer.rs +++ b/crates/hir/src/source_analyzer.rs @@ -28,8 +28,9 @@ use syntax::{ }; use crate::{ - db::HirDatabase, semantics::PathResolution, Adt, Const, Field, Function, Local, MacroDef, - ModuleDef, Static, Struct, Trait, Type, TypeAlias, TypeParam, Variant, + code_model::BuiltinType, db::HirDatabase, semantics::PathResolution, Adt, Const, Field, + Function, Local, MacroDef, ModuleDef, Static, Struct, Trait, Type, TypeAlias, TypeParam, + Variant, }; use base_db::CrateId; @@ -479,7 +480,7 @@ fn resolve_hir_path_( } TypeNs::EnumVariantId(it) => PathResolution::Def(Variant::from(it).into()), TypeNs::TypeAliasId(it) => PathResolution::Def(TypeAlias::from(it).into()), - TypeNs::BuiltinType(it) => PathResolution::Def(it.into()), + TypeNs::BuiltinType(it) => PathResolution::Def(BuiltinType::from(it).into()), TypeNs::TraitId(it) => PathResolution::Def(Trait::from(it).into()), }) }; @@ -555,7 +556,7 @@ fn resolve_hir_path_qualifier( TypeNs::AdtSelfType(it) | TypeNs::AdtId(it) => PathResolution::Def(Adt::from(it).into()), TypeNs::EnumVariantId(it) => PathResolution::Def(Variant::from(it).into()), TypeNs::TypeAliasId(it) => PathResolution::Def(TypeAlias::from(it).into()), - TypeNs::BuiltinType(it) => PathResolution::Def(it.into()), + TypeNs::BuiltinType(it) => PathResolution::Def(BuiltinType::from(it).into()), TypeNs::TraitId(it) => PathResolution::Def(Trait::from(it).into()), }) } -- cgit v1.2.3