You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@groovy.apache.org by "Eric Milles (JIRA)" <ji...@apache.org> on 2019/04/24 17:34:00 UTC

[jira] [Comment Edited] (GROOVY-8990) No compiler error for mismatched generics when using &

    [ https://issues.apache.org/jira/browse/GROOVY-8990?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16825355#comment-16825355 ] 

Eric Milles edited comment on GROOVY-8990 at 4/24/19 5:33 PM:
--------------------------------------------------------------

This change fixes 3 things:
 1. unbounded wildcard (aka "?") is allowed to be used as a general-purpose substitute
 2. multiple upper bound interfaces are tested against {{nType}}
 3. error location is moved from "C" to "T" in {{C<T>}}
{code:java}
    private void checkGenericsUsage(ClassNode n, ClassNode cn, Boolean isAnonInnerClass) {
        ...
        for (int i = 0; i < nTypes.length; i++) {
            ClassNode nType = nTypes[i].getType();
            ClassNode cnType = cnTypes[i].getType();
            // check nested type parameters
            checkGenericsUsage(nType, nType.redirect());
            /* GRECLIPSE edit
            // check bounds
            if (!nType.isDerivedFrom(cnType)) {
                if (cnType.isInterface() && nType.implementsInterface(cnType)) continue;
                addError("The type " + nTypes[i].getName() +
                        " is not a valid substitute for the bounded parameter <" +
                        getPrintName(cnTypes[i]) + ">", n);
            }
            */
            // unbounded wildcard (aka "?") is universal substitute
            if (!(nTypes[i].isWildcard() && nTypes[i].getLowerBound() == null && nTypes[i].getUpperBounds() == null)) {
                // check lower or upper bound(s)
                ClassNode[] bounds = cnTypes[i].getUpperBounds();
                boolean firstBoundValid = nType.isDerivedFrom(cnType) || (cnType.isInterface() && nType.implementsInterface(cnType));
                if (!firstBoundValid || (bounds != null && bounds.length > 1 && !Arrays.stream(bounds).skip(1).allMatch(nType::implementsInterface))) {
                    addError("The type " + nTypes[i].getName() + " is not a valid substitute for the bounded parameter <" + getPrintName(cnTypes[i]) + ">", nTypes[i]);
                }
            }
            // GRECLIPSE end
        }
    }
{code}


was (Author: emilles):
This change fixes 3 things:
1. unbounded wildcard (aka "?") is allowed to be used as a general-purpose substitute
2. multiple upper bound interfaces are tested against {{nType}}
3. error location is moved from "C" to "T" in {{C<T>}}

{code:java}
    private void checkGenericsUsage(ClassNode n, ClassNode cn, Boolean isAnonInnerClass) {
        ...
        for (int i = 0; i < nTypes.length; i++) {
            ClassNode nType = nTypes[i].getType();
            ClassNode cnType = cnTypes[i].getType();
            // check nested type parameters
            checkGenericsUsage(nType, nType.redirect());
            /* GRECLIPSE edit
            // check bounds
            if (!nType.isDerivedFrom(cnType)) {
                if (cnType.isInterface() && nType.implementsInterface(cnType)) continue;
                addError("The type " + nTypes[i].getName() +
                        " is not a valid substitute for the bounded parameter <" +
                        getPrintName(cnTypes[i]) + ">", n);
            }
            */
            // unbounded wildcard (aka "?") is universal substitute
            if (!(nTypes[i].isWildcard() && nType.equals(ClassHelper.OBJECT_TYPE))) {
                // check lower or upper bound(s)
                ClassNode[] bounds = cnTypes[i].getUpperBounds();
                boolean firstBoundValid = nType.isDerivedFrom(cnType) || (cnType.isInterface() && nType.implementsInterface(cnType));
                if (!firstBoundValid || (bounds != null && bounds.length > 1 && !Arrays.stream(bounds).skip(1).allMatch(nType::implementsInterface))) {
                    addError("The type " + nTypes[i].getName() + " is not a valid substitute for the bounded parameter <" + getPrintName(cnTypes[i]) + ">", nType);
                }
            }
            // GRECLIPSE end
        }
    }
{code}

> No compiler error for mismatched generics when using &
> ------------------------------------------------------
>
>                 Key: GROOVY-8990
>                 URL: https://issues.apache.org/jira/browse/GROOVY-8990
>             Project: Groovy
>          Issue Type: Bug
>          Components: Compiler
>    Affects Versions: 2.4.16, 3.0.0-alpha-4, 2.5.6
>            Reporter: Eric Milles
>            Priority: Major
>
> Consider the following:
> {code:java}
> interface I {}
> class C<T extends Number & I> { T meth() {} }
> class X extends C<Integer> {} // should produce error "The type Integer is not a valid substitute for the bounded parameter <T extends java.lang.Number & I>"
> new C<Integer>() // should produce error "The type Integer is not a valid substitute for the bounded parameter <T extends java.lang.Number & I>"
> {code}
> If the type bound Number is not satisfied, an error is produced.  However, failure to satisfy the additional interface(s) does not produce a compiler error.



--
This message was sent by Atlassian JIRA
(v7.6.3#76005)