Skip to content

Commit a0358f4

Browse files
committed
Also support generic constants
1 parent 22d0073 commit a0358f4

File tree

2 files changed

+20
-9
lines changed

2 files changed

+20
-9
lines changed

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

+9-9
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;
3233
use rustc_middle::middle::codegen_fn_attrs::{CodegenFnAttrFlags, CodegenFnAttrs};
3334
use rustc_middle::middle::privacy::{self, Level};
3435
use rustc_middle::mir::interpret::{ConstAllocation, ErrorHandled, GlobalAlloc};
3536
use rustc_middle::query::Providers;
3637
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;
@@ -206,19 +206,19 @@ impl<'tcx> ReachableContext<'tcx> {
206206
}
207207
}
208208

209-
// Reachable constants will be inlined into other crates
210-
// unconditionally, so we need to make sure that their
211-
// contents are also reachable.
212-
hir::ItemKind::Const(..) => {
209+
hir::ItemKind::Const(_, _, init) => {
210+
// Only things actually ending up in the final constant need to be reachable.
211+
// Everything else is either already available as `mir_for_ctfe`, or can't be used
212+
// by codegen anyway.
213213
match self.tcx.const_eval_poly_to_alloc(item.owner_id.def_id.into()) {
214214
Ok(alloc) => {
215215
let alloc = self.tcx.global_alloc(alloc.alloc_id).unwrap_memory();
216216
self.propagate_from_alloc(alloc);
217217
}
218-
Err(ErrorHandled::TooGeneric(span)) => span_bug!(
219-
span,
220-
"generic constants aren't implemented in reachability"
221-
),
218+
// Reachable generic constants will be inlined into other crates
219+
// unconditionally, so we need to make sure that their
220+
// contents are also reachable.
221+
Err(ErrorHandled::TooGeneric(_)) => self.visit_nested_body(init),
222222
Err(ErrorHandled::Reported(..)) => {}
223223
}
224224
}

Diff for: tests/codegen/dont_codegen_private_const_fn_only_used_in_const_eval.rs

+11
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
44
//@compile-flags: --crate-type=lib -Copt-level=0
55

6+
#![feature(generic_const_items)]
7+
68
const fn foo() {}
79

810
pub static FOO: () = foo();
@@ -14,3 +16,12 @@ const fn bar() {}
1416
pub const BAR: () = bar();
1517

1618
// CHECK-NOT: define{{.*}}bar{{.*}}
19+
20+
const fn baz() {}
21+
22+
#[rustfmt::skip]
23+
pub const BAZ<const C: bool>: () = if C {
24+
baz()
25+
};
26+
27+
// CHECK: define{{.*}}baz{{.*}}

0 commit comments

Comments
 (0)