r/programming • u/jonjonbee • Jun 05 '18
Code golfing challenge leads to discovery of string concatenation bug in JDK 9+ compiler
https://stackoverflow.com/questions/50683786/why-does-arrayin-i-give-different-results-in-java-8-and-java-10
2.2k
Upvotes
View all comments
Show parent comment
1
u/[deleted] Jun 05 '18 edited Jun 05 '18
You know - you're right here. I'm wrong. The case where there is an optimisation - e.g., with JDK 8, which I was testing it with, a StringBuilder is constructed, and yet it is actually correct (though retarded). In JDK 9 they started doing a much simpler syntax sugar expansion - with
InvokeDynamic #0:makeConcatWithConstants:(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;- I did not know it (because fuck Java 9) and did not care to even read the first few answers to the OP post.Still, this is also an optimisation - this thing is obviously an intrinsic used by the JVM runtime to do the analysis (and StringBuilder conversion) I was talking about. But, now it's especially funny that they managed to screw it up with an expression-level syntax sugar - previously, before JDK 9, it was an expansion of a single binary expression into a complex expression, an equivalent of
(new StringBuilder(L)).append(R) .ToString().No. It's the only thing that matters here.
What they did is: AST -> typed AST -> lowering to JVM, and this lowering have different expansion rules for different types of nodes like
+=.What anyone who is not a complete retard would do:
L OP= Binto a statement-expression node{lvalue TMP = &L; *TMP = *TMP OP R; return *TMP;}(see - I used statement-expressions and explicit lvalues here, and they do not exist in your first AST, you have a new IR here, stripped of postfix and infix operators andOP=, but enhanced with statement-expressions and lvalues, which do not exist in Java or your first AST). As a bonus, expand++and--here.+binary operator, feel free to dispatch it as anaddinstruction ormakeConcatWithConstantsdepending on type.No space for screwing up here whatsoever. And if you actually do, you'll know it immediately, as
i++will stop to work.They were - before JDK 9, and being lazy and typing on mobile, I assumed it's still the case. Turns out they simplified it, used an intrinsic, and still managed to fuck it up. I'm impressed.
Sorry, but no, you don't, if you keep insisting that introducing more IR levels and more passes is not going to reduce this (or any other) problem to completely trivial, so simple that it's impossible to screw up even if you want to. That's the most fundamental thing about compilers, not understanding this is unforgivable (why? see the bug we're discussing). You may even be on a same level as those who wrote
javac- it still won't be qualified as knowing more than nothing at all, as long as you do not know the only thing that is important. Anything else is immaterial.And this is exactly the main point I am trying to communicate. I do not care if anyone is offended, the message itself is far too important to miss any opportunity to highlight it - compilers can reduce complexity all the way down to nothing, and if you're not exploiting this feature, you're doing everything wrong and must reconsider your ways immediately. This message still holds, it does not really matter what the syntax sugar is expanded into - a single intrinsic or a half-assed StringBuilder optimisation, it's still done the wrong way. Also, it's pretty much the only message I consistently push here, nothing else is important.