Skip to content

Commit 2c123f5

Browse files
committed
review
1 parent 76283ec commit 2c123f5

File tree

12 files changed

+43
-28
lines changed

12 files changed

+43
-28
lines changed

compiler/rustc_infer/src/infer/canonical/canonicalizer.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,11 @@ struct Canonicalizer<'cx, 'tcx> {
294294
// Note that indices is only used once `var_values` is big enough to be
295295
// heap-allocated.
296296
indices: FxHashMap<GenericArg<'tcx>, BoundVar>,
297+
/// Maps each `sub_relations_root_var` to the index of the first variable which used it.
298+
///
299+
/// This means in case two type variables have the same sub relations root,
300+
/// we set the `sub_root` of the second variable to the position of the first.
301+
/// Otherwise the `sub_root` of each type variable is just its own position.
297302
sub_root_lookup_table: SsoHashMap<ty::TyVid, usize>,
298303
canonicalize_mode: &'cx dyn CanonicalizeMode,
299304
needs_canonical_flags: TypeFlags,
@@ -662,7 +667,7 @@ impl<'cx, 'tcx> Canonicalizer<'cx, 'tcx> {
662667
}
663668

664669
fn get_or_insert_sub_root(&mut self, vid: ty::TyVid) -> ty::BoundVar {
665-
let root_vid = self.infcx.unwrap().sub_root_var(vid);
670+
let root_vid = self.infcx.unwrap().sub_relations_root_var(vid);
666671
let idx =
667672
*self.sub_root_lookup_table.entry(root_vid).or_insert_with(|| self.variables.len());
668673
ty::BoundVar::from(idx)

compiler/rustc_infer/src/infer/canonical/mod.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,10 +93,11 @@ impl<'tcx> InferCtxt<'tcx> {
9393
match kind {
9494
CanonicalVarKind::Ty { ui, sub_root } => {
9595
let vid = self.next_ty_vid_in_universe(span, universe_map(ui));
96-
// Fetch the `sub_root` in case it exists.
96+
// If this inference variable is related to an earlier variable
97+
// via subtyping, we need to add that info to the inference context.
9798
if let Some(prev) = previous_var_values.get(sub_root.as_usize()) {
9899
if let &ty::Infer(ty::TyVar(sub_root)) = prev.expect_ty().kind() {
99-
self.inner.borrow_mut().type_variables().sub(vid, sub_root);
100+
self.sub_relate_ty_vids_raw(vid, sub_root);
100101
} else {
101102
unreachable!()
102103
}

compiler/rustc_infer/src/infer/context.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,8 @@ impl<'tcx> rustc_type_ir::InferCtxtLike for InferCtxt<'tcx> {
5959
self.root_var(var)
6060
}
6161

62-
fn sub_root_ty_var(&self, var: ty::TyVid) -> ty::TyVid {
63-
self.sub_root_var(var)
62+
fn sub_relations_root_var(&self, var: ty::TyVid) -> ty::TyVid {
63+
self.sub_relations_root_var(var)
6464
}
6565

6666
fn root_const_var(&self, var: ty::ConstVid) -> ty::ConstVid {
@@ -183,8 +183,8 @@ impl<'tcx> rustc_type_ir::InferCtxtLike for InferCtxt<'tcx> {
183183
self.inner.borrow_mut().type_variables().equate(a, b);
184184
}
185185

186-
fn sub_ty_vids_raw(&self, a: ty::TyVid, b: ty::TyVid) {
187-
self.sub_ty_vids_raw(a, b);
186+
fn sub_relate_ty_vids_raw(&self, a: ty::TyVid, b: ty::TyVid) {
187+
self.sub_relate_ty_vids_raw(a, b);
188188
}
189189

190190
fn equate_int_vids_raw(&self, a: ty::IntVid, b: ty::IntVid) {

compiler/rustc_infer/src/infer/mod.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -764,7 +764,7 @@ impl<'tcx> InferCtxt<'tcx> {
764764
let r_b = self.shallow_resolve(predicate.skip_binder().b);
765765
match (r_a.kind(), r_b.kind()) {
766766
(&ty::Infer(ty::TyVar(a_vid)), &ty::Infer(ty::TyVar(b_vid))) => {
767-
self.sub_ty_vids_raw(a_vid, b_vid);
767+
self.sub_relate_ty_vids_raw(a_vid, b_vid);
768768
return Err((a_vid, b_vid));
769769
}
770770
_ => {}
@@ -1129,12 +1129,12 @@ impl<'tcx> InferCtxt<'tcx> {
11291129
self.inner.borrow_mut().type_variables().root_var(var)
11301130
}
11311131

1132-
pub fn sub_ty_vids_raw(&self, a: ty::TyVid, b: ty::TyVid) {
1133-
self.inner.borrow_mut().type_variables().sub(a, b);
1132+
pub fn sub_relate_ty_vids_raw(&self, a: ty::TyVid, b: ty::TyVid) {
1133+
self.inner.borrow_mut().type_variables().sub_relate(a, b);
11341134
}
11351135

1136-
pub fn sub_root_var(&self, var: ty::TyVid) -> ty::TyVid {
1137-
self.inner.borrow_mut().type_variables().sub_root_var(var)
1136+
pub fn sub_relations_root_var(&self, var: ty::TyVid) -> ty::TyVid {
1137+
self.inner.borrow_mut().type_variables().sub_relations_root_var(var)
11381138
}
11391139

11401140
pub fn root_const_var(&self, var: ty::ConstVid) -> ty::ConstVid {

compiler/rustc_infer/src/infer/relate/generalize.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -522,7 +522,7 @@ impl<'tcx> TypeRelation<TyCtxt<'tcx>> for Generalizer<'_, 'tcx> {
522522
// Record that `vid` and `new_var_id` have to be subtypes
523523
// of each other. This is currently only used for diagnostics.
524524
// To see why, see the docs in the `type_variables` module.
525-
inner.type_variables().sub(vid, new_var_id);
525+
inner.type_variables().sub_relate(vid, new_var_id);
526526
// If we're in the new solver and create a new inference
527527
// variable inside of an alias we eagerly constrain that
528528
// inference variable to prevent unexpected ambiguity errors.

compiler/rustc_infer/src/infer/type_variable.rs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,9 @@ pub(crate) struct TypeVariableStorage<'tcx> {
6363
/// constraint `?X == ?Y`. This table also stores, for each key,
6464
/// the known value.
6565
eq_relations: ut::UnificationTableStorage<TyVidEqKey<'tcx>>,
66-
/// Only used by `-Znext-solver` and for diagnostics.
66+
/// Only used by `-Znext-solver` and for diagnostics. Tracks whether
67+
/// type variables are related via subtyping at all, ignoring which of
68+
/// the two is the subtype.
6769
///
6870
/// When reporting ambiguity errors, we sometimes want to
6971
/// treat all inference vars which are subtypes of each
@@ -155,7 +157,7 @@ impl<'tcx> TypeVariableTable<'_, 'tcx> {
155157
self.storage.values[vid].origin
156158
}
157159

158-
/// Records that `a == b`, depending on `dir`.
160+
/// Records that `a == b`.
159161
///
160162
/// Precondition: neither `a` nor `b` are known.
161163
pub(crate) fn equate(&mut self, a: ty::TyVid, b: ty::TyVid) {
@@ -165,10 +167,11 @@ impl<'tcx> TypeVariableTable<'_, 'tcx> {
165167
self.sub_relations().union(a, b);
166168
}
167169

168-
/// Records that `a <: b`, depending on `dir`.
170+
/// Records that `a` and `b` are related via subtyping. We don't track
171+
/// which of the two is the subtype.
169172
///
170173
/// Precondition: neither `a` nor `b` are known.
171-
pub(crate) fn sub(&mut self, a: ty::TyVid, b: ty::TyVid) {
174+
pub(crate) fn sub_relate(&mut self, a: ty::TyVid, b: ty::TyVid) {
172175
debug_assert!(self.probe(a).is_unknown());
173176
debug_assert!(self.probe(b).is_unknown());
174177
self.sub_relations().union(a, b);
@@ -234,12 +237,12 @@ impl<'tcx> TypeVariableTable<'_, 'tcx> {
234237
/// Returns the "root" variable of `vid` in the `sub_relations`
235238
/// equivalence table. All type variables that have been are
236239
/// related via equality or subtyping will yield the same root
237-
/// variable (per the union-find algorithm), so `sub_root_var(a)
238-
/// == sub_root_var(b)` implies that:
240+
/// variable (per the union-find algorithm), so `sub_relations_root_var(a)
241+
/// == sub_relations_root_var(b)` implies that:
239242
/// ```text
240243
/// exists X. (a <: X || X <: a) && (b <: X || X <: b)
241244
/// ```
242-
pub(crate) fn sub_root_var(&mut self, vid: ty::TyVid) -> ty::TyVid {
245+
pub(crate) fn sub_relations_root_var(&mut self, vid: ty::TyVid) -> ty::TyVid {
243246
self.sub_relations().find(vid).vid
244247
}
245248

compiler/rustc_next_trait_solver/src/canonicalizer.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,11 @@ pub struct Canonicalizer<'a, D: SolverDelegate<Interner = I>, I: Interner> {
6767
variables: &'a mut Vec<I::GenericArg>,
6868
var_kinds: Vec<CanonicalVarKind<I>>,
6969
variable_lookup_table: HashMap<I::GenericArg, usize>,
70+
/// Maps each `sub_relations_root_var` to the index of the first variable which used it.
71+
///
72+
/// This means in case two type variables have the same sub relations root,
73+
/// we set the `sub_root` of the second variable to the position of the first.
74+
/// Otherwise the `sub_root` of each type variable is just its own position.
7075
sub_root_lookup_table: HashMap<ty::TyVid, usize>,
7176
binder_index: ty::DebruijnIndex,
7277

@@ -273,7 +278,7 @@ impl<'a, D: SolverDelegate<Interner = I>, I: Interner> Canonicalizer<'a, D, I> {
273278
}
274279

275280
fn get_or_insert_sub_root(&mut self, vid: ty::TyVid) -> ty::BoundVar {
276-
let root_vid = self.delegate.sub_root_ty_var(vid);
281+
let root_vid = self.delegate.sub_relations_root_var(vid);
277282
let idx =
278283
*self.sub_root_lookup_table.entry(root_vid).or_insert_with(|| self.variables.len());
279284
ty::BoundVar::from(idx)

compiler/rustc_next_trait_solver/src/solve/eval_ctxt/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -900,8 +900,8 @@ where
900900
&& goal.param_env.visit_with(&mut visitor).is_continue()
901901
}
902902

903-
pub(super) fn sub_ty_vids_raw(&self, a: ty::TyVid, b: ty::TyVid) {
904-
self.delegate.sub_ty_vids_raw(a, b)
903+
pub(super) fn sub_relate_ty_vids_raw(&self, a: ty::TyVid, b: ty::TyVid) {
904+
self.delegate.sub_relate_ty_vids_raw(a, b)
905905
}
906906

907907
#[instrument(level = "trace", skip(self, param_env), ret)]

compiler/rustc_next_trait_solver/src/solve/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ where
121121
fn compute_subtype_goal(&mut self, goal: Goal<I, ty::SubtypePredicate<I>>) -> QueryResult<I> {
122122
match (goal.predicate.a.kind(), goal.predicate.b.kind()) {
123123
(ty::Infer(ty::TyVar(a_vid)), ty::Infer(ty::TyVar(b_vid))) => {
124-
self.sub_ty_vids_raw(a_vid, b_vid);
124+
self.sub_relate_ty_vids_raw(a_vid, b_vid);
125125
self.evaluate_added_goals_and_make_canonical_response(Certainty::AMBIGUOUS)
126126
}
127127
_ => {

compiler/rustc_trait_selection/src/error_reporting/infer/need_type_info.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -894,7 +894,8 @@ impl<'a, 'tcx> FindInferSourceVisitor<'a, 'tcx> {
894894
use ty::{Infer, TyVar};
895895
match (inner_ty.kind(), target_ty.kind()) {
896896
(&Infer(TyVar(a_vid)), &Infer(TyVar(b_vid))) => {
897-
self.tecx.sub_root_var(a_vid) == self.tecx.sub_root_var(b_vid)
897+
self.tecx.sub_relations_root_var(a_vid)
898+
== self.tecx.sub_relations_root_var(b_vid)
898899
}
899900
_ => false,
900901
}

0 commit comments

Comments
 (0)