-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
Conversation
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())), |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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?
I don't understand how where
predicates could be used, and where in this situation, so I have a hard time understanding where to put this comment and what to put in it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Jarcho Any suggestion on the comment to add? I'm still confused about what you meant here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If the syntax for it hasn't changed it's T: ~const Trait
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Comment added, a false negative example added as well.
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`.
62311c7
to
43b7d87
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you.
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, withconst_trait
traits whose implementations may or may not beconst
.changelog: [
missing_const_for_fn
]: when checking if a function or method can be called from aconst
context, look at the concrete implementation rather than at the trait definitionFixes #14658
r? @Jarcho