[
https://issues.apache.org/jira/browse/GROOVY-12754?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=18146034#comment-18146034
]
ASF GitHub Bot commented on GROOVY-12754:
-----------------------------------------
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.
> 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)