Incoherent invocation type inference?

Maurizio Cimadamore maurizio.cimadamore at oracle.com
Thu Jan 19 17:22:59 UTC 2017


Not sure about your proposed patch.

To me the warning should be a property of the method declaration, not of 
the specific inference.

If a method returns a naked type-variable which is not mentioned 
anywhere in the method parameter types -> Lint warning (not an unchecked 
warning - just an optional Lint one - category TBD).

It's true that, by looking at the callsite, you can also warn for 
clients passing 'null' arguments, but the extra benefit is not worth the 
extra complexity IMHO. And, I think this is a problem of bad API, not 
one of bad clients. A well-designed API should not have any methods that 
match the criteria stated above - as their behavior would ultimately be 
at the client's mercy.

Maurizio


On 18/01/17 12:50, B. Blaser wrote:
> Hi,
>
> 2017-01-16 19:33 GMT+01:00 B. Blaser <bsrbnd at gmail.com>:
>> 2017-01-16 18:03 GMT+01:00 Maurizio Cimadamore <maurizio.cimadamore at oracle.com>:
>>> On 16/01/17 16:49, B. Blaser wrote:
>>>> Intersections seem to be dangerous only in the case of a naked return
>>>> type variable with no parameter provided ("get()" or "get(null)")
>>>> because get() wouldn't know about the return type constraints. We
>>>> could, for example, update the rule as follows: "emit a lint warning
>>>> if the return type T is a naked type variable infered to an
>>>> intersection type and no constraint is given by the actual arguments"?
>>>>
>>>> Or are you suggesting to modify the rules of intersection types on the
>>>> language side?
>>> I think the Lint warning makes sense for naked return type regardless of
>>> intersections - as in the example I wrote.
>> Ok for this first rule.
> What do you think of something like the draft below
> (Infer.instantiateMethod() upon rev. b6960e2da008)?
> Thanks,
> Bernard
>
>>> Maurizio
> 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
> @@ -182,6 +182,26 @@
>           final InferenceContext inferenceContext = new
> InferenceContext(this, tvars);  //B0
>           inferenceException.clear();
>           try {
> +            if (resultInfo != null && resultInfo.pt != Type.noType &&
> mt.restype.hasTag(TYPEVAR)) {
> +                int nbConstraints = 0;
> +                if (mt.argtypes.length() == argtypes.length()) {
> +                    for (int i=0; i<mt.argtypes.length(); i++) {
> +                        Type pType = mt.argtypes.get(i);
> +                        Type aType = argtypes.get(i);
> +                        if (pType.contains(mt.restype) && aType !=
> syms.botType)
> +                            nbConstraints++;
> +                    }
> +                }
> +                if (nbConstraints == 0) {
> +                    chk.warnUnchecked(env.tree.pos(),
> +                           "unchecked.invocation.type.inference",
> +                           Kinds.kindName(msym),
> +                           msym.name,
> +                           Kinds.kindName(msym.location()),
> +                           msym.location());
> +                }
> +            }
> +
>               DeferredAttr.DeferredAttrContext deferredAttrContext =
>                           resolveContext.deferredAttrContext(msym,
> inferenceContext, resultInfo, warn);
>
> diff --git a/src/jdk.compiler/share/classes/com/sun/tools/javac/resources/compiler.properties
> b/src/jdk.compiler/share/classes/com/sun/tools/javac/resources/compiler.properties
> --- a/src/jdk.compiler/share/classes/com/sun/tools/javac/resources/compiler.properties
> +++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/resources/compiler.properties
> @@ -1748,6 +1748,10 @@
>       required: {2}\n\
>       found: {3}
>
> +# 0: symbol kind, 1: name, 2: symbol kind, 3: symbol
> +compiler.warn.unchecked.invocation.type.inference=\
> +    unchecked invocation type inference: {0} {1} in {2} {3} misses
> constraints from actual arguments
> +
>   # 0: type
>   compiler.warn.unchecked.generic.array.creation=\
>       unchecked generic array creation for varargs parameter of type {0}



More information about the compiler-dev mailing list