@@ -18,6 +18,7 @@ use rustc_middle::middle::codegen_fn_attrs::CodegenFnAttrFlags;
1818use rustc_middle:: middle:: dependency_format:: Linkage ;
1919use rustc_middle:: middle:: exported_symbols:: ExportedSymbol ;
2020use rustc_middle:: mir;
21+ use rustc_middle:: ty:: layout:: MaybeResult ;
2122use rustc_middle:: ty:: {
2223 self ,
2324 layout:: { LayoutOf , TyAndLayout } ,
@@ -159,6 +160,35 @@ fn try_resolve_did(tcx: TyCtxt<'_>, path: &[&str], namespace: Option<Namespace>)
159160 None
160161}
161162
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+
162192/// Call `f` for each exported symbol.
163193pub fn iter_exported_symbols < ' tcx > (
164194 tcx : TyCtxt < ' tcx > ,
@@ -259,23 +289,10 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
259289 try_resolve_did ( * self . eval_context_ref ( ) . tcx , path, None ) . is_some ( )
260290 }
261291
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-
275292 /// Evaluates the scalar at the specified path.
276293 fn eval_path ( & self , path : & [ & str ] ) -> OpTy < ' tcx > {
277294 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 ) ;
279296 // We don't give a span -- this isn't actually used directly by the program anyway.
280297 let const_val = this. eval_global ( instance) . unwrap_or_else ( |err| {
281298 panic ! ( "failed to evaluate required Rust item: {path:?}\n {err:?}" )
@@ -344,19 +361,13 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
344361 "`libc` crate is not reliably available on Windows targets; Miri should not use it there"
345362 ) ;
346363 }
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] )
351365 }
352366
353367 /// Helper function to get the `TyAndLayout` of a `windows` type
354368 fn windows_ty_layout ( & self , name : & str ) -> TyAndLayout < ' tcx > {
355369 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] )
360371 }
361372
362373 /// Project to the given *named* field (which must be a struct or union type).
0 commit comments