1
- use rustc_middle:: ty:: layout:: { LayoutCx , LayoutOf , TyAndLayout } ;
2
- use rustc_middle:: ty:: { ParamEnv , TyCtxt } ;
1
+ use rustc_middle:: ty:: layout:: { LayoutCx , LayoutError , LayoutOf , TyAndLayout } ;
2
+ use rustc_middle:: ty:: { ParamEnv , ParamEnvAnd , Ty , TyCtxt } ;
3
3
use rustc_session:: Limit ;
4
4
use rustc_target:: abi:: { Abi , FieldsShape , InitKind , Scalar , Variants } ;
5
5
@@ -20,15 +20,14 @@ use crate::interpret::{InterpCx, MemoryKind, OpTy};
20
20
/// to the full uninit check).
21
21
pub fn might_permit_raw_init < ' tcx > (
22
22
tcx : TyCtxt < ' tcx > ,
23
- param_env : ParamEnv < ' tcx > ,
24
- ty : TyAndLayout < ' tcx > ,
23
+ param_env_and_ty : ParamEnvAnd < ' tcx , Ty < ' tcx > > ,
25
24
kind : InitKind ,
26
- ) -> bool {
25
+ ) -> Result < bool , LayoutError < ' tcx > > {
27
26
if tcx. sess . opts . unstable_opts . strict_init_checks {
28
- might_permit_raw_init_strict ( ty , tcx, kind)
27
+ might_permit_raw_init_strict ( tcx . layout_of ( param_env_and_ty ) ? , tcx, kind)
29
28
} else {
30
- let layout_cx = LayoutCx { tcx, param_env } ;
31
- might_permit_raw_init_lax ( ty , & layout_cx, kind)
29
+ let layout_cx = LayoutCx { tcx, param_env : param_env_and_ty . param_env } ;
30
+ might_permit_raw_init_lax ( tcx . layout_of ( param_env_and_ty ) ? , & layout_cx, kind)
32
31
}
33
32
}
34
33
@@ -38,7 +37,7 @@ fn might_permit_raw_init_strict<'tcx>(
38
37
ty : TyAndLayout < ' tcx > ,
39
38
tcx : TyCtxt < ' tcx > ,
40
39
kind : InitKind ,
41
- ) -> bool {
40
+ ) -> Result < bool , LayoutError < ' tcx > > {
42
41
let machine = CompileTimeInterpreter :: new (
43
42
Limit :: new ( 0 ) ,
44
43
/*can_access_statics:*/ false ,
@@ -65,7 +64,7 @@ fn might_permit_raw_init_strict<'tcx>(
65
64
// This does *not* actually check that references are dereferenceable, but since all types that
66
65
// require dereferenceability also require non-null, we don't actually get any false negatives
67
66
// due to this.
68
- cx. validate_operand ( & ot) . is_ok ( )
67
+ Ok ( cx. validate_operand ( & ot) . is_ok ( ) )
69
68
}
70
69
71
70
/// Implements the 'lax' (default) version of the `might_permit_raw_init` checks; see that function for
@@ -74,7 +73,7 @@ fn might_permit_raw_init_lax<'tcx>(
74
73
this : TyAndLayout < ' tcx > ,
75
74
cx : & LayoutCx < ' tcx , TyCtxt < ' tcx > > ,
76
75
init_kind : InitKind ,
77
- ) -> bool {
76
+ ) -> Result < bool , LayoutError < ' tcx > > {
78
77
let scalar_allows_raw_init = move |s : Scalar | -> bool {
79
78
match init_kind {
80
79
InitKind :: Zero => {
@@ -103,20 +102,20 @@ fn might_permit_raw_init_lax<'tcx>(
103
102
} ;
104
103
if !valid {
105
104
// This is definitely not okay.
106
- return false ;
105
+ return Ok ( false ) ;
107
106
}
108
107
109
108
// Special magic check for references and boxes (i.e., special pointer types).
110
109
if let Some ( pointee) = this. ty . builtin_deref ( false ) {
111
- let pointee = cx. layout_of ( pointee. ty ) . expect ( "need to be able to compute layouts" ) ;
110
+ let pointee = cx. layout_of ( pointee. ty ) ? ;
112
111
// We need to ensure that the LLVM attributes `aligned` and `dereferenceable(size)` are satisfied.
113
112
if pointee. align . abi . bytes ( ) > 1 {
114
113
// 0x01-filling is not aligned.
115
- return false ;
114
+ return Ok ( false ) ;
116
115
}
117
116
if pointee. size . bytes ( ) > 0 {
118
117
// A 'fake' integer pointer is not sufficiently dereferenceable.
119
- return false ;
118
+ return Ok ( false ) ;
120
119
}
121
120
}
122
121
@@ -129,9 +128,9 @@ fn might_permit_raw_init_lax<'tcx>(
129
128
}
130
129
FieldsShape :: Arbitrary { offsets, .. } => {
131
130
for idx in 0 ..offsets. len ( ) {
132
- if !might_permit_raw_init_lax ( this. field ( cx, idx) , cx, init_kind) {
131
+ if !might_permit_raw_init_lax ( this. field ( cx, idx) , cx, init_kind) ? {
133
132
// We found a field that is unhappy with this kind of initialization.
134
- return false ;
133
+ return Ok ( false ) ;
135
134
}
136
135
}
137
136
}
@@ -148,5 +147,5 @@ fn might_permit_raw_init_lax<'tcx>(
148
147
}
149
148
}
150
149
151
- true
150
+ Ok ( true )
152
151
}
0 commit comments