[This proposal is also withdrawn.]
When discussing RFC PR 210 today, I realized that InferredLt
/ScopedLt
don’t solve any real problems of QuietDrop
/NoisyDrop
. So I now withdraw that proposal.
I said to @arcto in the RFC’s comments, “Other than lints, an editor powered by the compiler’s future analysis API may be able to highlight implicit dropping points.”
And then I realized, “using an editor” is not the only way to highlight something.
As @pnkfelix finds out, doing explicit early dropping is painful. But, we don’t need to be too explicit.
As @glaebhoerl points out, the actual dropping rule may still be (relatively) simple, just different.
And, though (typically) not as good as compilers, programmers are still good at reasoning about codes, just a little visual help may make a huge difference.
Design:
Reintroduce the ~
sigil as the early drop marker.
It can be used to mark a block, like: { ... ~}
.
Which means “there is at least one early drop happening in this code block, but not in its nested blocks”.
And { ... }
now means “there is no early drop here”.
Rules:
- When the compiler try to insert an early drop, it checks the enclosing block of the (to be) drop point, if the block is not marked by
~
, a compile error happens, otherwise, the compiler inserts the drop. - On the other hand, if the compiler finds
{ ... ~}
, but this marked block doesn’t immediately contain (to be) early drop points, a compile error also happens.
Advantages:
- Blocks immediately containing early drop points are guaranteed to be marked, and there are no false positives, the code will be easier to reason about even without any tools. Code with early drops will never look innocent, that is to say, if a piece of code looks innocent, it is.
- Explicitly writing
drop
calls are not required. -
{ ... ~}
is a lightweight notation, changing between{ ... }
and{ ... ~}
is very easy. - C++ uses
~
for destructors, and Rust can use it to mean “(early) calls of destructors”. - The effect of
~
doesn’t apply to nested blocks, so people will not use it like Java’sthrows Exception
. - This can be used in conjunction with
QuietDrop
/NoisyDrop
. (Though our experiments with static drop may prove that, with the reintroduction of~
,QuietDrop
/NoisyDrop
are unnecessary. If so I’ll be very happy.
Drawbacks:
Some keyboard layouts don’t have ~
.
When we removed ~
from Rust, this was one of the minor reasons, and the fact that ~
is lightweight, is an advantage now. Time changes.
Unresolved Questions:
What about match arms? Do we write => do_something(...) ~,
? Or => { do_something(...) ~}
?