aboutsummaryrefslogtreecommitdiff
path: root/crates
diff options
context:
space:
mode:
authorbors[bot] <26634292+bors[bot]@users.noreply.github.com>2021-02-12 13:18:30 +0000
committerGitHub <[email protected]>2021-02-12 13:18:30 +0000
commitdee5aba43a1b45131bf31268431fa71923f2ef2a (patch)
tree9a43e69bd5591d7e436fe236668d6769f5d500ea /crates
parenta044ac980cca07913e64a7c5fab53f7fe7cecb81 (diff)
parente938d769d922feb374967d5b695e3b3baa70a566 (diff)
Merge #7644
7644: Primitive completion r=jonas-schievink a=jonas-schievink Fixes https://github.com/rust-analyzer/rust-analyzer/issues/7642 Co-authored-by: Jonas Schievink <[email protected]>
Diffstat (limited to 'crates')
-rw-r--r--crates/completion/src/completions/qualified_path.rs34
-rw-r--r--crates/hir/src/code_model.rs20
-rw-r--r--crates/hir/src/from_id.rs21
-rw-r--r--crates/hir/src/semantics.rs2
-rw-r--r--crates/hir/src/source_analyzer.rs9
-rw-r--r--crates/hir_def/src/resolver.rs1
-rw-r--r--crates/hir_ty/src/lib.rs10
-rw-r--r--crates/hir_ty/src/lower.rs14
-rw-r--r--crates/ide/src/doc_links.rs5
-rw-r--r--crates/ide/src/hover.rs2
10 files changed, 89 insertions, 29 deletions
diff --git a/crates/completion/src/completions/qualified_path.rs b/crates/completion/src/completions/qualified_path.rs
index bbeaab496..2afa6979e 100644
--- a/crates/completion/src/completions/qualified_path.rs
+++ b/crates/completion/src/completions/qualified_path.rs
@@ -50,7 +50,8 @@ pub(crate) fn complete_qualified_path(acc: &mut Completions, ctx: &CompletionCon
50 } 50 }
51 } 51 }
52 PathResolution::Def(def @ hir::ModuleDef::Adt(_)) 52 PathResolution::Def(def @ hir::ModuleDef::Adt(_))
53 | PathResolution::Def(def @ hir::ModuleDef::TypeAlias(_)) => { 53 | PathResolution::Def(def @ hir::ModuleDef::TypeAlias(_))
54 | PathResolution::Def(def @ hir::ModuleDef::BuiltinType(_)) => {
54 if let hir::ModuleDef::Adt(Adt::Enum(e)) = def { 55 if let hir::ModuleDef::Adt(Adt::Enum(e)) = def {
55 for variant in e.variants(ctx.db) { 56 for variant in e.variants(ctx.db) {
56 acc.add_enum_variant(ctx, variant, None); 57 acc.add_enum_variant(ctx, variant, None);
@@ -59,6 +60,13 @@ pub(crate) fn complete_qualified_path(acc: &mut Completions, ctx: &CompletionCon
59 let ty = match def { 60 let ty = match def {
60 hir::ModuleDef::Adt(adt) => adt.ty(ctx.db), 61 hir::ModuleDef::Adt(adt) => adt.ty(ctx.db),
61 hir::ModuleDef::TypeAlias(a) => a.ty(ctx.db), 62 hir::ModuleDef::TypeAlias(a) => a.ty(ctx.db),
63 hir::ModuleDef::BuiltinType(builtin) => {
64 let module = match ctx.scope.module() {
65 Some(it) => it,
66 None => return,
67 };
68 builtin.ty(ctx.db, module)
69 }
62 _ => unreachable!(), 70 _ => unreachable!(),
63 }; 71 };
64 72
@@ -780,4 +788,28 @@ impl Foo {
780 "#]], 788 "#]],
781 ); 789 );
782 } 790 }
791
792 #[test]
793 fn completes_primitive_assoc_const() {
794 check(
795 r#"
796//- /lib.rs crate:lib deps:core
797fn f() {
798 u8::$0
799}
800
801//- /core.rs crate:core
802#[lang = "u8"]
803impl u8 {
804 pub const MAX: Self = 255;
805
806 pub fn func(self) {}
807}
808"#,
809 expect![[r#"
810 ct MAX pub const MAX: Self = 255;
811 me func(…) -> ()
812 "#]],
813 );
814 }
783} 815}
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};
6use either::Either; 6use either::Either;
7use hir_def::{ 7use hir_def::{
8 adt::{ReprKind, StructKind, VariantData}, 8 adt::{ReprKind, StructKind, VariantData},
9 builtin_type::BuiltinType,
10 expr::{BindingAnnotation, LabelId, Pat, PatId}, 9 expr::{BindingAnnotation, LabelId, Pat, PatId},
11 import_map, 10 import_map,
12 item_tree::ItemTreeNode, 11 item_tree::ItemTreeNode,
@@ -245,7 +244,7 @@ impl ModuleDef {
245 ModuleDef::Const(it) => it.name(db), 244 ModuleDef::Const(it) => it.name(db),
246 ModuleDef::Static(it) => it.name(db), 245 ModuleDef::Static(it) => it.name(db),
247 246
248 ModuleDef::BuiltinType(it) => Some(it.as_name()), 247 ModuleDef::BuiltinType(it) => Some(it.name()),
249 } 248 }
250 } 249 }
251 250
@@ -995,6 +994,23 @@ impl HasVisibility for TypeAlias {
995} 994}
996 995
997#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] 996#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
997pub struct BuiltinType {
998 pub(crate) inner: hir_def::builtin_type::BuiltinType,
999}
1000
1001impl BuiltinType {
1002 pub fn ty(self, db: &dyn HirDatabase, module: Module) -> Type {
1003 let resolver = module.id.resolver(db.upcast());
1004 Type::new_with_resolver(db, &resolver, Ty::builtin(self.inner))
1005 .expect("crate not present in resolver")
1006 }
1007
1008 pub fn name(self) -> Name {
1009 self.inner.as_name()
1010 }
1011}
1012
1013#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
998pub struct MacroDef { 1014pub struct MacroDef {
999 pub(crate) id: MacroDefId, 1015 pub(crate) id: MacroDefId,
1000} 1016}
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::{
11}; 11};
12 12
13use crate::{ 13use crate::{
14 code_model::GenericParam, Adt, AssocItem, DefWithBody, Field, GenericDef, Label, Local, 14 code_model::{BuiltinType, GenericParam},
15 MacroDef, ModuleDef, Variant, VariantDef, 15 Adt, AssocItem, DefWithBody, Field, GenericDef, Label, Local, MacroDef, ModuleDef, Variant,
16 VariantDef,
16}; 17};
17 18
18macro_rules! from_id { 19macro_rules! from_id {
@@ -111,7 +112,7 @@ impl From<ModuleDefId> for ModuleDef {
111 ModuleDefId::StaticId(it) => ModuleDef::Static(it.into()), 112 ModuleDefId::StaticId(it) => ModuleDef::Static(it.into()),
112 ModuleDefId::TraitId(it) => ModuleDef::Trait(it.into()), 113 ModuleDefId::TraitId(it) => ModuleDef::Trait(it.into()),
113 ModuleDefId::TypeAliasId(it) => ModuleDef::TypeAlias(it.into()), 114 ModuleDefId::TypeAliasId(it) => ModuleDef::TypeAlias(it.into()),
114 ModuleDefId::BuiltinType(it) => ModuleDef::BuiltinType(it), 115 ModuleDefId::BuiltinType(it) => ModuleDef::BuiltinType(it.into()),
115 } 116 }
116 } 117 }
117} 118}
@@ -127,7 +128,7 @@ impl From<ModuleDef> for ModuleDefId {
127 ModuleDef::Static(it) => ModuleDefId::StaticId(it.into()), 128 ModuleDef::Static(it) => ModuleDefId::StaticId(it.into()),
128 ModuleDef::Trait(it) => ModuleDefId::TraitId(it.into()), 129 ModuleDef::Trait(it) => ModuleDefId::TraitId(it.into()),
129 ModuleDef::TypeAlias(it) => ModuleDefId::TypeAliasId(it.into()), 130 ModuleDef::TypeAlias(it) => ModuleDefId::TypeAliasId(it.into()),
130 ModuleDef::BuiltinType(it) => ModuleDefId::BuiltinType(it), 131 ModuleDef::BuiltinType(it) => ModuleDefId::BuiltinType(it.into()),
131 } 132 }
132 } 133 }
133} 134}
@@ -274,3 +275,15 @@ impl From<ModuleDef> for ItemInNs {
274 } 275 }
275 } 276 }
276} 277}
278
279impl From<hir_def::builtin_type::BuiltinType> for BuiltinType {
280 fn from(inner: hir_def::builtin_type::BuiltinType) -> Self {
281 Self { inner }
282 }
283}
284
285impl From<BuiltinType> for hir_def::builtin_type::BuiltinType {
286 fn from(it: BuiltinType) -> Self {
287 it.inner
288 }
289}
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 {
49 match self { 49 match self {
50 PathResolution::Def(ModuleDef::Adt(adt)) => Some(TypeNs::AdtId((*adt).into())), 50 PathResolution::Def(ModuleDef::Adt(adt)) => Some(TypeNs::AdtId((*adt).into())),
51 PathResolution::Def(ModuleDef::BuiltinType(builtin)) => { 51 PathResolution::Def(ModuleDef::BuiltinType(builtin)) => {
52 Some(TypeNs::BuiltinType(*builtin)) 52 Some(TypeNs::BuiltinType((*builtin).into()))
53 } 53 }
54 PathResolution::Def(ModuleDef::Const(_)) 54 PathResolution::Def(ModuleDef::Const(_))
55 | PathResolution::Def(ModuleDef::Variant(_)) 55 | 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::{
28}; 28};
29 29
30use crate::{ 30use crate::{
31 db::HirDatabase, semantics::PathResolution, Adt, Const, Field, Function, Local, MacroDef, 31 code_model::BuiltinType, db::HirDatabase, semantics::PathResolution, Adt, Const, Field,
32 ModuleDef, Static, Struct, Trait, Type, TypeAlias, TypeParam, Variant, 32 Function, Local, MacroDef, ModuleDef, Static, Struct, Trait, Type, TypeAlias, TypeParam,
33 Variant,
33}; 34};
34use base_db::CrateId; 35use base_db::CrateId;
35 36
@@ -479,7 +480,7 @@ fn resolve_hir_path_(
479 } 480 }
480 TypeNs::EnumVariantId(it) => PathResolution::Def(Variant::from(it).into()), 481 TypeNs::EnumVariantId(it) => PathResolution::Def(Variant::from(it).into()),
481 TypeNs::TypeAliasId(it) => PathResolution::Def(TypeAlias::from(it).into()), 482 TypeNs::TypeAliasId(it) => PathResolution::Def(TypeAlias::from(it).into()),
482 TypeNs::BuiltinType(it) => PathResolution::Def(it.into()), 483 TypeNs::BuiltinType(it) => PathResolution::Def(BuiltinType::from(it).into()),
483 TypeNs::TraitId(it) => PathResolution::Def(Trait::from(it).into()), 484 TypeNs::TraitId(it) => PathResolution::Def(Trait::from(it).into()),
484 }) 485 })
485 }; 486 };
@@ -555,7 +556,7 @@ fn resolve_hir_path_qualifier(
555 TypeNs::AdtSelfType(it) | TypeNs::AdtId(it) => PathResolution::Def(Adt::from(it).into()), 556 TypeNs::AdtSelfType(it) | TypeNs::AdtId(it) => PathResolution::Def(Adt::from(it).into()),
556 TypeNs::EnumVariantId(it) => PathResolution::Def(Variant::from(it).into()), 557 TypeNs::EnumVariantId(it) => PathResolution::Def(Variant::from(it).into()),
557 TypeNs::TypeAliasId(it) => PathResolution::Def(TypeAlias::from(it).into()), 558 TypeNs::TypeAliasId(it) => PathResolution::Def(TypeAlias::from(it).into()),
558 TypeNs::BuiltinType(it) => PathResolution::Def(it.into()), 559 TypeNs::BuiltinType(it) => PathResolution::Def(BuiltinType::from(it).into()),
559 TypeNs::TraitId(it) => PathResolution::Def(Trait::from(it).into()), 560 TypeNs::TraitId(it) => PathResolution::Def(Trait::from(it).into()),
560 }) 561 })
561} 562}
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 {
361 } 361 }
362 362
363 pub fn krate(&self) -> Option<CrateId> { 363 pub fn krate(&self) -> Option<CrateId> {
364 // FIXME: can this ever be `None`?
364 self.module_scope().map(|t| t.0.krate()) 365 self.module_scope().map(|t| t.0.krate())
365 } 366 }
366 367
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};
27 27
28use base_db::{salsa, CrateId}; 28use base_db::{salsa, CrateId};
29use hir_def::{ 29use hir_def::{
30 builtin_type::BuiltinType,
30 expr::ExprId, 31 expr::ExprId,
31 type_ref::{Mutability, Rawness}, 32 type_ref::{Mutability, Rawness},
32 AdtId, AssocContainerId, DefWithBodyId, FunctionId, GenericDefId, HasModule, LifetimeParamId, 33 AdtId, AssocContainerId, DefWithBodyId, FunctionId, GenericDefId, HasModule, LifetimeParamId,
@@ -738,6 +739,15 @@ impl Ty {
738 Substs(sig.params_and_return), 739 Substs(sig.params_and_return),
739 ) 740 )
740 } 741 }
742 pub fn builtin(builtin: BuiltinType) -> Self {
743 Ty::simple(match builtin {
744 BuiltinType::Char => TypeCtor::Char,
745 BuiltinType::Bool => TypeCtor::Bool,
746 BuiltinType::Str => TypeCtor::Str,
747 BuiltinType::Int(t) => TypeCtor::Int(IntTy::from(t).into()),
748 BuiltinType::Float(t) => TypeCtor::Float(FloatTy::from(t).into()),
749 })
750 }
741 751
742 pub fn as_reference(&self) -> Option<(&Ty, Mutability)> { 752 pub fn as_reference(&self) -> Option<(&Ty, Mutability)> {
743 match self { 753 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;
27 27
28use crate::{ 28use crate::{
29 db::HirDatabase, 29 db::HirDatabase,
30 primitive::{FloatTy, IntTy},
31 utils::{ 30 utils::{
32 all_super_trait_refs, associated_type_by_name_including_super_traits, generics, 31 all_super_trait_refs, associated_type_by_name_including_super_traits, generics,
33 make_mut_slice, variant_data, 32 make_mut_slice, variant_data,
@@ -1051,17 +1050,6 @@ fn type_for_static(db: &dyn HirDatabase, def: StaticId) -> Binders<Ty> {
1051 Binders::new(0, Ty::from_hir(&ctx, &data.type_ref)) 1050 Binders::new(0, Ty::from_hir(&ctx, &data.type_ref))
1052} 1051}
1053 1052
1054/// Build the declared type of a static.
1055fn type_for_builtin(def: BuiltinType) -> Ty {
1056 Ty::simple(match def {
1057 BuiltinType::Char => TypeCtor::Char,
1058 BuiltinType::Bool => TypeCtor::Bool,
1059 BuiltinType::Str => TypeCtor::Str,
1060 BuiltinType::Int(t) => TypeCtor::Int(IntTy::from(t).into()),
1061 BuiltinType::Float(t) => TypeCtor::Float(FloatTy::from(t).into()),
1062 })
1063}
1064
1065fn fn_sig_for_struct_constructor(db: &dyn HirDatabase, def: StructId) -> PolyFnSig { 1053fn fn_sig_for_struct_constructor(db: &dyn HirDatabase, def: StructId) -> PolyFnSig {
1066 let struct_data = db.struct_data(def); 1054 let struct_data = db.struct_data(def);
1067 let fields = struct_data.variant_data.fields(); 1055 let fields = struct_data.variant_data.fields();
@@ -1186,7 +1174,7 @@ impl_from!(FunctionId, StructId, UnionId, EnumVariantId, ConstId, StaticId for V
1186/// namespace. 1174/// namespace.
1187pub(crate) fn ty_query(db: &dyn HirDatabase, def: TyDefId) -> Binders<Ty> { 1175pub(crate) fn ty_query(db: &dyn HirDatabase, def: TyDefId) -> Binders<Ty> {
1188 match def { 1176 match def {
1189 TyDefId::BuiltinType(it) => Binders::new(0, type_for_builtin(it)), 1177 TyDefId::BuiltinType(it) => Binders::new(0, Ty::builtin(it)),
1190 TyDefId::AdtId(it) => type_for_adt(db, it), 1178 TyDefId::AdtId(it) => type_for_adt(db, it),
1191 TyDefId::TypeAliasId(it) => type_for_type_alias(db, it), 1179 TyDefId::TypeAliasId(it) => type_for_type_alias(db, it),
1192 } 1180 }
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;
9 9
10use hir::{ 10use hir::{
11 db::{DefDatabase, HirDatabase}, 11 db::{DefDatabase, HirDatabase},
12 Adt, AsAssocItem, AsName, AssocItem, AssocItemContainer, Crate, Field, HasAttrs, ItemInNs, 12 Adt, AsAssocItem, AssocItem, AssocItemContainer, Crate, Field, HasAttrs, ItemInNs, ModuleDef,
13 ModuleDef,
14}; 13};
15use ide_db::{ 14use ide_db::{
16 defs::{Definition, NameClass, NameRefClass}, 15 defs::{Definition, NameClass, NameRefClass},
@@ -429,7 +428,7 @@ fn get_symbol_filename(db: &dyn HirDatabase, definition: &ModuleDef) -> Option<S
429 ModuleDef::Module(_) => "index.html".to_string(), 428 ModuleDef::Module(_) => "index.html".to_string(),
430 ModuleDef::Trait(t) => format!("trait.{}.html", t.name(db)), 429 ModuleDef::Trait(t) => format!("trait.{}.html", t.name(db)),
431 ModuleDef::TypeAlias(t) => format!("type.{}.html", t.name(db)), 430 ModuleDef::TypeAlias(t) => format!("type.{}.html", t.name(db)),
432 ModuleDef::BuiltinType(t) => format!("primitive.{}.html", t.as_name()), 431 ModuleDef::BuiltinType(t) => format!("primitive.{}.html", t.name()),
433 ModuleDef::Function(f) => format!("fn.{}.html", f.name(db)), 432 ModuleDef::Function(f) => format!("fn.{}.html", f.name(db)),
434 ModuleDef::Variant(ev) => { 433 ModuleDef::Variant(ev) => {
435 format!("enum.{}.html#variant.{}", ev.parent_enum(db).name(db), ev.name(db)) 434 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<Markup> {
334 ModuleDef::Static(it) => from_def_source(db, it, mod_path), 334 ModuleDef::Static(it) => from_def_source(db, it, mod_path),
335 ModuleDef::Trait(it) => from_def_source(db, it, mod_path), 335 ModuleDef::Trait(it) => from_def_source(db, it, mod_path),
336 ModuleDef::TypeAlias(it) => from_def_source(db, it, mod_path), 336 ModuleDef::TypeAlias(it) => from_def_source(db, it, mod_path),
337 ModuleDef::BuiltinType(it) => Some(Markup::fenced_block(&it)), 337 ModuleDef::BuiltinType(it) => Some(Markup::fenced_block(&it.name())),
338 }, 338 },
339 Definition::Local(it) => Some(Markup::fenced_block(&it.ty(db).display(db))), 339 Definition::Local(it) => Some(Markup::fenced_block(&it.ty(db).display(db))),
340 Definition::SelfType(impl_def) => { 340 Definition::SelfType(impl_def) => {