Skip to content

Commit b51a3a5

Browse files
committed
review
1 parent f514586 commit b51a3a5

File tree

12 files changed

+57
-40
lines changed

12 files changed

+57
-40
lines changed

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,12 @@ 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_unification_table_root_var` to the index of the first
298+
/// variable which used it.
299+
///
300+
/// This means in case two type variables have the same sub relations root,
301+
/// we set the `sub_root` of the second variable to the position of the first.
302+
/// Otherwise the `sub_root` of each type variable is just its own position.
297303
sub_root_lookup_table: SsoHashMap<ty::TyVid, usize>,
298304
canonicalize_mode: &'cx dyn CanonicalizeMode,
299305
needs_canonical_flags: TypeFlags,
@@ -662,7 +668,7 @@ impl<'cx, 'tcx> Canonicalizer<'cx, 'tcx> {
662668
}
663669

664670
fn get_or_insert_sub_root(&mut self, vid: ty::TyVid) -> ty::BoundVar {
665-
let root_vid = self.infcx.unwrap().sub_root_var(vid);
671+
let root_vid = self.infcx.unwrap().sub_unification_table_root_var(vid);
666672
let idx =
667673
*self.sub_root_lookup_table.entry(root_vid).or_insert_with(|| self.variables.len());
668674
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_unify_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_unification_table_root_var(&self, var: ty::TyVid) -> ty::TyVid {
63+
self.sub_unification_table_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_unify_ty_vids_raw(&self, a: ty::TyVid, b: ty::TyVid) {
187+
self.sub_unify_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_unify_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_unify_ty_vids_raw(&self, a: ty::TyVid, b: ty::TyVid) {
1133+
self.inner.borrow_mut().type_variables().sub_unify(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_unification_table_root_var(&self, var: ty::TyVid) -> ty::TyVid {
1137+
self.inner.borrow_mut().type_variables().sub_unification_table_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_unify(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: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -42,15 +42,15 @@ impl<'tcx> Rollback<sv::UndoLog<ut::Delegate<TyVidEqKey<'tcx>>>> for TypeVariabl
4242

4343
impl<'tcx> Rollback<sv::UndoLog<ut::Delegate<TyVidSubKey>>> for TypeVariableStorage<'tcx> {
4444
fn reverse(&mut self, undo: sv::UndoLog<ut::Delegate<TyVidSubKey>>) {
45-
self.sub_relations.reverse(undo)
45+
self.sub_unification_table.reverse(undo)
4646
}
4747
}
4848

4949
impl<'tcx> Rollback<UndoLog<'tcx>> for TypeVariableStorage<'tcx> {
5050
fn reverse(&mut self, undo: UndoLog<'tcx>) {
5151
match undo {
5252
UndoLog::EqRelation(undo) => self.eq_relations.reverse(undo),
53-
UndoLog::SubRelation(undo) => self.sub_relations.reverse(undo),
53+
UndoLog::SubRelation(undo) => self.sub_unification_table.reverse(undo),
5454
}
5555
}
5656
}
@@ -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
@@ -79,7 +81,7 @@ pub(crate) struct TypeVariableStorage<'tcx> {
7981
/// Even for something like `let x = returns_arg(); x.method();` the
8082
/// type of `x` is only a supertype of the argument of `returns_arg`. We
8183
/// still want to suggest specifying the type of the argument.
82-
sub_relations: ut::UnificationTableStorage<TyVidSubKey>,
84+
sub_unification_table: ut::UnificationTableStorage<TyVidSubKey>,
8385
}
8486

8587
pub(crate) struct TypeVariableTable<'a, 'tcx> {
@@ -155,23 +157,24 @@ 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) {
162164
debug_assert!(self.probe(a).is_unknown());
163165
debug_assert!(self.probe(b).is_unknown());
164166
self.eq_relations().union(a, b);
165-
self.sub_relations().union(a, b);
167+
self.sub_unification_table().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_unify(&mut self, a: ty::TyVid, b: ty::TyVid) {
172175
debug_assert!(self.probe(a).is_unknown());
173176
debug_assert!(self.probe(b).is_unknown());
174-
self.sub_relations().union(a, b);
177+
self.sub_unification_table().union(a, b);
175178
}
176179

177180
/// Instantiates `vid` with the type `ty`.
@@ -206,7 +209,7 @@ impl<'tcx> TypeVariableTable<'_, 'tcx> {
206209
) -> ty::TyVid {
207210
let eq_key = self.eq_relations().new_key(TypeVariableValue::Unknown { universe });
208211

209-
let sub_key = self.sub_relations().new_key(());
212+
let sub_key = self.sub_unification_table().new_key(());
210213
debug_assert_eq!(eq_key.vid, sub_key.vid);
211214

212215
let index = self.storage.values.push(TypeVariableData { origin });
@@ -231,16 +234,16 @@ impl<'tcx> TypeVariableTable<'_, 'tcx> {
231234
self.eq_relations().find(vid).vid
232235
}
233236

234-
/// Returns the "root" variable of `vid` in the `sub_relations`
235-
/// equivalence table. All type variables that have been are
236-
/// 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:
237+
/// Returns the "root" variable of `vid` in the `sub_unification_table`
238+
/// equivalence table. All type variables that have been are related via
239+
/// equality or subtyping will yield the same root variable (per the
240+
/// union-find algorithm), so `sub_unification_table_root_var(a)
241+
/// == sub_unification_table_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 {
243-
self.sub_relations().find(vid).vid
245+
pub(crate) fn sub_unification_table_root_var(&mut self, vid: ty::TyVid) -> ty::TyVid {
246+
self.sub_unification_table().find(vid).vid
244247
}
245248

