Skip to content

Commit f1977c9

Browse files
Check for initialization of layout-restricted types
1 parent f94942d commit f1977c9

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
@@ -9,6 +9,8 @@ use rustc_session::lint::Level;
99
use rustc_span::def_id::{DefId, LocalDefId};
1010
use rustc_span::Span;
1111

12+
use std::ops::Bound;
13+
1214
struct UnsafetyVisitor<'a, 'tcx> {
1315
tcx: TyCtxt<'tcx>,
1416
thir: &'a Thir<'tcx>,
@@ -153,6 +155,17 @@ impl<'a, 'tcx> Visitor<'a, 'tcx> for UnsafetyVisitor<'a, 'tcx> {
153155
ExprKind::InlineAsm { .. } | ExprKind::LlvmInlineAsm { .. } => {
154156
self.requires_unsafe(expr.span, UseOfInlineAssembly);
155157
}
158+
ExprKind::Adt {
159+
adt_def,
160+
variant_index: _,
161+
substs: _,
162+
user_ty: _,
163+
fields: _,
164+
base: _,
165+
} => match self.tcx.layout_scalar_valid_range(adt_def.did) {
166+
(Bound::Unbounded, Bound::Unbounded) => {}
167+
_ => self.requires_unsafe(expr.span, InitializingTypeWith),
168+
},
156169
_ => {}
157170
}
158171

@@ -195,7 +208,6 @@ impl BodyUnsafety {
195208
enum UnsafeOpKind {
196209
CallToUnsafeFunction,
197210
UseOfInlineAssembly,
198-
#[allow(dead_code)] // FIXME
199211
InitializingTypeWith,
200212
#[allow(dead_code)] // FIXME
201213
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)