44//! An item is "externally reachable" if it is relevant for other crates. This obviously includes
55//! all public items. However, some of these items cannot be compiled to machine code (because they
66//! are generic), and for some the machine code is not sufficient (because we want to cross-crate
7- //! inline them, or run them in the compile-time MIR interpreter ). These items "need cross-crate
8- //! MIR". When a reachable function `f` needs cross-crate MIR, then all the functions it calls also
9- //! become reachable, as they will be necessary to use the MIR of `f` from another crate.
7+ //! inline them). These items "need cross-crate MIR". When a reachable function `f` needs
8+ //! cross-crate MIR, then all the functions it calls also become reachable, as they will be
9+ //! necessary to use the MIR of `f` from another crate.
1010
1111use hir:: def_id:: LocalDefIdSet ;
1212use rustc_data_structures:: stack:: ensure_sufficient_stack;
@@ -26,6 +26,12 @@ use rustc_target::spec::abi::Abi;
2626
2727/// Determines whether this (assumed to be reachable) item "needs cross-crate MIR", i.e. whether
2828/// another crate needs to be able to access this items' MIR.
29+ ///
30+ /// On top of what was explained above, `const fn` also "need cross-crate MIR". This is *not*
31+ /// because we need the MIR to interpret them (MIR for const-eval and MIR for codegen is separate,
32+ /// and MIR for const-eval is always encoded). Instead, it is because `const fn` can create `fn()`
33+ /// pointers to other items which end up in the evaluated result of the constant and can then be
34+ /// called from other crates. Those items must be considered reachable.
2935fn needs_cross_crate_mir ( tcx : TyCtxt < ' _ > , def_id : DefId ) -> bool {
3036 tcx. generics_of ( def_id) . requires_monomorphization ( tcx)
3137 || tcx. cross_crate_inlinable ( def_id)
@@ -449,6 +455,8 @@ fn reachable_set(tcx: TyCtxt<'_>, (): ()) -> LocalDefIdSet {
449455 // items of non-exported traits (or maybe all local traits?) unless their respective
450456 // trait items are used from inlinable code through method call syntax or UFCS, or their
451457 // trait is a lang item.
458+ // (But if you implement this, don't forget to take into account that vtables and also
459+ // make trait methods reachable!)
452460 let crate_items = tcx. hir_crate_items ( ( ) ) ;
453461
454462 for id in crate_items. items ( ) {
0 commit comments