Skip to content

Commit 5620969

Browse files
committed
Auto merge of rust-lang#122505 - oli-obk:visit_nested_body2, r=<try>
Don't walk the bodies of free constants for reachability. follow-up to rust-lang#122371 we don't have generic constants yet, so we don't need to handle failure to eval a free constant. Associated consts and generic consts will be a different topic, that I will look into in a follow-up r? `@tmiasko`
2 parents fe61575 + d46c98d commit 5620969

File tree

8 files changed

+161
-47
lines changed

8 files changed

+161
-47
lines changed

Cargo.lock

+1
Original file line numberDiff line numberDiff line change
@@ -4400,6 +4400,7 @@ dependencies = [
44004400
"rustc_lexer",
44014401
"rustc_macros",
44024402
"rustc_middle",
4403+
"rustc_privacy",
44034404
"rustc_session",
44044405
"rustc_span",
44054406
"rustc_target",

compiler/rustc_middle/src/mir/interpret/queries.rs

+24-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
use super::{ErrorHandled, EvalToConstValueResult, EvalToValTreeResult, GlobalId};
1+
use super::{
2+
ErrorHandled, EvalToAllocationRawResult, EvalToConstValueResult, EvalToValTreeResult, GlobalId,
3+
};
24

35
use crate::mir;
46
use crate::query::TyCtxtEnsure;
@@ -12,7 +14,7 @@ use rustc_span::{Span, DUMMY_SP};
1214

1315
impl<'tcx> TyCtxt<'tcx> {
1416
/// Evaluates a constant without providing any generic parameters. This is useful to evaluate consts
15-
/// that can't take any generic arguments like statics, const items or enum discriminants. If a
17+
/// that can't take any generic arguments like const items or enum discriminants. If a
1618
/// generic parameter is used within the constant `ErrorHandled::ToGeneric` will be returned.
1719
#[instrument(skip(self), level = "debug")]
1820
pub fn const_eval_poly(self, def_id: DefId) -> EvalToConstValueResult<'tcx> {
@@ -26,6 +28,25 @@ impl<'tcx> TyCtxt<'tcx> {
2628
let param_env = self.param_env(def_id).with_reveal_all_normalized(self);
2729
self.const_eval_global_id(param_env, cid, None)
2830
}
31+
32+
/// Evaluates a constant without providing any generic parameters. This is useful to evaluate consts
33+
/// that can't take any generic arguments like const items or enum discriminants. If a
34+
/// generic parameter is used within the constant `ErrorHandled::ToGeneric` will be returned.
35+
#[instrument(skip(self), level = "debug")]
36+
pub fn const_eval_poly_to_alloc(self, def_id: DefId) -> EvalToAllocationRawResult<'tcx> {
37+
// In some situations def_id will have generic parameters within scope, but they aren't allowed
38+
// to be used. So we can't use `Instance::mono`, instead we feed unresolved generic parameters
39+
// into `const_eval` which will return `ErrorHandled::ToGeneric` if any of them are
40+
// encountered.
41+
let args = GenericArgs::identity_for_item(self, def_id);
42+
let instance = ty::Instance::new(def_id, args);
43+
let cid = GlobalId { instance, promoted: None };
44+
let param_env = self.param_env(def_id).with_reveal_all_normalized(self);
45+
let inputs = self.erase_regions(param_env.and(cid));
46+
// The query doesn't know where it is being invoked, so we need to fix the span.
47+
self.eval_to_allocation_raw(inputs)
48+
}
49+
2950
/// Resolves and evaluates a constant.
3051
///
3152
/// The constant can be located on a trait like `<A as B>::C`, in which case the given
@@ -177,7 +198,7 @@ impl<'tcx> TyCtxt<'tcx> {
177198

178199
impl<'tcx> TyCtxtEnsure<'tcx> {
179200
/// Evaluates a constant without providing any generic parameters. This is useful to evaluate consts
180-
/// that can't take any generic arguments like statics, const items or enum discriminants. If a
201+
/// that can't take any generic arguments like const items or enum discriminants. If a
181202
/// generic parameter is used within the constant `ErrorHandled::ToGeneric` will be returned.
182203
#[instrument(skip(self), level = "debug")]
183204
pub fn const_eval_poly(self, def_id: DefId) {

compiler/rustc_passes/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ rustc_index = { path = "../rustc_index" }
1818
rustc_lexer = { path = "../rustc_lexer" }
1919
rustc_macros = { path = "../rustc_macros" }
2020
rustc_middle = { path = "../rustc_middle" }
21+
rustc_privacy = { path = "../rustc_privacy" }
2122
rustc_session = { path = "../rustc_session" }
2223
rustc_span = { path = "../rustc_span" }
2324
rustc_target = { path = "../rustc_target" }

compiler/rustc_passes/src/reachable.rs

+92-42
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,18 @@
66
// reachable as well.
77

88
use hir::def_id::LocalDefIdSet;
9+
use rustc_data_structures::stack::ensure_sufficient_stack;
910
use rustc_hir as hir;
1011
use rustc_hir::def::{DefKind, Res};
1112
use rustc_hir::def_id::{DefId, LocalDefId};
1213
use rustc_hir::intravisit::{self, Visitor};
1314
use rustc_hir::Node;
1415
use rustc_middle::middle::codegen_fn_attrs::{CodegenFnAttrFlags, CodegenFnAttrs};
1516
use rustc_middle::middle::privacy::{self, Level};
16-
use rustc_middle::mir::interpret::{ConstAllocation, GlobalAlloc};
17+
use rustc_middle::mir::interpret::{ConstAllocation, ErrorHandled, GlobalAlloc};
1718
use rustc_middle::query::Providers;
18-
use rustc_middle::ty::{self, TyCtxt};
19+
use rustc_middle::ty::{self, ExistentialTraitRef, TyCtxt};
20+
use rustc_privacy::DefIdVisitor;
1921
use rustc_session::config::CrateType;
2022
use rustc_target::spec::abi::Abi;
2123

@@ -65,23 +67,8 @@ impl<'tcx> Visitor<'tcx> for ReachableContext<'tcx> {
6567
_ => None,
6668
};
6769

68-
if let Some(res) = res
69-
&& let Some(def_id) = res.opt_def_id().and_then(|el| el.as_local())
70-
{
71-
if self.def_id_represents_local_inlined_item(def_id.to_def_id()) {
72-
self.worklist.push(def_id);
73-
} else {
74-
match res {
75-
// Reachable constants and reachable statics can have their contents inlined
76-
// into other crates. Mark them as reachable and recurse into their body.
77-
Res::Def(DefKind::Const | DefKind::AssocConst | DefKind::Static { .. }, _) => {
78-
self.worklist.push(def_id);
79-
}
80-
_ => {
81-
self.reachable_symbols.insert(def_id);
82-
}
83-
}
84-
}
70+
if let Some(res) = res {
71+
self.propagate_item(res);
8572
}
8673

8774
intravisit::walk_expr(self, expr)
@@ -198,20 +185,22 @@ impl<'tcx> ReachableContext<'tcx> {
198185
// Reachable constants will be inlined into other crates
199186
// unconditionally, so we need to make sure that their
200187
// contents are also reachable.
201-
hir::ItemKind::Const(_, _, init) => {
202-
self.visit_nested_body(init);
188+
hir::ItemKind::Const(..) => {
189+
match self.tcx.const_eval_poly_to_alloc(item.owner_id.def_id.into()) {
190+
Ok(alloc) => {
191+
let alloc = self.tcx.global_alloc(alloc.alloc_id).unwrap_memory();
192+
self.propagate_from_alloc(alloc);
193+
}
194+
Err(ErrorHandled::TooGeneric(span)) => span_bug!(
195+
span,
196+
"generic constants aren't implemented in reachability"
197+
),
198+
Err(ErrorHandled::Reported(..)) => {}
199+
}
203200
}
204-
205-
// Reachable statics are inlined if read from another constant or static
206-
// in other crates. Additionally anonymous nested statics may be created
207-
// when evaluating a static, so preserve those, too.
208-
hir::ItemKind::Static(_, _, init) => {
209-
// FIXME(oli-obk): remove this body walking and instead walk the evaluated initializer
210-
// to find nested items that end up in the final value instead of also marking symbols
211-
// as reachable that are only needed for evaluation.
212-
self.visit_nested_body(init);
201+
hir::ItemKind::Static(..) => {
213202
if let Ok(alloc) = self.tcx.eval_static_initializer(item.owner_id.def_id) {
214-
self.propagate_statics_from_alloc(item.owner_id.def_id, alloc);
203+
self.propagate_from_alloc(alloc);
215204
}
216205
}
217206

@@ -282,28 +271,89 @@ impl<'tcx> ReachableContext<'tcx> {
282271
}
283272
}
284273

285-
/// Finds anonymous nested statics created for nested allocations and adds them to `reachable_symbols`.
286-
fn propagate_statics_from_alloc(&mut self, root: LocalDefId, alloc: ConstAllocation<'tcx>) {
274+
/// Finds things to add to `reachable_symbols` within allocations.
275+
/// In contrast to visit_nested_body this ignores things that were only needed to evaluate
276+
/// the allocation.
277+
fn propagate_from_alloc(&mut self, alloc: ConstAllocation<'tcx>) {
287278
if !self.any_library {
288279
return;
289280
}
290281
for (_, prov) in alloc.0.provenance().ptrs().iter() {
291282
match self.tcx.global_alloc(prov.alloc_id()) {
292283
GlobalAlloc::Static(def_id) => {
293-
if let Some(def_id) = def_id.as_local()
294-
&& self.tcx.local_parent(def_id) == root
295-
// This is the main purpose of this function: add the def_id we find
296-
// to `reachable_symbols`.
297-
&& self.reachable_symbols.insert(def_id)
298-
&& let Ok(alloc) = self.tcx.eval_static_initializer(def_id)
299-
{
300-
self.propagate_statics_from_alloc(root, alloc);
284+
self.propagate_item(Res::Def(self.tcx.def_kind(def_id), def_id))
285+
}
286+
GlobalAlloc::Function(instance) => {
287+
// Manually visit to actually see the instance's `DefId`. Type visitors won't see it
288+
self.propagate_item(Res::Def(
289+
self.tcx.def_kind(instance.def_id()),
290+
instance.def_id(),
291+
));
292+
self.visit(instance.args);
293+
}
294+
GlobalAlloc::VTable(ty, trait_ref) => {
295+
self.visit(ty);
296+
// Manually visit to actually see the trait's `DefId`. Type visitors won't see it
297+
if let Some(trait_ref) = trait_ref {
298+
let ExistentialTraitRef { def_id, args } = trait_ref.skip_binder();
299+
self.visit_def_id(def_id, "", &"");
300+
self.visit(args);
301301
}
302302
}
303-
GlobalAlloc::Function(_) | GlobalAlloc::VTable(_, _) | GlobalAlloc::Memory(_) => {}
303+
GlobalAlloc::Memory(alloc) => self.propagate_from_alloc(alloc),
304304
}
305305
}
306306
}
307+
308+
fn propagate_item(&mut self, res: Res) {
309+
let Res::Def(kind, def_id) = res else { return };
310+
let Some(def_id) = def_id.as_local() else { return };
311+
match kind {
312+
DefKind::Static { nested: true, .. } => {
313+
// This is the main purpose of this function: add the def_id we find
314+
// to `reachable_symbols`.
315+
if self.reachable_symbols.insert(def_id) {
316+
if let Ok(alloc) = self.tcx.eval_static_initializer(def_id) {
317+
// This cannot cause infinite recursion, because we abort by inserting into the
318+
// work list once we hit a normal static. Nested statics, even if they somehow
319+
// become recursive, are also not infinitely recursing, because of the
320+
// `reachable_symbols` check above.
321+
// We still need to protect against stack overflow due to deeply nested statics.
322+
ensure_sufficient_stack(|| self.propagate_from_alloc(alloc));
323+
}
324+
}
325+
}
326+
// Reachable constants and reachable statics can have their contents inlined
327+
// into other crates. Mark them as reachable and recurse into their body.
328+
DefKind::Const | DefKind::AssocConst | DefKind::Static { .. } => {
329+
self.worklist.push(def_id);
330+
}
331+
_ => {
332+
if self.def_id_represents_local_inlined_item(def_id.to_def_id()) {
333+
self.worklist.push(def_id);
334+
} else {
335+
self.reachable_symbols.insert(def_id);
336+
}
337+
}
338+
}
339+
}
340+
}
341+
342+
impl<'tcx> DefIdVisitor<'tcx> for ReachableContext<'tcx> {
343+
type Result = ();
344+
345+
fn tcx(&self) -> TyCtxt<'tcx> {
346+
self.tcx
347+
}
348+
349+
fn visit_def_id(
350+
&mut self,
351+
def_id: DefId,
352+
_kind: &str,
353+
_descr: &dyn std::fmt::Display,
354+
) -> Self::Result {
355+
self.propagate_item(Res::Def(self.tcx.def_kind(def_id), def_id))
356+
}
307357
}
308358

309359
fn check_item<'tcx>(

compiler/rustc_privacy/src/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ impl<'tcx> fmt::Display for LazyDefPathStr<'tcx> {
6767
/// First, it doesn't have overridable `fn visit_trait_ref`, so we have to catch trait `DefId`s
6868
/// manually. Second, it doesn't visit some type components like signatures of fn types, or traits
6969
/// in `impl Trait`, see individual comments in `DefIdVisitorSkeleton::visit_ty`.
70-
trait DefIdVisitor<'tcx> {
70+
pub trait DefIdVisitor<'tcx> {
7171
type Result: VisitorResult = ();
7272
const SHALLOW: bool = false;
7373
const SKIP_ASSOC_TYS: bool = false;
@@ -98,7 +98,7 @@ trait DefIdVisitor<'tcx> {
9898
}
9999
}
100100

101-
struct DefIdVisitorSkeleton<'v, 'tcx, V: ?Sized> {
101+
pub struct DefIdVisitorSkeleton<'v, 'tcx, V: ?Sized> {
102102
def_id_visitor: &'v mut V,
103103
visited_opaque_tys: FxHashSet<DefId>,
104104
dummy: PhantomData<TyCtxt<'tcx>>,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
//! This test checks that we do not monomorphize functions that are only
2+
//! used to evaluate static items, but never used in runtime code.
3+
4+
//@compile-flags: --crate-type=lib -Copt-level=0
5+
6+
const fn foo() {}
7+
8+
pub static FOO: () = foo();
9+
10+
// CHECK-NOT: define{{.*}}foo{{.*}}
11+
12+
const fn bar() {}
13+
14+
pub const BAR: () = bar();
15+
16+
// CHECK-NOT: define{{.*}}bar{{.*}}

tests/ui/cross-crate/auxiliary/static_init_aux.rs

+21
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
pub static V: &u32 = &X;
22
pub static F: fn() = f;
33
pub static G: fn() = G0;
4+
pub static H: &(dyn Fn() + Sync) = &h;
5+
pub static I: fn() = Helper(j).mk();
46

57
static X: u32 = 42;
68
static G0: fn() = g;
@@ -12,3 +14,22 @@ pub fn v() -> *const u32 {
1214
fn f() {}
1315

1416
fn g() {}
17+
18+
fn h() {}
19+
20+
#[derive(Copy, Clone)]
21+
struct Helper<T: Copy>(T);
22+
23+
impl<T: Copy + FnOnce()> Helper<T> {
24+
const fn mk(self) -> fn() {
25+
i::<T>
26+
}
27+
}
28+
29+
fn i<T: FnOnce()>() {
30+
assert_eq!(std::mem::size_of::<T>(), 0);
31+
// unsafe to work around the lack of a `Default` impl for function items
32+
unsafe { (std::mem::transmute_copy::<(), T>(&()))() }
33+
}
34+
35+
fn j() {}

tests/ui/cross-crate/static-init.rs

+4
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ extern crate static_init_aux as aux;
66
static V: &u32 = aux::V;
77
static F: fn() = aux::F;
88
static G: fn() = aux::G;
9+
static H: &(dyn Fn() + Sync) = aux::H;
10+
static I: fn() = aux::I;
911

1012
fn v() -> *const u32 {
1113
V
@@ -15,4 +17,6 @@ fn main() {
1517
assert_eq!(aux::v(), crate::v());
1618
F();
1719
G();
20+
H();
21+
I();
1822
}

0 commit comments

Comments
 (0)