Skip to content

Commit a6db486

Browse files
authored
Unrolled build for #145941
Rollup merge of #145941 - Urgau:int_to_ptr_transmutes-unsized, r=lcnr Disable `integer_to_ptr_transmutes` suggestion for unsized types This PR disables the machine-applicable `integer_to_ptr_transmutes` lint suggestion for unsized types, as [`std::ptr::with_exposed_provenance`](https://doc.rust-lang.org/std/ptr/fn.with_exposed_provenance.html) requires sized types. We should probably mention [`std::ptr::from_raw_parts`](https://doc.rust-lang.org/std/ptr/fn.from_raw_parts.html) when it becomes stable. Related to #145935
2 parents b416342 + a8c837e commit a6db486

File tree

4 files changed

+69
-13
lines changed

4 files changed

+69
-13
lines changed

compiler/rustc_lint/src/lints.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1554,7 +1554,7 @@ impl<'a> LintDiagnostic<'a, ()> for DropGlue<'_> {
15541554
#[help(lint_help_exposed_provenance)]
15551555
pub(crate) struct IntegerToPtrTransmutes<'tcx> {
15561556
#[subdiagnostic]
1557-
pub suggestion: IntegerToPtrTransmutesSuggestion<'tcx>,
1557+
pub suggestion: Option<IntegerToPtrTransmutesSuggestion<'tcx>>,
15581558
}
15591559

15601560
#[derive(Subdiagnostic)]

compiler/rustc_lint/src/transmute.rs

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -169,19 +169,25 @@ fn check_int_to_ptr_transmute<'tcx>(
169169
expr.hir_id,
170170
expr.span,
171171
IntegerToPtrTransmutes {
172-
suggestion: if dst.is_ref() {
173-
IntegerToPtrTransmutesSuggestion::ToRef {
174-
dst: *inner_ty,
175-
suffix,
176-
ref_mutbl: mutbl.prefix_str(),
177-
start_call: expr.span.shrink_to_lo().until(arg.span),
178-
}
172+
suggestion: if layout_inner_ty.is_sized() {
173+
Some(if dst.is_ref() {
174+
IntegerToPtrTransmutesSuggestion::ToRef {
175+
dst: *inner_ty,
176+
suffix,
177+
ref_mutbl: mutbl.prefix_str(),
178+
start_call: expr.span.shrink_to_lo().until(arg.span),
179+
}
180+
} else {
181+
IntegerToPtrTransmutesSuggestion::ToPtr {
182+
dst: *inner_ty,
183+
suffix,
184+
start_call: expr.span.shrink_to_lo().until(arg.span),
185+
}
186+
})
179187
} else {
180-
IntegerToPtrTransmutesSuggestion::ToPtr {
181-
dst: *inner_ty,
182-
suffix,
183-
start_call: expr.span.shrink_to_lo().until(arg.span),
184-
}
188+
// We can't suggest using `with_exposed_provenance` for unsized type
189+
// so don't suggest anything.
190+
None
185191
},
186192
},
187193
);

tests/ui/lint/int_to_ptr-unsized.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Checks for the `integer_to_pointer_transmutes` lint with unsized types
2+
//
3+
// Related to https://github.com/rust-lang/rust/issues/145935
4+
5+
//@ check-pass
6+
7+
#![allow(non_camel_case_types)]
8+
#![allow(unused_unsafe)]
9+
10+
#[cfg(target_pointer_width = "64")]
11+
type usizemetadata = i128;
12+
13+
#[cfg(target_pointer_width = "32")]
14+
type usizemetadata = i64;
15+
16+
unsafe fn unsized_type(a: usize) {
17+
let _ref = unsafe { std::mem::transmute::<usizemetadata, &'static str>(0xff) };
18+
//~^ WARN transmuting an integer to a pointer
19+
let _ptr = unsafe { std::mem::transmute::<usizemetadata, *const [u8]>(0xff) };
20+
//~^ WARN transmuting an integer to a pointer
21+
}
22+
23+
fn main() {}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
warning: transmuting an integer to a pointer creates a pointer without provenance
2+
--> $DIR/int_to_ptr-unsized.rs:17:25
3+
|
4+
LL | let _ref = unsafe { std::mem::transmute::<usizemetadata, &'static str>(0xff) };
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
6+
|
7+
= note: this is dangerous because dereferencing the resulting pointer is undefined behavior
8+
= note: exposed provenance semantics can be used to create a pointer based on some previously exposed provenance
9+
= help: if you truly mean to create a pointer without provenance, use `std::ptr::without_provenance_mut`
10+
= help: for more information about transmute, see <https://doc.rust-lang.org/std/mem/fn.transmute.html#transmutation-between-pointers-and-integers>
11+
= help: for more information about exposed provenance, see <https://doc.rust-lang.org/std/ptr/index.html#exposed-provenance>
12+
= note: `#[warn(integer_to_ptr_transmutes)]` on by default
13+
14+
warning: transmuting an integer to a pointer creates a pointer without provenance
15+
--> $DIR/int_to_ptr-unsized.rs:19:25
16+
|
17+
LL | let _ptr = unsafe { std::mem::transmute::<usizemetadata, *const [u8]>(0xff) };
18+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
19+
|
20+
= note: this is dangerous because dereferencing the resulting pointer is undefined behavior
21+
= note: exposed provenance semantics can be used to create a pointer based on some previously exposed provenance
22+
= help: if you truly mean to create a pointer without provenance, use `std::ptr::without_provenance_mut`
23+
= help: for more information about transmute, see <https://doc.rust-lang.org/std/mem/fn.transmute.html#transmutation-between-pointers-and-integers>
24+
= help: for more information about exposed provenance, see <https://doc.rust-lang.org/std/ptr/index.html#exposed-provenance>
25+
26+
warning: 2 warnings emitted
27+

0 commit comments

Comments
 (0)