From 495c974bfadb3814686a71dee265bd75c2c4cae7 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 | 22 +++++++++++++++++-- tests/ui/consts/const-eval/const-fn-slice.rs | 11 ++++++++++ 2 files changed, 31 insertions(+), 2 deletions(-) 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..7ea4891e93342 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,7 +848,24 @@ 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 + { + if let FnDef(def_id, ..) = self_ty.kind() { + if tcx.is_const_fn_raw(*def_id) { + return; + } + } + } if !nonconst_call_permission { let obligation = Obligation::new( tcx, @@ -865,7 +884,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, ())]; +}