@@ -18,6 +18,7 @@ use rustc_middle::middle::codegen_fn_attrs::CodegenFnAttrFlags;
18
18
use rustc_middle:: middle:: dependency_format:: Linkage ;
19
19
use rustc_middle:: middle:: exported_symbols:: ExportedSymbol ;
20
20
use rustc_middle:: mir;
21
+ use rustc_middle:: ty:: layout:: MaybeResult ;
21
22
use rustc_middle:: ty:: {
22
23
self ,
23
24
layout:: { LayoutOf , TyAndLayout } ,
@@ -159,6 +160,35 @@ fn try_resolve_did(tcx: TyCtxt<'_>, path: &[&str], namespace: Option<Namespace>)
159
160
None
160
161
}
161
162
163
+ /// Gets an instance for a path; fails gracefully if the path does not exist.
164
+ pub fn try_resolve_path < ' tcx > (
165
+ tcx : TyCtxt < ' tcx > ,
166
+ path : & [ & str ] ,
167
+ namespace : Namespace ,
168
+ ) -> Option < ty:: Instance < ' tcx > > {
169
+ let did = try_resolve_did ( tcx, path, Some ( namespace) ) ?;
170
+ Some ( ty:: Instance :: mono ( tcx, did) )
171
+ }
172
+
173
+ /// Gets an instance for a path.
174
+ #[ track_caller]
175
+ pub fn resolve_path < ' tcx > (
176
+ tcx : TyCtxt < ' tcx > ,
177
+ path : & [ & str ] ,
178
+ namespace : Namespace ,
179
+ ) -> ty:: Instance < ' tcx > {
180
+ try_resolve_path ( tcx, path, namespace)
181
+ . unwrap_or_else ( || panic ! ( "failed to find required Rust item: {path:?}" ) )
182
+ }
183
+
184
+ /// Gets the layout of a type at a path.
185
+ #[ track_caller]
186
+ pub fn path_ty_layout < ' tcx > ( cx : & impl LayoutOf < ' tcx > , path : & [ & str ] ) -> TyAndLayout < ' tcx > {
187
+ let ty =
188
+ resolve_path ( cx. tcx ( ) , path, Namespace :: TypeNS ) . ty ( cx. tcx ( ) , ty:: ParamEnv :: reveal_all ( ) ) ;
189
+ cx. layout_of ( ty) . to_result ( ) . ok ( ) . unwrap ( )
190
+ }
191
+
162
192
/// Call `f` for each exported symbol.
163
193
pub fn iter_exported_symbols < ' tcx > (
164
194
tcx : TyCtxt < ' tcx > ,
@@ -259,23 +289,10 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
259
289
try_resolve_did ( * self . eval_context_ref ( ) . tcx , path, None ) . is_some ( )
260
290
}
261
291
262
- /// Gets an instance for a path; fails gracefully if the path does not exist.
263
- fn try_resolve_path ( & self , path : & [ & str ] , namespace : Namespace ) -> Option < ty:: Instance < ' tcx > > {
264
- let tcx = self . eval_context_ref ( ) . tcx . tcx ;
265
- let did = try_resolve_did ( tcx, path, Some ( namespace) ) ?;
266
- Some ( ty:: Instance :: mono ( tcx, did) )
267
- }
268
-
269
- /// Gets an instance for a path.
270
- fn resolve_path ( & self , path : & [ & str ] , namespace : Namespace ) -> ty:: Instance < ' tcx > {
271
- self . try_resolve_path ( path, namespace)
272
- . unwrap_or_else ( || panic ! ( "failed to find required Rust item: {path:?}" ) )
273
- }
274
-
275
292
/// Evaluates the scalar at the specified path.
276
293
fn eval_path ( & self , path : & [ & str ] ) -> OpTy < ' tcx > {
277
294
let this = self . eval_context_ref ( ) ;
278
- let instance = this . resolve_path ( path, Namespace :: ValueNS ) ;
295
+ let instance = resolve_path ( * this . tcx , path, Namespace :: ValueNS ) ;
279
296
// We don't give a span -- this isn't actually used directly by the program anyway.
280
297
let const_val = this. eval_global ( instance) . unwrap_or_else ( |err| {
281
298
panic ! ( "failed to evaluate required Rust item: {path:?}\n {err:?}" )
@@ -344,19 +361,13 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
344
361
"`libc` crate is not reliably available on Windows targets; Miri should not use it there"
345
362
) ;
346
363
}
347
- let ty = this
348
- . resolve_path ( & [ "libc" , name] , Namespace :: TypeNS )
349
- . ty ( * this. tcx , ty:: ParamEnv :: reveal_all ( ) ) ;
350
- this. layout_of ( ty) . unwrap ( )
364
+ path_ty_layout ( this, & [ "libc" , name] )
351
365
}
352
366
353
367
/// Helper function to get the `TyAndLayout` of a `windows` type
354
368
fn windows_ty_layout ( & self , name : & str ) -> TyAndLayout < ' tcx > {
355
369
let this = self . eval_context_ref ( ) ;
356
- let ty = this
357
- . resolve_path ( & [ "std" , "sys" , "pal" , "windows" , "c" , name] , Namespace :: TypeNS )
358
- . ty ( * this. tcx , ty:: ParamEnv :: reveal_all ( ) ) ;
359
- this. layout_of ( ty) . unwrap ( )
370
+ path_ty_layout ( this, & [ "std" , "sys" , "pal" , "windows" , "c" , name] )
360
371
}
361
372
362
373
/// Project to the given *named* field (which must be a struct or union type).
0 commit comments