Skip to content

Commit 495c974

Browse files
committed
Fix const-fn check in const_eval
1 parent 4770d91 commit 495c974

File tree

2 files changed

+31
-2
lines changed

2 files changed

+31
-2
lines changed

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

+20-2
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,7 +848,24 @@ impl<'tcx> Visitor<'tcx> for Checker<'_, 'tcx> {
846848
{
847849
nonconst_call_permission = true;
848850
}
849-
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+
{
863+
if let FnDef(def_id, ..) = self_ty.kind() {
864+
if tcx.is_const_fn_raw(*def_id) {
865+
return;
866+
}
867+
}
868+
}
850869
if !nonconst_call_permission {
851870
let obligation = Obligation::new(
852871
tcx,
@@ -865,7 +884,6 @@ impl<'tcx> Visitor<'tcx> for Checker<'_, 'tcx> {
865884
&e,
866885
);
867886
}
868-
869887
self.check_op(ops::FnCallNonConst {
870888
caller,
871889
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)