Skip to content

Commit 72d8e7e

Browse files
committed
Use TraitId in TraitRef
1 parent 6560e4f commit 72d8e7e

File tree

6 files changed

+68
-68
lines changed

6 files changed

+68
-68
lines changed

crates/ra_hir/src/code_model.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -741,7 +741,7 @@ impl Trait {
741741
}
742742

743743
pub fn trait_ref(self, db: &impl HirDatabase) -> TraitRef {
744-
TraitRef::for_trait(db, self)
744+
TraitRef::for_trait(db, self.id)
745745
}
746746

747747
pub fn is_auto(self, db: &impl DefDatabase) -> bool {

crates/ra_hir/src/ty.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ use ra_db::{impl_intern_key, salsa};
2626

2727
use crate::{
2828
db::HirDatabase, expr::ExprId, util::make_mut_slice, Adt, Crate, FloatTy, IntTy, Mutability,
29-
Name, Trait, Uncertain,
29+
Name, Uncertain,
3030
};
3131
use display::{HirDisplay, HirFormatter};
3232

@@ -445,7 +445,7 @@ impl Deref for Substs {
445445
#[derive(Clone, PartialEq, Eq, Debug, Hash)]
446446
pub struct TraitRef {
447447
/// FIXME name?
448-
pub trait_: Trait,
448+
pub trait_: TraitId,
449449
pub substs: Substs,
450450
}
451451

@@ -676,7 +676,7 @@ impl Ty {
676676
}
677677

678678
/// If this is an `impl Trait` or `dyn Trait`, returns that trait.
679-
pub fn inherent_trait(&self) -> Option<Trait> {
679+
pub fn inherent_trait(&self) -> Option<TraitId> {
680680
match self {
681681
Ty::Dyn(predicates) | Ty::Opaque(predicates) => {
682682
predicates.iter().find_map(|pred| match pred {
@@ -988,7 +988,10 @@ impl HirDisplay for Ty {
988988
write!(
989989
f,
990990
"{}",
991-
trait_ref.trait_.name(f.db).unwrap_or_else(Name::missing)
991+
f.db.trait_data(trait_ref.trait_)
992+
.name
993+
.clone()
994+
.unwrap_or_else(Name::missing)
992995
)?;
993996
if trait_ref.substs.len() > 1 {
994997
write!(f, "<")?;
@@ -1049,7 +1052,7 @@ impl TraitRef {
10491052
} else {
10501053
write!(f, ": ")?;
10511054
}
1052-
write!(f, "{}", self.trait_.name(f.db).unwrap_or_else(Name::missing))?;
1055+
write!(f, "{}", f.db.trait_data(self.trait_).name.clone().unwrap_or_else(Name::missing))?;
10531056
if self.substs.len() > 1 {
10541057
write!(f, "<")?;
10551058
f.write_joined(&self.substs[1..], ", ")?;

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

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -143,24 +143,27 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
143143
id: ExprOrPatId,
144144
) -> Option<(ValueNs, Option<Substs>)> {
145145
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-
}
146+
let item =
147+
self.db.trait_data(trait_).items.iter().map(|(_name, id)| (*id).into()).find_map(
148+
|item| match item {
149+
AssocItem::Function(func) => {
150+
if segment.name == func.name(self.db) {
151+
Some(AssocItem::Function(func))
152+
} else {
153+
None
154+
}
155+
}
154156

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-
})?;
157+
AssocItem::Const(konst) => {
158+
if konst.name(self.db).map_or(false, |n| n == segment.name) {
159+
Some(AssocItem::Const(konst))
160+
} else {
161+
None
162+
}
163+
}
164+
AssocItem::TypeAlias(_) => None,
165+
},
166+
)?;
164167
let def = match item {
165168
AssocItem::Function(f) => ValueNs::FunctionId(f.id),
166169
AssocItem::Const(c) => ValueNs::ConstId(c.id),
@@ -212,7 +215,7 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
212215
.fill_with_params()
213216
.build();
214217
self.obligations.push(super::Obligation::Trait(TraitRef {
215-
trait_: t,
218+
trait_: t.id,
216219
substs: trait_substs,
217220
}));
218221
Some(substs)

crates/ra_hir/src/ty/lower.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use hir_def::{
1515
resolver::{HasResolver, Resolver, TypeNs},
1616
type_ref::{TypeBound, TypeRef},
1717
AdtId, AstItemDef, EnumVariantId, FunctionId, GenericDefId, HasModule, LocalStructFieldId,
18-
Lookup, StructId, VariantId,
18+
Lookup, StructId, TraitId, VariantId,
1919
};
2020
use ra_arena::map::ArenaMap;
2121
use ra_db::CrateId;
@@ -172,7 +172,7 @@ impl Ty {
172172
let segment = &remaining_segments[0];
173173
let associated_ty = associated_type_by_name_including_super_traits(
174174
db,
175-
trait_ref.trait_.id,
175+
trait_ref.trait_,
176176
&segment.name,
177177
);
178178
match associated_ty {
@@ -263,7 +263,7 @@ impl Ty {
263263
GenericPredicate::Implemented(tr) if tr.self_ty() == &self_ty => Some(tr.trait_),
264264
_ => None,
265265
});
266-
let traits = traits_from_env.flat_map(|t| all_super_traits(db, t.id)).map(Trait::from);
266+
let traits = traits_from_env.flat_map(|t| all_super_traits(db, t)).map(Trait::from);
267267
for t in traits {
268268
if let Some(associated_ty) = db.trait_data(t.id).associated_type_by_name(&segment.name)
269269
{
@@ -423,7 +423,7 @@ impl TraitRef {
423423
if let Some(self_ty) = explicit_self_ty {
424424
make_mut_slice(&mut substs.0)[0] = self_ty;
425425
}
426-
TraitRef { trait_: resolved, substs }
426+
TraitRef { trait_: resolved.id, substs }
427427
}
428428

429429
pub(crate) fn from_hir(
@@ -450,8 +450,8 @@ impl TraitRef {
450450
substs_from_path_segment(db, resolver, segment, Some(resolved.id.into()), !has_self_param)
451451
}
452452

453-
pub(crate) fn for_trait(db: &impl HirDatabase, trait_: Trait) -> TraitRef {
454-
let substs = Substs::identity(&db.generic_params(trait_.id.into()));
453+
pub(crate) fn for_trait(db: &impl HirDatabase, trait_: TraitId) -> TraitRef {
454+
let substs = Substs::identity(&db.generic_params(trait_.into()));
455455
TraitRef { trait_, substs }
456456
}
457457

@@ -510,7 +510,7 @@ fn assoc_type_bindings_from_type_bound<'a>(
510510
.flat_map(|args_and_bindings| args_and_bindings.bindings.iter())
511511
.map(move |(name, type_ref)| {
512512
let associated_ty =
513-
associated_type_by_name_including_super_traits(db, trait_ref.trait_.id, &name);
513+
associated_type_by_name_including_super_traits(db, trait_ref.trait_, &name);
514514
let associated_ty = match associated_ty {
515515
None => return GenericPredicate::Error,
516516
Some(t) => t,

crates/ra_hir/src/ty/method_resolution.rs

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ impl CrateImplBlocks {
6868
if let Some(tr) =
6969
TraitRef::from_hir(db, &resolver, &trait_ref, Some(target_ty))
7070
{
71-
res.impls_by_trait.entry(tr.trait_.id).or_default().push(impl_id);
71+
res.impls_by_trait.entry(tr.trait_).or_default().push(impl_id);
7272
}
7373
}
7474
None => {
@@ -249,13 +249,11 @@ fn iterate_trait_method_candidates<T>(
249249
let traits_from_env = env
250250
.trait_predicates_for_self_ty(&ty.value)
251251
.map(|tr| tr.trait_)
252-
.flat_map(|t| all_super_traits(db, t.id))
253-
.map(Trait::from);
254-
let traits = inherent_trait
255-
.chain(traits_from_env)
256-
.chain(resolver.traits_in_scope(db).into_iter().map(Trait::from));
252+
.flat_map(|t| all_super_traits(db, t));
253+
let traits =
254+
inherent_trait.chain(traits_from_env).chain(resolver.traits_in_scope(db).into_iter());
257255
'traits: for t in traits {
258-
let data = db.trait_data(t.id);
256+
let data = db.trait_data(t);
259257

260258
// we'll be lazy about checking whether the type implements the
261259
// trait, but if we find out it doesn't, we'll skip the rest of the
@@ -330,7 +328,7 @@ pub(crate) fn implements_trait(
330328
db: &impl HirDatabase,
331329
resolver: &Resolver,
332330
krate: Crate,
333-
trait_: Trait,
331+
trait_: TraitId,
334332
) -> bool {
335333
if ty.value.inherent_trait() == Some(trait_) {
336334
// FIXME this is a bit of a hack, since Chalk should say the same thing
@@ -373,11 +371,11 @@ impl Ty {
373371
fn generic_implements_goal(
374372
db: &impl HirDatabase,
375373
env: Arc<TraitEnvironment>,
376-
trait_: Trait,
374+
trait_: TraitId,
377375
self_ty: Canonical<Ty>,
378376
) -> Canonical<InEnvironment<super::Obligation>> {
379377
let num_vars = self_ty.num_vars;
380-
let substs = super::Substs::build_for_def(db, trait_.id)
378+
let substs = super::Substs::build_for_def(db, trait_)
381379
.push(self_ty.value)
382380
.fill_with_bound_vars(num_vars as u32)
383381
.build();

crates/ra_hir/src/ty/traits/chalk.rs

Lines changed: 23 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ use chalk_ir::{
99
};
1010
use chalk_rust_ir::{AssociatedTyDatum, AssociatedTyValue, ImplDatum, StructDatum, TraitDatum};
1111

12-
use hir_def::{lang_item::LangItemTarget, ContainerId, GenericDefId, Lookup, TraitId, TypeAliasId};
12+
use hir_def::{
13+
lang_item::LangItemTarget, AstItemDef, ContainerId, GenericDefId, Lookup, TraitId, TypeAliasId,
14+
};
1315
use hir_expand::name;
1416

1517
use ra_db::salsa::{InternId, InternKey};
@@ -19,7 +21,7 @@ use crate::{
1921
db::HirDatabase,
2022
ty::display::HirDisplay,
2123
ty::{ApplicationTy, GenericPredicate, ProjectionTy, Substs, TraitRef, Ty, TypeCtor, TypeWalk},
22-
Crate, ImplBlock, Trait, TypeAlias,
24+
Crate, ImplBlock, TypeAlias,
2325
};
2426

2527
/// This represents a trait whose name we could not resolve.
@@ -167,15 +169,15 @@ impl ToChalk for TraitRef {
167169
}
168170
}
169171

170-
impl ToChalk for Trait {
172+
impl ToChalk for TraitId {
171173
type Chalk = chalk_ir::TraitId;
172174

173175
fn to_chalk(self, _db: &impl HirDatabase) -> chalk_ir::TraitId {
174-
chalk_ir::TraitId(id_to_chalk(self.id))
176+
chalk_ir::TraitId(id_to_chalk(self))
175177
}
176178

177-
fn from_chalk(_db: &impl HirDatabase, trait_id: chalk_ir::TraitId) -> Trait {
178-
Trait { id: id_from_chalk(trait_id.0) }
179+
fn from_chalk(_db: &impl HirDatabase, trait_id: chalk_ir::TraitId) -> TraitId {
180+
id_from_chalk(trait_id.0)
179181
}
180182
}
181183

@@ -443,10 +445,10 @@ where
443445
if trait_id == UNKNOWN_TRAIT {
444446
return Vec::new();
445447
}
446-
let trait_: Trait = from_chalk(self.db, trait_id);
448+
let trait_: TraitId = from_chalk(self.db, trait_id);
447449
let mut result: Vec<_> = self
448450
.db
449-
.impls_for_trait(self.krate, trait_)
451+
.impls_for_trait(self.krate, trait_.into())
450452
.iter()
451453
.copied()
452454
.map(Impl::ImplBlock)
@@ -459,7 +461,7 @@ where
459461
[super::FnTrait::FnOnce, super::FnTrait::FnMut, super::FnTrait::Fn].iter()
460462
{
461463
if let Some(actual_trait) = get_fn_trait(self.db, self.krate, fn_trait) {
462-
if trait_.id == actual_trait {
464+
if trait_ == actual_trait {
463465
let impl_ = super::ClosureFnTraitImplData { def, expr, fn_trait };
464466
result.push(Impl::ClosureFnTraitImpl(impl_).to_chalk(self.db));
465467
}
@@ -516,7 +518,7 @@ pub(crate) fn associated_ty_data_query(
516518
where_clauses: vec![],
517519
};
518520
let datum = AssociatedTyDatum {
519-
trait_id: Trait::from(trait_).to_chalk(db),
521+
trait_id: trait_.to_chalk(db),
520522
id,
521523
name: lalrpop_intern::intern(&db.type_alias_data(type_alias).name.to_string()),
522524
binders: make_binders(bound_data, generic_params.count_params_including_parent()),
@@ -548,29 +550,23 @@ pub(crate) fn trait_datum_query(
548550
associated_ty_ids: vec![],
549551
});
550552
}
551-
let trait_: Trait = from_chalk(db, trait_id);
552-
debug!("trait {:?} = {:?}", trait_id, trait_.name(db));
553-
let generic_params = db.generic_params(trait_.id.into());
553+
let trait_: TraitId = from_chalk(db, trait_id);
554+
let trait_data = db.trait_data(trait_);
555+
debug!("trait {:?} = {:?}", trait_id, trait_data.name);
556+
let generic_params = db.generic_params(trait_.into());
554557
let bound_vars = Substs::bound_vars(&generic_params);
555558
let flags = chalk_rust_ir::TraitFlags {
556-
auto: trait_.is_auto(db),
557-
upstream: trait_.module(db).krate() != krate,
559+
auto: trait_data.auto,
560+
upstream: trait_.module(db).krate != krate.crate_id,
558561
non_enumerable: true,
559562
coinductive: false, // only relevant for Chalk testing
560563
// FIXME set these flags correctly
561564
marker: false,
562565
fundamental: false,
563566
};
564-
let where_clauses = convert_where_clauses(db, trait_.id.into(), &bound_vars);
565-
let associated_ty_ids = trait_
566-
.items(db)
567-
.into_iter()
568-
.filter_map(|trait_item| match trait_item {
569-
crate::AssocItem::TypeAlias(type_alias) => Some(type_alias.id),
570-
_ => None,
571-
})
572-
.map(|type_alias| type_alias.to_chalk(db))
573-
.collect();
567+
let where_clauses = convert_where_clauses(db, trait_.into(), &bound_vars);
568+
let associated_ty_ids =
569+
trait_data.associated_types().map(|type_alias| type_alias.to_chalk(db)).collect();
574570
let trait_datum_bound = chalk_rust_ir::TraitDatumBound { where_clauses };
575571
let trait_datum = TraitDatum {
576572
id: trait_id,
@@ -661,7 +657,7 @@ fn impl_block_datum(
661657
};
662658

663659
let impl_datum_bound = chalk_rust_ir::ImplDatumBound { trait_ref, where_clauses };
664-
let trait_data = db.trait_data(trait_.id);
660+
let trait_data = db.trait_data(trait_);
665661
let associated_ty_value_ids = impl_block
666662
.items(db)
667663
.into_iter()
@@ -785,7 +781,7 @@ fn type_alias_associated_ty_value(
785781
.expect("assoc ty value should not exist") // we don't return any assoc ty values if the impl'd trait can't be resolved
786782
.trait_;
787783
let assoc_ty = db
788-
.trait_data(trait_.id)
784+
.trait_data(trait_)
789785
.associated_type_by_name(&type_alias.name(db))
790786
.expect("assoc ty value should not exist"); // validated when building the impl data as well
791787
let generic_params = db.generic_params(impl_block.id.into());

0 commit comments

Comments
 (0)