246249
/// Retrieves the type to which `vid` has been instantiated, if
@@ -261,8 +264,8 @@ impl<'tcx> TypeVariableTable<'_, 'tcx> {
261264
}
262265

263266
#[inline]
264-
fn sub_relations(&mut self) -> super::UnificationTable<'_, 'tcx, TyVidSubKey> {
265-
self.storage.sub_relations.with_log(self.undo_log)
267+
fn sub_unification_table(&mut self) -> super::UnificationTable<'_, 'tcx, TyVidSubKey> {
268+
self.storage.sub_unification_table.with_log(self.undo_log)
266269
}
267270

268271
/// Returns a range of the type variables created during the snapshot.

compiler/rustc_next_trait_solver/src/canonicalizer.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,12 @@ 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_unification_table_root_var` to the index of the first
71+
/// variable which used it.
72+
///
73+
/// This means in case two type variables have the same sub relations root,
74+
/// we set the `sub_root` of the second variable to the position of the first.
75+
/// Otherwise the `sub_root` of each type variable is just its own position.
7076
sub_root_lookup_table: HashMap<ty::TyVid, usize>,
7177
binder_index: ty::DebruijnIndex,
7278

@@ -273,7 +279,7 @@ impl<'a, D: SolverDelegate<Interner = I>, I: Interner> Canonicalizer<'a, D, I> {
273279
}
274280

275281
fn get_or_insert_sub_root(&mut self, vid: ty::TyVid) -> ty::BoundVar {
276-
let root_vid = self.delegate.sub_root_ty_var(vid);
282+
let root_vid = self.delegate.sub_unification_table_root_var(vid);
277283
let idx =
278284
*self.sub_root_lookup_table.entry(root_vid).or_insert_with(|| self.variables.len());
279285
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_unify_ty_vids_raw(&self, a: ty::TyVid, b: ty::TyVid) {
904+
self.delegate.sub_unify_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_unify_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_unification_table_root_var(a_vid)
898+
== self.tecx.sub_unification_table_root_var(b_vid)
898899
}
899900
_ => false,
900901
}

0 commit comments

Comments
 (0)