Skip to content

Commit 554fad7

Browse files
committed
Permit calls to default const fns of impl const
1 parent 27e863b commit 554fad7

File tree

1 file changed

+25
-7
lines changed

1 file changed

+25
-7
lines changed

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

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

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-
};
888+
if !tcx.is_const_fn_raw(callee) {
889+
let mut permitted = false;
890+
891+
let callee_trait = tcx.trait_of_item(callee);
892+
if let Some(trait_id) = callee_trait {
893+
if tcx.has_attr(caller, sym::default_method_body_is_const) {
894+
// permit call to non-const fn when caller has default_method_body_is_const..
895+
if tcx.trait_of_item(caller) == callee_trait {
896+
// ..and caller and callee are in the same trait.
897+
permitted = true;
898+
}
899+
}
900+
let mut const_impls = true;
901+
tcx.for_each_relevant_impl(trait_id, substs.type_at(0), |imp| {
902+
if const_impls {
903+
if let hir::Constness::NotConst = tcx.impl_constness(imp) {
904+
const_impls = false;
905+
}
906+
}
907+
});
908+
if const_impls {
909+
permitted = true;
910+
}
911+
}
893912

894-
if !(caller_has_attr && in_same_trait) {
895-
if !tcx.is_const_fn_raw(callee) {
913+
if !permitted {
896914
self.check_op(ops::FnCallNonConst);
897915
return;
898916
}

0 commit comments

Comments
 (0)