Skip to content

Commit 56d79ad

Browse files
committed
Skip check for calling functions in same trait
1 parent d8d4cc3 commit 56d79ad

File tree

2 files changed

+15
-5
lines changed

2 files changed

+15
-5
lines changed

compiler/rustc_mir/src/transform/check_consts/validation.rs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -885,9 +885,17 @@ impl Visitor<'tcx> for Validator<'mir, 'tcx> {
885885
return;
886886
}
887887

888-
if !tcx.is_const_fn_raw(callee) {
889-
self.check_op(ops::FnCallNonConst);
890-
return;
888+
let caller_has_attr = tcx.has_attr(caller, sym::default_method_body_is_const);
889+
let in_same_trait = match (tcx.trait_of_item(caller), tcx.trait_of_item(callee)) {
890+
(Some(t1), Some(t2)) => t1 == t2,
891+
_ => false
892+
};
893+
894+
if !(caller_has_attr && in_same_trait) {
895+
if !tcx.is_const_fn_raw(callee) {
896+
self.check_op(ops::FnCallNonConst);
897+
return;
898+
}
891899
}
892900

893901
// If the `const fn` we are trying to call is not const-stable, ensure that we have

src/test/ui/rfc-2632-const-trait-impl/const-default-method-bodies.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
// TODO fix this test
1+
// check-pass
2+
// TODO remove this^
23

34
#![feature(const_trait_impl)]
5+
#![feature(const_fn_trait_bound)] // FIXME is this needed?
46
#![allow(incomplete_features)]
57

68
trait ConstDefaultFn: Sized {
@@ -25,7 +27,7 @@ impl const ConstDefaultFn for ConstImpl {
2527

2628
const fn test() {
2729
NonConstImpl.a();
28-
//~^ ERROR calls in constant functions are limited to constant functions, tuple structs and tuple variants
30+
// TODO ~^ ERROR calls in constant functions are limited to constant functions, tuple structs and tuple variants
2931
ConstImpl.a();
3032
}
3133

0 commit comments

Comments
 (0)