You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@groovy.apache.org by su...@apache.org on 2020/06/13 13:40:21 UTC
[groovy] 02/06: GROOVY-7549: STC: if "=" RHS type is inaccessible,
revert to origin type
This is an automated email from the ASF dual-hosted git repository.
sunlan pushed a commit to branch GROOVY_3_0_X
in repository https://gitbox.apache.org/repos/asf/groovy.git
commit 95f6a8941c68cecab3f79d31a741e6f6a2d24ebb
Author: Eric Milles <er...@thomsonreuters.com>
AuthorDate: Wed Jun 10 12:22:45 2020 -0500
GROOVY-7549: STC: if "=" RHS type is inaccessible, revert to origin type
(cherry picked from commit e919cefe14bbf1da26d024a851180f40a7f374a3)
---
.../transform/stc/StaticTypeCheckingVisitor.java | 9 ++++
.../transform/stc/TypeInferenceSTCTest.groovy | 59 ++++++++++++++++++++++
2 files changed, 68 insertions(+)
diff --git a/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java b/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java
index 7945eaf..da55cbf 100644
--- a/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java
+++ b/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java
@@ -843,6 +843,15 @@ public class StaticTypeCheckingVisitor extends ClassCodeVisitorSupport {
} else if (lType.isUsingGenerics() && !lType.isEnum() && hasRHSIncompleteGenericTypeInfo(resultType)) {
// for example, LHS is List<ConcreteClass> and RHS is List<T> where T is a placeholder
resultType = lType;
+ } else {
+ // GROOVY-7549: RHS type may not be accessible to enclosing class
+ int modifiers = resultType.getModifiers();
+ ClassNode enclosingType = typeCheckingContext.getEnclosingClassNode();
+ if (!Modifier.isPublic(modifiers) && !enclosingType.equals(resultType)
+ && !getOutermost(enclosingType).equals(getOutermost(resultType))
+ && (Modifier.isPrivate(modifiers) || !Objects.equals(enclosingType.getPackageName(), resultType.getPackageName()))) {
+ resultType = originType; // TODO: Find accesible type in hierarchy of resultType?
+ }
}
// make sure we keep primitive types
diff --git a/src/test/groovy/transform/stc/TypeInferenceSTCTest.groovy b/src/test/groovy/transform/stc/TypeInferenceSTCTest.groovy
index 93fadb4..dcee926 100644
--- a/src/test/groovy/transform/stc/TypeInferenceSTCTest.groovy
+++ b/src/test/groovy/transform/stc/TypeInferenceSTCTest.groovy
@@ -23,6 +23,7 @@ import org.codehaus.groovy.ast.ClassNode
import org.codehaus.groovy.ast.MethodNode
import org.codehaus.groovy.ast.tools.WideningCategories
import org.codehaus.groovy.classgen.GeneratorContext
+import org.codehaus.groovy.control.CompilationUnit
import org.codehaus.groovy.control.CompilePhase
import org.codehaus.groovy.control.SourceUnit
import org.codehaus.groovy.control.customizers.CompilationCustomizer
@@ -930,6 +931,64 @@ class TypeInferenceSTCTest extends StaticTypeCheckingTestCase {
'''
}
+ // GROOVY-7549
+ void testShouldKeepDeclTypeWhenAssignedInaccessibleT() {
+ config.targetDirectory = File.createTempDir()
+ def parentDir = File.createTempDir()
+ try {
+ new File(parentDir, 'a').mkdir()
+ new File(parentDir, 'b').mkdir()
+
+ def a = new File(parentDir, 'a/Main.groovy')
+ a.write '''
+ package a
+ class Main {
+ static main(args) {
+ Face f = b.Maker.make() // returns b.Impl
+ assert f.meth() == 1234
+ }
+ }
+ '''
+ def b = new File(parentDir, 'a/Face.groovy')
+ b.write '''
+ package a
+ interface Face {
+ int meth()
+ }
+ '''
+ def c = new File(parentDir, 'b/Impl.groovy')
+ c.write '''
+ package b
+ @groovy.transform.PackageScope
+ class Impl implements a.Face {
+ int meth() {
+ 1234
+ }
+ }
+ '''
+ def d = new File(parentDir, 'b/Maker.groovy')
+ d.write '''
+ package b
+ class Maker {
+ static Impl make() { // probably should return a.Face
+ new Impl()
+ }
+ }
+ '''
+
+ def loader = new GroovyClassLoader(this.class.classLoader)
+ def cu = new CompilationUnit(config, null, loader)
+ cu.addSources(a, b, c, d)
+ cu.compile()
+
+ loader.addClasspath(config.targetDirectory.absolutePath)
+ loader.loadClass('a.Main', true).main()
+ } finally {
+ config.targetDirectory.deleteDir()
+ parentDir.deleteDir()
+ }
+ }
+
// GROOVY-9077
void testInferredTypeForPropertyThatResolvesToMethod() {
assertScript '''