Skip to content

Commit

Permalink
Fix const-fn check in const_eval
Browse files Browse the repository at this point in the history
  • Loading branch information
ouz-a committed Nov 17, 2023
1 parent 4770d91 commit 495c974
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
22 changes: 20 additions & 2 deletions compiler/rustc_const_eval/src/transform/check_consts/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 _;
Expand Down Expand Up @@ -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,
Expand All @@ -865,7 +884,6 @@ impl<'tcx> Visitor<'tcx> for Checker<'_, 'tcx> {
&e,
);
}

self.check_op(ops::FnCallNonConst {
caller,
callee,
Expand Down
11 changes: 11 additions & 0 deletions tests/ui/consts/const-eval/const-fn-slice.rs
Original file line number Diff line number Diff line change
@@ -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, ())];
}

0 comments on commit 495c974

Please sign in to comment.