Skip to content

Commit

Permalink
Sync from rust 9e394f5
Browse files Browse the repository at this point in the history
  • Loading branch information
bjorn3 committed Sep 26, 2024
2 parents 3a3a634 + 6fdc133 commit 39440c5
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 10 deletions.
15 changes: 10 additions & 5 deletions src/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -652,7 +652,7 @@ fn codegen_stmt<'tcx>(
lval.write_cvalue(fx, res);
}
Rvalue::Cast(
CastKind::PointerCoercion(PointerCoercion::ReifyFnPointer),
CastKind::PointerCoercion(PointerCoercion::ReifyFnPointer, _),
ref operand,
to_ty,
) => {
Expand All @@ -677,7 +677,7 @@ fn codegen_stmt<'tcx>(
}
}
Rvalue::Cast(
CastKind::PointerCoercion(PointerCoercion::UnsafeFnPointer),
CastKind::PointerCoercion(PointerCoercion::UnsafeFnPointer, _),
ref operand,
to_ty,
) => {
Expand All @@ -688,6 +688,7 @@ fn codegen_stmt<'tcx>(
Rvalue::Cast(
CastKind::PointerCoercion(
PointerCoercion::MutToConstPointer | PointerCoercion::ArrayToPointer,
_,
),
..,
) => {
Expand Down Expand Up @@ -741,7 +742,7 @@ fn codegen_stmt<'tcx>(
}
}
Rvalue::Cast(
CastKind::PointerCoercion(PointerCoercion::ClosureFnPointer(_)),
CastKind::PointerCoercion(PointerCoercion::ClosureFnPointer(_), _),
ref operand,
_to_ty,
) => {
Expand All @@ -763,14 +764,18 @@ fn codegen_stmt<'tcx>(
}
}
Rvalue::Cast(
CastKind::PointerCoercion(PointerCoercion::Unsize),
CastKind::PointerCoercion(PointerCoercion::Unsize, _),
ref operand,
_to_ty,
) => {
let operand = codegen_operand(fx, operand);
crate::unsize::coerce_unsized_into(fx, operand, lval);
}
Rvalue::Cast(CastKind::DynStar, ref operand, _) => {
Rvalue::Cast(
CastKind::PointerCoercion(PointerCoercion::DynStar, _),
ref operand,
_,
) => {
let operand = codegen_operand(fx, operand);
crate::unsize::coerce_dyn_star(fx, operand, lval);
}
Expand Down
8 changes: 4 additions & 4 deletions src/constant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,13 +161,13 @@ pub(crate) fn codegen_const_value<'tcx>(
fx.module.declare_func_in_func(func_id, &mut fx.bcx.func);
fx.bcx.ins().func_addr(fx.pointer_type, local_func_id)
}
GlobalAlloc::VTable(ty, trait_ref) => {
GlobalAlloc::VTable(ty, dyn_ty) => {
let data_id = data_id_for_vtable(
fx.tcx,
&mut fx.constants_cx,
fx.module,
ty,
trait_ref,
dyn_ty.principal(),
);
let local_data_id =
fx.module.declare_data_in_func(data_id, &mut fx.bcx.func);
Expand Down Expand Up @@ -456,8 +456,8 @@ fn define_all_allocs(tcx: TyCtxt<'_>, module: &mut dyn Module, cx: &mut Constant
GlobalAlloc::Memory(target_alloc) => {
data_id_for_alloc_id(cx, module, alloc_id, target_alloc.inner().mutability)
}
GlobalAlloc::VTable(ty, trait_ref) => {
data_id_for_vtable(tcx, cx, module, ty, trait_ref)
GlobalAlloc::VTable(ty, dyn_ty) => {
data_id_for_vtable(tcx, cx, module, ty, dyn_ty.principal())
}
GlobalAlloc::Static(def_id) => {
if tcx.codegen_fn_attrs(def_id).flags.contains(CodegenFnAttrFlags::THREAD_LOCAL)
Expand Down
17 changes: 16 additions & 1 deletion src/unsize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,22 @@ pub(crate) fn unsized_info<'tcx>(
let old_info =
old_info.expect("unsized_info: missing old info for trait upcasting coercion");
if data_a.principal_def_id() == data_b.principal_def_id() {
// A NOP cast that doesn't actually change anything, should be allowed even with invalid vtables.
// Codegen takes advantage of the additional assumption, where if the
// principal trait def id of what's being casted doesn't change,
// then we don't need to adjust the vtable at all. This
// corresponds to the fact that `dyn Tr<A>: Unsize<dyn Tr<B>>`
// requires that `A = B`; we don't allow *upcasting* objects
// between the same trait with different args. If we, for
// some reason, were to relax the `Unsize` trait, it could become
// unsound, so let's assert here that the trait refs are *equal*.
//
// We can use `assert_eq` because the binders should have been anonymized,
// and because higher-ranked equality now requires the binders are equal.
debug_assert_eq!(
data_a.principal(),
data_b.principal(),
"NOP unsize vtable changed principal trait ref: {data_a} -> {data_b}"
);
return old_info;
}

Expand Down

0 comments on commit 39440c5

Please sign in to comment.