Skip to content

Commit 4e415a2

Browse files
committed
Remove ns-polymorphic type_for_def
1 parent 475367d commit 4e415a2

File tree

9 files changed

+129
-165
lines changed

9 files changed

+129
-165
lines changed

crates/ra_hir/src/code_model.rs

Lines changed: 7 additions & 8 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> {
@@ -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: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ pub(crate) use infer::{infer_query, InferTy, InferenceResult};
3737
pub use lower::CallableDef;
3838
pub(crate) use lower::{
3939
callable_item_sig, field_types_query, generic_defaults_query,
40-
generic_predicates_for_param_query, generic_predicates_query, type_for_def, Namespace,
41-
TypableDef,
40+
generic_predicates_for_param_query, generic_predicates_query, ty_query, value_ty_query,
41+
TyDefId, TypableDef, ValueTyDefId,
4242
};
4343
pub(crate) use traits::{InEnvironment, Obligation, ProjectionPredicate, TraitEnvironment};
4444

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: 3 additions & 4 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)

0 commit comments

Comments
 (0)