-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
RFC for unused const fn results #2450
Conversation
The third criterion exists as when params are being moved, the function might invoke the param's `Drop` impl. | ||
If this impl contains side effects, the code might not actually be not dead. | ||
Thus, the lint should require that all moved input params either don't override the default `Drop` impl or use `const Drop` or something like it. | ||
An initial version of the lint could simply require that overriding the `Drop` |
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.
This needs to be enforced recursively, as otherwise a simple wrapper type around a Drop type would suffice to trick the analysis. Maybe we should just start out with copy types?
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.
We might already need a recursive check for UnsafeCell
. Or does UnsafeCell
inhibit Copy
?
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.
No, Cell
is Copy
and contains an UnsafeCell
.
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.
Is this what TyS::is_freeze is for?
const fn add_one<T: const AddOne>(v: T) { | ||
v.add_one(); | ||
} | ||
``` |
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.
Which of the two criteria does this example fall under? Remember ()
is inhabited.
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.
"have all its generic types known" and "have the types of all input parameters contain no mutability". I re-started the numbering for the second group of criteria, but this seems to be confusing, so I might change that.
[summary]: #summary | ||
|
||
Add a lint for unused results of `const fn` functions, | ||
if we know for sure that the invocation is dead code. |
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.
IIRC 'dead code' is synonymous with 'unreachable code'. The invocations in your examples aren't unreachable; it's just that their results are discarded (and since they are free of side effects, they can be optimised away).
I feel positive in general for this. Several points below:
|
This sounds great, @est31. I'd much rather piggy-back the lint on the already-happening constification (like slice len just happened, for example) than have an additional must-use-ification. |
None known to the RFC author. | ||
|
||
But languages that like Rust have a prominent concept of purity/side effect | ||
freedom where you can easily discard results may have such lints. |
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.
This sentence seems awkwardly phrased, with a long middle clause and no commas. Consider rephrasing it for clarity.
[I've put off commenting here long enough, it's becoming clear I'll never get the time and energy to write something that's as careful and nuanced as this topic deserves. So I'll just give it my best. Apologies if that winds up being curt or heavy-handed.] Like @scottmcm I am quite excited about the prospect of side-stepping the must_use-ification and getting better lints for free. However, after some reflection I am not sure whether that actually works, since const fns can diverge and that divergence can be desirable and a sufficient reason for calling. Divergence is discussed in the RFC, but hand-waved away by focusing on the not-obviously-useful case of infinite or long-running loops. Much more important, and harder to explain away, are panics, especially conditional ones (which can't be visible in the function signature). For example, consider an One can argue whether all of those are good coding style, but even if not, it makes clear that the proposed lint has serious false positives. Furthermore, these false positives are inherent in the approach and can't easily be fixed. Panics are an important tool in Rust, and as const fns encompass more and more of Rust, treating const fns without considering panics is as hopeless as approaching general Rust code without considering panics. 1 Now, to preempt some objections:
|
The RFC OP states that the goal is to "lint on side effect free const fn invocations that are not used by the program". The motivation then clarifies this as
Which is true right now, but as @rkruppe correctly asserted, won't be so in the future. Panicking is a side-effect, but still fine for While In rust-lang/rust#51570 I am writing an analysis that figures out whether a call to a const fn should be Thus, I suggest we use the same analysis to decide which function calls to lint. |
The period of my contributions to Rust upstream has reached an end. Thus I'm unable to continue my work on this. I still think something like this is a great addition. I urge anyone interested in this change to adopt and continue it from here on. Thanks. |
I'm interested in adopting this RFC. What is the first step that I should take to do so? |
@Aaron1011 Make a new branch of this one in your own fork; you should also try to address @rkruppe's and @oli-obk's comments / concerns in a new RFC. |
Proposes a lint on side effect free const fn invocations that are not used by the program.
Rendered
An example would be:
Thanks go to @Centril and @rkruppe who reviewed the drafts.