Check for initialization of layout-restricted types

This commit is contained in:
LeSeulArtichaut 2021-05-15 15:26:28 +02:00
parent d7787bbaef
commit 592fecbafb
8 changed files with 46 additions and 3 deletions

View File

@ -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>,
@ -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),
},
_ => {}
}
@ -216,7 +229,6 @@ impl BodyUnsafety {
enum UnsafeOpKind {
CallToUnsafeFunction,
UseOfInlineAssembly,
#[allow(dead_code)] // FIXME
InitializingTypeWith,
#[allow(dead_code)] // FIXME
CastOfPointerToInt,

View File

@ -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

View File

@ -1,3 +1,6 @@
// revisions: mir thir
// [thir]compile-flags: -Z thir-unsafeck
#![feature(rustc_attrs)]
#[rustc_layout_scalar_valid_range_start(1)]

View File

@ -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`.

View File

@ -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

View File

@ -1,3 +1,6 @@
// revisions: mir thir
// [thir]compile-flags: -Z thir-unsafeck
#![feature(rustc_attrs)]
#[rustc_layout_scalar_valid_range_start(1)]

View File

@ -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`.

View File

@ -1,4 +1,7 @@
// build-pass
// revisions: mir thir
// [thir]compile-flags: -Z thir-unsafeck
#![feature(rustc_attrs)]
macro_rules! apply {