Skip to content

Only encode constness for items that may conditionally be const #103263

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 5 additions & 8 deletions compiler/rustc_const_eval/src/const_eval/fn_queries.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,12 @@ pub fn is_parent_const_impl_raw(tcx: TyCtxt<'_>, def_id: LocalDefId) -> bool {
/// report whether said intrinsic has a `rustc_const_{un,}stable` attribute. Otherwise, return
/// `Constness::NotConst`.
fn constness(tcx: TyCtxt<'_>, def_id: DefId) -> hir::Constness {
if let Some(value) = tcx.opt_default_constness(def_id) {
return value;
}

let hir_id = tcx.hir().local_def_id_to_hir_id(def_id.expect_local());
match tcx.hir().get(hir_id) {
hir::Node::Ctor(_) => hir::Constness::Const,

hir::Node::ForeignItem(hir::ForeignItem { kind: hir::ForeignItemKind::Fn(..), .. }) => {
// Intrinsics use `rustc_const_{un,}stable` attributes to indicate constness. All other
// foreign items cannot be evaluated at compile-time.
Expand All @@ -46,12 +48,7 @@ fn constness(tcx: TyCtxt<'_>, def_id: DefId) -> hir::Constness {
hir::Constness::Const
}

hir::Node::Item(hir::Item { kind: hir::ItemKind::Const(..), .. })
| hir::Node::Item(hir::Item { kind: hir::ItemKind::Static(..), .. })
| hir::Node::TraitItem(hir::TraitItem { kind: hir::TraitItemKind::Const(..), .. })
| hir::Node::AnonConst(_)
| hir::Node::ImplItem(hir::ImplItem { kind: hir::ImplItemKind::Const(..), .. })
| hir::Node::ImplItem(hir::ImplItem {
hir::Node::ImplItem(hir::ImplItem {
kind:
hir::ImplItemKind::Fn(
hir::FnSig {
Expand Down
15 changes: 15 additions & 0 deletions compiler/rustc_metadata/src/rmeta/decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1332,6 +1332,21 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
tcx.arena.alloc_from_iter(self.root.lang_items_missing.decode(self))
}

fn get_constness(self, tcx: TyCtxt<'tcx>, id: DefIndex) -> hir::Constness {
let def_id = self.local_def_id(id);

if let Some(constness) = tcx.opt_default_constness(def_id) {
constness
} else {
self.cdata
.root
.tables
.constness
.get(self, id)
.unwrap_or_else(|| panic!("{:?} does not have constness", def_id))
}
}

fn exported_symbols(
self,
tcx: TyCtxt<'tcx>,
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ provide! { tcx, def_id, other, cdata,
impl_parent => { table }
impl_polarity => { table_direct }
impl_defaultness => { table_direct }
constness => { table_direct }
constness => { cdata.get_constness(tcx, def_id.index) }
coerce_unsized_info => { table }
mir_const_qualif => { table }
rendered_const => { table }
Expand Down
37 changes: 20 additions & 17 deletions compiler/rustc_metadata/src/rmeta/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1061,25 +1061,14 @@ fn should_encode_const(def_kind: DefKind) -> bool {

fn should_encode_constness(def_kind: DefKind) -> bool {
match def_kind {
DefKind::Struct
| DefKind::Union
| DefKind::Enum
| DefKind::Trait
| DefKind::AssocTy
| DefKind::Fn
| DefKind::Const
| DefKind::Static(..)
| DefKind::Ctor(..)
DefKind::Fn
| DefKind::AssocFn
| DefKind::AssocConst
| DefKind::AnonConst
| DefKind::InlineConst
| DefKind::OpaqueTy
| DefKind::ImplTraitPlaceholder
| DefKind::AssocTy
| DefKind::Trait
| DefKind::Impl
| DefKind::Closure
| DefKind::Generator
| DefKind::TyAlias => true,
| DefKind::Generator => true,
// It doesn't make sense to compute constness on these DefKinds
DefKind::Variant
| DefKind::TraitAlias
| DefKind::ForeignTy
Expand All @@ -1092,7 +1081,21 @@ fn should_encode_constness(def_kind: DefKind) -> bool {
| DefKind::Use
| DefKind::LifetimeParam
| DefKind::GlobalAsm
| DefKind::ExternCrate => false,
| DefKind::ExternCrate
// These items are not conditionally const,
// and therefore don't need their constness stored.
| DefKind::Struct
| DefKind::Union
| DefKind::Enum
| DefKind::TyAlias
| DefKind::OpaqueTy
| DefKind::ImplTraitPlaceholder
| DefKind::Const
| DefKind::Static(_)
| DefKind::Ctor(_, _)
| DefKind::AssocConst
| DefKind::AnonConst
| DefKind::InlineConst => false,
}
}

Expand Down
23 changes: 23 additions & 0 deletions compiler/rustc_middle/src/ty/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1724,6 +1724,29 @@ impl<'tcx> TyCtxt<'tcx> {
pub fn local_visibility(self, def_id: LocalDefId) -> Visibility {
self.visibility(def_id).expect_local()
}

/// The constness of an item that is independent of its signature.
///
/// Only some items have a "default constness", such as `struct`s
/// which make no sense being constant, and `const`s which are always
/// constant.
pub fn opt_default_constness(self, def_id: DefId) -> Option<hir::Constness> {
match self.def_kind(def_id) {
DefKind::Struct
| DefKind::Union
| DefKind::Enum
| DefKind::TyAlias
| DefKind::OpaqueTy
| DefKind::ImplTraitPlaceholder => Some(hir::Constness::NotConst),
DefKind::Const
| DefKind::Static(_)
| DefKind::Ctor(_, _)
| DefKind::AssocConst
| DefKind::AnonConst
| DefKind::InlineConst => Some(hir::Constness::Const),
_ => None,
}
}
}

/// A trait implemented for all `X<'a>` types that can be safely and
Expand Down