Skip to content

Commit 141fca6

Browse files
bors[bot]matklad
andauthored
Merge #2419
2419: Remove ns-polymorphic type_for_def r=matklad a=matklad Co-authored-by: Aleksey Kladov <aleksey.kladov@gmail.com>
2 parents 3b0fc8a + 4e415a2 commit 141fca6

File tree

12 files changed

+207
-241
lines changed

12 files changed

+207
-241
lines changed

crates/ra_hir/src/code_model.rs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,7 @@ use crate::{
2828
expr::{BindingAnnotation, Body, BodySourceMap, ExprValidator, Pat, PatId},
2929
ty::display::HirFormatter,
3030
ty::{
31-
self, InEnvironment, InferenceResult, Namespace, TraitEnvironment, TraitRef, Ty, TypeCtor,
32-
TypeWalk,
31+
self, InEnvironment, InferenceResult, TraitEnvironment, TraitRef, Ty, TypeCtor, TypeWalk,
3332
},
3433
CallableDef, Either, HirDisplay, Name, Source,
3534
};
@@ -354,11 +353,11 @@ impl Struct {
354353
}
355354

356355
pub fn ty(self, db: &impl HirDatabase) -> Ty {
357-
db.type_for_def(self.into(), Namespace::Types)
356+
db.ty(self.id.into())
358357
}
359358

360359
pub fn constructor_ty(self, db: &impl HirDatabase) -> Ty {
361-
db.type_for_def(self.into(), Namespace::Values)
360+
db.value_ty(self.id.into())
362361
}
363362

