Skip to content

Commit 34f773f

Browse files
committed
Use ParamEnv in TraitEnvironment
1 parent c251353 commit 34f773f

File tree

17 files changed

+216
-170
lines changed

17 files changed

+216
-170
lines changed

crates/hir-ty/src/autoderef.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ fn structurally_normalize_ty<'db>(
298298
) -> Option<(Ty<'db>, PredicateObligations<'db>)> {
299299
let mut ocx = ObligationCtxt::new(&table.infer_ctxt);
300300
let Ok(normalized_ty) =
301-
ocx.structurally_normalize_ty(&ObligationCause::misc(), table.param_env, ty)
301+
ocx.structurally_normalize_ty(&ObligationCause::misc(), table.trait_env.env, ty)
302302
else {
303303
// We shouldn't have errors here in the old solver, except for
304304
// evaluate/fulfill mismatches, but that's not a reason for an ICE.

crates/hir-ty/src/chalk_ext.rs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,13 @@ use crate::{
1515
AdtId, AliasEq, AliasTy, Binders, CallableDefId, CallableSig, Canonical, CanonicalVarKinds,
1616
ClosureId, DynTy, FnPointer, ImplTraitId, InEnvironment, Interner, Lifetime, ProjectionTy,
1717
QuantifiedWhereClause, Substitution, ToChalk, TraitRef, Ty, TyBuilder, TyKind, TypeFlags,
18-
WhereClause, db::HirDatabase, from_assoc_type_id, from_chalk_trait_id, from_foreign_def_id,
19-
from_placeholder_idx, generics::generics, to_chalk_trait_id, utils::ClosureSubst,
18+
WhereClause,
19+
db::HirDatabase,
20+
from_assoc_type_id, from_chalk_trait_id, from_foreign_def_id, from_placeholder_idx,
21+
generics::generics,
22+
next_solver::{DbInterner, mapping::NextSolverToChalk},
23+
to_chalk_trait_id,
24+
utils::ClosureSubst,
2025
};
2126

2227
pub trait TyExt {
@@ -372,7 +377,10 @@ impl TyExt for Ty {
372377
let trait_ref = TyBuilder::trait_ref(db, copy_trait).push(self).build();
373378
let env = db.trait_environment_for_body(owner);
374379
let goal = Canonical {
375-
value: InEnvironment::new(&env.env, trait_ref.cast(Interner)),
380+
value: InEnvironment::new(
381+
&env.env.to_chalk(DbInterner::new_with(db, Some(env.krate), env.block)),
382+
trait_ref.cast(Interner),
383+
),
376384
binders: CanonicalVarKinds::empty(Interner),
377385
};
378386
!db.trait_solve(crate_id, None, goal).no_solution()

crates/hir-ty/src/db.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,12 +182,12 @@ pub trait HirDatabase: DefDatabase + std::fmt::Debug {
182182
#[salsa::invoke(crate::lower::generic_predicates_query)]
183183
fn generic_predicates(&self, def: GenericDefId) -> GenericPredicates;
184184

185-
#[salsa::invoke(crate::lower::trait_environment_for_body_query)]
185+
#[salsa::invoke(crate::lower_nextsolver::trait_environment_for_body_query)]
186186
#[salsa::transparent]
187187
fn trait_environment_for_body<'db>(&'db self, def: DefWithBodyId)
188188
-> Arc<TraitEnvironment<'db>>;
189189

190-
#[salsa::invoke(crate::lower::trait_environment_query)]
190+
#[salsa::invoke(crate::lower_nextsolver::trait_environment_query)]
191191
fn trait_environment<'db>(&'db self, def: GenericDefId) -> Arc<TraitEnvironment<'db>>;
192192

193193
#[salsa::invoke(crate::lower::generic_defaults_with_diagnostics_query)]

crates/hir-ty/src/display.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -792,10 +792,7 @@ fn render_const_scalar_ns(
792792
let trait_env = TraitEnvironment::empty(f.krate());
793793
let interner = DbInterner::new_with(f.db, Some(trait_env.krate), trait_env.block);
794794
let infcx = interner.infer_ctxt().build(rustc_type_ir::TypingMode::PostAnalysis);
795-
let ty = infcx
796-
.at(&ObligationCause::new(), trait_env.env.to_nextsolver(interner))
797-
.deeply_normalize(ty)
798-
.unwrap_or(ty);
795+
let ty = infcx.at(&ObligationCause::new(), trait_env.env).deeply_normalize(ty).unwrap_or(ty);
799796
render_const_scalar_inner(f, b, memory_map, ty, trait_env)
800797
}
801798

crates/hir-ty/src/drop.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ use hir_def::signatures::StructFlags;
77
use stdx::never;
88
use triomphe::Arc;
99

10+
use crate::next_solver::DbInterner;
11+
use crate::next_solver::mapping::NextSolverToChalk;
1012
use crate::{
1113
AliasTy, Canonical, CanonicalVarKinds, ConcreteConst, ConstScalar, ConstValue, InEnvironment,
1214
Interner, ProjectionTy, TraitEnvironment, Ty, TyBuilder, TyKind, db::HirDatabase,
@@ -188,7 +190,10 @@ fn is_copy(db: &dyn HirDatabase, ty: Ty, env: Arc<TraitEnvironment<'_>>) -> bool
188190
};
189191
let trait_ref = TyBuilder::trait_ref(db, copy_trait).push(ty).build();
190192
let goal = Canonical {
191-
value: InEnvironment::new(&env.env, trait_ref.cast(Interner)),
193+
value: InEnvironment::new(
194+
&env.env.to_chalk(DbInterner::new_with(db, Some(env.krate), env.block)),
195+
trait_ref.cast(Interner),
196+
),
192197
binders: CanonicalVarKinds::empty(Interner),
193198
};
194199
db.trait_solve(env.krate, env.block, goal).certain()

crates/hir-ty/src/infer/closure.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,7 @@ impl<'db> InferenceContext<'db> {
318318
_ = self
319319
.table
320320
.infer_ctxt
321-
.at(&ObligationCause::new(), self.table.param_env)
321+
.at(&ObligationCause::new(), self.table.trait_env.env)
322322
.eq(DefineOpaqueTypes::Yes, inferred_fnptr_sig, generalized_fnptr_sig)
323323
.map(|infer_ok| self.table.register_infer_ok(infer_ok));
324324

@@ -703,15 +703,15 @@ impl<'db> InferenceContext<'db> {
703703
let cause = ObligationCause::new();
704704
let InferOk { value: (), obligations } = table
705705
.infer_ctxt
706-
.at(&cause, table.param_env)
706+
.at(&cause, table.trait_env.env)
707707
.eq(DefineOpaqueTypes::Yes, expected_ty, supplied_ty)?;
708708
all_obligations.extend(obligations);
709709
}
710710

711711
let supplied_output_ty = supplied_sig.output();
712712
let cause = ObligationCause::new();
713713
let InferOk { value: (), obligations } =
714-
table.infer_ctxt.at(&cause, table.param_env).eq(
714+
table.infer_ctxt.at(&cause, table.trait_env.env).eq(
715715
DefineOpaqueTypes::Yes,
716716
expected_sigs.liberated_sig.output(),
717717
supplied_output_ty,

crates/hir-ty/src/infer/coerce.rs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ impl<'a, 'b, 'db> Coerce<'a, 'b, 'db> {
144144
fn unify_raw(&mut self, a: Ty<'db>, b: Ty<'db>) -> InferResult<'db, Ty<'db>> {
145145
debug!("unify(a: {:?}, b: {:?}, use_lub: {})", a, b, self.use_lub);
146146
self.commit_if_ok(|this| {
147-
let at = this.infer_ctxt().at(&this.cause, this.table.param_env);
147+
let at = this.infer_ctxt().at(&this.cause, this.table.trait_env.env);
148148

149149
let res = if this.use_lub {
150150
at.lub(b, a)
@@ -330,7 +330,7 @@ impl<'a, 'b, 'db> Coerce<'a, 'b, 'db> {
330330
obligations.push(Obligation::new(
331331
self.interner(),
332332
self.cause.clone(),
333-
self.table.param_env,
333+
self.table.trait_env.env,
334334
Binder::dummy(PredicateKind::Coerce(CoercePredicate {
335335
a: source_ty,
336336
b: target_ty,
@@ -718,7 +718,7 @@ impl<'a, 'b, 'db> Coerce<'a, 'b, 'db> {
718718
let mut queue: SmallVec<[PredicateObligation<'db>; 4]> = smallvec![Obligation::new(
719719
self.interner(),
720720
cause,
721-
self.table.param_env,
721+
self.table.trait_env.env,
722722
TraitRef::new(
723723
self.interner(),
724724
coerce_unsized_did.into(),
@@ -1114,8 +1114,12 @@ impl<'db> InferenceContext<'db> {
11141114
match self.table.commit_if_ok(|table| {
11151115
// We need to eagerly handle nested obligations due to lazy norm.
11161116
let mut ocx = ObligationCtxt::new(&table.infer_ctxt);
1117-
let value =
1118-
ocx.lub(&ObligationCause::new(), table.param_env, prev_ty, new_ty)?;
1117+
let value = ocx.lub(
1118+
&ObligationCause::new(),
1119+
table.trait_env.env,
1120+
prev_ty,
1121+
new_ty,
1122+
)?;
11191123
if ocx.select_where_possible().is_empty() {
11201124
Ok(InferOk { value, obligations: ocx.into_pending_obligations() })
11211125
} else {
@@ -1158,7 +1162,7 @@ impl<'db> InferenceContext<'db> {
11581162
let sig = self
11591163
.table
11601164
.infer_ctxt
1161-
.at(&ObligationCause::new(), self.table.param_env)
1165+
.at(&ObligationCause::new(), self.table.trait_env.env)
11621166
.lub(a_sig, b_sig)
11631167
.map(|ok| self.table.register_infer_ok(ok))?;
11641168

@@ -1248,7 +1252,7 @@ impl<'db> InferenceContext<'db> {
12481252
.commit_if_ok(|table| {
12491253
table
12501254
.infer_ctxt
1251-
.at(&ObligationCause::new(), table.param_env)
1255+
.at(&ObligationCause::new(), table.trait_env.env)
12521256
.lub(prev_ty, new_ty)
12531257
})
12541258
.unwrap_err())
@@ -1498,7 +1502,7 @@ impl<'db, 'exprs> CoerceMany<'db, 'exprs> {
14981502
assert!(expression_ty.is_unit(), "if let hack without unit type");
14991503
icx.table
15001504
.infer_ctxt
1501-
.at(cause, icx.table.param_env)
1505+
.at(cause, icx.table.trait_env.env)
15021506
.eq(
15031507
// needed for tests/ui/type-alias-impl-trait/issue-65679-inst-opaque-ty-from-val-twice.rs
15041508
DefineOpaqueTypes::Yes,

crates/hir-ty/src/infer/expr.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1662,7 +1662,6 @@ impl<'db> InferenceContext<'db> {
16621662
});
16631663
self.resolver.reset_to_guard(g);
16641664
if let Some(prev_env) = prev_env {
1665-
self.table.param_env = prev_env.env.to_nextsolver(self.table.interner);
16661665
self.table.trait_env = prev_env;
16671666
}
16681667

@@ -2132,7 +2131,7 @@ impl<'db> InferenceContext<'db> {
21322131
let origin = ObligationCause::new();
21332132
ocx.sup(
21342133
&origin,
2135-
self.table.param_env,
2134+
self.table.trait_env.env,
21362135
expected_output.to_nextsolver(interner),
21372136
formal_output,
21382137
)?;
@@ -2239,7 +2238,7 @@ impl<'db> InferenceContext<'db> {
22392238
let formal_ty_error = this
22402239
.table
22412240
.infer_ctxt
2242-
.at(&ObligationCause::new(), this.table.param_env)
2241+
.at(&ObligationCause::new(), this.table.trait_env.env)
22432242
.eq(DefineOpaqueTypes::Yes, formal_input_ty, coerced_ty);
22442243

22452244
// If neither check failed, the types are compatible

crates/hir-ty/src/infer/unify.rs

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ use crate::{
2929
db::HirDatabase,
3030
fold_generic_args, fold_tys_and_consts,
3131
next_solver::{
32-
self, ClauseKind, DbInterner, ErrorGuaranteed, ParamEnv, Predicate, PredicateKind,
33-
SolverDefIds, Term, TraitRef,
32+
self, ClauseKind, DbInterner, ErrorGuaranteed, Predicate, PredicateKind, SolverDefIds,
33+
Term, TraitRef,
3434
fulfill::FulfillmentCtxt,
3535
infer::{
3636
DbInternerInferExt, InferCtxt, InferOk,
@@ -213,7 +213,6 @@ pub(crate) struct InferenceTable<'db> {
213213
pub(crate) db: &'db dyn HirDatabase,
214214
pub(crate) interner: DbInterner<'db>,
215215
pub(crate) trait_env: Arc<TraitEnvironment<'db>>,
216-
pub(crate) param_env: ParamEnv<'db>,
217216
pub(crate) tait_coercion_table: Option<FxHashMap<OpaqueTyId, Ty>>,
218217
pub(crate) infer_ctxt: InferCtxt<'db>,
219218
diverging_tys: FxHashSet<Ty>,
@@ -235,7 +234,6 @@ impl<'db> InferenceTable<'db> {
235234
InferenceTable {
236235
db,
237236
interner,
238-
param_env: trait_env.env.to_nextsolver(interner),
239237
trait_env,
240238
tait_coercion_table: None,
241239
fulfillment_cx: FulfillmentCtxt::new(&infer_ctxt),
@@ -426,7 +424,7 @@ impl<'db> InferenceTable<'db> {
426424
{
427425
let ty = self.resolve_vars_with_obligations(ty);
428426
self.infer_ctxt
429-
.at(&ObligationCause::new(), self.param_env)
427+
.at(&ObligationCause::new(), self.trait_env.env)
430428
.deeply_normalize(ty.clone())
431429
.unwrap_or(ty)
432430
}
@@ -741,7 +739,7 @@ impl<'db> InferenceTable<'db> {
741739
) -> InferResult<'db, ()> {
742740
let variance = rustc_type_ir::Variance::Invariant;
743741
let span = crate::next_solver::Span::dummy();
744-
match self.infer_ctxt.relate(self.param_env, lhs, variance, rhs, span) {
742+
match self.infer_ctxt.relate(self.trait_env.env, lhs, variance, rhs, span) {
745743
Ok(goals) => Ok(crate::infer::InferOk { goals, value: () }),
746744
Err(_) => Err(TypeError),
747745
}
@@ -798,7 +796,7 @@ impl<'db> InferenceTable<'db> {
798796

799797
fn structurally_normalize_term(&mut self, term: Term<'db>) -> Term<'db> {
800798
self.infer_ctxt
801-
.at(&ObligationCause::new(), self.param_env)
799+
.at(&ObligationCause::new(), self.trait_env.env)
802800
.structurally_normalize_term(term, &mut self.fulfillment_cx)
803801
.unwrap_or(term)
804802
}
@@ -818,7 +816,7 @@ impl<'db> InferenceTable<'db> {
818816
// in a reentrant borrow, causing an ICE.
819817
let result = self
820818
.infer_ctxt
821-
.at(&ObligationCause::misc(), self.param_env)
819+
.at(&ObligationCause::misc(), self.trait_env.env)
822820
.structurally_normalize_ty(ty, &mut self.fulfillment_cx);
823821
match result {
824822
Ok(normalized_ty) => normalized_ty,
@@ -874,14 +872,14 @@ impl<'db> InferenceTable<'db> {
874872
/// choice (during e.g. method resolution or deref).
875873
#[tracing::instrument(level = "debug", skip(self))]
876874
pub(crate) fn try_obligation(&mut self, predicate: Predicate<'db>) -> NextTraitSolveResult {
877-
let goal = next_solver::Goal { param_env: self.param_env, predicate };
875+
let goal = next_solver::Goal { param_env: self.trait_env.env, predicate };
878876
let canonicalized = self.canonicalize(goal);
879877

880878
next_trait_solve_canonical_in_ctxt(&self.infer_ctxt, canonicalized)
881879
}
882880

883881
pub(crate) fn register_obligation(&mut self, predicate: Predicate<'db>) {
884-
let goal = next_solver::Goal { param_env: self.param_env, predicate };
882+
let goal = next_solver::Goal { param_env: self.trait_env.env, predicate };
885883
self.register_obligation_in_env(goal)
886884
}
887885

0 commit comments

Comments
 (0)