Skip to content

Commit e9dfcc2

Browse files
authored
Unrolled build for #149444
Rollup merge of #149444 - fee1-dead-contrib:push-lurmmylquwsq, r=oli-obk collapse `constness` query `match` logic We already have the HIR node data, so no need for asking `def_kind` again. r? oli-obk
2 parents 03ce87d + cf91330 commit e9dfcc2

File tree

2 files changed

+37
-41
lines changed

2 files changed

+37
-41
lines changed

compiler/rustc_const_eval/src/const_eval/fn_queries.rs

Lines changed: 34 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,48 @@
1-
use rustc_hir as hir;
2-
use rustc_hir::def::DefKind;
31
use rustc_hir::def_id::{DefId, LocalDefId};
2+
use rustc_hir::{
3+
Constness, ExprKind, ForeignItemKind, ImplItem, ImplItemImplKind, ImplItemKind, Item, ItemKind,
4+
Node, TraitItem, TraitItemKind, VariantData,
5+
};
46
use rustc_middle::query::Providers;
57
use rustc_middle::ty::TyCtxt;
68

7-
fn parent_impl_or_trait_constness(tcx: TyCtxt<'_>, def_id: LocalDefId) -> hir::Constness {
8-
let parent_id = tcx.local_parent(def_id);
9-
match tcx.def_kind(parent_id) {
10-
DefKind::Impl { of_trait: true } => tcx.impl_trait_header(parent_id).constness,
11-
DefKind::Impl { of_trait: false } => tcx.constness(parent_id),
12-
DefKind::Trait => {
13-
if tcx.is_const_trait(parent_id.into()) {
14-
hir::Constness::Const
15-
} else {
16-
hir::Constness::NotConst
17-
}
18-
}
19-
_ => hir::Constness::NotConst,
20-
}
21-
}
22-
23-
/// Checks whether a function-like definition is considered to be `const`.
24-
fn constness(tcx: TyCtxt<'_>, def_id: LocalDefId) -> hir::Constness {
9+
/// Checks whether a function-like definition is considered to be `const`. Also stores constness of inherent impls.
10+
fn constness(tcx: TyCtxt<'_>, def_id: LocalDefId) -> Constness {
2511
let node = tcx.hir_node_by_def_id(def_id);
2612

2713
match node {
28-
hir::Node::Ctor(hir::VariantData::Tuple(..)) => hir::Constness::Const,
29-
hir::Node::ForeignItem(item) if let hir::ForeignItemKind::Fn(..) = item.kind => {
14+
Node::Ctor(VariantData::Tuple(..)) => Constness::Const,
15+
Node::ForeignItem(item) if let ForeignItemKind::Fn(..) = item.kind => {
3016
// Foreign functions cannot be evaluated at compile-time.
31-
hir::Constness::NotConst
17+
Constness::NotConst
3218
}
33-
hir::Node::Expr(e) if let hir::ExprKind::Closure(c) = e.kind => c.constness,
34-
hir::Node::Item(i) if let hir::ItemKind::Impl(impl_) = i.kind => impl_.constness,
35-
_ => {
36-
if let Some(fn_kind) = node.fn_kind() {
37-
if fn_kind.constness() == hir::Constness::Const {
38-
return hir::Constness::Const;
39-
}
40-
41-
// If the function itself is not annotated with `const`, it may still be a `const fn`
42-
// if it resides in a const trait impl.
43-
parent_impl_or_trait_constness(tcx, def_id)
44-
} else {
45-
tcx.dcx().span_bug(
46-
tcx.def_span(def_id),
47-
format!("should not be requesting the constness of items that can't be const: {node:#?}: {:?}", tcx.def_kind(def_id))
48-
)
19+
Node::Expr(e) if let ExprKind::Closure(c) = e.kind => c.constness,
20+
// FIXME(fee1-dead): extract this one out and rename this query to `fn_constness` so we don't need `is_const_fn` anymore.
21+
Node::Item(i) if let ItemKind::Impl(impl_) = i.kind => impl_.constness,
22+
Node::Item(Item { kind: ItemKind::Fn { sig, .. }, .. }) => sig.header.constness,
23+
Node::ImplItem(ImplItem {
24+
impl_kind: ImplItemImplKind::Trait { .. },
25+
kind: ImplItemKind::Fn(..),
26+
..
27+
}) => tcx.impl_trait_header(tcx.local_parent(def_id)).constness,
28+
Node::ImplItem(ImplItem {
29+
impl_kind: ImplItemImplKind::Inherent { .. },
30+
kind: ImplItemKind::Fn(sig, _),
31+
..
32+
}) => {
33+
match sig.header.constness {
34+
Constness::Const => Constness::Const,
35+
// inherent impl could be const
36+
Constness::NotConst => tcx.constness(tcx.local_parent(def_id)),
4937
}
5038
}
39+
Node::TraitItem(TraitItem { kind: TraitItemKind::Fn(..), .. }) => tcx.trait_def(tcx.local_parent(def_id)).constness,
40+
_ => {
41+
tcx.dcx().span_bug(
42+
tcx.def_span(def_id),
43+
format!("should not be requesting the constness of items that can't be const: {node:#?}: {:?}", tcx.def_kind(def_id))
44+
)
45+
}
5146
}
5247
}
5348

tests/crashes/137187.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
//@ known-bug: #137187
22
use std::ops::Add;
3-
trait A where
3+
4+
const trait A where
45
*const Self: Add,
56
{
6-
const fn b(c: *const Self) -> <*const Self as Add>::Output {
7+
fn b(c: *const Self) -> <*const Self as Add>::Output {
78
c + c
89
}
910
}

0 commit comments

Comments
 (0)