Skip to content

Commit

Permalink
Check for initialization of layout-restricted types
Browse files Browse the repository at this point in the history
  • Loading branch information
LeSeulArtichaut committed May 21, 2021
1 parent d7787bb commit 592fecb
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 3 deletions.
14 changes: 13 additions & 1 deletion compiler/rustc_mir_build/src/check_unsafety.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ use rustc_span::def_id::{DefId, LocalDefId};
use rustc_span::symbol::Symbol;
use rustc_span::Span;

use std::ops::Bound;

struct UnsafetyVisitor<'a, 'tcx> {
tcx: TyCtxt<'tcx>,
thir: &'a Thir<'tcx>,
Expand Down Expand Up @@ -174,6 +176,17 @@ impl<'a, 'tcx> Visitor<'a, 'tcx> for UnsafetyVisitor<'a, 'tcx> {
self.requires_unsafe(expr.span, DerefOfRawPointer);
}
}
ExprKind::Adt {
adt_def,
variant_index: _,
substs: _,
user_ty: _,
fields: _,
base: _,
} => match self.tcx.layout_scalar_valid_range(adt_def.did) {
(Bound::Unbounded, Bound::Unbounded) => {}
_ => self.requires_unsafe(expr.span, InitializingTypeWith),
},
_ => {}
}

Expand Down Expand Up @@ -216,7 +229,6 @@ impl BodyUnsafety {
enum UnsafeOpKind {
CallToUnsafeFunction,
UseOfInlineAssembly,
#[allow(dead_code)] // FIXME
InitializingTypeWith,
#[allow(dead_code)] // FIXME
CastOfPointerToInt,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error[E0133]: initializing type with `rustc_layout_scalar_valid_range` attr is unsafe and requires unsafe function or block
--> $DIR/ranged_ints.rs:7:14
--> $DIR/ranged_ints.rs:10:14
|
LL | let _x = NonZero(0);
| ^^^^^^^^^^ initializing type with `rustc_layout_scalar_valid_range` attr
Expand Down
3 changes: 3 additions & 0 deletions src/test/ui/unsafe/ranged_ints.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// revisions: mir thir
// [thir]compile-flags: -Z thir-unsafeck

#![feature(rustc_attrs)]

#[rustc_layout_scalar_valid_range_start(1)]
Expand Down
11 changes: 11 additions & 0 deletions src/test/ui/unsafe/ranged_ints.thir.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
error[E0133]: initializing type with `rustc_layout_scalar_valid_range` attr is unsafe and requires unsafe function or block
--> $DIR/ranged_ints.rs:10:14
|
LL | let _x = NonZero(0);
| ^^^^^^^^^^ initializing type with `rustc_layout_scalar_valid_range` attr
|
= note: initializing a layout restricted type's field with a value outside the valid range is undefined behavior

error: aborting due to previous error

For more information about this error, try `rustc --explain E0133`.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error[E0133]: initializing type with `rustc_layout_scalar_valid_range` attr is unsafe and requires unsafe function or block
--> $DIR/ranged_ints_const.rs:8:34
--> $DIR/ranged_ints_const.rs:11:34
|
LL | const fn foo() -> NonZero<u32> { NonZero(0) }
| ^^^^^^^^^^ initializing type with `rustc_layout_scalar_valid_range` attr
Expand Down
3 changes: 3 additions & 0 deletions src/test/ui/unsafe/ranged_ints_const.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// revisions: mir thir
// [thir]compile-flags: -Z thir-unsafeck

#![feature(rustc_attrs)]

#[rustc_layout_scalar_valid_range_start(1)]
Expand Down
11 changes: 11 additions & 0 deletions src/test/ui/unsafe/ranged_ints_const.thir.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
error[E0133]: initializing type with `rustc_layout_scalar_valid_range` attr is unsafe and requires unsafe function or block
--> $DIR/ranged_ints_const.rs:11:34
|
LL | const fn foo() -> NonZero<u32> { NonZero(0) }
| ^^^^^^^^^^ initializing type with `rustc_layout_scalar_valid_range` attr
|
= note: initializing a layout restricted type's field with a value outside the valid range is undefined behavior

error: aborting due to previous error

For more information about this error, try `rustc --explain E0133`.
3 changes: 3 additions & 0 deletions src/test/ui/unsafe/ranged_ints_macro.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
// build-pass
// revisions: mir thir
// [thir]compile-flags: -Z thir-unsafeck

#![feature(rustc_attrs)]

macro_rules! apply {
Expand Down

0 comments on commit 592fecb

Please sign in to comment.