Skip to content

Commit 39440c5

Browse files
committed
Sync from rust 9e394f5
2 parents 3a3a634 + 6fdc133 commit 39440c5

File tree

3 files changed

+30
-10
lines changed

3 files changed

+30
-10
lines changed

src/base.rs

+10-5
Original file line numberDiff line numberDiff line change
@@ -652,7 +652,7 @@ fn codegen_stmt<'tcx>(
652652
lval.write_cvalue(fx, res);
653653
}
654654
Rvalue::Cast(
655-
CastKind::PointerCoercion(PointerCoercion::ReifyFnPointer),
655+
CastKind::PointerCoercion(PointerCoercion::ReifyFnPointer, _),
656656
ref operand,
657657
to_ty,
658658
) => {
@@ -677,7 +677,7 @@ fn codegen_stmt<'tcx>(
677677
}
678678
}
679679
Rvalue::Cast(
680-
CastKind::PointerCoercion(PointerCoercion::UnsafeFnPointer),
680+
CastKind::PointerCoercion(PointerCoercion::UnsafeFnPointer, _),
681681
ref operand,
682682
to_ty,
683683
) => {
@@ -688,6 +688,7 @@ fn codegen_stmt<'tcx>(
688688
Rvalue::Cast(
689689
CastKind::PointerCoercion(
690690
PointerCoercion::MutToConstPointer | PointerCoercion::ArrayToPointer,
691+
_,
691692
),
692693
..,
693694
) => {
@@ -741,7 +742,7 @@ fn codegen_stmt<'tcx>(
741742
}
742743
}
743744
Rvalue::Cast(
744-
CastKind::PointerCoercion(PointerCoercion::ClosureFnPointer(_)),
745+
CastKind::PointerCoercion(PointerCoercion::ClosureFnPointer(_), _),
745746
ref operand,
746747
_to_ty,
747748
) => {
@@ -763,14 +764,18 @@ fn codegen_stmt<'tcx>(
763764
}
764765
}
765766
Rvalue::Cast(
766-
CastKind::PointerCoercion(PointerCoercion::Unsize),
767+
CastKind::PointerCoercion(PointerCoercion::Unsize, _),
767768
ref operand,
768769
_to_ty,
769770
) => {
770771
let operand = codegen_operand(fx, operand);
771772
crate::unsize::coerce_unsized_into(fx, operand, lval);
772773
}
773-
Rvalue::Cast(CastKind::DynStar, ref operand, _) => {
774+
Rvalue::Cast(
775+
CastKind::PointerCoercion(PointerCoercion::DynStar, _),
776+
ref operand,
777+
_,
778+
) => {
774779
let operand = codegen_operand(fx, operand);
775780
crate::unsize::coerce_dyn_star(fx, operand, lval);
776781
}

src/constant.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -161,13 +161,13 @@ pub(crate) fn codegen_const_value<'tcx>(
161161
fx.module.declare_func_in_func(func_id, &mut fx.bcx.func);
162162
fx.bcx.ins().func_addr(fx.pointer_type, local_func_id)
163163
}
164-
GlobalAlloc::VTable(ty, trait_ref) => {
164+
GlobalAlloc::VTable(ty, dyn_ty) => {
165165
let data_id = data_id_for_vtable(
166166
fx.tcx,
167167
&mut fx.constants_cx,
168168
fx.module,
169169
ty,
170-
trait_ref,
170+
dyn_ty.principal(),
171171
);
172172
let local_data_id =
173173
fx.module.declare_data_in_func(data_id, &mut fx.bcx.func);
@@ -456,8 +456,8 @@ fn define_all_allocs(tcx: TyCtxt<'_>, module: &mut dyn Module, cx: &mut Constant
456456
GlobalAlloc::Memory(target_alloc) => {
457457
data_id_for_alloc_id(cx, module, alloc_id, target_alloc.inner().mutability)
458458
}
459-
GlobalAlloc::VTable(ty, trait_ref) => {
460-
data_id_for_vtable(tcx, cx, module, ty, trait_ref)
459+
GlobalAlloc::VTable(ty, dyn_ty) => {
460+
data_id_for_vtable(tcx, cx, module, ty, dyn_ty.principal())
461461
}
462462
GlobalAlloc::Static(def_id) => {
463463
if tcx.codegen_fn_attrs(def_id).flags.contains(CodegenFnAttrFlags::THREAD_LOCAL)

src/unsize.rs

+16-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,22 @@ pub(crate) fn unsized_info<'tcx>(
3434
let old_info =
3535
old_info.expect("unsized_info: missing old info for trait upcasting coercion");
3636
if data_a.principal_def_id() == data_b.principal_def_id() {
37-
// A NOP cast that doesn't actually change anything, should be allowed even with invalid vtables.
37+
// Codegen takes advantage of the additional assumption, where if the
38+
// principal trait def id of what's being casted doesn't change,
39+
// then we don't need to adjust the vtable at all. This
40+
// corresponds to the fact that `dyn Tr<A>: Unsize<dyn Tr<B>>`
41+
// requires that `A = B`; we don't allow *upcasting* objects
42+
// between the same trait with different args. If we, for
43+
// some reason, were to relax the `Unsize` trait, it could become
44+
// unsound, so let's assert here that the trait refs are *equal*.
45+
//
46+
// We can use `assert_eq` because the binders should have been anonymized,
47+
// and because higher-ranked equality now requires the binders are equal.
48+
debug_assert_eq!(
49+
data_a.principal(),
50+
data_b.principal(),
51+
"NOP unsize vtable changed principal trait ref: {data_a} -> {data_b}"
52+
);
3853
return old_info;
3954
}
4055

0 commit comments

Comments
 (0)