paulk-asert commented on code in PR #3293:
URL: https://github.com/apache/groovy/pull/3293#discussion_r4595579019


##########
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:
   Good catch, thanks. The check was meant for `Outer$Inner`, which names the 
raw type as `Outer.Inner` does, but it took any `$` in the name that way.
   
   A top-level `I$O<T>` was fine, as the name of an inner class of it has no 
`$`. An inner class with a `$` in its own name was not: for `class In$ner` in 
`Outer<T>`, `List<In$ner>` stayed raw, so `List<Outer.In$ner> raw = inner` 
compiled where the same code with `Inner` is an error.
   
   Fixed in c5627befab: a name with a `$` is the raw type only if it is not the 
simple name of the class. There are tests for `In$ner` and for `Outer$Inner` 
staying raw.
   



##########
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:
   You can, but you were right that it was misread: an inner class with that 
annotation was taken for a record, so it was treated as static and its simple 
name stayed raw.
   
   Fixed in c5627befab: the name must now be `RecordType` or `RecordBase`, 
alone or in `groovy.transform`. There is a test with a look-alike annotation, 
and one for a real record staying static.
   
   The check is by name because nothing else marks a record at that point. A 
nested record is not static until `RecordTypeASTTransformation` has run, which 
is after names are resolved, and a hand-written `@RecordType` is not yet 
resolved then, so it has only its simple name. That leaves one case: your own 
annotation named exactly `RecordType`, in another package, would still be taken 
for a record. The effect is only that the simple name stays the raw type, as it 
was before this PR.
   
   That is the best that can be done here without tracking records earlier, for 
instance by marking the class when the `record` keyword or the annotation is 
first seen. That would be a separate change.
   



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to