Skip to content

Commit f007d6a

Browse files
Only encode constness for items that may conditionally be const
1 parent 4b8f431 commit f007d6a

File tree

4 files changed

+44
-9
lines changed

4 files changed

+44
-9
lines changed

compiler/rustc_const_eval/src/const_eval/fn_queries.rs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,12 @@ pub fn is_parent_const_impl_raw(tcx: TyCtxt<'_>, def_id: LocalDefId) -> bool {
2525
/// report whether said intrinsic has a `rustc_const_{un,}stable` attribute. Otherwise, return
2626
/// `Constness::NotConst`.
2727
fn constness(tcx: TyCtxt<'_>, def_id: DefId) -> hir::Constness {
28+
if let Some(value) = tcx.opt_default_constness(def_id) {
29+
return value;
30+
}
31+
2832
let hir_id = tcx.hir().local_def_id_to_hir_id(def_id.expect_local());
2933
match tcx.hir().get(hir_id) {
30-
hir::Node::Ctor(_) => hir::Constness::Const,
31-
3234
hir::Node::ForeignItem(hir::ForeignItem { kind: hir::ForeignItemKind::Fn(..), .. }) => {
3335
// Intrinsics use `rustc_const_{un,}stable` attributes to indicate constness. All other
3436
// foreign items cannot be evaluated at compile-time.
@@ -46,12 +48,7 @@ fn constness(tcx: TyCtxt<'_>, def_id: DefId) -> hir::Constness {
4648
hir::Constness::Const
4749
}
4850

49-
hir::Node::Item(hir::Item { kind: hir::ItemKind::Const(..), .. })
50-
| hir::Node::Item(hir::Item { kind: hir::ItemKind::Static(..), .. })
51-
| hir::Node::TraitItem(hir::TraitItem { kind: hir::TraitItemKind::Const(..), .. })
52-
| hir::Node::AnonConst(_)
53-
| hir::Node::ImplItem(hir::ImplItem { kind: hir::ImplItemKind::Const(..), .. })
54-
| hir::Node::ImplItem(hir::ImplItem {
51+
hir::Node::ImplItem(hir::ImplItem {
5552
kind:
5653
hir::ImplItemKind::Fn(
5754
hir::FnSig {

compiler/rustc_metadata/src/rmeta/decoder.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1332,6 +1332,21 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
13321332
tcx.arena.alloc_from_iter(self.root.lang_items_missing.decode(self))
13331333
}
13341334

1335+
fn get_constness(self, tcx: TyCtxt<'tcx>, id: DefIndex) -> hir::Constness {
1336+
let def_id = self.local_def_id(id);
1337+
1338+
if let Some(constness) = tcx.opt_default_constness(def_id) {
1339+
constness
1340+
} else {
1341+
self.cdata
1342+
.root
1343+
.tables
1344+
.constness
1345+
.get(self, id)
1346+
.unwrap_or_else(|| panic!("{:?} does not have constness", def_id))
1347+
}
1348+
}
1349+
13351350
fn exported_symbols(
13361351
self,
13371352
tcx: TyCtxt<'tcx>,

compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ provide! { tcx, def_id, other, cdata,
216216
impl_parent => { table }
217217
impl_polarity => { table_direct }
218218
impl_defaultness => { table_direct }
219-
constness => { table_direct }
219+
constness => { cdata.get_constness(tcx, def_id.index) }
220220
coerce_unsized_info => { table }
221221
mir_const_qualif => { table }
222222
rendered_const => { table }

compiler/rustc_middle/src/ty/context.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1724,6 +1724,29 @@ impl<'tcx> TyCtxt<'tcx> {
17241724
pub fn local_visibility(self, def_id: LocalDefId) -> Visibility {
17251725
self.visibility(def_id).expect_local()
17261726
}
1727+
1728+
/// The constness of an item that is independent of its signature.
1729+
///
1730+
/// Only some items have a "default constness", such as `struct`s
1731+
/// which make no sense being constant, and `const`s which are always
1732+
/// constant.
1733+
pub fn opt_default_constness(self, def_id: DefId) -> Option<hir::Constness> {
1734+
match self.def_kind(def_id) {
1735+
DefKind::Struct
1736+
| DefKind::Union
1737+
| DefKind::Enum
1738+
| DefKind::TyAlias
1739+
| DefKind::OpaqueTy
1740+
| DefKind::ImplTraitPlaceholder => Some(hir::Constness::NotConst),
1741+
DefKind::Const
1742+
| DefKind::Static(_)
1743+
| DefKind::Ctor(_, _)
1744+
| DefKind::AssocConst
1745+
| DefKind::AnonConst
1746+
| DefKind::InlineConst => Some(hir::Constness::Const),
1747+
_ => None,
1748+
}
1749+
}
17271750
}
17281751

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

0 commit comments

Comments
 (0)