364363
fn variant_data(self, db: &impl DefDatabase) -> Arc<VariantData> {
@@ -381,7 +380,7 @@ impl Union {
381380
}
382381

383382
pub fn ty(self, db: &impl HirDatabase) -> Ty {
384-
db.type_for_def(self.into(), Namespace::Types)
383+
db.ty(self.id.into())
385384
}
386385

387386
pub fn fields(self, db: &impl HirDatabase) -> Vec<StructField> {
@@ -442,7 +441,7 @@ impl Enum {
442441
}
443442

444443
pub fn ty(self, db: &impl HirDatabase) -> Ty {
445-
db.type_for_def(self.into(), Namespace::Types)
444+
db.ty(self.id.into())
446445
}
447446
}
448447

@@ -617,7 +616,7 @@ impl Function {
617616
}
618617

619618
pub fn ty(self, db: &impl HirDatabase) -> Ty {
620-
db.type_for_def(self.into(), Namespace::Values)
619+
db.value_ty(self.id.into())
621620
}
622621

623622
pub fn infer(self, db: &impl HirDatabase) -> Arc<InferenceResult> {
@@ -741,7 +740,7 @@ impl Trait {
741740
}
742741

743742
pub fn trait_ref(self, db: &impl HirDatabase) -> TraitRef {
744-
TraitRef::for_trait(db, self)
743+
TraitRef::for_trait(db, self.id)
745744
}
746745

747746
pub fn is_auto(self, db: &impl DefDatabase) -> bool {
@@ -797,7 +796,7 @@ impl TypeAlias {
797796
}
798797

799798
pub fn ty(self, db: &impl HirDatabase) -> Ty {
800-
db.type_for_def(self.into(), Namespace::Types)
799+
db.ty(self.id.into())
801800
}
802801

803802
pub fn name(self, db: &impl DefDatabase) -> Name {

crates/ra_hir/src/db.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ use crate::{
99
ty::{
1010
method_resolution::CrateImplBlocks,
1111
traits::{AssocTyValue, Impl},
12-
CallableDef, FnSig, GenericPredicate, InferenceResult, Namespace, Substs, Ty, TypableDef,
13-
TypeCtor,
12+
CallableDef, FnSig, GenericPredicate, InferenceResult, Substs, Ty, TyDefId, TypeCtor,
13+
ValueTyDefId,
1414
},
1515
Crate, DefWithBody, ImplBlock, Trait,
1616
};
@@ -37,8 +37,11 @@ pub trait HirDatabase: DefDatabase {
3737
#[salsa::invoke(crate::ty::infer_query)]
3838
fn infer(&self, def: DefWithBody) -> Arc<InferenceResult>;
3939

40-
#[salsa::invoke(crate::ty::type_for_def)]
41-
fn type_for_def(&self, def: TypableDef, ns: Namespace) -> Ty;
40+
#[salsa::invoke(crate::ty::ty_query)]
41+
fn ty(&self, def: TyDefId) -> Ty;
42+
43+
#[salsa::invoke(crate::ty::value_ty_query)]
44+
fn value_ty(&self, def: ValueTyDefId) -> Ty;
4245

4346
#[salsa::invoke(crate::ty::field_types_query)]
4447
fn field_types(&self, var: VariantId) -> Arc<ArenaMap<LocalStructFieldId, Ty>>;

crates/ra_hir/src/ty.rs

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,16 @@ use std::sync::Arc;
1919
use std::{fmt, iter, mem};
2020

2121
use hir_def::{
22-
generics::GenericParams, AdtId, ContainerId, DefWithBodyId, GenericDefId, HasModule, Lookup,
23-
TraitId, TypeAliasId,
22+
expr::ExprId, generics::GenericParams, type_ref::Mutability, AdtId, ContainerId, DefWithBodyId,
23+
GenericDefId, HasModule, Lookup, TraitId, TypeAliasId,
2424
};
2525
use ra_db::{impl_intern_key, salsa};
2626

2727
use crate::{
28-
db::HirDatabase, expr::ExprId, util::make_mut_slice, Adt, Crate, FloatTy, IntTy, Mutability,
29-
Name, Trait, Uncertain,
28+
db::HirDatabase,
29+
ty::primitive::{FloatTy, IntTy, Uncertain},
30+
util::make_mut_slice,
31+
Adt, Crate, Name,
3032
};
3133
use display::{HirDisplay, HirFormatter};
3234

@@ -35,8 +37,8 @@ pub(crate) use infer::{infer_query, InferTy, InferenceResult};
3537
pub use lower::CallableDef;
3638
pub(crate) use lower::{
3739
callable_item_sig, field_types_query, generic_defaults_query,
38-
generic_predicates_for_param_query, generic_predicates_query, type_for_def, Namespace,
39-
TypableDef,
40+
generic_predicates_for_param_query, generic_predicates_query, ty_query, value_ty_query,
41+
TyDefId, TypableDef, ValueTyDefId,
4042
};
4143
pub(crate) use traits::{InEnvironment, Obligation, ProjectionPredicate, TraitEnvironment};
4244

@@ -445,7 +447,7 @@ impl Deref for Substs {
445447
#[derive(Clone, PartialEq, Eq, Debug, Hash)]
446448
pub struct TraitRef {
447449
/// FIXME name?
448-
pub trait_: Trait,
450+
pub trait_: TraitId,
449451
pub substs: Substs,
450452
}
451453

@@ -676,7 +678,7 @@ impl Ty {
676678
}
677679

678680
/// If this is an `impl Trait` or `dyn Trait`, returns that trait.
679-
pub fn inherent_trait(&self) -> Option<Trait> {
681+
pub fn inherent_trait(&self) -> Option<TraitId> {
680682
match self {
681683
Ty::Dyn(predicates) | Ty::Opaque(predicates) => {
682684
predicates.iter().find_map(|pred| match pred {
@@ -988,7 +990,10 @@ impl HirDisplay for Ty {
988990
write!(
989991
f,
990992
"{}",
991-
trait_ref.trait_.name(f.db).unwrap_or_else(Name::missing)
993+
f.db.trait_data(trait_ref.trait_)
994+
.name
995+
.clone()
996+
.unwrap_or_else(Name::missing)
992997
)?;
993998
if trait_ref.substs.len() > 1 {
994999
write!(f, "<")?;
@@ -1049,7 +1054,7 @@ impl TraitRef {
10491054
} else {
10501055
write!(f, ": ")?;
10511056
}
1052-
write!(f, "{}", self.trait_.name(f.db).unwrap_or_else(Name::missing))?;
1057+
write!(f, "{}", f.db.trait_data(self.trait_).name.clone().unwrap_or_else(Name::missing))?;
10531058
if self.substs.len() > 1 {
10541059
write!(f, "<")?;
10551060
f.write_joined(&self.substs[1..], ", ")?;

crates/ra_hir/src/ty/infer.rs

Lines changed: 15 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,15 @@ use test_utils::tested_by;
3535

3636
use super::{
3737
traits::{Guidance, Obligation, ProjectionPredicate, Solution},
38-
ApplicationTy, InEnvironment, ProjectionTy, Substs, TraitEnvironment, TraitRef, Ty, TypableDef,
39-
TypeCtor, TypeWalk, Uncertain,
38+
ApplicationTy, InEnvironment, ProjectionTy, Substs, TraitEnvironment, TraitRef, Ty, TypeCtor,
39+
TypeWalk, Uncertain,
4040
};
4141
use crate::{
4242
code_model::TypeAlias,
4343
db::HirDatabase,
4444
expr::{BindingAnnotation, Body, ExprId, PatId},
4545
ty::infer::diagnostics::InferenceDiagnostic,
46-
Adt, AssocItem, DefWithBody, FloatTy, Function, IntTy, Path, StructField, VariantDef,
46+
AssocItem, DefWithBody, FloatTy, Function, IntTy, Path, StructField, VariantDef,
4747
};
4848

4949
macro_rules! ty_app {
@@ -520,45 +520,22 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
520520
None => return (Ty::Unknown, None),
521521
};
522522
let resolver = &self.resolver;
523-
let def: TypableDef =
524-
// FIXME: this should resolve assoc items as well, see this example:
525-
// https://play.rust-lang.org/?gist=087992e9e22495446c01c0d4e2d69521
526-
match resolver.resolve_path_in_type_ns_fully(self.db, &path) {
527-
Some(TypeNs::AdtId(AdtId::StructId(it))) => it.into(),
528-
Some(TypeNs::AdtId(AdtId::UnionId(it))) => it.into(),
529-
Some(TypeNs::AdtSelfType(adt)) => adt.into(),
530-
Some(TypeNs::EnumVariantId(it)) => it.into(),
531-
Some(TypeNs::TypeAliasId(it)) => it.into(),
532-
533-
Some(TypeNs::SelfType(_)) |
534-
Some(TypeNs::GenericParam(_)) |
535-
Some(TypeNs::BuiltinType(_)) |
536-
Some(TypeNs::TraitId(_)) |
537-
Some(TypeNs::AdtId(AdtId::EnumId(_))) |
538-
None => {
539-
return (Ty::Unknown, None)
540-
}
541-
};
542-
// FIXME remove the duplication between here and `Ty::from_path`?
543-
let substs = Ty::substs_from_path(self.db, resolver, path, def);
544-
match def {
545-
TypableDef::Adt(Adt::Struct(s)) => {
546-
let ty = s.ty(self.db);
523+
// FIXME: this should resolve assoc items as well, see this example:
524+
// https://play.rust-lang.org/?gist=087992e9e22495446c01c0d4e2d69521
525+
match resolver.resolve_path_in_type_ns_fully(self.db, &path) {
526+
Some(TypeNs::AdtId(AdtId::StructId(strukt))) => {
527+
let substs = Ty::substs_from_path(self.db, resolver, path, strukt.into());
528+
let ty = self.db.ty(strukt.into());
547529
let ty = self.insert_type_vars(ty.apply_substs(substs));
548-
(ty, Some(s.into()))
530+
(ty, Some(VariantDef::Struct(strukt.into())))
549531
}
550-
TypableDef::EnumVariant(var) => {
551-
let ty = var.parent_enum(self.db).ty(self.db);
532+
Some(TypeNs::EnumVariantId(var)) => {
533+
let substs = Ty::substs_from_path(self.db, resolver, path, var.into());
534+
let ty = self.db.ty(var.parent.into());
552535
let ty = self.insert_type_vars(ty.apply_substs(substs));
553-
(ty, Some(var.into()))
536+
(ty, Some(VariantDef::EnumVariant(var.into())))
554537
}
555-
TypableDef::Adt(Adt::Enum(_))
556-
| TypableDef::Adt(Adt::Union(_))
557-
| TypableDef::TypeAlias(_)
558-
| TypableDef::Function(_)
559-
| TypableDef::Const(_)
560-
| TypableDef::Static(_)
561-
| TypableDef::BuiltinType(_) => (Ty::Unknown, None),
538+
Some(_) | None => (Ty::Unknown, None),
562539
}
563540
}
564541

crates/ra_hir/src/ty/infer/expr.rs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ use crate::{
1717
expr::{Array, BinaryOp, Expr, ExprId, Literal, Statement, UnaryOp},
1818
ty::{
1919
autoderef, method_resolution, op, traits::InEnvironment, CallableDef, InferTy, IntTy,
20-
Mutability, Namespace, Obligation, ProjectionPredicate, ProjectionTy, Substs, TraitRef, Ty,
21-
TypeCtor, TypeWalk, Uncertain,
20+
Mutability, Obligation, ProjectionPredicate, ProjectionTy, Substs, TraitRef, Ty, TypeCtor,
21+
TypeWalk, Uncertain,
2222
},
2323
Name,
2424
};
@@ -558,11 +558,7 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
558558
Some((ty, func)) => {
559559
let ty = canonicalized_receiver.decanonicalize_ty(ty);
560560
self.write_method_resolution(tgt_expr, func);
561-
(
562-
ty,
563-
self.db.type_for_def(func.into(), Namespace::Values),
564-
Some(self.db.generic_params(func.id.into())),
565-
)
561+
(ty, self.db.value_ty(func.id.into()), Some(self.db.generic_params(func.id.into())))
566562
}
567563
None => (receiver_ty, Ty::Unknown, None),
568564
};

crates/ra_hir/src/ty/infer/path.rs

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use hir_def::{
77

88
use crate::{
99
db::HirDatabase,
10-
ty::{method_resolution, Namespace, Substs, Ty, TypableDef, TypeWalk},
10+
ty::{method_resolution, Substs, Ty, TypeWalk, ValueTyDefId},
1111
AssocItem, Container, Function, Name, Path,
1212
};
1313

@@ -56,7 +56,7 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
5656
}
5757
};
5858

59-
let typable: TypableDef = match value {
59+
let typable: ValueTyDefId = match value {
6060
ValueNs::LocalBinding(pat) => {
6161
let ty = self.result.type_of_pat.get(pat)?.clone();
6262
let ty = self.resolve_ty_as_possible(&mut vec![], ty);
@@ -69,11 +69,10 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
6969
ValueNs::EnumVariantId(it) => it.into(),
7070
};
7171

72-
let mut ty = self.db.type_for_def(typable, Namespace::Values);
72+
let mut ty = self.db.value_ty(typable);
7373
if let Some(self_subst) = self_subst {
7474
ty = ty.subst(&self_subst);
7575
}
76-
7776
let substs = Ty::substs_from_path(self.db, &self.resolver, path, typable);
7877
let ty = ty.subst(&substs);
7978
Some(ty)
@@ -143,24 +142,27 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
143142
id: ExprOrPatId,
144143
) -> Option<(ValueNs, Option<Substs>)> {
145144
let trait_ = trait_ref.trait_;
146-
let item = trait_.items(self.db).iter().copied().find_map(|item| match item {
147-
AssocItem::Function(func) => {
148-
if segment.name == func.name(self.db) {
149-
Some(AssocItem::Function(func))
150-
} else {
151-
None
152-
}
153-
}
145+
let item =
146+
self.db.trait_data(trait_).items.iter().map(|(_name, id)| (*id).into()).find_map(
147+
|item| match item {
148+
AssocItem::Function(func) => {
149+
if segment.name == func.name(self.db) {
150+
Some(AssocItem::Function(func))
151+
} else {
152+
None
153+
}
154+
}
154155

155-
AssocItem::Const(konst) => {
156-
if konst.name(self.db).map_or(false, |n| n == segment.name) {
157-
Some(AssocItem::Const(konst))
158-
} else {
159-
None
160-
}
161-
}
162-
AssocItem::TypeAlias(_) => None,
163-
})?;
156+
AssocItem::Const(konst) => {
157+
if konst.name(self.db).map_or(false, |n| n == segment.name) {
158+
Some(AssocItem::Const(konst))
159+
} else {
160+
None
161+
}
162+
}
163+
AssocItem::TypeAlias(_) => None,
164+
},
165+
)?;
164166
let def = match item {
165167
AssocItem::Function(f) => ValueNs::FunctionId(f.id),
166168
AssocItem::Const(c) => ValueNs::ConstId(c.id),
@@ -212,7 +214,7 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
212214
.fill_with_params()
213215
.build();
214216
self.obligations.push(super::Obligation::Trait(TraitRef {
215-
trait_: t,
217+
trait_: t.id,
216218
substs: trait_substs,
217219
}));
218220
Some(substs)

0 commit comments

Comments
 (0)