Skip to content

Commit 009a6c1

Browse files
committed
2 parents 7e46c5f + 1a67312 commit 009a6c1

31 files changed

Lines changed: 296 additions & 380 deletions

Cargo.lock

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1296,9 +1296,9 @@ dependencies = [
12961296

12971297
[[package]]
12981298
name = "ena"
1299-
version = "0.14.4"
1299+
version = "0.14.3"
13001300
source = "registry+https://github.com/rust-lang/crates.io-index"
1301-
checksum = "eabffdaee24bd1bf95c5ef7cec31260444317e72ea56c4c91750e8b7ee58d5f1"
1301+
checksum = "3d248bdd43ce613d87415282f69b9bb99d947d290b10962dd6c56233312c2ad5"
13021302
dependencies = [
13031303
"log",
13041304
]

compiler/rustc_data_structures/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ arrayvec = { version = "0.7", default-features = false }
99
bitflags = "2.4.1"
1010
either = "1.0"
1111
elsa = "1.11.0"
12-
ena = "0.14.4"
12+
ena = "0.14.3"
1313
indexmap = "2.12.1"
1414
jobserver_crate = { version = "0.1.28", package = "jobserver" }
1515
measureme = "12.0.1"

compiler/rustc_hir_typeck/src/expr.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1871,7 +1871,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
18711871
if !ocx.try_evaluate_obligations().is_empty() {
18721872
return Err(TypeError::Mismatch);
18731873
}
1874-
Ok(adt_ty)
1874+
Ok(self.resolve_vars_if_possible(adt_ty))
18751875
})
18761876
.ok()
18771877
});

compiler/rustc_hir_typeck/src/fallback.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,11 @@ impl<'tcx> FnCtxt<'_, 'tcx> {
357357
VecGraph::new(num_ty_vars, coercion_edges)
358358
}
359359

360+
/// If `ty` is an unresolved type variable, returns its root vid.
361+
fn root_vid(&self, ty: Ty<'tcx>) -> Option<ty::TyVid> {
362+
Some(self.root_var(self.shallow_resolve(ty).ty_vid()?))
363+
}
364+
360365
/// Given a set of diverging vids and coercions, walk the HIR to gather a
361366
/// set of suggestions which can be applied to preserve fallback to unit.
362367
fn try_to_suggest_annotations(

compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,12 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
276276

277277
// Record all the argument types, with the args
278278
// produced from the above subtyping unification.
279-
Ok(Some(formal_input_tys.to_vec()))
279+
Ok(Some(
280+
formal_input_tys
281+
.iter()
282+
.map(|&ty| self.resolve_vars_if_possible(ty))
283+
.collect(),
284+
))
280285
})
281286
.ok()
282287
})

compiler/rustc_infer/src/infer/mod.rs

Lines changed: 13 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -1099,45 +1099,22 @@ impl<'tcx> InferCtxt<'tcx> {
10991099
//
11001100
// Note: if these two lines are combined into one we get
11011101
// dynamic borrow errors on `self.inner`.
1102-
let (root_vid, value) =
1103-
self.inner.borrow_mut().type_variables().probe_with_root_vid(v);
1104-
value.known().map_or_else(
1105-
|| if root_vid == v { ty } else { Ty::new_var(self.tcx, root_vid) },
1106-
|t| self.shallow_resolve(t),
1107-
)
1102+
let known = self.inner.borrow_mut().type_variables().probe(v).known();
1103+
known.map_or(ty, |t| self.shallow_resolve(t))
11081104
}
11091105

