Disallow consumption through local lets #1501
kyouko-taiga
started this conversation in
Language design
Replies: 1 comment 4 replies
-
There is currently one simple explanation for when you can consume a value: when you can know (statically and locally) that there will be no live bindings to it immediately thereafter. That is true no matter what the value has been bound to. Adding a rule prohibiting consumption in one case makes the language more complicated, not simpler. |
Beta Was this translation helpful? Give feedback.
4 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I know it may look extreme, but I'm really tempted to try disallowing the consumption of local let bindings. I'm starting to think that the trick is causing more harm to understandability than it helps writing concise or "traditional-looking" code.
For instance, this is currently allowed (at least in principle):
We can accept this program because the compiler can figure out two things:
u
actually holds ownership of its value, so in fact it's as though it had been introduced withsink let
.u
,v
, ory
because we can just desugarv
asu
andy
asu.y
.None of these conditions holds if a non-trivial projection (e.g., a call to a subscript) is involved or if another use occurs after the consumption of
y
(e.g., replacereturn y
byy.deinit(); print(v.z)
). The the compiler will assume we're dealing with "regular"let
s and emit error messages accordingly. So we can build very similar-looking programs and get different behaviors for which an explanation requires a deep dive into the way the compiler thinks about lifetimes. For exampleThe problem here is that
u.y
is a property access on an opaque type and so we cannot partially consume an instance ofVec3
.Everything would be explained more easily if we said that consuming local lets is illegal. Then we'd have to rewrite the program like this:
For the low cost of 4 characters we get clearer intent and simpler rules.
On the other hand the code looks a little more foreign and we'd likely see more errors when dealing with indestructible types. Optimizations could get a little harder but I'm not too worried because we could probably remove bindings that have an obvious desugaring during access checking.
Beta Was this translation helpful? Give feedback.
All reactions