From 9ef8b709b12ff4096446e54fa3c25f1132c9c0c8 Mon Sep 17 00:00:00 2001 From: ouz-a Date: Fri, 17 Nov 2023 12:22:58 +0300 Subject: [PATCH] Fix const-fn check in const_eval --- .../src/transform/check_consts/check.rs | 19 ++++++++++++++++++- tests/ui/consts/const-eval/const-fn-slice.rs | 11 +++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) create mode 100644 tests/ui/consts/const-eval/const-fn-slice.rs diff --git a/compiler/rustc_const_eval/src/transform/check_consts/check.rs b/compiler/rustc_const_eval/src/transform/check_consts/check.rs index 76116e339859a..190f802bc7f8d 100644 --- a/compiler/rustc_const_eval/src/transform/check_consts/check.rs +++ b/compiler/rustc_const_eval/src/transform/check_consts/check.rs @@ -9,9 +9,11 @@ use rustc_infer::traits::{ImplSource, Obligation, ObligationCause}; use rustc_middle::mir::visit::{MutatingUseContext, NonMutatingUseContext, PlaceContext, Visitor}; use rustc_middle::mir::*; use rustc_middle::traits::BuiltinImplSource; +use rustc_middle::ty::FnDef; use rustc_middle::ty::GenericArgs; use rustc_middle::ty::{self, adjustment::PointerCoercion, Instance, InstanceDef, Ty, TyCtxt}; use rustc_middle::ty::{TraitRef, TypeVisitableExt}; +use rustc_middle::util::call_kind; use rustc_mir_dataflow::{self, Analysis}; use rustc_span::{sym, Span, Symbol}; use rustc_trait_selection::traits::error_reporting::TypeErrCtxtExt as _; @@ -846,6 +848,22 @@ impl<'tcx> Visitor<'tcx> for Checker<'_, 'tcx> { { nonconst_call_permission = true; } + let call_kind = call_kind( + tcx, + self.param_env, + callee, + fn_args, + *fn_span, + call_source.from_hir_call(), + None, + ); + if let call_kind::CallKind::FnCall { fn_trait_id: _, self_ty } = + call_kind + && let FnDef(def_id, ..) = self_ty.kind() + && tcx.is_const_fn_raw(*def_id) + { + return; + } if !nonconst_call_permission { let obligation = Obligation::new( @@ -865,7 +883,6 @@ impl<'tcx> Visitor<'tcx> for Checker<'_, 'tcx> { &e, ); } - self.check_op(ops::FnCallNonConst { caller, callee, diff --git a/tests/ui/consts/const-eval/const-fn-slice.rs b/tests/ui/consts/const-eval/const-fn-slice.rs new file mode 100644 index 0000000000000..061ad99424331 --- /dev/null +++ b/tests/ui/consts/const-eval/const-fn-slice.rs @@ -0,0 +1,11 @@ +//check-pass + +#![feature(const_trait_impl)] +#![feature(fn_traits)] +const fn f() -> usize { + 5 +} + +const fn main() { + let _ = [0; Fn::call(&f, ())]; +}