Skip to content

missing_const_for_fn: consider constness of instance #14759

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

samueltardieu
Copy link
Contributor

@samueltardieu samueltardieu commented May 8, 2025

When determining when a function or method can be called from a const context, the determination must be made on the instance, not on the declaration. This makes a difference, for example, with const_trait traits whose implementations may or may not be const.

changelog: [missing_const_for_fn]: when checking if a function or method can be called from a const context, look at the concrete implementation rather than at the trait definition

Fixes #14658

r? @Jarcho

When determining when a function or method can be called from a `const`
context, the determination must be made on the instance, not on the
declaration. This makes a difference, for example, with `const_trait`
traits whose implementations may or may not be `const`.
@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label May 8, 2025
if let ty::FnDef(fn_def_id, fn_substs) = *fn_ty.kind() {
let fn_def_id = match Instance::try_resolve(cx.tcx, cx.typing_env(), fn_def_id, fn_substs) {
Ok(Some(fn_inst)) => fn_inst.def_id(),
Ok(None) => return Err((span, format!("cannot resolve instance for {func:?}").into())),
Copy link
Contributor

Choose a reason for hiding this comment

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

Not being able to resolve to an instance doesn't mean the function can't be const. Not sure what the syntax is but there should be some way to constrain the type to implement the trait as a const trait.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't see how to do that.

I think this is still better to get false negatives rather than false positives, especially when none of the tested cases fails because of this change. There were also no lintcheck misses.

Copy link
Contributor

Choose a reason for hiding this comment

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

I think the current form of const traits is recentish so I wouldn't expect many uses. Can you just add a comment about this doesn't handle where predicates?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-review Status: Awaiting review from the assignee but also interested parties
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[False Positive] Regarding adding const to a function that can't be const
3 participants