RFR: 8295020: javac emits incorrect code for for-each on an intersection type.

Srikanth Adayapalam sadayapalam at openjdk.org
Fri Oct 14 10:19:07 UTC 2022


On Fri, 14 Oct 2022 10:03:20 GMT, Maurizio Cimadamore <mcimadamore at openjdk.org> wrote:

>> Eliminate needless casts and ensure invoked method is looked up against the appropriate receiver type
>
> src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Lower.java line 3521:
> 
>> 3519:             if (iterableType.getTypeArguments().nonEmpty())
>> 3520:                 iteratorTarget = types.erasure(iterableType.getTypeArguments().head);
>> 3521:             tree.expr.type = types.erasure(types.skipTypeVars(tree.expr.type, false));
> 
> Shouldn't types already be erased by now (Lower runs after TransTypes?)

Perhaps (actually very likely). This transformation is just simplifying existing code.

> src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Lower.java line 3523:
> 
>> 3521:             tree.expr.type = types.erasure(types.skipTypeVars(tree.expr.type, false));
>> 3522:             if (types.asSuper(tree.expr.type, syms.iterableType.tsym) == null)
>> 3523:                 tree.expr = make.TypeCast(types.erasure(iterableType), tree.expr);
> 
> Can we use the `coerce` function here, which is used to conditionally add a cast if required?

Sure, I will check out that suggestion, Thanks

-------------

PR: https://git.openjdk.org/jdk/pull/10710


More information about the compiler-dev mailing list