@@ -19,7 +19,7 @@ use log::debug;
1919
2020use rustc_driver:: Compilation ;
2121use rustc_errors:: emitter:: { ColorConfig , HumanReadableErrorType } ;
22- use rustc_hir:: def_id:: LOCAL_CRATE ;
22+ use rustc_hir:: { self as hir , def_id:: LOCAL_CRATE , Node } ;
2323use rustc_interface:: interface:: Config ;
2424use rustc_middle:: {
2525 middle:: exported_symbols:: { ExportedSymbol , SymbolExportLevel } ,
@@ -109,12 +109,27 @@ impl rustc_driver::Callbacks for MiriBeRustCompilerCalls {
109109 // https://github.com/rust-lang/rust/blob/2962e7c0089d5c136f4e9600b7abccfbbde4973d/compiler/rustc_codegen_ssa/src/back/symbol_export.rs#L62-L63
110110 // https://github.com/rust-lang/rust/blob/2962e7c0089d5c136f4e9600b7abccfbbde4973d/compiler/rustc_codegen_ssa/src/back/symbol_export.rs#L174
111111 tcx. reachable_set ( ( ) ) . iter ( ) . filter_map ( |& local_def_id| {
112- tcx. codegen_fn_attrs ( local_def_id)
113- . contains_extern_indicator ( )
114- . then_some ( (
115- ExportedSymbol :: NonGeneric ( local_def_id. to_def_id ( ) ) ,
116- SymbolExportLevel :: C ,
117- ) )
112+ // Do the same filtering that rustc does:
113+ // https://github.com/rust-lang/rust/blob/2962e7c0089d5c136f4e9600b7abccfbbde4973d/compiler/rustc_codegen_ssa/src/back/symbol_export.rs#L84-L102
114+ // Otherwise it may cause unexpected behaviours and ICEs
115+ // (https://github.com/rust-lang/rust/issues/86261).
116+ let is_reachable_non_generic = matches ! (
117+ tcx. hir( ) . get( tcx. hir( ) . local_def_id_to_hir_id( local_def_id) ) ,
118+ Node :: Item ( & hir:: Item {
119+ kind: hir:: ItemKind :: Static ( ..) | hir:: ItemKind :: Fn ( ..) ,
120+ ..
121+ } ) | Node :: ImplItem ( & hir:: ImplItem {
122+ kind: hir:: ImplItemKind :: Fn ( ..) ,
123+ ..
124+ } )
125+ if !tcx. generics_of( local_def_id) . requires_monomorphization( tcx)
126+ ) ;
127+ ( is_reachable_non_generic
128+ && tcx. codegen_fn_attrs ( local_def_id) . contains_extern_indicator ( ) )
129+ . then_some ( (
130+ ExportedSymbol :: NonGeneric ( local_def_id. to_def_id ( ) ) ,
131+ SymbolExportLevel :: C ,
132+ ) )
118133 } ) ,
119134 )
120135 }
0 commit comments