Check for initialization of layout-restricted types
This commit is contained in:
parent
d7787bbaef
commit
592fecbafb
@ -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,
|
||||
|
@ -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
|
@ -1,3 +1,6 @@
|
||||
// revisions: mir thir
|
||||
// [thir]compile-flags: -Z thir-unsafeck
|
||||
|
||||
#![feature(rustc_attrs)]
|
||||
|
||||
#[rustc_layout_scalar_valid_range_start(1)]
|
||||
|
11
src/test/ui/unsafe/ranged_ints.thir.stderr
Normal file
11
src/test/ui/unsafe/ranged_ints.thir.stderr
Normal 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`.
|
@ -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
|
@ -1,3 +1,6 @@
|
||||
// revisions: mir thir
|
||||
// [thir]compile-flags: -Z thir-unsafeck
|
||||
|
||||
#![feature(rustc_attrs)]
|
||||
|
||||
#[rustc_layout_scalar_valid_range_start(1)]
|
||||
|
11
src/test/ui/unsafe/ranged_ints_const.thir.stderr
Normal file
11
src/test/ui/unsafe/ranged_ints_const.thir.stderr
Normal 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`.
|
@ -1,4 +1,7 @@
|
||||
// build-pass
|
||||
// revisions: mir thir
|
||||
// [thir]compile-flags: -Z thir-unsafeck
|
||||
|
||||
#![feature(rustc_attrs)]
|
||||
|
||||
macro_rules! apply {
|
||||
|
Loading…
x
Reference in New Issue
Block a user