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 +++++---- crates/hir_def/src/resolver.rs | 1 + crates/hir_ty/src/lib.rs | 10 ++++++++++ crates/hir_ty/src/lower.rs | 14 +------------- crates/ide/src/doc_links.rs | 5 ++--- crates/ide/src/hover.rs | 2 +- 9 files changed, 56 insertions(+), 28 deletions(-) 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()), }) } diff --git a/crates/hir_def/src/resolver.rs b/crates/hir_def/src/resolver.rs index a8467c88e..e85f85e49 100644 --- a/crates/hir_def/src/resolver.rs +++ b/crates/hir_def/src/resolver.rs @@ -361,6 +361,7 @@ impl Resolver { } pub fn krate(&self) -> Option { + // FIXME: can this ever be `None`? self.module_scope().map(|t| t.0.krate()) } diff --git a/crates/hir_ty/src/lib.rs b/crates/hir_ty/src/lib.rs index 6bec389f8..50d248674 100644 --- a/crates/hir_ty/src/lib.rs +++ b/crates/hir_ty/src/lib.rs @@ -27,6 +27,7 @@ use std::{iter, mem, ops::Deref, sync::Arc}; use base_db::{salsa, CrateId}; use hir_def::{ + builtin_type::BuiltinType, expr::ExprId, type_ref::{Mutability, Rawness}, AdtId, AssocContainerId, DefWithBodyId, FunctionId, GenericDefId, HasModule, LifetimeParamId, @@ -738,6 +739,15 @@ impl Ty { Substs(sig.params_and_return), ) } + pub fn builtin(builtin: BuiltinType) -> Self { + Ty::simple(match builtin { + BuiltinType::Char => TypeCtor::Char, + BuiltinType::Bool => TypeCtor::Bool, + BuiltinType::Str => TypeCtor::Str, + BuiltinType::Int(t) => TypeCtor::Int(IntTy::from(t).into()), + BuiltinType::Float(t) => TypeCtor::Float(FloatTy::from(t).into()), + }) + } pub fn as_reference(&self) -> Option<(&Ty, Mutability)> { match self { diff --git a/crates/hir_ty/src/lower.rs b/crates/hir_ty/src/lower.rs index dfb573ff3..f9dc832bd 100644 --- a/crates/hir_ty/src/lower.rs +++ b/crates/hir_ty/src/lower.rs @@ -27,7 +27,6 @@ use test_utils::mark; use crate::{ db::HirDatabase, - primitive::{FloatTy, IntTy}, utils::{ all_super_trait_refs, associated_type_by_name_including_super_traits, generics, make_mut_slice, variant_data, @@ -1051,17 +1050,6 @@ fn type_for_static(db: &dyn HirDatabase, def: StaticId) -> Binders { Binders::new(0, Ty::from_hir(&ctx, &data.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(t) => TypeCtor::Int(IntTy::from(t).into()), - BuiltinType::Float(t) => TypeCtor::Float(FloatTy::from(t).into()), - }) -} - fn fn_sig_for_struct_constructor(db: &dyn HirDatabase, def: StructId) -> PolyFnSig { let struct_data = db.struct_data(def); let fields = struct_data.variant_data.fields(); @@ -1186,7 +1174,7 @@ impl_from!(FunctionId, StructId, UnionId, EnumVariantId, ConstId, StaticId for V /// namespace. pub(crate) fn ty_query(db: &dyn HirDatabase, def: TyDefId) -> Binders { match def { - TyDefId::BuiltinType(it) => Binders::new(0, type_for_builtin(it)), + TyDefId::BuiltinType(it) => Binders::new(0, Ty::builtin(it)), TyDefId::AdtId(it) => type_for_adt(db, it), TyDefId::TypeAliasId(it) => type_for_type_alias(db, it), } diff --git a/crates/ide/src/doc_links.rs b/crates/ide/src/doc_links.rs index 730e0dd0a..f94adec9b 100644 --- a/crates/ide/src/doc_links.rs +++ b/crates/ide/src/doc_links.rs @@ -9,8 +9,7 @@ use url::Url; use hir::{ db::{DefDatabase, HirDatabase}, - Adt, AsAssocItem, AsName, AssocItem, AssocItemContainer, Crate, Field, HasAttrs, ItemInNs, - ModuleDef, + Adt, AsAssocItem, AssocItem, AssocItemContainer, Crate, Field, HasAttrs, ItemInNs, ModuleDef, }; use ide_db::{ defs::{Definition, NameClass, NameRefClass}, @@ -429,7 +428,7 @@ fn get_symbol_filename(db: &dyn HirDatabase, definition: &ModuleDef) -> Option "index.html".to_string(), ModuleDef::Trait(t) => format!("trait.{}.html", t.name(db)), ModuleDef::TypeAlias(t) => format!("type.{}.html", t.name(db)), - ModuleDef::BuiltinType(t) => format!("primitive.{}.html", t.as_name()), + ModuleDef::BuiltinType(t) => format!("primitive.{}.html", t.name()), ModuleDef::Function(f) => format!("fn.{}.html", f.name(db)), ModuleDef::Variant(ev) => { format!("enum.{}.html#variant.{}", ev.parent_enum(db).name(db), ev.name(db)) diff --git a/crates/ide/src/hover.rs b/crates/ide/src/hover.rs index 5d2d072b1..00745238a 100644 --- a/crates/ide/src/hover.rs +++ b/crates/ide/src/hover.rs @@ -334,7 +334,7 @@ fn hover_for_definition(db: &RootDatabase, def: Definition) -> Option { ModuleDef::Static(it) => from_def_source(db, it, mod_path), ModuleDef::Trait(it) => from_def_source(db, it, mod_path), ModuleDef::TypeAlias(it) => from_def_source(db, it, mod_path), - ModuleDef::BuiltinType(it) => Some(Markup::fenced_block(&it)), + ModuleDef::BuiltinType(it) => Some(Markup::fenced_block(&it.name())), }, Definition::Local(it) => Some(Markup::fenced_block(&it.ty(db).display(db))), Definition::SelfType(impl_def) => { -- cgit v1.2.3