Flow scoping

Doug Lea dl at cs.oswego.edu
Fri Jan 4 12:39:30 UTC 2019


On 1/2/19 1:53 PM, Brian Goetz wrote:

> The hard ones (and the potentially controversial ones) were the last two — regarding use _after_ the declaring statement:
> 
> ```
> public void test() {
>     if (!(a instanceof String v)) 
>         throw new NotAStringException("a");
>     if (v.length() == 0)
>         throw new EmptyStringException();
>         
>     println(v.length());
> }
> ```
> 
> ```
> public void test() {
>     if (!(a instanceof String v)) 
>         return;
>         
>     println(v.length());
> }
> ```
> 
> In both of these cases, flow scoping allows `v` to be in scope in the last line — because `v` is DA at the point of use.  And this is because if the `insteanceof` does not match, control does not make it to the use.  In other words, we can use the full flow analysis — including reachability — to conclude the only way we could reach the use is if the binding is defined.  (If there was not a return or a throw, then the last use would not be in scope.)  
> 
> For some, this is uncomfortable at first, as not only does the scope bleed into the full `if` statement, but it bleeds out of it too. 

This seems readily explainable in the same way that other
"fall-throughs" work: There is an implicit else spanning the rest of the
block. Which is just a syntactic convenience to decrease brace-levels.
So it doesn't seem very worrisome on these grounds. Although I wonder
whether style guides requiring explicit braces will require them here.

-Doug




More information about the amber-spec-experts mailing list