Skip to content

Commit 3191a93

Browse files
Merge #8409
8409: Various remaining fixes for Chalk IR move r=flodiebold a=flodiebold CC #8313 Co-authored-by: Florian Diebold <flodiebold@gmail.com>
2 parents 6379839 + dc116f7 commit 3191a93

File tree

15 files changed

+78
-70
lines changed

15 files changed

+78
-70
lines changed

crates/hir/src/lib.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,8 @@ use hir_ty::{
5959
traits::FnTrait,
6060
AliasEq, AliasTy, BoundVar, CallableDefId, CallableSig, Canonical, CanonicalVarKinds, Cast,
6161
DebruijnIndex, InEnvironment, Interner, QuantifiedWhereClause, Scalar, Solution, Substitution,
62-
TraitEnvironment, Ty, TyBuilder, TyDefId, TyExt, TyKind, TyVariableKind, WhereClause,
62+
TraitEnvironment, TraitRefExt, Ty, TyBuilder, TyDefId, TyExt, TyKind, TyVariableKind,
63+
WhereClause,
6364
};
6465
use itertools::Itertools;
6566
use rustc_hash::FxHashSet;
@@ -1790,7 +1791,7 @@ impl Type {
17901791
.build();
17911792

17921793
let goal = Canonical {
1793-
value: hir_ty::InEnvironment::new(self.env.env.clone(), trait_ref.cast(&Interner)),
1794+
value: hir_ty::InEnvironment::new(&self.env.env, trait_ref.cast(&Interner)),
17941795
binders: CanonicalVarKinds::empty(&Interner),
17951796
};
17961797

@@ -1807,9 +1808,9 @@ impl Type {
18071808
.push(self.ty.clone())
18081809
.fill(args.iter().map(|t| t.ty.clone()))
18091810
.build();
1810-
let goal = Canonical::new(
1811+
let goal = hir_ty::make_canonical(
18111812
InEnvironment::new(
1812-
self.env.env.clone(),
1813+
&self.env.env,
18131814
AliasEq {
18141815
alias: AliasTy::Projection(projection),
18151816
ty: TyKind::BoundVar(BoundVar::new(DebruijnIndex::INNERMOST, 0))

crates/hir_ty/src/chalk_ext.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -202,12 +202,12 @@ impl TyExt for Ty {
202202
.map(|pred| pred.clone().substitute(&Interner, &substs))
203203
.filter(|wc| match &wc.skip_binders() {
204204
WhereClause::Implemented(tr) => {
205-
tr.self_type_parameter(&Interner) == self
205+
&tr.self_type_parameter(&Interner) == self
206206
}
207207
WhereClause::AliasEq(AliasEq {
208208
alias: AliasTy::Projection(proj),
209209
ty: _,
210-
}) => proj.self_type_parameter(&Interner) == self,
210+
}) => &proj.self_type_parameter(&Interner) == self,
211211
_ => false,
212212
})
213213
.collect::<Vec<_>>();
@@ -293,3 +293,13 @@ impl ProjectionTyExt for ProjectionTy {
293293
}
294294
}
295295
}
296+
297+
pub trait TraitRefExt {
298+
fn hir_trait_id(&self) -> TraitId;
299+
}
300+
301+
impl TraitRefExt for TraitRef {
302+
fn hir_trait_id(&self) -> TraitId {
303+
from_chalk_trait_id(self.trait_id)
304+
}
305+
}

crates/hir_ty/src/display.rs

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ use crate::{
2424
traits::chalk::from_chalk, utils::generics, AdtId, AliasEq, AliasTy, CallableDefId,
2525
CallableSig, Const, ConstValue, DomainGoal, GenericArg, ImplTraitId, Interner, Lifetime,
2626
LifetimeData, LifetimeOutlives, Mutability, OpaqueTy, ProjectionTy, ProjectionTyExt,
27-
QuantifiedWhereClause, Scalar, TraitRef, Ty, TyExt, TyKind, WhereClause,
27+
QuantifiedWhereClause, Scalar, TraitRef, TraitRefExt, Ty, TyExt, TyKind, WhereClause,
2828
};
2929

3030
pub struct HirFormatter<'a> {
@@ -616,12 +616,12 @@ impl HirDisplay for Ty {
616616
.map(|pred| pred.clone().substitute(&Interner, &substs))
617617
.filter(|wc| match &wc.skip_binders() {
618618
WhereClause::Implemented(tr) => {
619-
tr.self_type_parameter(&Interner) == self
619+
&tr.self_type_parameter(&Interner) == self
620620
}
621621
WhereClause::AliasEq(AliasEq {
622622
alias: AliasTy::Projection(proj),
623623
ty: _,
624-
}) => proj.self_type_parameter(&Interner) == self,
624+
}) => &proj.self_type_parameter(&Interner) == self,
625625
_ => false,
626626
})
627627
.collect::<Vec<_>>();
@@ -745,7 +745,7 @@ fn write_bounds_like_dyn_trait(
745745
// existential) here, which is the only thing that's
746746
// possible in actual Rust, and hence don't print it
747747
write!(f, "{}", f.db.trait_data(trait_).name)?;
748-
if let [_, params @ ..] = &*trait_ref.substitution.interned() {
748+
if let [_, params @ ..] = &*trait_ref.substitution.interned().as_slice() {
749749
if is_fn_trait {
750750
if let Some(args) =
751751
params.first().and_then(|it| it.assert_ty_ref(&Interner).as_tuple())
@@ -792,31 +792,29 @@ fn write_bounds_like_dyn_trait(
792792
Ok(())
793793
}
794794

795-
impl TraitRef {
796-
fn hir_fmt_ext(&self, f: &mut HirFormatter, use_as: bool) -> Result<(), HirDisplayError> {
797-
if f.should_truncate() {
798-
return write!(f, "{}", TYPE_HINT_TRUNCATION);
799-
}
795+
fn fmt_trait_ref(tr: &TraitRef, f: &mut HirFormatter, use_as: bool) -> Result<(), HirDisplayError> {
796+
if f.should_truncate() {
797+
return write!(f, "{}", TYPE_HINT_TRUNCATION);
798+
}
800799

801-
self.self_type_parameter(&Interner).hir_fmt(f)?;
802-
if use_as {
803-
write!(f, " as ")?;
804-
} else {
805-
write!(f, ": ")?;
806-
}
807-
write!(f, "{}", f.db.trait_data(self.hir_trait_id()).name)?;
808-
if self.substitution.len(&Interner) > 1 {
809-
write!(f, "<")?;
810-
f.write_joined(&self.substitution.interned()[1..], ", ")?;
811-
write!(f, ">")?;
812-
}
813-
Ok(())
800+
tr.self_type_parameter(&Interner).hir_fmt(f)?;
801+
if use_as {
802+
write!(f, " as ")?;
803+
} else {
804+
write!(f, ": ")?;
814805
}
806+
write!(f, "{}", f.db.trait_data(tr.hir_trait_id()).name)?;
807+
if tr.substitution.len(&Interner) > 1 {
808+
write!(f, "<")?;
809+
f.write_joined(&tr.substitution.interned()[1..], ", ")?;
810+
write!(f, ">")?;
811+
}
812+
Ok(())
815813
}
816814

817815
impl HirDisplay for TraitRef {
818816
fn hir_fmt(&self, f: &mut HirFormatter) -> Result<(), HirDisplayError> {
819-
self.hir_fmt_ext(f, false)
817+
fmt_trait_ref(self, f, false)
820818
}
821819
}
822820

@@ -830,7 +828,7 @@ impl HirDisplay for WhereClause {
830828
WhereClause::Implemented(trait_ref) => trait_ref.hir_fmt(f)?,
831829
WhereClause::AliasEq(AliasEq { alias: AliasTy::Projection(projection_ty), ty }) => {
832830
write!(f, "<")?;
833-
projection_ty.trait_ref(f.db).hir_fmt_ext(f, true)?;
831+
fmt_trait_ref(&projection_ty.trait_ref(f.db), f, true)?;
834832
write!(
835833
f,
836834
">::{} = ",

crates/hir_ty/src/infer.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,7 @@ impl<'a> InferenceContext<'a> {
336336
self.last_obligations_check = Some(self.table.revision);
337337
let obligations = mem::replace(&mut self.obligations, Vec::new());
338338
for obligation in obligations {
339-
let in_env = InEnvironment::new(self.trait_env.env.clone(), obligation.clone());
339+
let in_env = InEnvironment::new(&self.trait_env.env, obligation.clone());
340340
let canonicalized = self.canonicalizer().canonicalize_obligation(in_env);
341341
let solution =
342342
self.db.trait_solve(self.resolver.krate().unwrap(), canonicalized.value.clone());

crates/hir_ty/src/infer/coerce.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ impl<'a> InferenceContext<'a> {
139139
b.push(from_ty.clone()).push(to_ty.clone()).build()
140140
};
141141

142-
let goal = InEnvironment::new(self.trait_env.env.clone(), trait_ref.cast(&Interner));
142+
let goal = InEnvironment::new(&self.trait_env.env, trait_ref.cast(&Interner));
143143

144144
let canonicalizer = self.canonicalizer();
145145
let canonicalized = canonicalizer.canonicalize_obligation(goal);

crates/hir_ty/src/infer/pat.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ impl<'a> InferenceContext<'a> {
122122
let ty = match &body[pat] {
123123
&Pat::Tuple { ref args, ellipsis } => {
124124
let expectations = match expected.as_tuple() {
125-
Some(parameters) => &*parameters.interned(),
125+
Some(parameters) => &*parameters.interned().as_slice(),
126126
_ => &[],
127127
};
128128

crates/hir_ty/src/infer/path.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ use hir_def::{
1111
use hir_expand::name::Name;
1212

1313
use crate::{
14-
method_resolution, Interner, Substitution, Ty, TyBuilder, TyExt, TyKind, ValueTyDefId,
14+
method_resolution, Interner, Substitution, TraitRefExt, Ty, TyBuilder, TyExt, TyKind,
15+
ValueTyDefId,
1516
};
1617

1718
use super::{ExprOrPatId, InferenceContext, TraitRef};

crates/hir_ty/src/lib.rs

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ use crate::{db::HirDatabase, display::HirDisplay, utils::generics};
4141

4242
pub use autoderef::autoderef;
4343
pub use builder::TyBuilder;
44-
pub use chalk_ext::{ProjectionTyExt, TyExt};
44+
pub use chalk_ext::*;
4545
pub use infer::{could_unify, InferenceResult};
4646
pub use lower::{
4747
associated_type_shorthand_candidates, callable_item_sig, CallableDefId, ImplTraitLoweringMode,
@@ -107,22 +107,18 @@ pub fn make_only_type_binders<T>(num_vars: usize, value: T) -> Binders<T> {
107107
)
108108
}
109109

110-
impl TraitRef {
111-
pub fn hir_trait_id(&self) -> TraitId {
112-
from_chalk_trait_id(self.trait_id)
113-
}
114-
}
115-
116-
impl<T> Canonical<T> {
117-
pub fn new(value: T, kinds: impl IntoIterator<Item = TyVariableKind>) -> Self {
118-
let kinds = kinds.into_iter().map(|tk| {
119-
chalk_ir::CanonicalVarKind::new(
120-
chalk_ir::VariableKind::Ty(tk),
121-
chalk_ir::UniverseIndex::ROOT,
122-
)
123-
});
124-
Self { value, binders: chalk_ir::CanonicalVarKinds::from_iter(&Interner, kinds) }
125-
}
110+
// FIXME: get rid of this
111+
pub fn make_canonical<T>(
112+
value: T,
113+
kinds: impl IntoIterator<Item = TyVariableKind>,
114+
) -> Canonical<T> {
115+
let kinds = kinds.into_iter().map(|tk| {
116+
chalk_ir::CanonicalVarKind::new(
117+
chalk_ir::VariableKind::Ty(tk),
118+
chalk_ir::UniverseIndex::ROOT,
119+
)
120+
});
121+
Canonical { value, binders: chalk_ir::CanonicalVarKinds::from_iter(&Interner, kinds) }
126122
}
127123

128124
/// A function signature as seen by type inference: Several parameter types and

crates/hir_ty/src/lower.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ use crate::{
3535
AliasEq, AliasTy, Binders, BoundVar, CallableSig, DebruijnIndex, DynTy, FnPointer, FnSig,
3636
FnSubst, ImplTraitId, OpaqueTy, PolyFnSig, ProjectionTy, QuantifiedWhereClause,
3737
QuantifiedWhereClauses, ReturnTypeImplTrait, ReturnTypeImplTraits, Substitution,
38-
TraitEnvironment, TraitRef, Ty, TyBuilder, TyKind, TypeWalk, WhereClause,
38+
TraitEnvironment, TraitRef, TraitRefExt, Ty, TyBuilder, TyKind, TypeWalk, WhereClause,
3939
};
4040

4141
#[derive(Debug)]

crates/hir_ty/src/method_resolution.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ use crate::{
2222
static_lifetime,
2323
utils::all_super_traits,
2424
AdtId, Canonical, CanonicalVarKinds, DebruijnIndex, FnPointer, FnSig, ForeignDefId,
25-
InEnvironment, Interner, Scalar, Substitution, TraitEnvironment, Ty, TyBuilder, TyExt, TyKind,
26-
TypeWalk,
25+
InEnvironment, Interner, Scalar, Substitution, TraitEnvironment, TraitRefExt, Ty, TyBuilder,
26+
TyExt, TyKind, TypeWalk,
2727
};
2828

2929
/// This is used as a key for indexing impls.
@@ -845,7 +845,7 @@ fn generic_implements_goal(
845845
let obligation = trait_ref.cast(&Interner);
846846
Canonical {
847847
binders: CanonicalVarKinds::from_iter(&Interner, kinds),
848-
value: InEnvironment::new(env.env.clone(), obligation),
848+
value: InEnvironment::new(&env.env, obligation),
849849
}
850850
}
851851

0 commit comments

Comments
 (0)