7644: Primitive completion r=jonas-schievink a=jonas-schievink

Fixes https://github.com/rust-analyzer/rust-analyzer/issues/7642

Co-authored-by: Jonas Schievink <jonasschievink@gmail.com>
This commit is contained in:
bors[bot] 2021-02-12 13:18:30 +00:00 committed by GitHub
commit dee5aba43a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 89 additions and 29 deletions

View file

@ -50,7 +50,8 @@ pub(crate) fn complete_qualified_path(acc: &mut Completions, ctx: &CompletionCon
}
}
PathResolution::Def(def @ hir::ModuleDef::Adt(_))
| PathResolution::Def(def @ hir::ModuleDef::TypeAlias(_)) => {
| PathResolution::Def(def @ hir::ModuleDef::TypeAlias(_))
| PathResolution::Def(def @ hir::ModuleDef::BuiltinType(_)) => {
if let hir::ModuleDef::Adt(Adt::Enum(e)) = def {
for variant in e.variants(ctx.db) {
acc.add_enum_variant(ctx, variant, None);
@ -59,6 +60,13 @@ pub(crate) fn complete_qualified_path(acc: &mut Completions, ctx: &CompletionCon
let ty = match def {
hir::ModuleDef::Adt(adt) => adt.ty(ctx.db),
hir::ModuleDef::TypeAlias(a) => a.ty(ctx.db),
hir::ModuleDef::BuiltinType(builtin) => {
let module = match ctx.scope.module() {
Some(it) => it,
None => return,
};
builtin.ty(ctx.db, module)
}
_ => unreachable!(),
};
@ -780,4 +788,28 @@ impl Foo {
"#]],
);
}
#[test]
fn completes_primitive_assoc_const() {
check(
r#"
//- /lib.rs crate:lib deps:core
fn f() {
u8::$0
}
//- /core.rs crate:core
#[lang = "u8"]
impl u8 {
pub const MAX: Self = 255;
pub fn func(self) {}
}
"#,
expect![[r#"
ct MAX pub const MAX: Self = 255;
me func() -> ()
"#]],
);
}
}

View file

@ -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,

View file

@ -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<ModuleDefId> 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<ModuleDef> 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<ModuleDef> for ItemInNs {
}
}
}
impl From<hir_def::builtin_type::BuiltinType> for BuiltinType {
fn from(inner: hir_def::builtin_type::BuiltinType) -> Self {
Self { inner }
}
}
impl From<BuiltinType> for hir_def::builtin_type::BuiltinType {
fn from(it: BuiltinType) -> Self {
it.inner
}
}

View file

@ -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(_))

View file

@ -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()),
})
}

View file

@ -361,6 +361,7 @@ impl Resolver {
}
pub fn krate(&self) -> Option<CrateId> {
// FIXME: can this ever be `None`?
self.module_scope().map(|t| t.0.krate())
}

View file

@ -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 {

View file

@ -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<Ty> {
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<Ty> {
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),
}

View file

@ -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<S
ModuleDef::Module(_) => "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))

View file

@ -334,7 +334,7 @@ fn hover_for_definition(db: &RootDatabase, def: Definition) -> Option<Markup> {
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) => {