Skip to content

Commit 9ef8b70

Browse files
committed
Fix const-fn check in const_eval
1 parent 4770d91 commit 9ef8b70

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

compiler/rustc_const_eval/src/transform/check_consts/check.rs

+18-1
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,11 @@ use rustc_infer::traits::{ImplSource, Obligation, ObligationCause};
99
use rustc_middle::mir::visit::{MutatingUseContext, NonMutatingUseContext, PlaceContext, Visitor};
1010
use rustc_middle::mir::*;
1111
use rustc_middle::traits::BuiltinImplSource;
12+
use rustc_middle::ty::FnDef;
1213
use rustc_middle::ty::GenericArgs;
1314
use rustc_middle::ty::{self, adjustment::PointerCoercion, Instance, InstanceDef, Ty, TyCtxt};
1415
use rustc_middle::ty::{TraitRef, TypeVisitableExt};
16+
use rustc_middle::util::call_kind;
1517
use rustc_mir_dataflow::{self, Analysis};
1618
use rustc_span::{sym, Span, Symbol};
1719
use rustc_trait_selection::traits::error_reporting::TypeErrCtxtExt as _;
@@ -846,6 +848,22 @@ impl<'tcx> Visitor<'tcx> for Checker<'_, 'tcx> {
846848
{
847849
nonconst_call_permission = true;
848850
}
851+
let call_kind = call_kind(
852+
tcx,
853+
self.param_env,
854+
callee,
855+
fn_args,
856+
*fn_span,
857+
call_source.from_hir_call(),
858+
None,
859+
);
860+
if let call_kind::CallKind::FnCall { fn_trait_id: _, self_ty } =
861+
call_kind
862+
&& let FnDef(def_id, ..) = self_ty.kind()
863+
&& tcx.is_const_fn_raw(*def_id)
864+
{
865+
return;
866+
}
849867

850868
if !nonconst_call_permission {
851869
let obligation = Obligation::new(
@@ -865,7 +883,6 @@ impl<'tcx> Visitor<'tcx> for Checker<'_, 'tcx> {
865883
&e,
866884
);
867885
}
868-
869886
self.check_op(ops::FnCallNonConst {
870887
caller,
871888
callee,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
//check-pass
2+
3+
#![feature(const_trait_impl)]
4+
#![feature(fn_traits)]
5+
const fn f() -> usize {
6+
5
7+
}
8+
9+
const fn main() {
10+
let _ = [0; Fn::call(&f, ())];
11+
}

0 commit comments

Comments
 (0)