Skip to content

Commit

Permalink
Only encode constness for items that may conditionally be const
Browse files Browse the repository at this point in the history
  • Loading branch information
compiler-errors committed Oct 19, 2022
1 parent 4b8f431 commit f007d6a
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 9 deletions.
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
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

0 comments on commit f007d6a

Please sign in to comment.