Skip to content

Commit 592fecb

Browse files
Check for initialization of layout-restricted types
1 parent d7787bb commit 592fecb

8 files changed

+46
-3
lines changed

compiler/rustc_mir_build/src/check_unsafety.rs

+13-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ use rustc_span::def_id::{DefId, LocalDefId};
1010
use rustc_span::symbol::Symbol;
1111
use rustc_span::Span;
1212

13+
use std::ops::Bound;
14+
1315
struct UnsafetyVisitor<'a, 'tcx> {
1416
tcx: TyCtxt<'tcx>,
1517
thir: &'a Thir<'tcx>,
@@ -174,6 +176,17 @@ impl<'a, 'tcx> Visitor<'a, 'tcx> for UnsafetyVisitor<'a, 'tcx> {
174176
self.requires_unsafe(expr.span, DerefOfRawPointer);
175177
}
176178
}
179+
ExprKind::Adt {
180+
adt_def,
181+
variant_index: _,
182+
substs: _,
183+
user_ty: _,
184+
fields: _,
185+
base: _,
186+
} => match self.tcx.layout_scalar_valid_range(adt_def.did) {
187+
(Bound::Unbounded, Bound::Unbounded) => {}
188+
_ => self.requires_unsafe(expr.span, InitializingTypeWith),
189+
},
177190
_ => {}
178191
}
179192

@@ -216,7 +229,6 @@ impl BodyUnsafety {
216229
enum UnsafeOpKind {
217230
CallToUnsafeFunction,
218231
UseOfInlineAssembly,
219-
#[allow(dead_code)] // FIXME
220232
InitializingTypeWith,
221233
#[allow(dead_code)] // FIXME
222234
CastOfPointerToInt,

src/test/ui/unsafe/ranged_ints.stderr renamed to src/test/ui/unsafe/ranged_ints.mir.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error[E0133]: initializing type with `rustc_layout_scalar_valid_range` attr is unsafe and requires unsafe function or block
2-
--> $DIR/ranged_ints.rs:7:14
2+
--> $DIR/ranged_ints.rs:10:14
33
|
44
LL | let _x = NonZero(0);
55
| ^^^^^^^^^^ initializing type with `rustc_layout_scalar_valid_range` attr

src/test/ui/unsafe/ranged_ints.rs

+3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
// revisions: mir thir
2+
// [thir]compile-flags: -Z thir-unsafeck
3+
14
#![feature(rustc_attrs)]
25

36
#[rustc_layout_scalar_valid_range_start(1)]
+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
error[E0133]: initializing type with `rustc_layout_scalar_valid_range` attr is unsafe and requires unsafe function or block
2+
--> $DIR/ranged_ints.rs:10:14
3+
|
4+
LL | let _x = NonZero(0);
5+
| ^^^^^^^^^^ initializing type with `rustc_layout_scalar_valid_range` attr
6+
|
7+
= note: initializing a layout restricted type's field with a value outside the valid range is undefined behavior
8+
9+
error: aborting due to previous error
10+
11+
For more information about this error, try `rustc --explain E0133`.

src/test/ui/unsafe/ranged_ints_const.stderr renamed to src/test/ui/unsafe/ranged_ints_const.mir.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error[E0133]: initializing type with `rustc_layout_scalar_valid_range` attr is unsafe and requires unsafe function or block
2-
--> $DIR/ranged_ints_const.rs:8:34
2+
--> $DIR/ranged_ints_const.rs:11:34
33
|
44
LL | const fn foo() -> NonZero<u32> { NonZero(0) }
55
| ^^^^^^^^^^ initializing type with `rustc_layout_scalar_valid_range` attr

src/test/ui/unsafe/ranged_ints_const.rs

+3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
// revisions: mir thir
2+
// [thir]compile-flags: -Z thir-unsafeck
3+
14
#![feature(rustc_attrs)]
25

36
#[rustc_layout_scalar_valid_range_start(1)]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
error[E0133]: initializing type with `rustc_layout_scalar_valid_range` attr is unsafe and requires unsafe function or block
2+
--> $DIR/ranged_ints_const.rs:11:34
3+
|
4+
LL | const fn foo() -> NonZero<u32> { NonZero(0) }
5+
| ^^^^^^^^^^ initializing type with `rustc_layout_scalar_valid_range` attr
6+
|
7+
= note: initializing a layout restricted type's field with a value outside the valid range is undefined behavior
8+
9+
error: aborting due to previous error
10+
11+
For more information about this error, try `rustc --explain E0133`.

src/test/ui/unsafe/ranged_ints_macro.rs

+3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
// build-pass
2+
// revisions: mir thir
3+
// [thir]compile-flags: -Z thir-unsafeck
4+
25
#![feature(rustc_attrs)]
36

47
macro_rules! apply {

0 commit comments

Comments
 (0)