From be4cd53536768f3b4dc27316dccfa1a0f595fc7e Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Tue, 11 Jun 2024 09:05:36 +0200 Subject: [PATCH] reachable computation: clarify comments around consts --- compiler/rustc_passes/src/reachable.rs | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/compiler/rustc_passes/src/reachable.rs b/compiler/rustc_passes/src/reachable.rs index 74b89546e6f..6dd8eaf7e67 100644 --- a/compiler/rustc_passes/src/reachable.rs +++ b/compiler/rustc_passes/src/reachable.rs @@ -207,18 +207,21 @@ fn propagate_node(&mut self, node: &Node<'tcx>, search_item: LocalDefId) { } hir::ItemKind::Const(_, _, init) => { - // Only things actually ending up in the final constant need to be reachable. - // Everything else is either already available as `mir_for_ctfe`, or can't be used - // by codegen anyway. + // Only things actually ending up in the final constant value are reachable + // for codegen. Everything else is only needed during const-eval, so even if + // const-eval happens in a downstream crate, all they need is + // `mir_for_ctfe`. match self.tcx.const_eval_poly_to_alloc(item.owner_id.def_id.into()) { Ok(alloc) => { let alloc = self.tcx.global_alloc(alloc.alloc_id).unwrap_memory(); self.propagate_from_alloc(alloc); } - // Reachable generic constants will be inlined into other crates - // unconditionally, so we need to make sure that their - // contents are also reachable. + // We can't figure out which value the constant will evaluate to. In + // lieu of that, we have to consider everything mentioned in the const + // initializer reachable, since it *may* end up in the final value. Err(ErrorHandled::TooGeneric(_)) => self.visit_nested_body(init), + // If there was an error evaluating the const, nothing can be reachable + // via it, and anyway compilation will fail. Err(ErrorHandled::Reported(..)) => {} } }