240 points · by javinpaul · 8y ago · 96 comments
For the unfamiliar, the way string concatenation is compiled changed with invokedynamic.
Previously, it would compile to either Sting.concat calls or transform into a StringBuilder, but with invoke dynamic we can leave it to the runtime to optimize when it first runs.
I'm not surprised. Java 9 has minor adoption, and having side effects on the left hand side of an assignment operation is really really bad practice.
So you'd need a codebase that (1) was updated or written in a very new Java version, (2) contained the smelly code and (3) had a harmful double side-effect that got noticed. That's a pretty rare combination.
No it isn't. Something like this:
data[index++] += 'a'
Would easily pass any reasonable code review. It's not that smelly or unusual.
It might be unusual with strings, which makes this less common, but it's not an unusual format at all. It's a non-trivial part of the reason ++ operator exists in the first place.
I wouldn't pass that in a code review without comment!
I'd ask the author to consider breaking down the two actions into two separate lines so that it's more clear that there are two actions and what the sequential ordering of them is.
Inline side effects like that are a mess. Without looking it up, I could not even tell you if the sequence point ordering of that kind of expression in general is defined in C!
And I have some dislike for "clever" code. I trade keystrokes for readability every day.
(Unless, as we can see, someone doesn't know what they are doing in compiler maintenance, but I mean besides that.)
data[ index ++ ] = data[ index ++ ] + “a”;
But that can’t be the right expansion.Ugh. Today’s code sample on Sesame Street, brought to you by the letters “F” & “U”, and the number 2. Why would you subject somebody to something that edgy?
[0] Allegedly the fix was available in a non-public build of 8u162
You haven't heard of the bug in binary search that existed for nine years then?
https://ai.googleblog.com/2006/06/extra-extra-read-all-about...
Even if it isn't LTS, does it make sense to.stay on the 8, which will be end of life soon anyway?
At least, programming using modules makes the codebase ready for Java 11.
The only thing that I don't like is the removal of javapackager without substituting it (jlink cannot produce bundles with applications, only a light redistributed image)
"+=" is [usually explained as] shorthand for repeating the left side. Why should
> foo[i++] += 1
evaluate differently than
> foo[i++] = foo[i++] + 1
?
The way it currently works is the way I would expect it to work, but I'm evidently in the minority here. I also don't write Java and since this is a change from Java 8, it needs to be fixed.
Let me state that again: yes, of course this bug needs to be fixed.
The spec is how it should work, regardless of developer's opinions.
> Why should [a] evaluate differently than [b]?
In my opinion (and I'm guessing in the opinion of the spec authors, though I can't be sure), saying "x += y" means "add y to x", so I'd expect the language to:
1) Figure out what "x" is once
2) Add "y" to it
Your opinion seems to be that saying "x += y" means "desugar to x = x + y" and you expect the language to: 1) Figure out what "x" is as a value
2) Add "y" to the value of "x"
3) Figure out what "x" is as a location (which may differ from 1)
4) Store the result into "x"
To me step 3 is very surprising, that "x" may differ, which is why I think the spec's definition is the more intuitive one (plus, it doesn't require knowledge of "desugaring" things).Rather than a redundancy and sugar on + and = ("I do not want to type the left side twice"), it is a distinction I am making to the compiler ("I do not want you to evaluate the left side twice").
The title [Edit: since changed] is a little bit fear-monger-y. It's only broken when the left hand side is not safe to evaluate more than once.
It can be prevented with a compile-time flag.
Java 9 broke other things. Here is one: https://github.com/andy-goryachev/FxDock/issues/8 WebView is working fine in 8, totally broken in 9, partially broken in 10. Do they have a QA department?
The ease of avoidance is unrelated to whether it's broken behavior or not.
array[i++] += i ... array[i++] += "a"
is just is broken. So is appending to array[foo()] or foo().fieldAlso: it only breaks when it's an array of Strings.
The brakes on my car aren't broken most of the time, it's only when I push down on the pedal!
Unfortunately, the `++` / `--` made it to Java, and are still used sometimes, despite JVM not being PDP-11, and definitely not trying to conserve every precious byte of RAM.
But importantly your original point about "depending on order of their evaluation" is wrong. Most of the examples don't care about order at all. They break because things happen the wrong number of times.
I didn't know the Java compiler converts this automatically now. Since which version is this? I suspect there might be still edge cases where it doesn't work.
The bug happens with a function call on the LHS too.
> nor by allowing assignment operators (including augmented assignment ops) to evaluate to the value of their RHS
I'm confused. The assignment is happening last, and whatever it evaluates to is being discarded. How would changing what it evaluates to do anything?
But more importantly, the comment was about implementing Java itself in Rust, not the broken function, and that is why it wouldn't change anything.
See: substrateVM, graal
[2]> (macroexpand '(incf (aref a (incf i)) x))
(LET*
((#:G3214 A) (#:G3215 (INCF I)) (#:G3217 X)
(#:G3216 (+ (AREF #:G3214 #:G3215) #:G3217)))
(SYSTEM::STORE #:G3214 #:G3215 #:G3216)) ;
T
You can't just blindly expand the += by copying the expressions/AST node on the left. Or in general; you don't take expressions in the program and blindly duplicate them. How can someone working on compilers not know that???This bug is a little narrower than this makes it out to be, it ONLY fails when the array on the left is a String[ ]. The way String operations get compiled down to bytecode changed a lot around those releases so the bug is more like an unforeseen interaction between optimization passes than a naive MST/Macro implementation.
The "funny" thing here to me is the surprising gap in the regression testing given the quality of most of the JDK. go figure
On the other hand this explanation is stupid, as a programmer can implement `add()` in a wrong way.