@@ -1372,7 +1372,7 @@ pub struct PointeeInfo {
1372
1372
1373
1373
/// Used in `might_permit_raw_init` to indicate the kind of initialisation
1374
1374
/// that is checked to be valid
1375
- #[ derive( Copy , Clone , Debug ) ]
1375
+ #[ derive( Copy , Clone , Debug , PartialEq , Eq ) ]
1376
1376
pub enum InitKind {
1377
1377
Zero ,
1378
1378
Uninit ,
@@ -1487,14 +1487,18 @@ impl<'a, Ty> TyAndLayout<'a, Ty> {
1487
1487
///
1488
1488
/// `init_kind` indicates if the memory is zero-initialized or left uninitialized.
1489
1489
///
1490
- /// `strict` is an opt-in debugging flag added in #97323 that enables more checks.
1490
+ /// This code is intentionally conservative, and will not detect
1491
+ /// * zero init of an enum whose 0 variant does not allow zero initialization
1492
+ /// * making uninitialized types who have a full valid range (ints, floats, raw pointers)
1493
+ /// * Any form of invalid value being made inside an array (unless the value is uninhabited)
1491
1494
///
1492
- /// This is conservative: in doubt, it will answer `true`.
1495
+ /// A strict form of these checks that uses const evaluation exists in
1496
+ /// `rustc_const_eval::might_permit_raw_init`, and a tracking issue for making these checks
1497
+ /// stricter is <https://github.com/rust-lang/rust/issues/66151>.
1493
1498
///
1494
- /// FIXME: Once we removed all the conservatism, we could alternatively
1495
- /// create an all-0/all-undef constant and run the const value validator to see if
1496
- /// this is a valid value for the given type.
1497
- pub fn might_permit_raw_init < C > ( self , cx : & C , init_kind : InitKind , strict : bool ) -> bool
1499
+ /// FIXME: Once all the conservatism is removed from here, and the checks are ran by default,
1500
+ /// we can use the const evaluation checks always instead.
1501
+ pub fn might_permit_raw_init < C > ( self , cx : & C , init_kind : InitKind ) -> bool
1498
1502
where
1499
1503
Self : Copy ,
1500
1504
Ty : TyAbiInterface < ' a , C > ,
@@ -1507,13 +1511,8 @@ impl<'a, Ty> TyAndLayout<'a, Ty> {
1507
1511
s. valid_range ( cx) . contains ( 0 )
1508
1512
}
1509
1513
InitKind :: Uninit => {
1510
- if strict {
1511
- // The type must be allowed to be uninit (which means "is a union").
1512
- s. is_uninit_valid ( )
1513
- } else {
1514
- // The range must include all values.
1515
- s. is_always_valid ( cx)
1516
- }
1514
+ // The range must include all values.
1515
+ s. is_always_valid ( cx)
1517
1516
}
1518
1517
}
1519
1518
} ;
@@ -1534,19 +1533,12 @@ impl<'a, Ty> TyAndLayout<'a, Ty> {
1534
1533
// If we have not found an error yet, we need to recursively descend into fields.
1535
1534
match & self . fields {
1536
1535
FieldsShape :: Primitive | FieldsShape :: Union { .. } => { }
1537
- FieldsShape :: Array { count , .. } => {
1536
+ FieldsShape :: Array { .. } => {
1538
1537
// FIXME(#66151): For now, we are conservative and do not check arrays by default.
1539
- if strict
1540
- && * count > 0
1541
- && !self . field ( cx, 0 ) . might_permit_raw_init ( cx, init_kind, strict)
1542
- {
1543
- // Found non empty array with a type that is unhappy about this kind of initialization
1544
- return false ;
1545
- }
1546
1538
}
1547
1539
FieldsShape :: Arbitrary { offsets, .. } => {
1548
1540
for idx in 0 ..offsets. len ( ) {
1549
- if !self . field ( cx, idx) . might_permit_raw_init ( cx, init_kind, strict ) {
1541
+ if !self . field ( cx, idx) . might_permit_raw_init ( cx, init_kind) {
1550
1542
// We found a field that is unhappy with this kind of initialization.
1551
1543
return false ;
1552
1544
}
0 commit comments