11101106
ty::IntVar(v) => {
1111-
let (root, value) =
1112-
self.inner.borrow_mut().int_unification_table().inlined_probe_key_value(v);
1113-
match value {
1107+
match self.inner.borrow_mut().int_unification_table().probe_value(v) {
11141108
ty::IntVarValue::IntType(ty) => Ty::new_int(self.tcx, ty),
11151109
ty::IntVarValue::UintType(ty) => Ty::new_uint(self.tcx, ty),
1116-
ty::IntVarValue::Unknown => {
1117-
if root == v {
1118-
ty
1119-
} else {
1120-
Ty::new_int_var(self.tcx, root)
1121-
}
1122-
}
1110+
ty::IntVarValue::Unknown => ty,
11231111
}
11241112
}
11251113

11261114
ty::FloatVar(v) => {
1127-
let (root, value) = self
1128-
.inner
1129-
.borrow_mut()
1130-
.float_unification_table()
1131-
.inlined_probe_key_value(v);
1132-
match value {
1115+
match self.inner.borrow_mut().float_unification_table().probe_value(v) {
11331116
ty::FloatVarValue::Known(ty) => Ty::new_float(self.tcx, ty),
1134-
ty::FloatVarValue::Unknown => {
1135-
if root == v {
1136-
ty
1137-
} else {
1138-
Ty::new_float_var(self.tcx, root)
1139-
}
1140-
}
1117+
ty::FloatVarValue::Unknown => ty,
11411118
}
11421119
}
11431120

@@ -1151,16 +1128,13 @@ impl<'tcx> InferCtxt<'tcx> {
11511128
pub fn shallow_resolve_const(&self, ct: ty::Const<'tcx>) -> ty::Const<'tcx> {
11521129
match ct.kind() {
11531130
ty::ConstKind::Infer(infer_ct) => match infer_ct {
1154-
InferConst::Var(vid) => {
1155-
let (root, value) = self
1156-
.inner
1157-
.borrow_mut()
1158-
.const_unification_table()
1159-
.inlined_probe_key_value(vid);
1160-
value.known().unwrap_or_else(|| {
1161-
if root.vid == vid { ct } else { ty::Const::new_var(self.tcx, root.vid) }
1162-
})
1163-
}
1131+
InferConst::Var(vid) => self
1132+
.inner
1133+
.borrow_mut()
1134+
.const_unification_table()
1135+
.probe_value(vid)
1136+
.known()
1137+
.unwrap_or(ct),
11641138
InferConst::Fresh(_) => ct,
11651139
},
11661140
ty::ConstKind::Param(_)
@@ -1184,13 +1158,6 @@ impl<'tcx> InferCtxt<'tcx> {
11841158
self.inner.borrow_mut().type_variables().root_var(var)
11851159
}
11861160

1187-
/// If `ty` is an unresolved type variable, returns its root vid.
1188-
pub fn root_vid(&self, ty: Ty<'tcx>) -> Option<ty::TyVid> {
1189-
let (root, value) =
1190-
self.inner.borrow_mut().type_variables().inlined_probe_with_vid(ty.ty_vid()?);
1191-
value.is_unknown().then_some(root)
1192-
}
1193-
11941161
pub fn sub_unify_ty_vids_raw(&self, a: ty::TyVid, b: ty::TyVid) {
11951162
self.inner.borrow_mut().type_variables().sub_unify(a, b);
11961163
}
@@ -1250,36 +1217,6 @@ impl<'tcx> InferCtxt<'tcx> {
12501217
value.fold_with(&mut r)
12511218
}
12521219

1253-
/// Normally, we shallow-resolve unresolved type variables to their root
1254-
/// variables. This is mainly done for performance reasons, and in most
1255-
/// cases resolving to the root variable (instead of the variable itself)
1256-
/// does not affect type inference.
1257-
///
1258-
/// However, there is an exceptional case: *fudging*. Fudging is intended
1259-
/// to guide inference rather than impose hard requirements. But our current
1260-
/// handling here is somewhat janky.
1261-
///
1262-
/// In particular, inference variables that are considered equal within the
1263-
/// fudging scope may not remain equal outside of it. This makes it observable
1264-
/// which inference variable we resolve to. For backwards compatibility, we
1265-
/// avoid resolving to the root variable by using this function inside the
1266-
/// fudge instead of [`InferCtxt::resolve_vars_if_possible`].
1267-
///
1268-
/// See #153869 for more details.
1269-
pub fn resolve_vars_if_possible_for_fudging<T>(&self, value: T) -> T
1270-
where
1271-
T: TypeFoldable<TyCtxt<'tcx>>,
1272-
{
1273-
if let Err(guar) = value.error_reported() {
1274-
self.set_tainted_by_errors(guar);
1275-
}
1276-
if !value.has_non_region_infer() {
1277-
return value;
1278-
}
1279-
let mut r = resolve::OpportunisticVarResolver::new_for_fudging(self);
1280-
value.fold_with(&mut r)
1281-
}
1282-
12831220
pub fn resolve_numeric_literals_with_default<T>(&self, value: T) -> T
12841221
where
12851222
T: TypeFoldable<TyCtxt<'tcx>>,

compiler/rustc_infer/src/infer/resolve.rs

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,6 @@ use crate::infer::TyOrConstInferVar;
1717
/// points for correctness.
1818
pub struct OpportunisticVarResolver<'a, 'tcx> {
1919
infcx: &'a InferCtxt<'tcx>,
20-
/// If true, we don't resolve ty/const vars to their roots.
21-
/// See comments on [`InferCtxt::resolve_vars_if_possible_for_fudging`]
22-
for_fudging: bool,
2320
/// We're able to use a cache here as the folder does
2421
/// not have any mutable state.
2522
cache: DelayedMap<Ty<'tcx>, Ty<'tcx>>,
@@ -28,12 +25,7 @@ pub struct OpportunisticVarResolver<'a, 'tcx> {
2825
impl<'a, 'tcx> OpportunisticVarResolver<'a, 'tcx> {
2926
#[inline]
3027
pub fn new(infcx: &'a InferCtxt<'tcx>) -> Self {
31-
OpportunisticVarResolver { infcx, for_fudging: false, cache: Default::default() }
32-
}
33-
34-
#[inline]
35-
pub fn new_for_fudging(infcx: &'a InferCtxt<'tcx>) -> Self {
36-
OpportunisticVarResolver { infcx, for_fudging: true, cache: Default::default() }
28+
OpportunisticVarResolver { infcx, cache: Default::default() }
3729
}
3830
}
3931

@@ -51,7 +43,6 @@ impl<'a, 'tcx> TypeFolder<TyCtxt<'tcx>> for OpportunisticVarResolver<'a, 'tcx> {
5143
} else {
5244
let shallow = self.infcx.shallow_resolve(t);
5345
let res = shallow.super_fold_with(self);
54-
let res = if self.for_fudging && res.is_ty_var() { t } else { res };
5546
assert!(self.cache.insert(t, res));
5647
res
5748
}
@@ -61,11 +52,8 @@ impl<'a, 'tcx> TypeFolder<TyCtxt<'tcx>> for OpportunisticVarResolver<'a, 'tcx> {
6152
if !ct.has_non_region_infer() {
6253
ct // micro-optimize -- if there is nothing in this const that this fold affects...
6354
} else {
64-
let res = self.infcx.shallow_resolve_const(ct);
65-
if self.for_fudging && res.is_ct_infer() {
66-
return ct;
67-
};
68-
res.super_fold_with(self)
55+
let ct = self.infcx.shallow_resolve_const(ct);
56+
ct.super_fold_with(self)
6957
}
7058
}
7159

compiler/rustc_infer/src/infer/snapshot/fudge.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ impl<'tcx> InferCtxt<'tcx> {
101101
// going to be popped, so we will have to
102102
// eliminate any references to them.
103103
let snapshot_vars = SnapshotVarData::new(self, variable_lengths);
104-
Ok((snapshot_vars, self.resolve_vars_if_possible_for_fudging(value)))
104+
Ok((snapshot_vars, self.resolve_vars_if_possible(value)))
105105
})?;
106106

107107
// At this point, we need to replace any of the now-popped

compiler/rustc_infer/src/infer/type_variable.rs

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -258,25 +258,6 @@ impl<'tcx> TypeVariableTable<'_, 'tcx> {
258258
self.eq_relations().inlined_probe_value(vid)
259259
}
260260

261-
/// Retrieves the type to which `vid` has been instantiated, if
262-
/// any, along with the root `vid`.
263-
pub(crate) fn probe_with_root_vid(
264-
&mut self,
265-
vid: ty::TyVid,
266-
) -> (ty::TyVid, TypeVariableValue<'tcx>) {
267-
self.inlined_probe_with_vid(vid)
268-
}
269-
270-
/// An always-inlined variant of `probe_with_root_vid`, for very hot call sites.
271-
#[inline(always)]
272-
pub(crate) fn inlined_probe_with_vid(
273-
&mut self,
274-
vid: ty::TyVid,
275-
) -> (ty::TyVid, TypeVariableValue<'tcx>) {
276-
let (id, value) = self.eq_relations().inlined_probe_key_value(vid);
277-
(id.vid, value)
278-
}
279-
280261
#[inline]
281262
fn eq_relations(&mut self) -> super::UnificationTable<'_, 'tcx, TyVidEqKey<'tcx>> {
282263
self.storage.eq_relations.with_log(self.undo_log)

compiler/rustc_type_ir/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ edition = "2024"
88
arrayvec = { version = "0.7", default-features = false }
99
bitflags = "2.4.1"
1010
derive-where = "1.6.1"
11-
ena = "0.14.4"
11+
ena = "0.14.3"
1212
indexmap = "2.0.0"
1313
rustc-hash = "2.0.0"
1414
rustc_abi = { path = "../rustc_abi", default-features = false }

0 commit comments

Comments
 (0)