Skip to content

Commit 434c3f8

Browse files
committed
Require the constness query to only be invoked on things that can have constness
1 parent 78a87ce commit 434c3f8

File tree

5 files changed

+24
-12
lines changed

5 files changed

+24
-12
lines changed

compiler/rustc_const_eval/src/const_eval/fn_queries.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,7 @@ fn constness(tcx: TyCtxt<'_>, def_id: LocalDefId) -> hir::Constness {
1818
let node = tcx.hir_node_by_def_id(def_id);
1919

2020
match node {
21-
hir::Node::Ctor(_)
22-
| hir::Node::AnonConst(_)
23-
| hir::Node::ConstBlock(_)
21+
hir::Node::Ctor(hir::VariantData::Tuple(..))
2422
| hir::Node::ImplItem(hir::ImplItem { kind: hir::ImplItemKind::Const(..), .. }) => {
2523
hir::Constness::Const
2624
}
@@ -41,7 +39,10 @@ fn constness(tcx: TyCtxt<'_>, def_id: LocalDefId) -> hir::Constness {
4139
let is_const = is_parent_const_impl_raw(tcx, def_id);
4240
if is_const { hir::Constness::Const } else { hir::Constness::NotConst }
4341
} else {
44-
hir::Constness::NotConst
42+
tcx.dcx().span_bug(
43+
tcx.def_span(def_id),
44+
format!("should not be requesting the constness of items that can't be const: {node:#?}: {:?}", tcx.def_kind(def_id))
45+
)
4546
}
4647
}
4748
}

compiler/rustc_hir/src/hir.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -4044,9 +4044,7 @@ impl<'hir> Node<'hir> {
40444044
_ => None,
40454045
},
40464046
Node::TraitItem(ti) => match ti.kind {
4047-
TraitItemKind::Fn(ref sig, TraitFn::Provided(_)) => {
4048-
Some(FnKind::Method(ti.ident, sig))
4049-
}
4047+
TraitItemKind::Fn(ref sig, _) => Some(FnKind::Method(ti.ident, sig)),
40504048
_ => None,
40514049
},
40524050
Node::ImplItem(ii) => match ii.kind {

compiler/rustc_metadata/src/rmeta/encoder.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -1268,8 +1268,7 @@ fn should_encode_constness(def_kind: DefKind) -> bool {
12681268
| DefKind::AssocFn
12691269
| DefKind::Closure
12701270
| DefKind::Impl { of_trait: true }
1271-
| DefKind::Variant
1272-
| DefKind::Ctor(..) => true,
1271+
| DefKind::Ctor(_, CtorKind::Fn) => true,
12731272

12741273
DefKind::Struct
12751274
| DefKind::Union
@@ -1296,6 +1295,8 @@ fn should_encode_constness(def_kind: DefKind) -> bool {
12961295
| DefKind::LifetimeParam
12971296
| DefKind::GlobalAsm
12981297
| DefKind::ExternCrate
1298+
| DefKind::Ctor(_, CtorKind::Const)
1299+
| DefKind::Variant
12991300
| DefKind::SyntheticCoroutineBody => false,
13001301
}
13011302
}

compiler/rustc_middle/src/query/mod.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -746,7 +746,9 @@ rustc_queries! {
746746
desc { |tcx| "computing drop-check constraints for `{}`", tcx.def_path_str(key) }
747747
}
748748

749-
/// Returns `true` if this is a const fn / const impl.
749+
/// Returns the constness of functions and impls.
750+
///
751+
/// Will ICE if used on things that are always const or never const.
750752
///
751753
/// **Do not call this function manually.** It is only meant to cache the base data for the
752754
/// higher-level functions. Consider using `is_const_fn` or `is_const_trait_impl` instead.

compiler/rustc_middle/src/ty/mod.rs

+12-2
Original file line numberDiff line numberDiff line change
@@ -2014,7 +2014,17 @@ impl<'tcx> TyCtxt<'tcx> {
20142014
self.constness(def_id) == hir::Constness::Const
20152015
}
20162016
DefKind::Trait => self.is_const_trait(def_id),
2017-
DefKind::AssocTy | DefKind::AssocFn => {
2017+
DefKind::AssocTy => {
2018+
let parent_def_id = self.parent(def_id);
2019+
match self.def_kind(parent_def_id) {
2020+
DefKind::Impl { of_trait: false } => false,
2021+
DefKind::Impl { of_trait: true } | DefKind::Trait => {
2022+
self.is_conditionally_const(parent_def_id)
2023+
}
2024+
_ => bug!("unexpected parent item of associated type: {parent_def_id:?}"),
2025+
}
2026+
}
2027+
DefKind::AssocFn => {
20182028
let parent_def_id = self.parent(def_id);
20192029
match self.def_kind(parent_def_id) {
20202030
DefKind::Impl { of_trait: false } => {
@@ -2023,7 +2033,7 @@ impl<'tcx> TyCtxt<'tcx> {
20232033
DefKind::Impl { of_trait: true } | DefKind::Trait => {
20242034
self.is_conditionally_const(parent_def_id)
20252035
}
2026-
_ => bug!("unexpected parent item of associated item: {parent_def_id:?}"),
2036+
_ => bug!("unexpected parent item of associated fn: {parent_def_id:?}"),
20272037
}
20282038
}
20292039
DefKind::OpaqueTy => match self.opaque_ty_origin(def_id) {

0 commit comments

Comments
 (0)