Skip to content

Commit c02dff7

Browse files
committed
Fix transmute_undefined_repr when converting between a pointer and a type containing a pointer
1 parent 73367f8 commit c02dff7

File tree

3 files changed

+39
-4
lines changed

3 files changed

+39
-4
lines changed

clippy_lints/src/transmute/transmute_undefined_repr.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,8 @@ fn reduce_ty<'tcx>(cx: &LateContext<'tcx>, mut ty: Ty<'tcx>) -> ReducedTy<'tcx>
273273
ReducedTy::UnorderedFields(ty)
274274
}
275275
},
276-
ty::Ref(..) | ty::RawPtr(_) => ReducedTy::Ref(ty),
276+
ty::Ref(_, ty, _) => ReducedTy::Ref(ty),
277+
ty::RawPtr(ty) => ReducedTy::Ref(ty.ty),
277278
_ => ReducedTy::Other(ty),
278279
};
279280
}

tests/ui/transmute_undefined_repr.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,5 +44,19 @@ fn main() {
4444
// issue #8417
4545
let _: Ty2C<Ty2<u32, i32>, ()> = core::mem::transmute(value::<Ty2<u32, i32>>()); // Ok, Ty2 types are the same
4646
let _: Ty2<u32, i32> = core::mem::transmute(value::<Ty2C<Ty2<u32, i32>, ()>>()); // Ok, Ty2 types are the same
47+
48+
// Ty2 types are the same
49+
let _: &'static mut Ty2<u32, u32> = core::mem::transmute(value::<Box<Ty2<u32, u32>>>()); // Ok
50+
// Ty2 types are the same
51+
let _: Box<Ty2<u32, u32>> = core::mem::transmute(value::<&'static mut Ty2<u32, u32>>()); // Ok
52+
let _: *mut Ty2<u32, u32> = core::mem::transmute(value::<Box<Ty2<u32, u32>>>()); // Ok, Ty2 types are the same
53+
let _: Box<Ty2<u32, u32>> = core::mem::transmute(value::<*mut Ty2<u32, u32>>()); // Ok, Ty2 types are the same
54+
55+
// Different Ty2 instances
56+
let _: &'static mut Ty2<u32, f32> = core::mem::transmute(value::<Box<Ty2<u32, u32>>>()); // Lint
57+
// Different Ty2 instances
58+
let _: Box<Ty2<u32, u32>> = core::mem::transmute(value::<&'static mut Ty2<u32, f32>>()); // Lint
59+
60+
4761
}
4862
}

tests/ui/transmute_undefined_repr.stderr

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,37 @@ LL | let _: Ty<Ty2<u32, i32>> = core::mem::transmute(value::<Ty2<u32, f3
2828
|
2929
= note: two instances of the same generic type (`Ty2`) may have different layouts
3030

31-
error: transmute to `&Ty2<u32, f32>` which has an undefined layout
31+
error: transmute from `Ty<&Ty2<u32, i32>>` to `&Ty2<u32, f32>`, both of which have an undefined layout
3232
--> $DIR/transmute_undefined_repr.rs:35:33
3333
|
3434
LL | let _: &Ty2<u32, f32> = core::mem::transmute(value::<Ty<&Ty2<u32, i32>>>()); // Lint, different Ty2 instances
3535
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
36+
|
37+
= note: two instances of the same generic type (`Ty2`) may have different layouts
3638

37-
error: transmute from `&Ty2<u32, f32>` which has an undefined layout
39+
error: transmute from `&Ty2<u32, f32>` to `Ty<&Ty2<u32, i32>>`, both of which have an undefined layout
3840
--> $DIR/transmute_undefined_repr.rs:36:37
3941
|
4042
LL | let _: Ty<&Ty2<u32, i32>> = core::mem::transmute(value::<&Ty2<u32, f32>>()); // Lint, different Ty2 instances
4143
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
44+
|
45+
= note: two instances of the same generic type (`Ty2`) may have different layouts
46+
47+
error: transmute from `std::boxed::Box<Ty2<u32, u32>>` to `&mut Ty2<u32, f32>`, both of which have an undefined layout
48+
--> $DIR/transmute_undefined_repr.rs:56:45
49+
|
50+
LL | let _: &'static mut Ty2<u32, f32> = core::mem::transmute(value::<Box<Ty2<u32, u32>>>()); // Lint
51+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
52+
|
53+
= note: two instances of the same generic type (`Ty2`) may have different layouts
54+
55+
error: transmute from `&mut Ty2<u32, f32>` to `std::boxed::Box<Ty2<u32, u32>>`, both of which have an undefined layout
56+
--> $DIR/transmute_undefined_repr.rs:58:37
57+
|
58+
LL | let _: Box<Ty2<u32, u32>> = core::mem::transmute(value::<&'static mut Ty2<u32, f32>>()); // Lint
59+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
60+
|
61+
= note: two instances of the same generic type (`Ty2`) may have different layouts
4262

43-
error: aborting due to 6 previous errors
63+
error: aborting due to 8 previous errors
4464

0 commit comments

Comments
 (0)