[patch] Java definite assignment

Per Bothner per@bothner.com
Wed Feb 7 12:57:00 GMT 2001

Mo DeJong <mdejong@cygnus.com> writes:

> I am still not sure about this case:
>         int i;
>         while (true) {
>             if (false) {
> 	        i = 0;
>                 break;
>             }
>         }
>         i++;
> It fails the definite assignment
> analysis for 16.2.7, but it
> satisfies the rules for 16.2.9.

> I think in this case, we have hit #4,
> it is both definitely assigned and
> definitely unassigned (fun).

You are still not following the definitions in the JLS.
Clearly, 'i' is definitely assigned after the 'i = 0' and
hence before the 'break'.
'i' is both definitely assigned and unassigned after 'true' when false.[16.1.1]
'i' is assigned after the 'while' because it is assigned after true when false
and is assigned after the 'break'.  [16.2.9]

Hence 'i' is definitely assigned before the 'i++'.  It is *not* definitely
unassigned before the 'i++'. QED.

> Do we need to treat this as an unreachable statement?

That is a different analysis, that has nothing to do with definite assignment.
(There is no match for the word "reach" in chapter 16.)
According to section 14.20, whether the i++ is reachable depends on
whether the 'break' is reachable.  The 'break' is reachable because
the 'if' is reachable.  This is inspite of the fact that the condition
is 'true', due to the special case explained in 14.20.

> For example, javac pukes on the following:
>         int i;
>         while (true) {}
>         i++;
> tmp7.java:7: unreachable statement
>         i++;
>         ^
> 1 error

14.20 gives you the answer:  The 'while' cannot complete normally,
so the 'i++' is unreachable.

> Is doing this definite assignment
> analysis the only way we can
> determine if the loop cannot exit?

Whether the "loop cannot exit" is a separate question from "definite
assigment" analysis or "unreachable statement" analysis.  The latter
have technical definitions in the JLS.  The former is the halting
problem.  However, if unreachable statement analysis determinates that
the loop "cannot complete normally" [which is a technical term], then
we know it cannot exit normally.  However, the converse does not follow.
	--Per Bothner
per@bothner.com   http://www.bothner.com/~per/

More information about the Java mailing list