Skip to content

Commit a3048bf

Browse files
committed
lub pairs of closure before deciding to coerce to fnptrs
1 parent 7ab55ed commit a3048bf

File tree

3 files changed

+116
-47
lines changed

3 files changed

+116
-47
lines changed

compiler/rustc_hir_typeck/src/coercion.rs

Lines changed: 43 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1093,12 +1093,6 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
10931093
exprs.len()
10941094
);
10951095

1096-
// The following check fixes #88097, where the compiler erroneously
1097-
// attempted to coerce a closure type to itself via a function pointer.
1098-
if prev_ty == new_ty {
1099-
return Ok(prev_ty);
1100-
}
1101-
11021096
let is_force_inline = |ty: Ty<'tcx>| {
11031097
if let ty::FnDef(did, _) = ty.kind() {
11041098
matches!(self.tcx.codegen_fn_attrs(did).inline, InlineAttr::Force { .. })
@@ -1123,39 +1117,49 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
11231117
if is_capturing_closure(prev_ty) || is_capturing_closure(new_ty) {
11241118
(None, None)
11251119
} else {
1120+
let lubbed_tys = || self.commit_if_ok(|snapshot| {
1121+
let outer_universe = self.infcx.universe();
1122+
1123+
// We need to eagerly handle nested obligations due to lazy norm.
1124+
let result = if self.next_trait_solver() {
1125+
let ocx = ObligationCtxt::new(self);
1126+
let value = ocx.lub(cause, self.param_env, prev_ty, new_ty)?;
1127+
if ocx.select_where_possible().is_empty() {
1128+
Ok(InferOk { value, obligations: ocx.into_pending_obligations() })
1129+
} else {
1130+
Err(TypeError::Mismatch)
1131+
}
1132+
} else {
1133+
self.at(cause, self.param_env).lub(prev_ty, new_ty)
1134+
};
1135+
1136+
self.leak_check(outer_universe, Some(snapshot))?;
1137+
result
1138+
});
1139+
11261140
match (prev_ty.kind(), new_ty.kind()) {
1127-
(ty::FnDef(..), ty::FnDef(..)) => {
1128-
// Don't reify if the function types have a LUB, i.e., they
1129-
// are the same function and their parameters have a LUB.
1130-
match self.commit_if_ok(|snapshot| {
1131-
let outer_universe = self.infcx.universe();
1132-
1133-
// We need to eagerly handle nested obligations due to lazy norm.
1134-
let result = if self.next_trait_solver() {
1135-
let ocx = ObligationCtxt::new(self);
1136-
let value = ocx.lub(cause, self.param_env, prev_ty, new_ty)?;
1137-
if ocx.select_where_possible().is_empty() {
1138-
Ok(InferOk {
1139-
value,
1140-
obligations: ocx.into_pending_obligations(),
1141-
})
1142-
} else {
1143-
Err(TypeError::Mismatch)
1144-
}
1145-
} else {
1146-
self.at(cause, self.param_env).lub(prev_ty, new_ty)
1147-
};
1148-
1149-
self.leak_check(outer_universe, Some(snapshot))?;
1150-
result
1151-
}) {
1152-
// We have a LUB of prev_ty and new_ty, just return it.
1153-
Ok(ok) => return Ok(self.register_infer_ok_obligations(ok)),
1154-
Err(_) => {
1155-
(Some(prev_ty.fn_sig(self.tcx)), Some(new_ty.fn_sig(self.tcx)))
1156-
}
1141+
// Don't coerce pairs of fndefs or pairs of closures to fn ptrs
1142+
// if they can just be lubbed.
1143+
//
1144+
// See #88097 or `lub_closures_before_fnptr_coercion.rs` for where
1145+
// we would erroneously coerce closures to fnptrs when attempting to
1146+
// coerce a closure to itself.
1147+
(ty::FnDef(..), ty::FnDef(..)) => match lubbed_tys() {
1148+
Ok(ok) => return Ok(self.register_infer_ok_obligations(ok)),
1149+
Err(_) => (Some(prev_ty.fn_sig(self.tcx)), Some(new_ty.fn_sig(self.tcx))),
1150+
},
1151+
(ty::Closure(_, args_a), ty::Closure(_, args_b)) => match lubbed_tys() {
1152+
Ok(ok) => return Ok(self.register_infer_ok_obligations(ok)),
1153+
Err(_) => {
1154+
let a_sig = self
1155+
.tcx
1156+
.signature_unclosure(args_a.as_closure().sig(), hir::Safety::Safe);
1157+
let b_sig = self
1158+
.tcx
1159+
.signature_unclosure(args_b.as_closure().sig(), hir::Safety::Safe);
1160+
(Some(a_sig), Some(b_sig))
11571161
}
1158-
}
1162+
},
11591163
(ty::Closure(_, args), ty::FnDef(..)) => {
11601164
let b_sig = new_ty.fn_sig(self.tcx);
11611165
let a_sig =
@@ -1168,16 +1172,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
11681172
self.tcx.signature_unclosure(args.as_closure().sig(), a_sig.safety());
11691173
(Some(a_sig), Some(b_sig))
11701174
}
1171-
(ty::Closure(_, args_a), ty::Closure(_, args_b)) => (
1172-
Some(
1173-
self.tcx
1174-
.signature_unclosure(args_a.as_closure().sig(), hir::Safety::Safe),
1175-
),
1176-
Some(
1177-
self.tcx
1178-
.signature_unclosure(args_b.as_closure().sig(), hir::Safety::Safe),
1179-
),
1180-
),
1175+
// ty::FnPtr x ty::FnPtr is fine to just be handled through a normal `unify`
1176+
// call using `lub` which is what will happen on the normal path.
11811177
_ => (None, None),
11821178
}
11831179
}

tests/ui/coercion/issue-88097.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
// behavior has been fixed.
44

55
//@ check-pass
6+
//@ revisions: current next
7+
//@ ignore-compare-mode-next-solver (explicit revisions)
8+
//@[next] compile-flags: -Znext-solver
69

710
fn peculiar() -> impl Fn(u8) -> u8 {
811
return |x| x + 1
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
//@ check-pass
2+
//@ compile-flags: -Znext-solver
3+
4+
#![feature(type_alias_impl_trait)]
5+
6+
// Test that when lubbing two equal closure tys with different
7+
// structural identities (i.e. `PartialEq::eq` on `ty::Ty` would be false)
8+
// we don't coerce-lub to a fnptr.
9+
//
10+
// Most of this test is involved jank to be able to leak the hidden type
11+
// of an opaque with a hidden type of `Closure<C1, C2>`. This then allows
12+
// us to substitute `C1` and `C2` for arbitrary types in the parent scope.
13+
//
14+
// See: <https://github.com/lcnr/random-rust-snippets/issues/13>
15+
16+
struct WaddupGamers<T, U>(Option<T>, U);
17+
impl<T: Leak<Unpin = U>, U> Unpin for WaddupGamers<T, U> {}
18+
unsafe impl<T: Leak<Send = U>, U> Send for WaddupGamers<T, U> {}
19+
pub trait Leak {
20+
type Unpin;
21+
type Send;
22+
}
23+
impl<T> Leak for (T,) {
24+
type Unpin = T;
25+
type Send = T;
26+
}
27+
fn define<C1, C2>() -> impl Sized {
28+
WaddupGamers(None::<C1>, || ())
29+
}
30+
31+
fn require_unpin<T: Unpin>(_: T) {}
32+
fn require_send<T: Send>(_: T) {}
33+
fn mk<T>() -> T { todo!() }
34+
35+
type NameMe<T> = impl Sized;
36+
type NameMe2<T> = impl Sized;
37+
38+
#[define_opaque(NameMe, NameMe2)]
39+
fn leak<T>()
40+
where
41+
T: Leak<Unpin = NameMe<T>, Send = NameMe2<T>>,
42+
{
43+
require_unpin(define::<T, for<'a> fn(&'a ())>());
44+
require_send(define::<T, for<'a> fn(&'a ())>());
45+
46+
// This is the actual logic for lubbing two closures
47+
// with syntactically different `ty::Ty`s:
48+
49+
// lhs: Closure<C1=T, C2=for<'a1> fn(&'a1 ())>
50+
let lhs = mk::<NameMe<T>>();
51+
// lhs: Closure<C1=T, C2=for<'a2> fn(&'a2 ())>
52+
let rhs = mk::<NameMe2<T>>();
53+
54+
macro_rules! lub {
55+
($lhs:expr, $rhs:expr) => {
56+
if true { $lhs } else { $rhs }
57+
};
58+
}
59+
60+
// Lubbed to either:
61+
// - `Closure<C1=T, C2=for<'a> fn(&'a ())>`
62+
// - `fn(&())`
63+
let lubbed = lub!(lhs, rhs);
64+
65+
// Use transmute to assert the size of `lubbed` is (), i.e.
66+
// that it is a ZST closure type not a fnptr.
67+
unsafe { std::mem::transmute::<_, ()>(lubbed) };
68+
}
69+
70+
fn main() {}

0 commit comments

Comments
 (0)