Skip to content

Commit 6bdfd13

Browse files
Add a debug assertion in codegen that unsize casts of the same principal trait def id are truly NOPs
1 parent 481886e commit 6bdfd13

File tree

3 files changed

+33
-16
lines changed

3 files changed

+33
-16
lines changed

compiler/rustc_codegen_cranelift/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

compiler/rustc_codegen_ssa/src/base.rs

+17
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,23 @@ fn unsized_info<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(
124124
let old_info =
125125
old_info.expect("unsized_info: missing old info for trait upcasting coercion");
126126
if data_a.principal_def_id() == data_b.principal_def_id() {
127+
// Codegen takes advantage of the additional assumption, where if the
128+
// principal trait def id of what's being casted doesn't change,
129+
// then we don't need to adjust the vtable at all. This
130+
// corresponds to the fact that `dyn Tr<A>: Unsize<dyn Tr<B>>`
131+
// requires that `A = B`; we don't allow *upcasting* objects
132+
// between the same trait with different args. If we, for
133+
// some reason, were to relax the `Unsize` trait, it could become
134+
// unsound, so let's assert here that the trait refs are *equal*.
135+
//
136+
// We can use `assert_eq` because the binders should have been anonymized,
137+
// and because higher-ranked equality now requires the binders are equal.
138+
debug_assert_eq!(
139+
data_a.principal(),
140+
data_b.principal(),
141+
"NOP unsize vtable changed principal trait ref: {data_a} -> {data_b}"
142+
);
143+
127144
// A NOP cast that doesn't actually change anything, should be allowed even with
128145
// invalid vtables.
129146
return old_info;

compiler/rustc_mir_transform/src/validate.rs

-15
Original file line numberDiff line numberDiff line change
@@ -1236,21 +1236,6 @@ impl<'a, 'tcx> Visitor<'tcx> for TypeChecker<'a, 'tcx> {
12361236
)) {
12371237
self.fail(location, format!("Unsize coercion, but `{op_ty}` isn't coercible to `{target_type}`"));
12381238
}
1239-
1240-
// FIXME: Codegen has an additional assumption, where if the
1241-
// principal trait def id of what's being casted doesn't change,
1242-
// then we don't need to adjust the vtable at all. This
1243-
// corresponds to the fact that `dyn Tr<A>: Unsize<dyn Tr<B>>`
1244-
// requires that `A = B`; we don't allow *upcasting* objects
1245-
// between the same trait with different args. Nothing actually
1246-
// validates this, though. While it's true right now, if we for
1247-
// some reason were to relax the `Unsize` trait, it could become
1248-
// unsound. We should eventually validate that, but it would
1249-
// require peeling `&Box<Struct<.., dyn Tr<A>, ..>>` down to
1250-
// the trait object that's being unsized, and that's rather
1251-
// annoying, and also it would need to be opportunistic since
1252-
// this MIR is not yet fully monomorphized, so we may bottom
1253-
// out in an alias or a projection or something.
12541239
}
12551240
CastKind::IntToInt | CastKind::IntToFloat => {
12561241
let input_valid = op_ty.is_integral() || op_ty.is_char() || op_ty.is_bool();

0 commit comments

Comments
 (0)