blackdrag commented on code in PR #3381:
URL: https://github.com/apache/groovy/pull/3381#discussion_r4766295734
##########
src/test/groovy/bugs/Groovy12469.groovy:
##########
@@ -41,7 +41,7 @@ final class Groovy12469 {
@CompileStatic
<T extends Annotation> T getRetention(Class<T> clazz) {
- Target.getAnnotation(Target)
+ (T) Target.getAnnotation(Target)
Review Comment:
why is this cast needed now?
##########
src/test/groovy/bugs/RegressionTest.groovy:
##########
@@ -5554,97 +5554,96 @@ def cfg = new ConfigSlurper().parse("""
}
// GROOVY-12775: STC: the result of collect is not checked against a
target's Class<? extends X> type argument
- @NotYetImplemented @Test
+ @Test
void testGroovy12775() {
def err = shouldFail '''
@groovy.transform.TypeChecked
- def test() {
+ class C {
+ static class IntList extends ArrayList<Integer> { }
def <T> void classes(List<Object> items) {
List<Class<? extends T>> list = items.collect {
it.getClass() }
}
- class IntList extends ArrayList<Integer> { }
- List<Class<? extends List<String>>> list = [IntList].collect {
it }
+ void test() {
+ List<Class<? extends List<String>>> list =
[IntList].collect { it }
+ }
}
'''
Review Comment:
I think you changed the nature of this test too much. If you want to test it
with an explicit inner class structure feel free to do so in parallel, but
please keep the class in method variant as well.
##########
src/test/groovy/groovy/transform/stc/DelegatesToSTCTest.groovy:
##########
@@ -800,7 +800,7 @@ class DelegatesToSTCTest extends StaticTypeCheckingTestCase
{
}
def <T,U extends Configurable<T>> U configure(Class<U> clazz,
@DelegatesTo(type='T') Closure spec) {
- Configurable<T> obj = (Configurable<T>) clazz.newInstance()
+ U obj = clazz.newInstance()
Review Comment:
Did you make this change because it is no longer needed to do the cast or
because the old variant will not compile anymore?
##########
src/test/groovy/bugs/RegressionTest.groovy:
##########
@@ -5672,9 +5670,37 @@ def cfg = new ConfigSlurper().parse("""
Function<Object, String> f = C::pick
f.apply('x')
}
- assert cs() == 'String'
+ @groovy.transform.TypeChecked
+ def tc() {
+ Function<Object, String> f = C::pick
+ f.apply('x')
+ }
+ def dyn() {
+ Function<Object, String> f = C::pick
+ f.apply('x')
+ }
+ // @CompileStatic selects by the SAM's declared parameter types
(like Java);
+ // @TypeChecked preserves dynamic Groovy dispatch by runtime
argument types
+ assert cs() == 'Object'
Review Comment:
you should add a variant with Function<String,String> here, because that
should return 'String' under static compilation if I am not wrong
##########
src/test/groovy/gls/innerClass/InnerClassTest.groovy:
##########
@@ -3552,6 +3552,115 @@ final class InnerClassTest {
assert box.m() == 'pmpm'
'''
}
+
+ // GROOVY-12771
+ @Test
+ void testInheritedMemberClassResolvedInNestedClass() {
Review Comment:
the fixes for generics and this fix for the resolving a class are very
different pairs of shoes. They should not be mixed in the same PR, that is
already too big.
##########
src/test/groovy/groovy/transform/stc/STCnAryExpressionTest.groovy:
##########
@@ -190,9 +190,9 @@ class STCnAryExpressionTest extends
StaticTypeCheckingTestCase {
void testSpaceshipOperatorWithPrimitiveRightOperand() {
assertScript '''
// relational operators bind tighter than <=>: "a <=> b > 0" is "a
<=> (b > 0)"
- static <T extends Comparable<T>> int typeParameterAndBoolean(T a,
int b) { a <=> b > 0 }
- static <T extends Comparable<T>> int inClosure(T a, int b) {
Closure<Integer> c = { a <=> b > 0 }; c() }
- static <T extends Comparable<T>> int typeParameterAndInt(T a) { a
<=> 1 }
+ static <T extends Comparable<Boolean>> int
typeParameterAndBoolean(T a, int b) { a <=> b > 0 }
+ static <T extends Comparable<Boolean>> int inClosure(T a, int b) {
Closure<Integer> c = { a <=> b > 0 }; c() }
+ static <T extends Comparable<Integer>> int typeParameterAndInt(T
a) { a <=> 1 }
Review Comment:
I would have expected `Comparable<Integer>` in all cases. Why is
`Comparable<Boolean>` valid? We do not compare to a boolean, we compare to an
int.
##########
src/test/groovy/org/codehaus/groovy/classgen/asm/sc/bugs/Groovy7298Bug.groovy:
##########
@@ -33,6 +33,7 @@ final class Groovy7298Bug extends StaticTypeCheckingTestCase
implements StaticCo
[1].each {
return executeLogicForAvailablePort(closure)
}
+ return null
Review Comment:
why was this added?
##########
src/test/groovy/groovy/transform/stc/MethodReferenceTest.groovy:
##########
@@ -889,6 +889,162 @@ final class MethodReferenceTest {
assert err.message =~ /Possible loss of precision from long to int/
}
+ // GROOVY-12781
+ @Test
+ void testOverloadedMethodReferenceStaticVsDynamicDispatchAndSoundness() {
+ assertScript shell, '''
+ class C {
+ static String pick(String s) { 'String' }
+ static String pick(Object o) { 'Object' }
+ static Integer narrow(String s) { 1 }
+ static List narrow(Object o) { ['Object'] }
+ Integer inst(String s) { 1 }
+ List inst(Object o) { ['Object'] }
+ }
+ class Base {
+ Object value() { 'base' }
+ }
+ class Sub extends Base {
+ @Override
+ String value() { 'sub' }
+ }
+ def <T, R> Function<T, R> toFunction(Function<T, R> fn) { fn }
+
+ @CompileStatic
+ void checkStatic() {
+ Function<Object, String> f = C::pick
+ assert f.apply('x') == 'Object'
+ Function<Object, List> g = C::narrow
+ assert g.apply('x') == ['Object']
+ Function<Object, List> h = toFunction(C::narrow)
+ assert h.apply('x') == ['Object']
+ BiFunction<C, Object, List> inst = C::inst
+ assert inst.apply(new C(), 'x') == ['Object']
+ Supplier<String> sup = new Sub().&value
+ assert sup.get() == 'sub'
+ }
+
+ @TypeChecked
+ void checkTypeChecked() {
+ Function<Object, String> f = C::pick
+ assert f.apply('x') == 'String'
+ Function<String, Integer> exact = C::narrow
+ assert exact.apply('x') == 1
+ Function<Object, Object> common = C::narrow
+ assert common.apply('x') == 1
+ assert common.apply(42) == ['Object']
+ Supplier<String> sup = new Sub().&value
+ assert sup.get() == 'sub'
+ }
+
+ checkStatic()
+ checkTypeChecked()
+ '''
+
+ for (code in [
+ '@TypeChecked void m() { Function<Object, List> f = C::narrow }',
+ '@TypeChecked void m() { Function<Object, List> f = C.&narrow }',
+ '@CompileStatic void m() { Function<Object, List> f = C.&narrow }',
+ '@TypeChecked void m() { BiFunction<C, Object, List> f = C::inst
}',
+ '@TypeChecked void m() { BiFunction<C, Object, List> f = C.&inst
}',
+ '@CompileStatic void m() { BiFunction<C, Object, List> f = C.&inst
}',
+ '@TypeChecked void m() { Function<Number, Integer> f = Math.&abs
}',
+ '@CompileStatic void m() { Function<Number, Integer> f = Math.&abs
}'
+ ]) {
+ def err = shouldFail shell, """
+ class C {
+ static Integer narrow(String s) { 1 }
+ static List narrow(Object o) { [] }
+ Integer inst(String s) { 1 }
+ List inst(Object o) { [] }
+ }
+ $code
+ """
+ assert err.message.contains('Invalid return type')
+ }
+
+ def err = shouldFail shell, '''
+ class C {
+ static Integer narrow(String s) { 1 }
+ static List narrow(Object o) { [] }
+ }
+ def <T, R> Function<T, R> toFunction(Function<T, R> fn) { fn }
+ @TypeChecked
+ void m() {
+ Function<Object, List> f = toFunction(C::narrow)
+ }
+ '''
+ assert err.message.contains('Incompatible generic argument types')
+ }
+
+ @Test
+ void testDynamicMethodPointerAndReferenceEmptySelectableSetAndOpenCall() {
+ // Dynamic dispatch (.& and @TypeChecked ::) must reject when no
overload matches the SAM parameters
+ for (code in [
+ '@CompileStatic void m(C c) { Function<Date, String> f =
c.&takeString }',
+ '@TypeChecked void m(C c) { Function<Date, String> f =
c.&takeString }',
+ '@CompileStatic void m() { Function<Date, String> f =
C.&staticTakeString }',
+ '@TypeChecked void m() { Function<Date, String> f =
C::staticTakeString }',
+ '@CompileStatic void m() { Supplier<String> s =
C.&staticTakeString }',
+ '@TypeChecked void m() { Supplier<String> s = C::staticTakeString
}',
+ ]) {
+ def err = shouldFail shell, """
+ class C {
+ String takeString(String s) { s }
+ static String staticTakeString(String s) { s }
+ }
+ $code
+ """
+ assert err.message.contains('Cannot find matching method')
+ }
+
+ // Open generic call wrapping dynamic method pointer or @TypeChecked
method reference,
+ // plus explicit type arguments and concrete SAM parameter types (not
open)
+ for (code in [
+ '@CompileStatic void m() { Function<Object, List> f =
toFunction(C.&narrow) }',
+ '@TypeChecked void m() { Function<Object, List> f =
toFunction(C.&narrow) }',
Review Comment:
Aren`t those two supposed to work?
##########
src/test/groovy/groovy/transform/stc/DelegatesToSTCTest.groovy:
##########
@@ -663,7 +663,7 @@ class DelegatesToSTCTest extends StaticTypeCheckingTestCase
{
@groovy.transform.TupleConstructor
class Person { String name }
- def <T> List<T> names(
+ def <T> List<Object> names(
@DelegatesTo.Target List<T> list,
@DelegatesTo(genericTypeIndex = 0) Closure cl) {
list.collect {
Review Comment:
This does not look like it should compile with <Object>. And I do not mean
the method calls. I mean Closure<R> delegating to List<T>, with T being
specified and R not. cl() returns Object, but why is that allowed for collect
on a List<T>? The could would make more sense to me if it stays List<T> like
before and instead it must be Closure<T>. If you then say that this would make
the method calls work, that are in this test (which I did not look at), then in
my opinion this is either the wrong message to give or it misses a message. Or
I understand generics wrongly here, which is also entirely possible, but then
please explain
--
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]