forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rollup merge of rust-lang#63346 - RalfJung:zeroed-lint, r=eddyb
Lint on some incorrect uses of mem::zeroed / mem::uninitialized Cc rust-lang#62825 and https://internals.rust-lang.org/t/make-mem-uninitialized-and-mem-zeroed-panic-for-some-types-where-0-is-a-niche/10605 This does not yet handle `NonNull`/`NonZero*`, but it is a start. I also improved some doc issues I hit on the way, and added a useful helper to `TyS`. EDIT: I added the relnotes label mostly as a proposal -- I think this is worth mentioning, but leave the decision up to the release team.
- Loading branch information
Showing
15 changed files
with
354 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
// ignore-tidy-linelength | ||
// This test checks that calling `mem::{uninitialized,zeroed}` with certain types results | ||
// in a lint. | ||
|
||
#![feature(never_type)] | ||
#![allow(deprecated)] | ||
#![deny(invalid_value)] | ||
|
||
use std::mem::{self, MaybeUninit}; | ||
|
||
enum Void {} | ||
|
||
struct Ref(&'static i32); | ||
|
||
struct Wrap<T> { wrapped: T } | ||
|
||
#[allow(unused)] | ||
fn generic<T: 'static>() { | ||
unsafe { | ||
let _val: &'static T = mem::zeroed(); //~ ERROR: does not permit zero-initialization | ||
let _val: &'static T = mem::uninitialized(); //~ ERROR: does not permit being left uninitialized | ||
|
||
let _val: Wrap<&'static T> = mem::zeroed(); //~ ERROR: does not permit zero-initialization | ||
let _val: Wrap<&'static T> = mem::uninitialized(); //~ ERROR: does not permit being left uninitialized | ||
} | ||
} | ||
|
||
fn main() { | ||
unsafe { | ||
let _val: ! = mem::zeroed(); //~ ERROR: does not permit zero-initialization | ||
let _val: ! = mem::uninitialized(); //~ ERROR: does not permit being left uninitialized | ||
|
||
let _val: (i32, !) = mem::zeroed(); //~ ERROR: does not permit zero-initialization | ||
let _val: (i32, !) = mem::uninitialized(); //~ ERROR: does not permit being left uninitialized | ||
|
||
let _val: Void = mem::zeroed(); //~ ERROR: does not permit zero-initialization | ||
let _val: Void = mem::uninitialized(); //~ ERROR: does not permit being left uninitialized | ||
|
||
let _val: &'static i32 = mem::zeroed(); //~ ERROR: does not permit zero-initialization | ||
let _val: &'static i32 = mem::uninitialized(); //~ ERROR: does not permit being left uninitialized | ||
|
||
let _val: Ref = mem::zeroed(); //~ ERROR: does not permit zero-initialization | ||
let _val: Ref = mem::uninitialized(); //~ ERROR: does not permit being left uninitialized | ||
|
||
let _val: fn() = mem::zeroed(); //~ ERROR: does not permit zero-initialization | ||
let _val: fn() = mem::uninitialized(); //~ ERROR: does not permit being left uninitialized | ||
|
||
let _val: Wrap<fn()> = mem::zeroed(); //~ ERROR: does not permit zero-initialization | ||
let _val: Wrap<fn()> = mem::uninitialized(); //~ ERROR: does not permit being left uninitialized | ||
|
||
// Some types that should work just fine. | ||
let _val: Option<&'static i32> = mem::zeroed(); | ||
let _val: Option<fn()> = mem::zeroed(); | ||
let _val: MaybeUninit<&'static i32> = mem::zeroed(); | ||
let _val: bool = mem::zeroed(); | ||
let _val: i32 = mem::zeroed(); | ||
} | ||
} |
Oops, something went wrong.