kind: UnsafetyViolationKind::BorrowPacked(lint_root)
}], &[]);
}
+ if context.is_mutating_use() {
+ self.check_mut_borrowing_layout_constrained_field(place);
+ }
}
let old_source_info = self.source_info;
if let &Place::Local(local) = base {
(node_id, is_used && !within_unsafe)
}));
}
+ fn check_mut_borrowing_layout_constrained_field(
+ &mut self,
+ mut place: &Place<'tcx>,
+ ) {
+ while let &Place::Projection(box Projection {
+ ref base, ref elem
+ }) = place {
+ match *elem {
+ ProjectionElem::Field(..) => {
+ let ty = base.ty(&self.mir.local_decls, self.tcx).to_ty(self.tcx);
+ match ty.sty {
+ ty::Adt(def, _) => match self.tcx.layout_scalar_valid_range(def.did) {
+ (Bound::Unbounded, Bound::Unbounded) => {},
+ _ => {
+ let source_info = self.source_info;
+ self.register_violations(&[UnsafetyViolation {
+ source_info,
+ description: Symbol::intern(
+ "borrow of layout constrained field",
+ ).as_interned_str(),
+ details:
+ Symbol::intern(
+ "references to fields of layout constrained fields \
+ lose the constraints",
+ ).as_interned_str(),
+ kind: UnsafetyViolationKind::MinConstFn,
+ }], &[]);
+ }
+ },
+ _ => {}
+ }
+ }
+ _ => {}
+ }
+ place = base;
+ }
+ }
}
pub(crate) fn provide(providers: &mut Providers) {
--- /dev/null
+#![feature(rustc_attrs)]
+
+#[rustc_layout_scalar_valid_range_start(1)]
+#[repr(transparent)]
+pub(crate) struct NonZero<T>(pub(crate) T);
+fn main() {
+ let _x = NonZero(0); //~ ERROR initializing type with `rustc_layout_scalar_valid_range` attr
+}
\ No newline at end of file
--- /dev/null
+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
+ |
+LL | let _x = NonZero(0); //~ ERROR initializing type with `rustc_layout_scalar_valid_range` attr
+ | ^^^^^^^^^^ initializing type with `rustc_layout_scalar_valid_range` attr
+ |
+ = note: initializing `NonZero` with a `0` violates layout constraints and is undefined behavior
+
+error: aborting due to previous error
+
+For more information about this error, try `rustc --explain E0133`.
--- /dev/null
+#![feature(rustc_attrs)]
+
+#[rustc_layout_scalar_valid_range_start(1)]
+#[repr(transparent)]
+pub(crate) struct NonZero<T>(pub(crate) T);
+fn main() {
+ let mut x = unsafe { NonZero(1) };
+ let y = &mut x.0; //~ ERROR borrow of layout constrained field is unsafe
+}
\ No newline at end of file
--- /dev/null
+error[E0133]: borrow of layout constrained field is unsafe and requires unsafe function or block
+ --> $DIR/ranged_ints2.rs:8:13
+ |
+LL | let y = &mut x.0; //~ ERROR borrow of layout constrained field is unsafe
+ | ^^^^^^^^ borrow of layout constrained field
+ |
+ = note: references to fields of layout constrained fields lose the constraints
+
+error: aborting due to previous error
+
+For more information about this error, try `rustc --explain E0133`.