Skip to content

Commit 97b131c

Browse files
committed
Auto merge of #148602 - BoxyUwU:coercion_cleanup_uncontroversial, r=lcnr
misc coercion cleanups and handle safety correctly r? lcnr ### "remove normalize call" Fixes #132765 If the normalization fails we would sometimes get a `TypeError` containing inference variables created inside of the probe used by coercion. These would then get leaked out causing ICEs in diagnostics logic ### "leak check and lub for closure<->closure coerce-lubs of same defids" Fixes rust-lang/trait-system-refactor-initiative#233 ```rust fn peculiar() -> impl Fn(u8) -> u8 { return |x| x + 1 } ``` the `|x| x + 1` expr has a type of `Closure(?31t)` which we wind up inferring the RPIT to. The `CoerceMany` `ret_coercion` for the whole `peculiar` typeck has an expected type of `RPIT` (unnormalized). When we type check the `return |x| x + 1` expr we go from the never type to `Closure(?31t)` which then participates in the `ret_coercion` giving us a `coerce-lub(RPIT, Closure(?31t))`. Normalizing `RPIT` gives us some `Closure(?50t)` where `?31t` and `?50t` have been unified with `?31t` as the root var. `resolve_vars_if_possible` doesn't resolve infer vars to their roots so these wind up with different structural identities so the fast path doesn't apply and we fall back to coercing to a `fn` ptr. cc #147193 which also fixes this New solver probably just gets more inference variables here because canonicalization + generally different approach to normalization of opaques. Idk :3 ### FCP worthy stuffy there are some other FCP worthy things but they're in my FCP comment which also contains some analysis of the breaking nature of the previously listed changes in this PR: #148602 (comment)
2 parents 864339a + 2f95ecf commit 97b131c

File tree

30 files changed

+657
-445
lines changed

30 files changed

+657
-445
lines changed

compiler/rustc_borrowck/src/type_check/mod.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1061,7 +1061,10 @@ impl<'a, 'tcx> Visitor<'tcx> for TypeChecker<'a, 'tcx> {
10611061

10621062
Rvalue::Cast(cast_kind, op, ty) => {
10631063
match *cast_kind {
1064-
CastKind::PointerCoercion(PointerCoercion::ReifyFnPointer, coercion_source) => {
1064+
CastKind::PointerCoercion(
1065+
PointerCoercion::ReifyFnPointer(target_safety),
1066+
coercion_source,
1067+
) => {
10651068
let is_implicit_coercion = coercion_source == CoercionSource::Implicit;
10661069
let src_ty = op.ty(self.body, tcx);
10671070
let mut src_sig = src_ty.fn_sig(tcx);
@@ -1078,6 +1081,10 @@ impl<'a, 'tcx> Visitor<'tcx> for TypeChecker<'a, 'tcx> {
10781081
src_sig = safe_sig;
10791082
}
10801083

1084+
if src_sig.safety().is_safe() && target_safety.is_unsafe() {
1085+
src_sig = tcx.safe_to_unsafe_sig(src_sig);
1086+
}
1087+
10811088
// HACK: This shouldn't be necessary... We can remove this when we actually
10821089
// get binders with where clauses, then elaborate implied bounds into that
10831090
// binder, and implement a higher-ranked subtyping algorithm that actually

compiler/rustc_codegen_cranelift/src/base.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -689,7 +689,7 @@ fn codegen_stmt<'tcx>(fx: &mut FunctionCx<'_, '_, 'tcx>, cur_block: Block, stmt:
689689
lval.write_cvalue(fx, res);
690690
}
691691
Rvalue::Cast(
692-
CastKind::PointerCoercion(PointerCoercion::ReifyFnPointer, _),
692+
CastKind::PointerCoercion(PointerCoercion::ReifyFnPointer(_), _),
693693
ref operand,
694694
to_ty,
695695
) => {

compiler/rustc_codegen_ssa/src/mir/rvalue.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -405,7 +405,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
405405
let lladdr = bx.ptrtoint(llptr, llcast_ty);
406406
OperandValue::Immediate(lladdr)
407407
}
408-
mir::CastKind::PointerCoercion(PointerCoercion::ReifyFnPointer, _) => {
408+
mir::CastKind::PointerCoercion(PointerCoercion::ReifyFnPointer(_), _) => {
409409
match *operand.layout.ty.kind() {
410410
ty::FnDef(def_id, args) => {
411411
let instance = ty::Instance::resolve_for_fn_ptr(

compiler/rustc_const_eval/src/check_consts/check.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -627,7 +627,7 @@ impl<'tcx> Visitor<'tcx> for Checker<'_, 'tcx> {
627627
| PointerCoercion::ArrayToPointer
628628
| PointerCoercion::UnsafeFnPointer
629629
| PointerCoercion::ClosureFnPointer(_)
630-
| PointerCoercion::ReifyFnPointer,
630+
| PointerCoercion::ReifyFnPointer(_),
631631
_,
632632
),
633633
_,

compiler/rustc_const_eval/src/interpret/cast.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
7474
bug!("{cast_kind:?} casts are for borrowck only, not runtime MIR");
7575
}
7676

77-
CastKind::PointerCoercion(PointerCoercion::ReifyFnPointer, _) => {
77+
CastKind::PointerCoercion(PointerCoercion::ReifyFnPointer(_), _) => {
7878
// All reifications must be monomorphic, bail out otherwise.
7979
ensure_monomorphic_enough(*self.tcx, src.layout.ty)?;
8080

0 commit comments

Comments
 (0)