Join GitHub today
GitHub is home to over 31 million developers working together to host and review code, manage projects, and build software together.
Sign upRFC for unused const fn results #2450
Conversation
oli-obk
reviewed
May 29, 2018
| 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` |
This comment has been minimized.
This comment has been minimized.
oli-obk
May 29, 2018
Contributor
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?
This comment has been minimized.
This comment has been minimized.
est31
May 30, 2018
Author
Contributor
We might already need a recursive check for UnsafeCell. Or does UnsafeCell inhibit Copy?
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
fstirlitz
reviewed
May 30, 2018
| const fn add_one<T: const AddOne>(v: T) { | ||
| v.add_one(); | ||
| } | ||
| ``` |
This comment has been minimized.
This comment has been minimized.
fstirlitz
May 30, 2018
Which of the two criteria does this example fall under? Remember () is inhabited.
This comment has been minimized.
This comment has been minimized.
est31
May 30, 2018
Author
Contributor
"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.
fstirlitz
reviewed
May 30, 2018
| [summary]: #summary | ||
|
|
||
| Add a lint for unused results of `const fn` functions, | ||
| if we know for sure that the invocation is dead code. |
This comment has been minimized.
This comment has been minimized.
fstirlitz
May 30, 2018
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).
This comment has been minimized.
This comment has been minimized.
|
I feel positive in general for this. Several points below:
|
This comment has been minimized.
This comment has been minimized.
|
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. |
joshtriplett
reviewed
Jun 4, 2018
| 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. |
This comment has been minimized.
This comment has been minimized.
joshtriplett
Jun 4, 2018
•
Member
This sentence seems awkwardly phrased, with a long middle clause and no commas. Consider rephrasing it for clarity.
Centril
added
the
T-lang
label
Jun 11, 2018
This comment has been minimized.
This comment has been minimized.
|
[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:
|
This comment has been minimized.
This comment has been minimized.
|
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. |
This comment has been minimized.
This comment has been minimized.
|
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. |
est31
closed this
Jul 3, 2018
This comment has been minimized.
This comment has been minimized.
Aaron1011
commented
Feb 14, 2019
|
I'm interested in adopting this RFC. What is the first step that I should take to do so? |
This comment has been minimized.
This comment has been minimized.
|
@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. |
est31 commentedMay 29, 2018
•
edited
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.