1
1
use rustc_abi:: { BackendRepr , FieldsShape , Scalar , Variants } ;
2
- use rustc_middle:: bug ;
2
+ use rustc_middle:: query :: TyCtxtAt ;
3
3
use rustc_middle:: ty:: layout:: {
4
4
HasTyCtxt , LayoutCx , LayoutError , LayoutOf , TyAndLayout , ValidityRequirement ,
5
5
} ;
6
- use rustc_middle:: ty:: { PseudoCanonicalInput , Ty , TyCtxt } ;
6
+ use rustc_middle:: ty:: { PseudoCanonicalInput , ScalarInt , Ty , TyCtxt } ;
7
+ use rustc_middle:: { bug, ty} ;
7
8
8
9
use crate :: const_eval:: { CanAccessMutGlobal , CheckAlignment , CompileTimeMachine } ;
9
10
use crate :: interpret:: { InterpCx , MemoryKind } ;
@@ -34,7 +35,7 @@ pub fn check_validity_requirement<'tcx>(
34
35
35
36
let layout_cx = LayoutCx :: new ( tcx, input. typing_env ) ;
36
37
if kind == ValidityRequirement :: Uninit || tcx. sess . opts . unstable_opts . strict_init_checks {
37
- check_validity_requirement_strict ( layout, & layout_cx, kind)
38
+ Ok ( check_validity_requirement_strict ( layout, & layout_cx, kind) )
38
39
} else {
39
40
check_validity_requirement_lax ( layout, & layout_cx, kind)
40
41
}
@@ -46,7 +47,7 @@ fn check_validity_requirement_strict<'tcx>(
46
47
ty : TyAndLayout < ' tcx > ,
47
48
cx : & LayoutCx < ' tcx > ,
48
49
kind : ValidityRequirement ,
49
- ) -> Result < bool , & ' tcx LayoutError < ' tcx > > {
50
+ ) -> bool {
50
51
let machine = CompileTimeMachine :: new ( CanAccessMutGlobal :: No , CheckAlignment :: Error ) ;
51
52
52
53
let mut cx = InterpCx :: new ( cx. tcx ( ) , rustc_span:: DUMMY_SP , cx. typing_env , machine) ;
@@ -69,14 +70,13 @@ fn check_validity_requirement_strict<'tcx>(
69
70
// due to this.
70
71
// The value we are validating is temporary and discarded at the end of this function, so
71
72
// there is no point in reseting provenance and padding.
72
- Ok ( cx
73
- . validate_operand (
74
- & allocated. into ( ) ,
75
- /*recursive*/ false ,
76
- /*reset_provenance_and_padding*/ false ,
77
- )
78
- . discard_err ( )
79
- . is_some ( ) )
73
+ cx. validate_operand (
74
+ & allocated. into ( ) ,
75
+ /*recursive*/ false ,
76
+ /*reset_provenance_and_padding*/ false ,
77
+ )
78
+ . discard_err ( )
79
+ . is_some ( )
80
80
}
81
81
82
82
/// Implements the 'lax' (default) version of the [`check_validity_requirement`] checks; see that
@@ -168,3 +168,29 @@ fn check_validity_requirement_lax<'tcx>(
168
168
169
169
Ok ( true )
170
170
}
171
+
172
+ pub ( crate ) fn validate_scalar_in_layout < ' tcx > (
173
+ tcx : TyCtxtAt < ' tcx > ,
174
+ scalar : ScalarInt ,
175
+ ty : Ty < ' tcx > ,
176
+ ) -> bool {
177
+ let machine = CompileTimeMachine :: new ( CanAccessMutGlobal :: No , CheckAlignment :: Error ) ;
178
+
179
+ let typing_env = ty:: TypingEnv :: fully_monomorphized ( ) ;
180
+ let mut cx = InterpCx :: new ( tcx. tcx , tcx. span , typing_env, machine) ;
181
+
182
+ let Ok ( layout) = cx. layout_of ( ty) else { return false } ;
183
+ let allocated = cx
184
+ . allocate ( layout, MemoryKind :: Machine ( crate :: const_eval:: MemoryKind :: Heap ) )
185
+ . expect ( "OOM: failed to allocate for uninit check" ) ;
186
+
187
+ cx. write_scalar ( scalar, & allocated) . unwrap ( ) ;
188
+
189
+ cx. validate_operand (
190
+ & allocated. into ( ) ,
191
+ /*recursive*/ false ,
192
+ /*reset_provenance_and_padding*/ false ,
193
+ )
194
+ . discard_err ( )
195
+ . is_some ( )
196
+ }
0 commit comments