Also support generic constants

This commit is contained in:
Oli Scherer 2024-06-03 15:32:13 +00:00
parent 22d0073d47
commit a0358f44b3
2 changed files with 20 additions and 9 deletions

View File

@ -29,12 +29,12 @@
use rustc_hir::def_id::{DefId, LocalDefId}; use rustc_hir::def_id::{DefId, LocalDefId};
use rustc_hir::intravisit::{self, Visitor}; use rustc_hir::intravisit::{self, Visitor};
use rustc_hir::Node; use rustc_hir::Node;
use rustc_middle::bug;
use rustc_middle::middle::codegen_fn_attrs::{CodegenFnAttrFlags, CodegenFnAttrs}; use rustc_middle::middle::codegen_fn_attrs::{CodegenFnAttrFlags, CodegenFnAttrs};
use rustc_middle::middle::privacy::{self, Level}; use rustc_middle::middle::privacy::{self, Level};
use rustc_middle::mir::interpret::{ConstAllocation, ErrorHandled, GlobalAlloc}; use rustc_middle::mir::interpret::{ConstAllocation, ErrorHandled, GlobalAlloc};
use rustc_middle::query::Providers; use rustc_middle::query::Providers;
use rustc_middle::ty::{self, ExistentialTraitRef, TyCtxt}; use rustc_middle::ty::{self, ExistentialTraitRef, TyCtxt};
use rustc_middle::{bug, span_bug};
use rustc_privacy::DefIdVisitor; use rustc_privacy::DefIdVisitor;
use rustc_session::config::CrateType; use rustc_session::config::CrateType;
use tracing::debug; use tracing::debug;
@ -206,19 +206,19 @@ fn propagate_node(&mut self, node: &Node<'tcx>, search_item: LocalDefId) {
} }
} }
// Reachable constants will be inlined into other crates hir::ItemKind::Const(_, _, init) => {
// unconditionally, so we need to make sure that their // Only things actually ending up in the final constant need to be reachable.
// contents are also reachable. // Everything else is either already available as `mir_for_ctfe`, or can't be used
hir::ItemKind::Const(..) => { // by codegen anyway.
match self.tcx.const_eval_poly_to_alloc(item.owner_id.def_id.into()) { match self.tcx.const_eval_poly_to_alloc(item.owner_id.def_id.into()) {
Ok(alloc) => { Ok(alloc) => {
let alloc = self.tcx.global_alloc(alloc.alloc_id).unwrap_memory(); let alloc = self.tcx.global_alloc(alloc.alloc_id).unwrap_memory();
self.propagate_from_alloc(alloc); self.propagate_from_alloc(alloc);
} }
Err(ErrorHandled::TooGeneric(span)) => span_bug!( // Reachable generic constants will be inlined into other crates
span, // unconditionally, so we need to make sure that their
"generic constants aren't implemented in reachability" // contents are also reachable.
), Err(ErrorHandled::TooGeneric(_)) => self.visit_nested_body(init),
Err(ErrorHandled::Reported(..)) => {} Err(ErrorHandled::Reported(..)) => {}
} }
} }

View File

@ -3,6 +3,8 @@
//@compile-flags: --crate-type=lib -Copt-level=0 //@compile-flags: --crate-type=lib -Copt-level=0
#![feature(generic_const_items)]
const fn foo() {} const fn foo() {}
pub static FOO: () = foo(); pub static FOO: () = foo();
@ -14,3 +16,12 @@ const fn bar() {}
pub const BAR: () = bar(); pub const BAR: () = bar();
// CHECK-NOT: define{{.*}}bar{{.*}} // CHECK-NOT: define{{.*}}bar{{.*}}
const fn baz() {}
#[rustfmt::skip]
pub const BAZ<const C: bool>: () = if C {
baz()
};
// CHECK: define{{.*}}baz{{.*}}