Skip to content

Commit d8d4cc3

Browse files
committed
Treat trait fns marked with the attr as const
1 parent 3660a4e commit d8d4cc3

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

compiler/rustc_mir/src/const_eval/fn_queries.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use rustc_hir::def_id::{DefId, LocalDefId};
33
use rustc_middle::hir::map::blocks::FnLikeNode;
44
use rustc_middle::ty::query::Providers;
55
use rustc_middle::ty::TyCtxt;
6-
use rustc_span::symbol::Symbol;
6+
use rustc_span::symbol::{sym, Symbol};
77
use rustc_target::spec::abi::Abi;
88

99
/// Whether the `def_id` counts as const fn in your current crate, considering all active
@@ -60,6 +60,9 @@ fn is_const_fn_raw(tcx: TyCtxt<'_>, def_id: DefId) -> bool {
6060
return true;
6161
}
6262

63+
if tcx.has_attr(def_id, sym::default_method_body_is_const) {
64+
return true;
65+
}
6366
// If the function itself is not annotated with `const`, it may still be a `const fn`
6467
// if it resides in a const trait impl.
6568
is_parent_const_impl_raw(tcx, hir_id)
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// TODO fix this test
2+
3+
#![feature(const_trait_impl)]
4+
#![allow(incomplete_features)]
5+
6+
trait ConstDefaultFn: Sized {
7+
fn b(self);
8+
9+
#[default_method_body_is_const]
10+
fn a(self) {
11+
self.b();
12+
}
13+
}
14+
15+
struct NonConstImpl;
16+
struct ConstImpl;
17+
18+
impl ConstDefaultFn for NonConstImpl {
19+
fn b(self) {}
20+
}
21+
22+
impl const ConstDefaultFn for ConstImpl {
23+
fn b(self) {}
24+
}
25+
26+
const fn test() {
27+
NonConstImpl.a();
28+
//~^ ERROR calls in constant functions are limited to constant functions, tuple structs and tuple variants
29+
ConstImpl.a();
30+
}
31+
32+
fn main() {}

0 commit comments

Comments
 (0)