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

ASF GitHub Bot commented on GROOVY-12754:
-----------------------------------------

blackdrag commented on code in PR #3293:
URL: https://github.com/apache/groovy/pull/3293#discussion_r4594248823


##########
src/main/java/org/codehaus/groovy/control/ResolveVisitor.java:
##########
@@ -474,6 +483,102 @@ private void resolveOrFail(final ClassNode type, final 
String msg, final ASTNode
         addError("unable to resolve class " + type.toString(false) + msg, 
node);
     }
 
+    /**
+     * JLS 6.5.5.1, 8.1.3, 8.5: within {@code Outer<T>}, the simple name 
{@code Inner}
+     * of an inner class is the type {@code Outer<T>.Inner}, and within a 
subclass that
+     * extends {@code Outer<String>} it is {@code Outer<String>.Inner}. 
Neither is the
+     * raw type {@code Outer.Inner}. Records the enclosing type for such a 
name. A name
+     * used where the type variables are out of scope, as in a static method, 
stays raw.
+     */
+    private void setOuterClassTypeOfSimpleName(ClassNode type) {
+        while (type.isArray()) type = type.getComponentType();
+        if (!type.isRedirectNode() || type.isGenericsPlaceHolder() || 
type.getOuterClassType() != null) return;
+        String name = type.getUnresolvedName();
+        if (name.indexOf('.') != -1 || name.indexOf('$') != -1) return; // 
Outer.Inner is the raw type

Review Comment:
   are you aware that `I$O` is a legal class name, even for a top level class?



##########
src/main/java/org/codehaus/groovy/control/ResolveVisitor.java:
##########
@@ -474,6 +483,102 @@ private void resolveOrFail(final ClassNode type, final 
String msg, final ASTNode
         addError("unable to resolve class " + type.toString(false) + msg, 
node);
     }
 
+    /**
+     * JLS 6.5.5.1, 8.1.3, 8.5: within {@code Outer<T>}, the simple name 
{@code Inner}
+     * of an inner class is the type {@code Outer<T>.Inner}, and within a 
subclass that
+     * extends {@code Outer<String>} it is {@code Outer<String>.Inner}. 
Neither is the
+     * raw type {@code Outer.Inner}. Records the enclosing type for such a 
name. A name
+     * used where the type variables are out of scope, as in a static method, 
stays raw.
+     */
+    private void setOuterClassTypeOfSimpleName(ClassNode type) {
+        while (type.isArray()) type = type.getComponentType();
+        if (!type.isRedirectNode() || type.isGenericsPlaceHolder() || 
type.getOuterClassType() != null) return;
+        String name = type.getUnresolvedName();
+        if (name.indexOf('.') != -1 || name.indexOf('$') != -1) return; // 
Outer.Inner is the raw type
+        ClassNode inner = type.redirect(), outer = inner.getOuterClass();
+        if (outer == null || isStaticMember(inner)) return;
+        if (type.getGenericsTypes() == null && inner.getGenericsTypes() != 
null) return; // raw type
+
+        for (ClassNode cn = currentClass; cn != null; cn = cn.getOuterClass()) 
{
+            boolean declares = cn.equals(outer);
+            if (declares || inherits(cn, outer)) {
+                ClassNode outerType = typeOfThis(cn);
+                if (outerType != null && !declares) {
+                    outerType = GenericsUtils.parameterizeType(outerType, 
outer);
+                }
+                if (outerType != null && (outerType.getOuterClassType() != null
+                        || (outerType.isRedirectNode() && 
outerType.getGenericsTypes() != null))) {
+                    type.setOuterClassType(outerType);
+                }
+                return;
+            }
+            if (isRecord(cn)) return; // the type variables of its enclosing 
classes are out of scope
+        }
+    }
+
+    private static boolean isStaticMember(final ClassNode cn) {
+        return isStaticMemberType(cn) || isRecord(cn);
+    }
+
+    /**
+     * A nested record is implicitly static (JLS 8.10), which its declaration 
does not
+     * show until {@code RecordTypeASTTransformation} has run.
+     */
+    private static boolean isRecord(final ClassNode cn) {
+        for (AnnotationNode annotation : cn.getAnnotations()) {
+            String name = annotation.getClassNode().getName();
+            if (name.endsWith("RecordType") || name.endsWith("RecordBase")) 
return true;

Review Comment:
   So I cannot have "@ARecordType class Foo{}"?





> STC: an upper-bounded wildcard argument is checked against a type parameter's 
> bound the wrong way round
> -------------------------------------------------------------------------------------------------------
>
>                 Key: GROOVY-12754
>                 URL: https://issues.apache.org/jira/browse/GROOVY-12754
>             Project: Groovy
>          Issue Type: Bug
>          Components: Static Type Checker
>    Affects Versions: 6.0.0
>            Reporter: Scott Murphy Heiberg
>            Priority: Major
>
> Static type checking rejects a call that passes an upper-bounded wildcard, 
> such as {{List<? extends Integer>}}, to a generic method whose type parameter 
> is bounded by a supertype of the wildcard's bound. It accepts the call when 
> the wildcard's bound is wider than the type parameter's. javac does the 
> opposite in both cases.
> {code:groovy}
> @groovy.transform.CompileStatic
> class C {
>     static <T extends Number> T first(List<T> list) { list.get(0) }
>     static <T extends CharSequence> int len(Class<T> type) { 0 }
>     static <T extends Integer> T narrow(List<T> list) { list.get(0) }
>     static Object a(List<? extends Integer> ints) { first(ints) }  // 
> rejected, javac accepts
>     static int b(Class<? extends String> type) { len(type) }       // 
> rejected, javac accepts
>     static Object c(List<? extends Number> nums) { narrow(nums) }  // 
> accepted, javac rejects
> }
> {code}
> {noformat}
> [Static type checking] - Cannot call <T extends java.lang.Number> 
> C#first(java.util.List<T>) with arguments [java.util.List<? extends 
> java.lang.Integer>]
> [Static type checking] - Cannot call <T extends java.lang.CharSequence> 
> C#len(java.lang.Class<T>) with arguments [java.lang.Class<? extends 
> java.lang.String>]
> {noformat}
> The capture of {{? extends B}} is a subtype of {{B}} (JLS 5.1.10), so it is 
> within the bound of {{T}} exactly when {{B}} is. javac reports "inference 
> variable T has incompatible bounds" for {{c}}. Groovy compiles {{c}}, and 
> {{c([1.5d])}} returns {{1}}.
> {{StaticTypeCheckingSupport#compatibleConnection}} tests a wildcard witness 
> with {{connection.isCompatibleWith(bound)}}, which asks whether the type 
> parameter's bound fits within {{? extends B}}. For a type argument {{B}}, it 
> tests {{? super B}} against the bound. An upper-bounded wildcard should take 
> the same test.
> PR: https://github.com/apache/groovy/pull/3285



--
This message was sent by Atlassian Jira
(v10.3.26#10030026)

Reply via email to