rust/compiler
bors df8ac8f1d7 Auto merge of #122568 - RalfJung:mentioned-items, r=oli-obk
recursively evaluate the constants in everything that is 'mentioned'

This is another attempt at fixing https://github.com/rust-lang/rust/issues/107503. The previous attempt at https://github.com/rust-lang/rust/pull/112879 seems stuck in figuring out where the [perf regression](https://perf.rust-lang.org/compare.html?start=c55d1ee8d4e3162187214692229a63c2cc5e0f31&end=ec8de1ebe0d698b109beeaaac83e60f4ef8bb7d1&stat=instructions:u) comes from. In  https://github.com/rust-lang/rust/pull/122258 I learned some things, which informed the approach this PR is taking.

Quoting from the new collector docs, which explain the high-level idea:
```rust
//! One important role of collection is to evaluate all constants that are used by all the items
//! which are being collected. Codegen can then rely on only encountering constants that evaluate
//! successfully, and if a constant fails to evaluate, the collector has much better context to be
//! able to show where this constant comes up.
//!
//! However, the exact set of "used" items (collected as described above), and therefore the exact
//! set of used constants, can depend on optimizations. Optimizing away dead code may optimize away
//! a function call that uses a failing constant, so an unoptimized build may fail where an
//! optimized build succeeds. This is undesirable.
//!
//! To fix this, the collector has the concept of "mentioned" items. Some time during the MIR
//! pipeline, before any optimization-level-dependent optimizations, we compute a list of all items
//! that syntactically appear in the code. These are considered "mentioned", and even if they are in
//! dead code and get optimized away (which makes them no longer "used"), they are still
//! "mentioned". For every used item, the collector ensures that all mentioned items, recursively,
//! do not use a failing constant. This is reflected via the [`CollectionMode`], which determines
//! whether we are visiting a used item or merely a mentioned item.
//!
//! The collector and "mentioned items" gathering (which lives in `rustc_mir_transform::mentioned_items`)
//! need to stay in sync in the following sense:
//!
//! - For every item that the collector gather that could eventually lead to build failure (most
//!   likely due to containing a constant that fails to evaluate), a corresponding mentioned item
//!   must be added. This should use the exact same strategy as the ecollector to make sure they are
//!   in sync. However, while the collector works on monomorphized types, mentioned items are
//!   collected on generic MIR -- so any time the collector checks for a particular type (such as
//!   `ty::FnDef`), we have to just onconditionally add this as a mentioned item.
//! - In `visit_mentioned_item`, we then do with that mentioned item exactly what the collector
//!   would have done during regular MIR visiting. Basically you can think of the collector having
//!   two stages, a pre-monomorphization stage and a post-monomorphization stage (usually quite
//!   literally separated by a call to `self.monomorphize`); the pre-monomorphizationn stage is
//!   duplicated in mentioned items gathering and the post-monomorphization stage is duplicated in
//!   `visit_mentioned_item`.
//! - Finally, as a performance optimization, the collector should fill `used_mentioned_item` during
//!   its MIR traversal with exactly what mentioned item gathering would have added in the same
//!   situation. This detects mentioned items that have *not* been optimized away and hence don't
//!   need a dedicated traversal.

enum CollectionMode {
    /// Collect items that are used, i.e., actually needed for codegen.
    ///
    /// Which items are used can depend on optimization levels, as MIR optimizations can remove
    /// uses.
    UsedItems,
    /// Collect items that are mentioned. The goal of this mode is that it is independent of
    /// optimizations: the set of "mentioned" items is computed before optimizations are run.
    ///
    /// The exact contents of this set are *not* a stable guarantee. (For instance, it is currently
    /// computed after drop-elaboration. If we ever do some optimizations even in debug builds, we
    /// might decide to run them before computing mentioned items.) The key property of this set is
    /// that it is optimization-independent.
    MentionedItems,
}
```
And the `mentioned_items` MIR body field docs:
```rust
    /// Further items that were mentioned in this function and hence *may* become monomorphized,
    /// depending on optimizations. We use this to avoid optimization-dependent compile errors: the
    /// collector recursively traverses all "mentioned" items and evaluates all their
    /// `required_consts`.
    ///
    /// This is *not* soundness-critical and the contents of this list are *not* a stable guarantee.
    /// All that's relevant is that this set is optimization-level-independent, and that it includes
    /// everything that the collector would consider "used". (For example, we currently compute this
    /// set after drop elaboration, so some drop calls that can never be reached are not considered
    /// "mentioned".) See the documentation of `CollectionMode` in
    /// `compiler/rustc_monomorphize/src/collector.rs` for more context.
    pub mentioned_items: Vec<Spanned<MentionedItem<'tcx>>>,
```

