Skip to content

Commit eaf8af5

Browse files
resolve before canonicalization, ICE if unresolved
1 parent 5bea48b commit eaf8af5

File tree

3 files changed

+115
-68
lines changed

3 files changed

+115
-68
lines changed

compiler/rustc_middle/src/traits/solve.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ impl<'tcx> std::ops::Deref for ExternalConstraints<'tcx> {
147147
}
148148

149149
/// Additional constraints returned on success.
150-
#[derive(Debug, PartialEq, Eq, Clone, Hash, HashStable, Default)]
150+
#[derive(Debug, PartialEq, Eq, Clone, Hash, HashStable, Default, TypeVisitable, TypeFoldable)]
151151
pub struct ExternalConstraintsData<'tcx> {
152152
// FIXME: implement this.
153153
pub region_constraints: QueryRegionConstraints<'tcx>,

compiler/rustc_trait_selection/src/solve/canonicalize.rs

+35-59
Original file line numberDiff line numberDiff line change
@@ -207,23 +207,18 @@ impl<'tcx> TypeFolder<TyCtxt<'tcx>> for Canonicalizer<'_, 'tcx> {
207207
t
208208
}
209209

210-
fn fold_region(&mut self, mut r: ty::Region<'tcx>) -> ty::Region<'tcx> {
211-
match self.canonicalize_mode {
212-
CanonicalizeMode::Input => {
213-
// Don't resolve infer vars in input, since it affects
214-
// caching and may cause trait selection bugs which rely
215-
// on regions to be equal.
216-
}
217-
CanonicalizeMode::Response { .. } => {
218-
if let ty::ReVar(vid) = *r {
219-
r = self
220-
.infcx
221-
.inner
222-
.borrow_mut()
223-
.unwrap_region_constraints()
224-
.opportunistic_resolve_var(self.infcx.tcx, vid);
225-
}
226-
}
210+
fn fold_region(&mut self, r: ty::Region<'tcx>) -> ty::Region<'tcx> {
211+
if let ty::ReVar(vid) = *r {
212+
let resolved_region = self
213+
.infcx
214+
.inner
215+
.borrow_mut()
216+
.unwrap_region_constraints()
217+
.opportunistic_resolve_var(self.infcx.tcx, vid);
218+
assert_eq!(
219+
r, resolved_region,
220+
"region var should have been resolved, {r} -> {resolved_region}"
221+
);
227222
}
228223

229224
let kind = match *r {
@@ -278,38 +273,22 @@ impl<'tcx> TypeFolder<TyCtxt<'tcx>> for Canonicalizer<'_, 'tcx> {
278273
ty::Region::new_late_bound(self.interner(), self.binder_index, br)
279274
}
280275

281-
fn fold_ty(&mut self, mut t: Ty<'tcx>) -> Ty<'tcx> {
276+
fn fold_ty(&mut self, t: Ty<'tcx>) -> Ty<'tcx> {
282277
let kind = match *t.kind() {
283-
ty::Infer(ty::TyVar(mut vid)) => {
284-
// We need to canonicalize the *root* of our ty var.
285-
// This is so that our canonical response correctly reflects
286-
// any equated inference vars correctly!
287-
let root_vid = self.infcx.root_var(vid);
288-
if root_vid != vid {
289-
t = Ty::new_var(self.infcx.tcx, root_vid);
290-
vid = root_vid;
291-
}
292-
293-
match self.infcx.probe_ty_var(vid) {
294-
Ok(t) => return self.fold_ty(t),
295-
Err(ui) => CanonicalVarKind::Ty(CanonicalTyVarKind::General(ui)),
296-
}
278+
ty::Infer(ty::TyVar(vid)) => {
279+
assert_eq!(self.infcx.root_var(vid), vid, "ty vid should have been resolved");
280+
let Err(ui) = self.infcx.probe_ty_var(vid) else {
281+
bug!("ty var should have been resolved: {t}");
282+
};
283+
CanonicalVarKind::Ty(CanonicalTyVarKind::General(ui))
297284
}
298285
ty::Infer(ty::IntVar(vid)) => {
299-
let nt = self.infcx.opportunistic_resolve_int_var(vid);
300-
if nt != t {
301-
return self.fold_ty(nt);
302-
} else {
303-
CanonicalVarKind::Ty(CanonicalTyVarKind::Int)
304-
}
286+
assert_eq!(self.infcx.opportunistic_resolve_int_var(vid), t);
287+
CanonicalVarKind::Ty(CanonicalTyVarKind::Int)
305288
}
306289
ty::Infer(ty::FloatVar(vid)) => {
307-
let nt = self.infcx.opportunistic_resolve_float_var(vid);
308-
if nt != t {
309-
return self.fold_ty(nt);
310-
} else {
311-
CanonicalVarKind::Ty(CanonicalTyVarKind::Float)
312-
}
290+
assert_eq!(self.infcx.opportunistic_resolve_float_var(vid), t);
291+
CanonicalVarKind::Ty(CanonicalTyVarKind::Float)
313292
}
314293
ty::Infer(ty::FreshTy(_) | ty::FreshIntTy(_) | ty::FreshFloatTy(_)) => {
315294
bug!("fresh var during canonicalization: {t:?}")
@@ -372,22 +351,19 @@ impl<'tcx> TypeFolder<TyCtxt<'tcx>> for Canonicalizer<'_, 'tcx> {
372351
Ty::new_bound(self.infcx.tcx, self.binder_index, bt)
373352
}
374353

375-
fn fold_const(&mut self, mut c: ty::Const<'tcx>) -> ty::Const<'tcx> {
354+
fn fold_const(&mut self, c: ty::Const<'tcx>) -> ty::Const<'tcx> {
376355
let kind = match c.kind() {
377-
ty::ConstKind::Infer(ty::InferConst::Var(mut vid)) => {
378-
// We need to canonicalize the *root* of our const var.
379-
// This is so that our canonical response correctly reflects
380-
// any equated inference vars correctly!
381-
let root_vid = self.infcx.root_const_var(vid);
382-
if root_vid != vid {
383-
c = ty::Const::new_var(self.infcx.tcx, root_vid, c.ty());
384-
vid = root_vid;
385-
}
386-
387-
match self.infcx.probe_const_var(vid) {
388-
Ok(c) => return self.fold_const(c),
389-
Err(universe) => CanonicalVarKind::Const(universe, c.ty()),
390-
}
356+
ty::ConstKind::Infer(ty::InferConst::Var(vid)) => {
357+
assert_eq!(
358+
self.infcx.root_const_var(vid),
359+
vid,
360+
"const var should have been resolved"
361+
);
362+
let Err(ui) = self.infcx.probe_const_var(vid) else {
363+
bug!("const var should have been resolved");
364+
};
365+
// FIXME: we should fold this ty eventually
366+
CanonicalVarKind::Const(ui, c.ty())
391367
}
392368
ty::ConstKind::Infer(ty::InferConst::Fresh(_)) => {
393369
bug!("fresh var during canonicalization: {c:?}")

compiler/rustc_trait_selection/src/solve/eval_ctxt/canonical.rs

+79-8
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,15 @@ use rustc_index::IndexVec;
1616
use rustc_infer::infer::canonical::query_response::make_query_region_constraints;
1717
use rustc_infer::infer::canonical::CanonicalVarValues;
1818
use rustc_infer::infer::canonical::{CanonicalExt, QueryRegionConstraints};
19+
use rustc_infer::infer::InferCtxt;
1920
use rustc_middle::traits::query::NoSolution;
2021
use rustc_middle::traits::solve::{
21-
ExternalConstraintsData, MaybeCause, PredefinedOpaquesData, QueryInput,
22+
ExternalConstraintsData, MaybeCause, PredefinedOpaquesData, QueryInput,
23+
};
24+
use rustc_middle::ty::{
25+
self, BoundVar, GenericArgKind, Ty, TyCtxt, TypeFoldable, TypeFolder, TypeSuperFoldable,
26+
TypeVisitableExt,
2227
};
23-
use rustc_middle::ty::{self, BoundVar, GenericArgKind, Ty, TyCtxt, TypeFoldable};
2428
use rustc_span::DUMMY_SP;
2529
use std::iter;
2630
use std::ops::Deref;
@@ -32,6 +36,10 @@ impl<'tcx> EvalCtxt<'_, 'tcx> {
3236
&self,
3337
goal: Goal<'tcx, T>,
3438
) -> (Vec<ty::GenericArg<'tcx>>, CanonicalInput<'tcx, T>) {
39+
let opaque_types = self.infcx.clone_opaque_types_for_query_response();
40+
let (goal, opaque_types) =
41+
(goal, opaque_types).fold_with(&mut EagerResolver { infcx: self.infcx });
42+
3543
let mut orig_values = Default::default();
3644
let canonical_goal = Canonicalizer::canonicalize(
3745
self.infcx,
@@ -40,11 +48,9 @@ impl<'tcx> EvalCtxt<'_, 'tcx> {
4048
QueryInput {
4149
goal,
4250
anchor: self.infcx.defining_use_anchor,
43-
predefined_opaques_in_body: self.tcx().mk_predefined_opaques_in_body(
44-
PredefinedOpaquesData {
45-
opaque_types: self.infcx.clone_opaque_types_for_query_response(),
46-
},
47-
),
51+
predefined_opaques_in_body: self
52+
.tcx()
53+
.mk_predefined_opaques_in_body(PredefinedOpaquesData { opaque_types }),
4854
},
4955
);
5056
(orig_values, canonical_goal)
@@ -69,6 +75,7 @@ impl<'tcx> EvalCtxt<'_, 'tcx> {
6975
previous call to `try_evaluate_added_goals!`"
7076
);
7177

78+
let certainty = certainty.unify_with(goals_certainty);
7279
if let Certainty::OVERFLOW = certainty {
7380
// If we have overflow, it's probable that we're substituting a type
7481
// into itself infinitely and any partial substitutions in the query
@@ -84,10 +91,12 @@ impl<'tcx> EvalCtxt<'_, 'tcx> {
8491
return Ok(self.make_ambiguous_response_no_constraints(MaybeCause::Overflow));
8592
}
8693

87-
let certainty = certainty.unify_with(goals_certainty);
8894
let var_values = self.var_values;
8995
let external_constraints = self.compute_external_query_constraints()?;
9096

97+
let (var_values, external_constraints) =
98+
(var_values, external_constraints).fold_with(&mut EagerResolver { infcx: self.infcx });
99+
91100
let canonical = Canonicalizer::canonicalize(
92101
self.infcx,
93102
CanonicalizeMode::Response { max_input_universe: self.max_input_universe },
@@ -334,3 +343,65 @@ impl<'tcx> EvalCtxt<'_, 'tcx> {
334343
Ok(())
335344
}
336345
}
346+
347+
/// Resolves ty, region, and const vars to their inferred values or their root vars.
348+
struct EagerResolver<'a, 'tcx> {
349+
infcx: &'a InferCtxt<'tcx>,
350+
}
351+
352+
impl<'tcx> TypeFolder<TyCtxt<'tcx>> for EagerResolver<'_, 'tcx> {
353+
fn interner(&self) -> TyCtxt<'tcx> {
354+
self.infcx.tcx
355+
}
356+
357+
fn fold_ty(&mut self, t: Ty<'tcx>) -> Ty<'tcx> {
358+
match *t.kind() {
359+
ty::Infer(ty::TyVar(vid)) => match self.infcx.probe_ty_var(vid) {
360+
Ok(t) => t.fold_with(self),
361+
Err(_) => Ty::new_var(self.infcx.tcx, self.infcx.root_var(vid)),
362+
},
363+
ty::Infer(ty::IntVar(vid)) => self.infcx.opportunistic_resolve_int_var(vid),
364+
ty::Infer(ty::FloatVar(vid)) => self.infcx.opportunistic_resolve_float_var(vid),
365+
_ => {
366+
if t.has_infer() {
367+
t.super_fold_with(self)
368+
} else {
369+
t
370+
}
371+
}
372+
}
373+
}
374+
375+
fn fold_region(&mut self, r: ty::Region<'tcx>) -> ty::Region<'tcx> {
376+
match *r {
377+
ty::ReVar(vid) => self
378+
.infcx
379+
.inner
380+
.borrow_mut()
381+
.unwrap_region_constraints()
382+
.opportunistic_resolve_var(self.infcx.tcx, vid),
383+
_ => r,
384+
}
385+
}
386+
387+
fn fold_const(&mut self, c: ty::Const<'tcx>) -> ty::Const<'tcx> {
388+
match c.kind() {
389+
ty::ConstKind::Infer(ty::InferConst::Var(vid)) => {
390+
// FIXME: we need to fold the ty too, I think.
391+
match self.infcx.probe_const_var(vid) {
392+
Ok(c) => c.fold_with(self),
393+
Err(_) => {
394+
ty::Const::new_var(self.infcx.tcx, self.infcx.root_const_var(vid), c.ty())
395+
}
396+
}
397+
}
398+
_ => {
399+
if c.has_infer() {
400+
c.super_fold_with(self)
401+
} else {
402+
c
403+
}
404+
}
405+
}
406+
}
407+
}

0 commit comments

Comments
 (0)