Skip to content

Commit a61cd86

Browse files
committed
Methods of const traits are const
1 parent 2776bdf commit a61cd86

File tree

2 files changed

+13
-12
lines changed

2 files changed

+13
-12
lines changed

compiler/rustc_const_eval/src/const_eval/fn_queries.rs

+12-8
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,18 @@ use rustc_hir::def_id::{DefId, LocalDefId};
44
use rustc_middle::query::Providers;
55
use rustc_middle::ty::TyCtxt;
66

7-
fn parent_impl_constness(tcx: TyCtxt<'_>, def_id: LocalDefId) -> hir::Constness {
7+
fn parent_impl_or_trait_constness(tcx: TyCtxt<'_>, def_id: LocalDefId) -> hir::Constness {
88
let parent_id = tcx.local_parent(def_id);
9-
if matches!(tcx.def_kind(parent_id), DefKind::Impl { .. })
10-
&& let Some(header) = tcx.impl_trait_header(parent_id)
11-
{
12-
header.constness
13-
} else {
14-
hir::Constness::NotConst
9+
match tcx.def_kind(parent_id) {
10+
DefKind::Impl { of_trait: true } => tcx.impl_trait_header(parent_id).unwrap().constness,
11+
DefKind::Trait => {
12+
if tcx.is_const_trait(parent_id.into()) {
13+
hir::Constness::Const
14+
} else {
15+
hir::Constness::NotConst
16+
}
17+
}
18+
_ => hir::Constness::NotConst,
1519
}
1620
}
1721

@@ -34,7 +38,7 @@ fn constness(tcx: TyCtxt<'_>, def_id: LocalDefId) -> hir::Constness {
3438

3539
// If the function itself is not annotated with `const`, it may still be a `const fn`
3640
// if it resides in a const trait impl.
37-
parent_impl_constness(tcx, def_id)
41+
parent_impl_or_trait_constness(tcx, def_id)
3842
} else {
3943
tcx.dcx().span_bug(
4044
tcx.def_span(def_id),

compiler/rustc_const_eval/src/const_eval/machine.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -360,10 +360,7 @@ impl<'tcx> interpret::Machine<'tcx> for CompileTimeMachine<'tcx> {
360360
// sensitive check here. But we can at least rule out functions that are not const at
361361
// all. That said, we have to allow calling functions inside a trait marked with
362362
// #[const_trait]. These *are* const-checked!
363-
// FIXME(const_trait_impl): why does `is_const_fn` not classify them as const?
364-
if (!ecx.tcx.is_const_fn(def) && !ecx.tcx.is_const_default_method(def))
365-
|| ecx.tcx.has_attr(def, sym::rustc_do_not_const_check)
366-
{
363+
if !ecx.tcx.is_const_fn(def) || ecx.tcx.has_attr(def, sym::rustc_do_not_const_check) {
367364
// We certainly do *not* want to actually call the fn
368365
// though, so be sure we return here.
369366
throw_unsup_format!("calling non-const function `{}`", instance)

0 commit comments

Comments
 (0)