JDK-8219318 (???): inferred type does not conform to upper bound(s) when collecting to a HashMap

Maurizio Cimadamore maurizio.cimadamore at oracle.com
Thu Dec 19 15:37:28 UTC 2019


> The problem is that when inferring 'n(m())' the bounds 'X <: I(W),
> I(Integer)' implies 'W=Integer' per §18.3.1 but with 'o(m())' the
> bounds 'X <: I(W), I(? extends Integer)' should probably imply 'W <:
> Integer' instead of inferring 'W=Object'.
>
> So, I did a voodoo cult devoted to inference which ended up with some
> black magic on our good old friend 'Types::isSameType', see below.
>
> While this experiment is somewhat incomplete, both examples 'putAll()'
> and 'o(m())' are now working without any 'langtools:tier1' failure.
>
> What do you think?

This extension is, more or less, what I was expecting/suggesting - but I 
think this is more of a question for our spec experts (there might be 
subtle reasons as to why this wasn't done already).

Maurizio

> Bernard
>
> diff --git a/src/jdk.compiler/share/classes/com/sun/tools/javac/code/Types.java
> b/src/jdk.compiler/share/classes/com/sun/tools/javac/code/Types.java
> --- a/src/jdk.compiler/share/classes/com/sun/tools/javac/code/Types.java
> +++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/code/Types.java
> @@ -1376,6 +1376,10 @@
>               @Override
>               public Boolean visitWildcardType(WildcardType t, Type s) {
>                   if (!s.hasTag(WILDCARD)) {
> +                    if (!t.isUnbound() && t.isExtendsBound() &&
> s.hasTag(UNDETVAR)) {
> +                        ((UndetVar)s).addBound(InferenceBound.UPPER,
> t.type, Types.this);
> +                        return true;
> +                    }
>                       return false;
>                   } else {
>                       WildcardType t2 = (WildcardType)s;
> diff --git a/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Infer.java
> b/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Infer.java
> --- a/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Infer.java
> +++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Infer.java
> @@ -944,6 +944,10 @@
>                           List<Type> allParamsSuperBound1 =
> commonSupers.fst.allparams();
>                           List<Type> allParamsSuperBound2 =
> commonSupers.snd.allparams();
>                           while (allParamsSuperBound1.nonEmpty() &&
> allParamsSuperBound2.nonEmpty()) {
> +                            if
> (allParamsSuperBound1.head.hasTag(WILDCARD) &&
> !allParamsSuperBound2.head.hasTag(WILDCARD)) {
> +                                //try to match wildcard and undet var bounds
> +                                isSameType(allParamsSuperBound1.head,
> inferenceContext.asUndetVar(allParamsSuperBound2.head));
> +                            }
>                               //traverse the list of all params comparing them
>                               if (!allParamsSuperBound1.head.hasTag(WILDCARD) &&
>
> !allParamsSuperBound2.head.hasTag(WILDCARD)) {


More information about the compiler-dev mailing list