Fixes #107503
2024-03-21 09:01:18 +00:00
..
rustc
rustc_abi Auto merge of #121668 - erikdesjardins:commonprim, r=scottmcm,oli-obk 2024-03-13 15:25:35 +00:00
rustc_arena rename ptr::invalid -> ptr::without_provenance 2024-02-21 20:15:52 +01:00
rustc_ast Auto merge of #122754 - Mark-Simulacrum:bootstrap-bump, r=albertlarsan68 2024-03-20 13:43:41 +00:00
rustc_ast_ir Fix typo in VisitorResult 2024-03-08 23:20:29 -05:00
rustc_ast_lowering Auto merge of #121587 - ShoyuVanilla:fix-issue-121267, r=TaKO8Ki 2024-03-21 04:11:09 +00:00
rustc_ast_passes Auto merge of #122055 - compiler-errors:stabilize-atb, r=oli-obk 2024-03-19 00:04:09 +00:00
rustc_ast_pretty Rename ast::StmtKind::Local into ast::StmtKind::Let 2024-03-14 12:42:04 +01:00
rustc_attr Rename IntoDiagnostic as Diagnostic. 2024-03-11 09:15:09 +11:00
rustc_baked_icu_data
rustc_borrowck Auto merge of #121123 - compiler-errors:item-assumptions, r=oli-obk 2024-03-21 06:12:24 +00:00
rustc_builtin_macros Make unexpected always "return" PResult<()> & add unexpected_any 2024-03-15 11:36:21 +00:00
rustc_codegen_cranelift Make ptr_guaranteed_cmp a rustc_intrinsic and favor its body over backends implementing it 2024-03-19 09:17:40 +00:00
rustc_codegen_gcc Auto merge of #122055 - compiler-errors:stabilize-atb, r=oli-obk 2024-03-19 00:04:09 +00:00
rustc_codegen_llvm Rollup merge of #121543 - onur-ozkan:clippy-args, r=oli-obk 2024-03-20 05:51:22 +01:00
rustc_codegen_ssa Rollup merge of #122691 - veera-sivarajan:bugfix-121099, r=Amanieu 2024-03-19 18:03:51 +01:00
rustc_const_eval Rollup merge of #121543 - onur-ozkan:clippy-args, r=oli-obk 2024-03-20 05:51:22 +01:00
rustc_data_structures Auto merge of #122568 - RalfJung:mentioned-items, r=oli-obk 2024-03-21 09:01:18 +00:00
rustc_driver
rustc_driver_impl Rollup merge of #121764 - Zoxc:incr-sess-no-source, r=oli-obk 2024-03-14 20:00:18 +01:00
rustc_error_codes Auto merge of #122055 - compiler-errors:stabilize-atb, r=oli-obk 2024-03-19 00:04:09 +00:00
rustc_error_messages Rename SubdiagnosticMessage as SubdiagMessage. 2024-03-05 12:14:49 +11:00
rustc_errors Auto merge of #122754 - Mark-Simulacrum:bootstrap-bump, r=albertlarsan68 2024-03-20 13:43:41 +00:00
rustc_expand Rollup merge of #122540 - WaffleLapkin:ununexpected, r=estebank 2024-03-20 05:51:22 +01:00
rustc_feature branch 1.78: replace-version-placeholder 2024-03-19 19:27:24 -04:00
rustc_fluent_macro Rename SubdiagnosticMessage as SubdiagMessage. 2024-03-05 12:14:49 +11:00
rustc_fs_util
rustc_graphviz
rustc_hir Auto merge of #121587 - ShoyuVanilla:fix-issue-121267, r=TaKO8Ki 2024-03-21 04:11:09 +00:00
rustc_hir_analysis Auto merge of #121123 - compiler-errors:item-assumptions, r=oli-obk 2024-03-21 06:12:24 +00:00
rustc_hir_pretty Rename hir::Let into hir::LetExpr 2024-03-20 16:47:11 +01:00
rustc_hir_typeck Auto merge of #121123 - compiler-errors:item-assumptions, r=oli-obk 2024-03-21 06:12:24 +00:00
rustc_incremental Make incremental sessions identity no longer depend on the crate names provided by source code 2024-03-13 16:40:02 +01:00
rustc_index doc: Add better explanation 2024-03-06 16:54:42 +01:00
rustc_index_macros
rustc_infer Auto merge of #121123 - compiler-errors:item-assumptions, r=oli-obk 2024-03-21 06:12:24 +00:00
rustc_interface Rollup merge of #121764 - Zoxc:incr-sess-no-source, r=oli-obk 2024-03-14 20:00:18 +01:00
rustc_lexer
rustc_lint Split item bounds and item super predicates 2024-03-20 13:00:34 -04:00
rustc_lint_defs Rollup merge of #121720 - tmandry:split-refining, r=compiler-errors 2024-03-16 23:28:47 +01:00
rustc_llvm Update the minimum external LLVM to 17 2024-03-17 10:11:04 -07:00
rustc_log rustc_log: expose tracing-tree "wraparound" in an env var 2024-03-03 12:33:26 +01:00
rustc_macros Rename diagnostic derive things. 2024-03-11 10:06:34 +11:00
rustc_metadata Auto merge of #121123 - compiler-errors:item-assumptions, r=oli-obk 2024-03-21 06:12:24 +00:00
rustc_middle Auto merge of #122568 - RalfJung:mentioned-items, r=oli-obk 2024-03-21 09:01:18 +00:00
rustc_mir_build Auto merge of #122568 - RalfJung:mentioned-items, r=oli-obk 2024-03-21 09:01:18 +00:00
rustc_mir_dataflow step cfgs 2024-03-20 08:49:13 -04:00
rustc_mir_transform Auto merge of #122568 - RalfJung:mentioned-items, r=oli-obk 2024-03-21 09:01:18 +00:00
rustc_monomorphize Auto merge of #122568 - RalfJung:mentioned-items, r=oli-obk 2024-03-21 09:01:18 +00:00
rustc_next_trait_solver Allow a way to add constructors for rustc_type_ir types 2024-03-04 15:39:59 +00:00
rustc_parse Rollup merge of #122540 - WaffleLapkin:ununexpected, r=estebank 2024-03-20 05:51:22 +01:00
rustc_parse_format Extend format arg help for simple tuple index access expression 2024-03-16 22:33:02 +00:00
rustc_passes Rollup merge of #122719 - oli-obk:nested_static_feed_hir, r=fee1-dead 2024-03-19 18:03:53 +01:00
rustc_pattern_analysis Rollup merge of #121823 - Nadrieril:never-witnesses, r=compiler-errors 2024-03-18 22:24:36 +01:00
rustc_privacy Test and implement reachability for trait objects and generic parameters of functions 2024-03-14 14:10:45 +00:00
rustc_query_impl Verify that query keys result in unique dep nodes 2024-03-12 05:31:41 +01:00
rustc_query_system Rollup merge of #122245 - saethlin:check-dep-graph-size, r=petrochenkov 2024-03-12 09:04:00 -07:00
rustc_resolve Auto merge of #121587 - ShoyuVanilla:fix-issue-121267, r=TaKO8Ki 2024-03-21 04:11:09 +00:00
rustc_serialize Stabilize associated type bounds 2024-03-08 20:56:25 +00:00
rustc_session Rollup merge of #122748 - nnethercote:rustc_session-pub, r=jackh726 2024-03-20 05:51:24 +01:00
rustc_smir Avoid various uses of Option<Span> in favor of using DUMMY_SP in the few cases that used None 2024-03-18 09:34:08 +00:00
rustc_span Only split by-ref/by-move futures for async closures 2024-03-19 16:59:23 -04:00
rustc_symbol_mangling Only split by-ref/by-move futures for async closures 2024-03-19 16:59:23 -04:00
rustc_target step cfgs 2024-03-20 08:49:13 -04:00
rustc_trait_selection Split item bounds and item super predicates 2024-03-20 13:00:34 -04:00
rustc_traits Merge check_mod_impl_wf and check_mod_type_wf 2024-03-07 06:27:09 +00:00
rustc_transmute Avoid various uses of Option<Span> in favor of using DUMMY_SP in the few cases that used None 2024-03-18 09:34:08 +00:00
rustc_ty_utils Fix ABI for FnMut/Fn impls for async closures 2024-03-19 16:59:24 -04:00
rustc_type_ir make type_flags(ReError) & HAS_ERROR 2024-03-20 17:29:58 +00:00
stable_mir resolve clippy errors 2024-03-20 00:12:00 +03:00