Skip to content

Commit ec46992

Browse files
committed
Auto merge of rust-lang#8720 - asquared31415:ptr-cast-ice-fix, r=Alexendoo,xFrednet
fix ICE in `cast_slice_different_sizes` fixes rust-lang#8708 changelog: fixes an ICE introduced in rust-lang#8445
2 parents c80cd4d + 8f8fc9f commit ec46992

File tree

3 files changed

+180
-44
lines changed

3 files changed

+180
-44
lines changed

clippy_lints/src/casts/cast_slice_different_sizes.rs

Lines changed: 68 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use clippy_utils::{diagnostics::span_lint_and_then, meets_msrv, msrvs, source::snippet_opt};
1+
use clippy_utils::{diagnostics::span_lint_and_then, meets_msrv, msrvs, source};
22
use if_chain::if_chain;
33
use rustc_ast::Mutability;
44
use rustc_hir::{Expr, ExprKind, Node};
@@ -8,32 +8,7 @@ use rustc_semver::RustcVersion;
88

99
use super::CAST_SLICE_DIFFERENT_SIZES;
1010

11-
fn is_child_of_cast(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool {
12-
let map = cx.tcx.hir();
13-
if_chain! {
14-
if let Some(parent_id) = map.find_parent_node(expr.hir_id);
15-
if let Some(parent) = map.find(parent_id);
16-
then {
17-
let expr = match parent {
18-
Node::Block(block) => {
19-
if let Some(parent_expr) = block.expr {
20-
parent_expr
21-
} else {
22-
return false;
23-
}
24-
},
25-
Node::Expr(expr) => expr,
26-
_ => return false,
27-
};
28-
29-
matches!(expr.kind, ExprKind::Cast(..))
30-
} else {
31-
false
32-
}
33-
}
34-
}
35-
36-
pub(super) fn check(cx: &LateContext<'_>, expr: &Expr<'_>, msrv: &Option<RustcVersion>) {
11+
pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, expr: &Expr<'tcx>, msrv: &Option<RustcVersion>) {
3712
// suggestion is invalid if `ptr::slice_from_raw_parts` does not exist
3813
if !meets_msrv(msrv.as_ref(), &msrvs::PTR_SLICE_RAW_PARTS) {
3914
return;
@@ -45,8 +20,13 @@ pub(super) fn check(cx: &LateContext<'_>, expr: &Expr<'_>, msrv: &Option<RustcVe
4520
return;
4621
}
4722

48-
if let Some((from_slice_ty, to_slice_ty)) = expr_cast_chain_tys(cx, expr) {
49-
if let (Ok(from_layout), Ok(to_layout)) = (cx.layout_of(from_slice_ty.ty), cx.layout_of(to_slice_ty.ty)) {
23+
if let Some(CastChainInfo {
24+
left_cast,
25+
start_ty,
26+
end_ty,
27+
}) = expr_cast_chain_tys(cx, expr)
28+
{
29+
if let (Ok(from_layout), Ok(to_layout)) = (cx.layout_of(start_ty.ty), cx.layout_of(end_ty.ty)) {
5030
let from_size = from_layout.size.bytes();
5131
let to_size = to_layout.size.bytes();
5232
if from_size != to_size && from_size != 0 && to_size != 0 {
@@ -56,21 +36,20 @@ pub(super) fn check(cx: &LateContext<'_>, expr: &Expr<'_>, msrv: &Option<RustcVe
5636
expr.span,
5737
&format!(
5838
"casting between raw pointers to `[{}]` (element size {}) and `[{}]` (element size {}) does not adjust the count",
59-
from_slice_ty, from_size, to_slice_ty, to_size,
39+
start_ty.ty, from_size, end_ty.ty, to_size,
6040
),
6141
|diag| {
62-
let cast_expr = match expr.kind {
63-
ExprKind::Cast(cast_expr, ..) => cast_expr,
64-
_ => unreachable!("expr should be a cast as checked by expr_cast_chain_tys"),
65-
};
66-
let ptr_snippet = snippet_opt(cx, cast_expr.span).unwrap();
42+
let ptr_snippet = source::snippet(cx, left_cast.span, "..");
6743

68-
let (mutbl_fn_str, mutbl_ptr_str) = match to_slice_ty.mutbl {
44+
let (mutbl_fn_str, mutbl_ptr_str) = match end_ty.mutbl {
6945
Mutability::Mut => ("_mut", "mut"),
7046
Mutability::Not => ("", "const"),
7147
};
7248
let sugg = format!(
73-
"core::ptr::slice_from_raw_parts{mutbl_fn_str}({ptr_snippet} as *{mutbl_ptr_str} {to_slice_ty}, ..)"
49+
"core::ptr::slice_from_raw_parts{mutbl_fn_str}({ptr_snippet} as *{mutbl_ptr_str} {}, ..)",
50+
// get just the ty from the TypeAndMut so that the printed type isn't something like `mut
51+
// T`, extract just the `T`
52+
end_ty.ty
7453
);
7554

7655
diag.span_suggestion(
@@ -86,6 +65,31 @@ pub(super) fn check(cx: &LateContext<'_>, expr: &Expr<'_>, msrv: &Option<RustcVe
8665
}
8766
}
8867

68+
fn is_child_of_cast(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool {
69+
let map = cx.tcx.hir();
70+
if_chain! {
71+
if let Some(parent_id) = map.find_parent_node(expr.hir_id);
72+
if let Some(parent) = map.find(parent_id);
73+
then {
74+
let expr = match parent {
75+
Node::Block(block) => {
76+
if let Some(parent_expr) = block.expr {
77+
parent_expr
78+
} else {
79+
return false;
80+
}
81+
},
82+
Node::Expr(expr) => expr,
83+
_ => return false,
84+
};
85+
86+
matches!(expr.kind, ExprKind::Cast(..))
87+
} else {
88+
false
89+
}
90+
}
91+
}
92+
8993
/// Returns the type T of the pointed to *const [T] or *mut [T] and the mutability of the slice if
9094
/// the type is one of those slices
9195
fn get_raw_slice_ty_mut(ty: Ty<'_>) -> Option<TypeAndMut<'_>> {
@@ -98,18 +102,40 @@ fn get_raw_slice_ty_mut(ty: Ty<'_>) -> Option<TypeAndMut<'_>> {
98102
}
99103
}
100104

101-
/// Returns the pair (original ptr T, final ptr U) if the expression is composed of casts
105+
struct CastChainInfo<'tcx> {
106+
/// The left most part of the cast chain, or in other words, the first cast in the chain
107+
/// Used for diagnostics
108+
left_cast: &'tcx Expr<'tcx>,
109+
/// The starting type of the cast chain
110+
start_ty: TypeAndMut<'tcx>,
111+
/// The final type of the cast chain
112+
end_ty: TypeAndMut<'tcx>,
113+
}
114+
115+
/// Returns a `CastChainInfo` with the left-most cast in the chain and the original ptr T and final
116+
/// ptr U if the expression is composed of casts.
102117
/// Returns None if the expr is not a Cast
103-
fn expr_cast_chain_tys<'tcx>(cx: &LateContext<'tcx>, expr: &Expr<'_>) -> Option<(TypeAndMut<'tcx>, TypeAndMut<'tcx>)> {
118+
fn expr_cast_chain_tys<'tcx>(cx: &LateContext<'tcx>, expr: &Expr<'tcx>) -> Option<CastChainInfo<'tcx>> {
104119
if let ExprKind::Cast(cast_expr, _cast_to_hir_ty) = expr.peel_blocks().kind {
105120
let cast_to = cx.typeck_results().expr_ty(expr);
106121
let to_slice_ty = get_raw_slice_ty_mut(cast_to)?;
107-
if let Some((inner_from_ty, _inner_to_ty)) = expr_cast_chain_tys(cx, cast_expr) {
108-
Some((inner_from_ty, to_slice_ty))
122+
123+
// If the expression that makes up the source of this cast is itself a cast, recursively
124+
// call `expr_cast_chain_tys` and update the end type with the final tartet type.
125+
// Otherwise, this cast is not immediately nested, just construct the info for this cast
126+
if let Some(prev_info) = expr_cast_chain_tys(cx, cast_expr) {
127+
Some(CastChainInfo {
128+
end_ty: to_slice_ty,
129+
..prev_info
130+
})
109131
} else {
110132
let cast_from = cx.typeck_results().expr_ty(cast_expr);
111133
let from_slice_ty = get_raw_slice_ty_mut(cast_from)?;
112-
Some((from_slice_ty, to_slice_ty))
134+
Some(CastChainInfo {
135+
left_cast: cast_expr,
136+
start_ty: from_slice_ty,
137+
end_ty: to_slice_ty,
138+
})
113139
}
114140
} else {
115141
None

tests/ui/cast_slice_different_sizes.rs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,44 @@ fn main() {
3939
let long_chain_restore =
4040
r_x as *const [i32] as *const [u32] as *const [u16] as *const [i8] as *const [u8] as *const [u32];
4141
}
42+
43+
// foo and foo2 should not fire, they're the same size
44+
fn foo(x: *mut [u8]) -> *mut [u8] {
45+
x as *mut [u8]
46+
}
47+
48+
fn foo2(x: *mut [u8]) -> *mut [u8] {
49+
x as *mut _
50+
}
51+
52+
// Test that casts as part of function returns work
53+
fn bar(x: *mut [u16]) -> *mut [u8] {
54+
x as *mut [u8]
55+
}
56+
57+
fn uwu(x: *mut [u16]) -> *mut [u8] {
58+
x as *mut _
59+
}
60+
61+
fn bar2(x: *mut [u16]) -> *mut [u8] {
62+
x as _
63+
}
64+
65+
// constify
66+
fn bar3(x: *mut [u16]) -> *const [u8] {
67+
x as _
68+
}
69+
70+
// unconstify
71+
fn bar4(x: *const [u16]) -> *mut [u8] {
72+
x as _
73+
}
74+
75+
// function returns plus blocks
76+
fn blocks(x: *mut [u16]) -> *mut [u8] {
77+
({ x }) as _
78+
}
79+
80+
fn more_blocks(x: *mut [u16]) -> *mut [u8] {
81+
{ ({ x }) as _ }
82+
}

tests/ui/cast_slice_different_sizes.stderr

Lines changed: 71 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,76 @@ error: casting between raw pointers to `[i32]` (element size 4) and `[u8]` (elem
4646
--> $DIR/cast_slice_different_sizes.rs:38:27
4747
|
4848
LL | let long_chain_loss = r_x as *const [i32] as *const [u32] as *const [u16] as *const [i8] as *const [u8];
49-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace with `ptr::slice_from_raw_parts`: `core::ptr::slice_from_raw_parts(r_x as *const [i32] as *const [u32] as *const [u16] as *const [i8] as *const u8, ..)`
49+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace with `ptr::slice_from_raw_parts`: `core::ptr::slice_from_raw_parts(r_x as *const [i32] as *const u8, ..)`
5050

51-
error: aborting due to 6 previous errors
51+
error: casting between raw pointers to `[u16]` (element size 2) and `[u8]` (element size 1) does not adjust the count
52+
--> $DIR/cast_slice_different_sizes.rs:53:36
53+
|
54+
LL | fn bar(x: *mut [u16]) -> *mut [u8] {
55+
| ____________________________________^
56+
LL | | x as *mut [u8]
57+
LL | | }
58+
| |_^ help: replace with `ptr::slice_from_raw_parts_mut`: `core::ptr::slice_from_raw_parts_mut(x as *mut u8, ..)`
59+
60+
error: casting between raw pointers to `[u16]` (element size 2) and `[u8]` (element size 1) does not adjust the count
61+
--> $DIR/cast_slice_different_sizes.rs:57:36
62+
|
63+
LL | fn uwu(x: *mut [u16]) -> *mut [u8] {
64+
| ____________________________________^
65+
LL | | x as *mut _
66+
LL | | }
67+
| |_^ help: replace with `ptr::slice_from_raw_parts_mut`: `core::ptr::slice_from_raw_parts_mut(x as *mut u8, ..)`
68+
69+
error: casting between raw pointers to `[u16]` (element size 2) and `[u8]` (element size 1) does not adjust the count
70+
--> $DIR/cast_slice_different_sizes.rs:61:37
71+
|
72+
LL | fn bar2(x: *mut [u16]) -> *mut [u8] {
73+
| _____________________________________^
74+
LL | | x as _
75+
LL | | }
76+
| |_^ help: replace with `ptr::slice_from_raw_parts_mut`: `core::ptr::slice_from_raw_parts_mut(x as *mut u8, ..)`
77+
78+
error: casting between raw pointers to `[u16]` (element size 2) and `[u8]` (element size 1) does not adjust the count
79+
--> $DIR/cast_slice_different_sizes.rs:66:39
80+
|
81+
LL | fn bar3(x: *mut [u16]) -> *const [u8] {
82+
| _______________________________________^
83+
LL | | x as _
84+
LL | | }
85+
| |_^ help: replace with `ptr::slice_from_raw_parts`: `core::ptr::slice_from_raw_parts(x as *const u8, ..)`
86+
87+
error: casting between raw pointers to `[u16]` (element size 2) and `[u8]` (element size 1) does not adjust the count
88+
--> $DIR/cast_slice_different_sizes.rs:71:39
89+
|
90+
LL | fn bar4(x: *const [u16]) -> *mut [u8] {
91+
| _______________________________________^
92+
LL | | x as _
93+
LL | | }
94+
| |_^ help: replace with `ptr::slice_from_raw_parts_mut`: `core::ptr::slice_from_raw_parts_mut(x as *mut u8, ..)`
95+
96+
error: casting between raw pointers to `[u16]` (element size 2) and `[u8]` (element size 1) does not adjust the count
97+
--> $DIR/cast_slice_different_sizes.rs:76:39
98+
|
99+
LL | fn blocks(x: *mut [u16]) -> *mut [u8] {
100+
| _______________________________________^
101+
LL | | ({ x }) as _
102+
LL | | }
103+
| |_^ help: replace with `ptr::slice_from_raw_parts_mut`: `core::ptr::slice_from_raw_parts_mut(({ x }) as *mut u8, ..)`
104+
105+
error: casting between raw pointers to `[u16]` (element size 2) and `[u8]` (element size 1) does not adjust the count
106+
--> $DIR/cast_slice_different_sizes.rs:80:44
107+
|
108+
LL | fn more_blocks(x: *mut [u16]) -> *mut [u8] {
109+
| ____________________________________________^
110+
LL | | { ({ x }) as _ }
111+
LL | | }
112+
| |_^ help: replace with `ptr::slice_from_raw_parts_mut`: `core::ptr::slice_from_raw_parts_mut(({ x }) as *mut u8, ..)`
113+
114+
error: casting between raw pointers to `[u16]` (element size 2) and `[u8]` (element size 1) does not adjust the count
115+
--> $DIR/cast_slice_different_sizes.rs:81:5
116+
|
117+
LL | { ({ x }) as _ }
118+
| ^^^^^^^^^^^^^^^^ help: replace with `ptr::slice_from_raw_parts_mut`: `core::ptr::slice_from_raw_parts_mut(({ x }) as *mut u8, ..)`
119+
120+
error: aborting due to 14 previous errors
52121

0 commit comments

Comments
 (0)