Try/Catch DU Exception

Olexandr Rotan rotanolexandr842 at gmail.com
Sat Jul 13 11:16:37 UTC 2024


It would be good to fix what you are saying. However, I don't agree that
this enhancement would introduce any new rule. Rule for final variables and
fields is one and only one in this context: it must be initialized once.
This fix would just make enforcement of this rule more precise by modifying
existing compiler rules that currently don't let through code that is
intuitively and factually correct, but falls into a blind spot of
specifications. Of course, JLS defines a set of rules on how to determine
whether the value was previously assigned or not, and, strictly speaking,
current behaviour is the right one. However, JLS here fails to let through
valid code from, lets say, a logical point of view. So, actually, no one
introduces new rules to the compiler. In fact, this fix would remove
exception from the rules that are semantically applied to final variables.

On Sat, Jul 13, 2024 at 12:54 PM Attila Kelemen <attila.kelemen85 at gmail.com>
wrote:

> Thanks, it is good to see `Thread.stop` go (though as far as I can see it
> was only made dysfunctional in JDK 20).
>
> That said, I think my main point still stands. It is a complicated rule to
> completely explain. A clear evidence to this is that Archie's original
> definition is certainly not what people would expect for this rule, because
> they would expect this to compile then:
>
> ```
> final int x;
> try {
>   if (cond1) x = f1();
>   else x = f2();
> } catch (Exception e) {
>   x = f3();
> }
> ```
>
> where the last statement of the `try` block is not an assignment statement.
>
> Also, my real concern here is that people are not pointing out where this
> is truly annoying. Because the reason you need that final so much because
> you want this to work:
>
> ```
> final int x;
> try {
>   x = 4;
> } catch (Exception e) {
>   x = 2;
> }
>
> executor.execute(() -> System.out.println(x));
> ```
>
> If so, then this "compiler fix" is a very special case, and not solving
> the true issue. Since it does not solve the following (which is equally
> annoying):
>
> ```
> int x = 4;
> if (something()) {
>   x = 2;
> }
>
> executor.execute(() -> System.out.println(x));
> // No more assignments to `x`
> ```
>
> Even though this could be solved by naturally relaxing the constraint on
> captured local variables, and in that case not many people would complain
> about Archie's issue in my opinion/
>
> Tagir Valeev <amaembo at gmail.com> ezt írta (időpont: 2024. júl. 13., Szo,
> 7:02):
>
>> Hello!
>>
>> On Fri, Jul 12, 2024 at 10:44 PM Attila Kelemen <
>> attila.kelemen85 at gmail.com> wrote:
>>
>>> I personally wouldn't like too much if the compiler tried to be very
>>> smart with this. The current behavior is very easy to reason about, and
>>> this proposal looks too special to me. Especially because then it raises
>>> more questions about `Thread.stop`.
>>>
>>
>> Thread.stop is not an issue anymore, as it's dysfunctional since Java 18:
>> https://bugs.openjdk.org/browse/JDK-8277861
>> It still could be possible to generate an exception in the thread using
>> JVM TI though. On the other hand, you can update final local variable using
>> JVM TI as well, so it should not be a problem.
>>
>>
>>> Also, the way this issue is presented is not much of a concern (at least
>>> for the example code). Not making the local variable final is kinda
>>> whatever. Where this is annoying is when you want to capture the variable
>>> (mostly in a lambda). However, if that is the issue that you are trying to
>>> address, then I think this is the wrong solution to that issue. I think the
>>> real solution to the issue would be relaxing the constraint to be able to
>>> capture a local variable: We should be able to capture it, iff the variable
>>> is definitely assigned before capturing, and there is definitely no
>>> assignment after it.
>>>
>>> Archie Cobbs <archie.cobbs at gmail.com> ezt írta (időpont: 2024. júl.
>>> 12., P, 22:24):
>>>
>>>> I'd like to propose a very minor language improvement and would
>>>> appreciate any feedback.
>>>>
>>>> This is a true corner case, but I bet most developers have tripped over
>>>> it a few times. It's easy to work around... but still...
>>>>
>>>> Here's a simple example:
>>>>
>>>>     void describeMyThread(Thread thread) {
>>>>         final String description;
>>>>         try {
>>>>             description = '"' + thread.getName() + "'";
>>>>         } catch (NullPointerException e) {
>>>>             description = "null";
>>>>         }
>>>>         System.out.println("the thread is " + description);
>>>>     }
>>>>
>>>> This doesn't compile:
>>>>
>>>>     DUTest.java:8: error: variable description might already have been
>>>> assigned
>>>>                 description = "(null)";
>>>>                 ^
>>>>
>>>> The error is a false positive: there is no way an exception can be
>>>> thrown in the try block *after* description is assigned, because
>>>> description being assigned is literally the last thing that occurs in
>>>> the try block.
>>>>
>>>> Developers intuitively know that description will be DU at the start
>>>> of the catch block, so the error feels surprising and makes the compiler
>>>> seem less smart than it should be.
>>>>
>>>> My proposal is to fix this by adding a "try/catch DU exception" to
>>>> §16.2.15:
>>>>
>>>> V is definitely unassigned before a catch block iff all of the
>>>> following are true:
>>>>     V is definitely unassigned after the try block *or the try block
>>>> ends with an assignment expression statement V=b and V is definitely
>>>> unassigned after b*
>>>>     V is definitely unassigned before every return statement ...
>>>>
>>>> A prototype compiler implementation is here
>>>> <https://github.com/openjdk/jdk/compare/master...archiecobbs:jdk:TryCatchDUException>
>>>> .
>>>>
>>>> Thoughts?
>>>>
>>>> -Archie
>>>>
>>>> --
>>>> Archie L. Cobbs
>>>>
>>>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/amber-dev/attachments/20240713/3daf6edc/attachment.htm>


More information about the amber-dev mailing list