A friend made me look at this page, and noticed a strange piece of code in the signature of one of the forum users.
The code is a one-liner that goes as follows:
On Local Error Resume Next: If Not Empty Is Nothing Then Do While Null: ReDim i(True To False) As Currency: Loop: Else Debug.Assert CCur(CLng(CInt(CBool(False Imp True Xor False Eqv True)))): Stop: On Local Error GoTo 0
Scrolling removed:
On Local Error Resume Next: If Not Empty Is Nothing Then Do While Null: ReDim i(True To False) As Currency: Loop: Else Debug.Assert CCur(CLng(CInt(CBool(False Imp True Xor False Eqv True)))): Stop: On Local Error GoTo 0
Quite mind-blowingly, that code compiles (I didn't try to run it, but that's irrelevant) if you just paste it as-is (and then don't touch it again!) into a valid procedure-level scope.
Some observations:
- If instruction separators / colons are replaced with new lines, it no longer compiles
On Local Error
can be simplified toOn Error
- The nested conversions aren't of any particular interest at first sight, but it turns out replacing that series of conversions and comparisons with a simple
Debug.Assert True
makes the code consistently compile, so something in there is messing up the compiler. - If the code is pasted, it compiles; if it's modified in any way (even merely removing
Local
) after the VBE has validated the line, it stops compiling, and nothing seems to make VBA understand it anymore, unless the line is deleted and re-pasted. - The latest rubberduck grammar/parser being closely modeled on the actual VBA specs, it parses and resolves just fine (which honestly blows my mind)
- If the line is known to not compile, and then cut/re-pasted, it doesn't compile... but re-pasting it again from outside the VBE, it suddenly compiles.
The question is, how does this code manage to compile against the VB language specs? Is it a bug in the VB[6|A|E] implementation? In other words, why/how does it work?
I think it has something to do with the instruction separator (:
) and the inline-if syntax - given there's no End If
statement, the thing is a single line and not a block.
But then, what makes that specific code be Schrödinger's code? What is it that makes it both legal and illegal at the same time?
If the code gets correctly parsed by a parser generated using a formal grammar definition (ANTLR), then it must be a legal construct? Then why does it stop being legal when you merely go back to that line and hit ENTER?