Skip to content

Commit dd61a57

Browse files
committed
lower_trait: defer generic param collection to avoid query cycle
1 parent 4876cc2 commit dd61a57

File tree

4 files changed

+30
-17
lines changed

4 files changed

+30
-17
lines changed

crates/hir-analysis/src/ty/trait_def.rs

+10-3
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,10 @@ use super::{
2424
unify::UnificationTable,
2525
};
2626
use crate::{
27-
ty::{trait_lower::collect_trait_impls, trait_resolution::constraint::super_trait_cycle},
27+
ty::{
28+
trait_lower::collect_trait_impls, trait_resolution::constraint::super_trait_cycle,
29+
ty_lower::collect_generic_params,
30+
},
2831
HirAnalysisDb,
2932
};
3033

@@ -310,16 +313,20 @@ impl<'db> TraitInstId<'db> {
310313
pub struct TraitDef<'db> {
311314
pub trait_: Trait<'db>,
312315
#[return_ref]
313-
pub(crate) param_set: GenericParamTypeSet<'db>,
314-
#[return_ref]
315316
pub methods: IndexMap<IdentId<'db>, TraitMethod<'db>>,
316317
}
317318

319+
#[salsa::tracked]
318320
impl<'db> TraitDef<'db> {
319321
pub fn params(self, db: &'db dyn HirAnalysisDb) -> &'db [TyId<'db>] {
320322
self.param_set(db).params(db)
321323
}
322324

325+
#[salsa::tracked(return_ref)]
326+
pub fn param_set(self, db: &'db dyn HirAnalysisDb) -> GenericParamTypeSet<'db> {
327+
collect_generic_params(db, self.trait_(db).into())
328+
}
329+
323330
pub fn self_param(self, db: &'db dyn HirAnalysisDb) -> TyId<'db> {
324331
self.param_set(db).trait_self(db).unwrap()
325332
}

crates/hir-analysis/src/ty/trait_lower.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use super::{
1010
func_def::FuncDef,
1111
trait_def::{does_impl_trait_conflict, Implementor, TraitDef, TraitInstId, TraitMethod},
1212
ty_def::{InvalidCause, Kind, TyId},
13-
ty_lower::{collect_generic_params, lower_generic_arg_list, GenericParamTypeSet},
13+
ty_lower::{collect_generic_params, lower_generic_arg_list},
1414
};
1515
use crate::{
1616
name_resolution::{resolve_path, PathRes},
@@ -22,8 +22,6 @@ type TraitImplTable<'db> = FxHashMap<TraitDef<'db>, Vec<Binder<Implementor<'db>>
2222

2323
#[salsa::tracked]
2424
pub(crate) fn lower_trait<'db>(db: &'db dyn HirAnalysisDb, trait_: Trait<'db>) -> TraitDef<'db> {
25-
let param_set = collect_generic_params(db, trait_.into());
26-
2725
let mut methods = IndexMap::<IdentId<'db>, TraitMethod<'db>>::default();
2826
for method in trait_.methods(db) {
2927
let Some(func) = lower_func(db, method) else {
@@ -36,7 +34,7 @@ pub(crate) fn lower_trait<'db>(db: &'db dyn HirAnalysisDb, trait_: Trait<'db>) -
3634
methods.entry(name).or_insert(trait_method);
3735
}
3836

39-
TraitDef::new(db, trait_, param_set, methods)
37+
TraitDef::new(db, trait_, methods)
4038
}
4139

4240
/// Collect all trait implementors in the ingot.
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,8 @@
11
trait Abi {
2-
type Selector: Encode<Self>
3-
type Encoder
2+
type Selector
43
}
54

65
struct SolAbi {}
76
impl Abi for SolAbi {
87
type Selector = [u8; 4]
98
}
10-
11-
trait Encode<A: Abi> {
12-
fn encode(self, mut e: A::Encoder)
13-
}
14-
15-
impl Encode<SolAbi> for [u8; 4] {
16-
fn encode(self, mut e: SolAbi::Encoder) {}
17-
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
trait Abi {
2+
type Selector: Encode<Self>
3+
type Encoder
4+
}
5+
6+
struct SolAbi {}
7+
impl Abi for SolAbi {
8+
type Selector = [u8; 4]
9+
}
10+
11+
trait Encode<A: Abi> {
12+
fn encode(self, mut e: A::Encoder)
13+
}
14+
15+
impl Encode<SolAbi> for [u8; 4] {
16+
fn encode(self, mut e: SolAbi::Encoder) {}
17+
}

0 commit comments

Comments
 (0)