@@ -105,24 +105,41 @@ fn try_resolve_did(tcx: TyCtxt<'_>, path: &[&str], namespace: Option<Namespace>)
105
105
( path, None )
106
106
} ;
107
107
108
- // First find the crate.
109
- let krate =
110
- tcx. crates ( ( ) ) . iter ( ) . find ( |& & krate| tcx. crate_name ( krate) . as_str ( ) == crate_name) ?;
111
- let mut cur_item = DefId { krate : * krate, index : CRATE_DEF_INDEX } ;
112
- // Then go over the modules.
113
- for & segment in modules {
114
- cur_item = find_children ( tcx, cur_item, segment)
115
- . find ( |item| tcx. def_kind ( item) == DefKind :: Mod ) ?;
116
- }
117
- // Finally, look up the desired item in this module, if any.
118
- match item {
119
- Some ( ( item_name, namespace) ) =>
120
- Some (
121
- find_children ( tcx, cur_item, item_name)
122
- . find ( |item| tcx. def_kind ( item) . ns ( ) == Some ( namespace) ) ?,
123
- ) ,
124
- None => Some ( cur_item) ,
108
+ // There may be more than one crate with this name. We try them all.
109
+ // (This is particularly relevant when running `std` tests as then there are two `std` crates:
110
+ // the one in the sysroot and the one locally built by `cargo test`.)
111
+ // FIXME: can we prefer the one from the sysroot?
112
+ ' crates: for krate in
113
+ tcx. crates ( ( ) ) . iter ( ) . filter ( |& & krate| tcx. crate_name ( krate) . as_str ( ) == crate_name)
114
+ {
115
+ let mut cur_item = DefId { krate : * krate, index : CRATE_DEF_INDEX } ;
116
+ // Go over the modules.
117
+ for & segment in modules {
118
+ let Some ( next_item) = find_children ( tcx, cur_item, segment)
119
+ . find ( |item| tcx. def_kind ( item) == DefKind :: Mod )
120
+ else {
121
+ continue ' crates;
122
+ } ;
123
+ cur_item = next_item;
124
+ }
125
+ // Finally, look up the desired item in this module, if any.
126
+ match item {
127
+ Some ( ( item_name, namespace) ) => {
128
+ let Some ( item) = find_children ( tcx, cur_item, item_name)
129
+ . find ( |item| tcx. def_kind ( item) . ns ( ) == Some ( namespace) )
130
+ else {
131
+ continue ' crates;
132
+ } ;
133
+ return Some ( item) ;
134
+ }
135
+ None => {
136
+ // Just return the module.
137
+ return Some ( cur_item) ;
138
+ }
139
+ }
125
140
}
141
+ // Item not found in any of the crates with the right name.
142
+ None
126
143
}
127
144
128
145
/// Convert a softfloat type to its corresponding hostfloat type.
@@ -968,10 +985,6 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
968
985
969
986
fn frame_in_std ( & self ) -> bool {
970
987
let this = self . eval_context_ref ( ) ;
971
- let Some ( start_fn) = this. tcx . lang_items ( ) . start_fn ( ) else {
972
- // no_std situations
973
- return false ;
974
- } ;
975
988
let frame = this. frame ( ) ;
976
989
// Make an attempt to get at the instance of the function this is inlined from.
977
990
let instance: Option < _ > = try {
@@ -982,13 +995,15 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
982
995
} ;
983
996
// Fall back to the instance of the function itself.
984
997
let instance = instance. unwrap_or ( frame. instance ) ;
985
- // Now check if this is in the same crate as start_fn.
986
- // As a special exception we also allow unit tests from
987
- // <https://github.com/rust-lang/miri-test-libstd/tree/master/std_miri_test> to call these
988
- // shims .
998
+ // Now check the crate it is in. We could try to be clever here and e.g. check if this is
999
+ // the same crate as `start_fn`, but that would not work for running std tests in Miri, so
1000
+ // we'd need some more hacks anyway. So we just check the name of the crate. If someone
1001
+ // calls their crate `std` then we'll just let them keep the pieces .
989
1002
let frame_crate = this. tcx . def_path ( instance. def_id ( ) ) . krate ;
990
- frame_crate == this. tcx . def_path ( start_fn) . krate
991
- || this. tcx . crate_name ( frame_crate) . as_str ( ) == "std_miri_test"
1003
+ let crate_name = this. tcx . crate_name ( frame_crate) ;
1004
+ let crate_name = crate_name. as_str ( ) ;
1005
+ // On miri-test-libstd, the name of the crate is different.
1006
+ crate_name == "std" || crate_name == "std_miri_test"
992
1007
}
993
1008
994
1009
/// Handler that should be called when unsupported functionality is encountered.
0 commit comments