Skip to content

Commit df4e12d

Browse files
committed
uninit/zeroed lint: warn against NULL vtables
1 parent 87cbf0a commit df4e12d

File tree

4 files changed

+35
-7
lines changed

4 files changed

+35
-7
lines changed

src/librustc_lint/builtin.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1949,6 +1949,8 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for InvalidValue {
19491949
Adt(..) if ty.is_box() => Some((format!("`Box` must be non-null"), None)),
19501950
FnPtr(..) => Some((format!("Function pointers must be non-null"), None)),
19511951
Never => Some((format!("The never type (`!`) has no valid value"), None)),
1952+
RawPtr(tm) if matches!(tm.ty.kind, Dynamic(..)) => // raw ptr to dyn Trait
1953+
Some((format!("The vtable of a wide raw pointer must be non-null"), None)),
19521954
// Primitive types with other constraints.
19531955
Bool if init == InitKind::Uninit =>
19541956
Some((format!("Booleans must be `true` or `false`"), None)),

src/librustc_lint/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#![feature(box_patterns)]
1616
#![feature(box_syntax)]
1717
#![feature(nll)]
18+
#![feature(matches_macro)]
1819

1920
#![recursion_limit="256"]
2021

src/test/ui/lint/uninitialized-zeroed.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,9 @@ fn main() {
6767
let _val: NonNull<i32> = mem::zeroed(); //~ ERROR: does not permit zero-initialization
6868
let _val: NonNull<i32> = mem::uninitialized(); //~ ERROR: does not permit being left uninitialized
6969

70+
let _val: *const dyn Send = mem::zeroed(); //~ ERROR: does not permit zero-initialization
71+
let _val: *const dyn Send = mem::uninitialized(); //~ ERROR: does not permit being left uninitialized
72+
7073
// Things that can be zero, but not uninit.
7174
let _val: bool = mem::zeroed();
7275
let _val: bool = mem::uninitialized(); //~ ERROR: does not permit being left uninitialized

src/test/ui/lint/uninitialized-zeroed.stderr

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -307,8 +307,30 @@ LL | let _val: NonNull<i32> = mem::uninitialized();
307307
|
308308
= note: std::ptr::NonNull<i32> must be non-null
309309

310+
error: the type `*const dyn std::marker::Send` does not permit zero-initialization
311+
--> $DIR/uninitialized-zeroed.rs:70:37
312+
|
313+
LL | let _val: *const dyn Send = mem::zeroed();
314+
| ^^^^^^^^^^^^^
315+
| |
316+
| this code causes undefined behavior when executed
317+
| help: use `MaybeUninit<T>` instead
318+
|
319+
= note: The vtable of a wide raw pointer must be non-null
320+
321+
error: the type `*const dyn std::marker::Send` does not permit being left uninitialized
322+
--> $DIR/uninitialized-zeroed.rs:71:37
323+
|
324+
LL | let _val: *const dyn Send = mem::uninitialized();
325+
| ^^^^^^^^^^^^^^^^^^^^
326+
| |
327+
| this code causes undefined behavior when executed
328+
| help: use `MaybeUninit<T>` instead
329+
|
330+
= note: The vtable of a wide raw pointer must be non-null
331+
310332
error: the type `bool` does not permit being left uninitialized
311-
--> $DIR/uninitialized-zeroed.rs:72:26
333+
--> $DIR/uninitialized-zeroed.rs:75:26
312334
|
313335
LL | let _val: bool = mem::uninitialized();
314336
| ^^^^^^^^^^^^^^^^^^^^
@@ -319,7 +341,7 @@ LL | let _val: bool = mem::uninitialized();
319341
= note: Booleans must be `true` or `false`
320342

321343
error: the type `Wrap<char>` does not permit being left uninitialized
322-
--> $DIR/uninitialized-zeroed.rs:75:32
344+
--> $DIR/uninitialized-zeroed.rs:78:32
323345
|
324346
LL | let _val: Wrap<char> = mem::uninitialized();
325347
| ^^^^^^^^^^^^^^^^^^^^
@@ -334,7 +356,7 @@ LL | struct Wrap<T> { wrapped: T }
334356
| ^^^^^^^^^^
335357

336358
error: the type `NonBig` does not permit being left uninitialized
337-
--> $DIR/uninitialized-zeroed.rs:78:28
359+
--> $DIR/uninitialized-zeroed.rs:81:28
338360
|
339361
LL | let _val: NonBig = mem::uninitialized();
340362
| ^^^^^^^^^^^^^^^^^^^^
@@ -345,7 +367,7 @@ LL | let _val: NonBig = mem::uninitialized();
345367
= note: NonBig must be initialized inside its custom valid range
346368

347369
error: the type `&'static i32` does not permit zero-initialization
348-
--> $DIR/uninitialized-zeroed.rs:81:34
370+
--> $DIR/uninitialized-zeroed.rs:84:34
349371
|
350372
LL | let _val: &'static i32 = mem::transmute(0usize);
351373
| ^^^^^^^^^^^^^^^^^^^^^^
@@ -356,7 +378,7 @@ LL | let _val: &'static i32 = mem::transmute(0usize);
356378
= note: References must be non-null
357379

358380
error: the type `&'static [i32]` does not permit zero-initialization
359-
--> $DIR/uninitialized-zeroed.rs:82:36
381+
--> $DIR/uninitialized-zeroed.rs:85:36
360382
|
361383
LL | let _val: &'static [i32] = mem::transmute((0usize, 0usize));
362384
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -367,7 +389,7 @@ LL | let _val: &'static [i32] = mem::transmute((0usize, 0usize));
367389
= note: References must be non-null
368390

369391
error: the type `std::num::NonZeroU32` does not permit zero-initialization
370-
--> $DIR/uninitialized-zeroed.rs:83:32
392+
--> $DIR/uninitialized-zeroed.rs:86:32
371393
|
372394
LL | let _val: NonZeroU32 = mem::transmute(0);
373395
| ^^^^^^^^^^^^^^^^^
@@ -377,5 +399,5 @@ LL | let _val: NonZeroU32 = mem::transmute(0);
377399
|
378400
= note: std::num::NonZeroU32 must be non-null
379401

380-
error: aborting due to 30 previous errors
402+
error: aborting due to 32 previous errors
381403

0 commit comments

Comments
 (0)