Skip to content

Commit 34258d6

Browse files
committed
Add a dedicated function for the common unify_and(identity) case
1 parent 804b6c9 commit 34258d6

File tree

1 file changed

+15
-14
lines changed

1 file changed

+15
-14
lines changed

Diff for: compiler/rustc_hir_typeck/src/coercion.rs

+15-14
Original file line numberDiff line numberDiff line change
@@ -103,11 +103,6 @@ fn coerce_mutbls<'tcx>(
103103
if from_mutbl >= to_mutbl { Ok(()) } else { Err(TypeError::Mutability) }
104104
}
105105

106-
/// Do not require any adjustments, i.e. coerce `x -> x`.
107-
fn identity(_: Ty<'_>) -> Vec<Adjustment<'_>> {
108-
vec![]
109-
}
110-
111106
fn simple<'tcx>(kind: Adjust) -> impl FnOnce(Ty<'tcx>) -> Vec<Adjustment<'tcx>> {
112107
move |target| vec![Adjustment { kind, target }]
113108
}
@@ -161,6 +156,12 @@ impl<'f, 'tcx> Coerce<'f, 'tcx> {
161156
})
162157
}
163158

159+
/// Unify two types (using sub or lub).
160+
fn unify(&self, a: Ty<'tcx>, b: Ty<'tcx>) -> CoerceResult<'tcx> {
161+
self.unify_raw(a, b)
162+
.and_then(|InferOk { value: ty, obligations }| success(vec![], ty, obligations))
163+
}
164+
164165
/// Unify two types (using sub or lub) and produce a specific coercion.
165166
fn unify_and<F>(&self, a: Ty<'tcx>, b: Ty<'tcx>, f: F) -> CoerceResult<'tcx>
166167
where
@@ -183,7 +184,7 @@ impl<'f, 'tcx> Coerce<'f, 'tcx> {
183184
return success(simple(Adjust::NeverToAny)(b), b, PredicateObligations::new());
184185
} else {
185186
// Otherwise the only coercion we can do is unification.
186-
return self.unify_and(a, b, identity);
187+
return self.unify(a, b);
187188
}
188189
}
189190

@@ -257,7 +258,7 @@ impl<'f, 'tcx> Coerce<'f, 'tcx> {
257258
}
258259
_ => {
259260
// Otherwise, just use unification rules.
260-
self.unify_and(a, b, identity)
261+
self.unify(a, b)
261262
}
262263
}
263264
}
@@ -297,7 +298,7 @@ impl<'f, 'tcx> Coerce<'f, 'tcx> {
297298
} else {
298299
// One unresolved type variable: just apply subtyping, we may be able
299300
// to do something useful.
300-
self.unify_and(a, b, identity)
301+
self.unify(a, b)
301302
}
302303
}
303304

@@ -325,7 +326,7 @@ impl<'f, 'tcx> Coerce<'f, 'tcx> {
325326
coerce_mutbls(mt_a.mutbl, mutbl_b)?;
326327
(r_a, mt_a)
327328
}
328-
_ => return self.unify_and(a, b, identity),
329+
_ => return self.unify(a, b),
329330
};
330331

331332
let span = self.cause.span;
@@ -702,7 +703,7 @@ impl<'f, 'tcx> Coerce<'f, 'tcx> {
702703
&& let ty::Dynamic(b_data, _, ty::DynStar) = b.kind()
703704
&& a_data.principal_def_id() == b_data.principal_def_id()
704705
{
705-
return self.unify_and(a, b, identity);
706+
return self.unify(a, b);
706707
}
707708

708709
// Check the obligations of the cast -- for example, when casting
@@ -917,7 +918,7 @@ impl<'f, 'tcx> Coerce<'f, 'tcx> {
917918
obligations.extend(o2);
918919
Ok(InferOk { value, obligations })
919920
}
920-
_ => self.unify_and(a, b, identity),
921+
_ => self.unify(a, b),
921922
}
922923
}
923924

@@ -964,7 +965,7 @@ impl<'f, 'tcx> Coerce<'f, 'tcx> {
964965
simple(Adjust::Pointer(PointerCoercion::ClosureFnPointer(safety))),
965966
)
966967
}
967-
_ => self.unify_and(a, b, identity),
968+
_ => self.unify(a, b),
968969
}
969970
}
970971

@@ -979,7 +980,7 @@ impl<'f, 'tcx> Coerce<'f, 'tcx> {
979980
let (is_ref, mt_a) = match *a.kind() {
980981
ty::Ref(_, ty, mutbl) => (true, ty::TypeAndMut { ty, mutbl }),
981982
ty::RawPtr(ty, mutbl) => (false, ty::TypeAndMut { ty, mutbl }),
982-
_ => return self.unify_and(a, b, identity),
983+
_ => return self.unify(a, b),
983984
};
984985
coerce_mutbls(mt_a.mutbl, mutbl_b)?;
985986

@@ -998,7 +999,7 @@ impl<'f, 'tcx> Coerce<'f, 'tcx> {
998999
} else if mt_a.mutbl != mutbl_b {
9991000
self.unify_and(a_raw, b, simple(Adjust::Pointer(PointerCoercion::MutToConstPointer)))
10001001
} else {
1001-
self.unify_and(a_raw, b, identity)
1002+
self.unify(a_raw, b)
10021003
}
10031004
}
10041005
}

0 commit comments

Comments
 (0)