Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix const-fn check in const_eval #118004

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion 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,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 } =
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could use let-chains here :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

made it more readable now

call_kind
&& let FnDef(def_id, ..) = self_ty.kind()
&& tcx.is_const_fn_raw(*def_id)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is_const_fn_raw only checks whether the function is const, and not whether it is const-stable. We'd need a test to check whether this would silently allow calling functions that are not const-stable.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how do we do that kind of check ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if other parts of the const checking code already check for const stability, so the first step would be creating an auxiliary ui test where upstream has a const-unstable function and downstream tries to call that function without the feature enabled (following the snippet for this ICE). If it already passes, then great. If it doesn't, we can change this from is_const_fn_raw to is_const_fn

{
return;
}

if !nonconst_call_permission {
let obligation = Obligation::new(
Expand All @@ -865,7 +883,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, ())];
}
Loading