Skip to content

Commit 22d0073

Browse files
committed
Don't walk the bodies of free constants for reachability.
1 parent 50b07aa commit 22d0073

File tree

3 files changed

+38
-8
lines changed

3 files changed

+38
-8
lines changed

Diff for: compiler/rustc_middle/src/mir/interpret/queries.rs

+23-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;
@@ -13,7 +15,7 @@ use tracing::{debug, instrument};
1315

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

178198
impl<'tcx> TyCtxtEnsure<'tcx> {
179199
/// 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
200+
/// that can't take any generic arguments like const items or enum discriminants. If a
181201
/// generic parameter is used within the constant `ErrorHandled::ToGeneric` will be returned.
182202
#[instrument(skip(self), level = "debug")]
183203
pub fn const_eval_poly(self, def_id: DefId) {

Diff for: compiler/rustc_passes/src/reachable.rs

+14-4
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,12 @@ use rustc_hir::def::{DefKind, Res};
2929
use rustc_hir::def_id::{DefId, LocalDefId};
3030
use rustc_hir::intravisit::{self, Visitor};
3131
use rustc_hir::Node;
32-
use rustc_middle::bug;
3332
use rustc_middle::middle::codegen_fn_attrs::{CodegenFnAttrFlags, CodegenFnAttrs};
3433
use rustc_middle::middle::privacy::{self, Level};
35-
use rustc_middle::mir::interpret::{ConstAllocation, GlobalAlloc};
34+
use rustc_middle::mir::interpret::{ConstAllocation, ErrorHandled, GlobalAlloc};
3635
use rustc_middle::query::Providers;
3736
use rustc_middle::ty::{self, ExistentialTraitRef, TyCtxt};
37+
use rustc_middle::{bug, span_bug};
3838
use rustc_privacy::DefIdVisitor;
3939
use rustc_session::config::CrateType;
4040
use tracing::debug;
@@ -209,8 +209,18 @@ impl<'tcx> ReachableContext<'tcx> {
209209
// Reachable constants will be inlined into other crates
210210
// unconditionally, so we need to make sure that their
211211
// contents are also reachable.
212-
hir::ItemKind::Const(_, _, init) => {
213-
self.visit_nested_body(init);
212+
hir::ItemKind::Const(..) => {
213+
match self.tcx.const_eval_poly_to_alloc(item.owner_id.def_id.into()) {
214+
Ok(alloc) => {
215+
let alloc = self.tcx.global_alloc(alloc.alloc_id).unwrap_memory();
216+
self.propagate_from_alloc(alloc);
217+
}
218+
Err(ErrorHandled::TooGeneric(span)) => span_bug!(
219+
span,
220+
"generic constants aren't implemented in reachability"
221+
),
222+
Err(ErrorHandled::Reported(..)) => {}
223+
}
214224
}
215225
hir::ItemKind::Static(..) => {
216226
if let Ok(alloc) = self.tcx.eval_static_initializer(item.owner_id.def_id) {

Diff for: tests/codegen/dont_codegen_private_const_fn_only_used_in_const_eval.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,4 @@ const fn bar() {}
1313

1414
pub const BAR: () = bar();
1515

16-
// CHECK: define{{.*}}bar{{.*}}
16+
// CHECK-NOT: define{{.*}}bar{{.*}}

0 commit comments

Comments
 (0)