You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@groovy.apache.org by pa...@apache.org on 2026/08/04 00:44:24 UTC
(groovy) branch master updated: GROOVY-12230: STC: String/GString conditional not accepted where String is expected
This is an automated email from the ASF dual-hosted git repository.
paulk-asert pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/groovy.git
The following commit(s) were added to refs/heads/master by this push:
new 9e72f5fe8b GROOVY-12230: STC: String/GString conditional not accepted where String is expected
9e72f5fe8b is described below
commit 9e72f5fe8b74e66740fb211e0b17d93a71243559
Author: Paul King <pa...@asert.com.au>
AuthorDate: Tue Aug 4 07:47:39 2026 +1000
GROOVY-12230: STC: String/GString conditional not accepted where String is expected
---
.../transform/stc/StaticTypeCheckingSupport.java | 4 +++-
.../groovy/transform/stc/MethodCallsSTCTest.groovy | 27 ++++++++++++++++++++++
2 files changed, 30 insertions(+), 1 deletion(-)
diff --git a/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingSupport.java b/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingSupport.java
index 207c2c3cd1..f054b72556 100644
--- a/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingSupport.java
+++ b/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingSupport.java
@@ -478,7 +478,9 @@ public abstract class StaticTypeCheckingSupport {
return isPrimitiveType(targetComponent) ? sourceComponent.equals(targetComponent) // GROOVY-11053: strict
: !isPrimitiveType(sourceComponent) && isAssignableTo(sourceComponent, targetComponent); // GROOVY-10720
}
- if (type.isDerivedFrom(GSTRING_TYPE) && isStringType(toBeAssignedTo)) {
+ // GROOVY-12230: the String/GString LUB arises for conditionals like "flag ? gstring : string"
+ // and converts to String just as a GString does; see also #checkCompatibleAssignmentTypes
+ if ((type.isDerivedFrom(GSTRING_TYPE) || isGStringOrGStringStringLUB(type)) && isStringType(toBeAssignedTo)) {
return true;
}
if (isStringType(type) && toBeAssignedTo.isDerivedFrom(GSTRING_TYPE)) {
diff --git a/src/test/groovy/groovy/transform/stc/MethodCallsSTCTest.groovy b/src/test/groovy/groovy/transform/stc/MethodCallsSTCTest.groovy
index 4ef385cb47..c382743f4a 100644
--- a/src/test/groovy/groovy/transform/stc/MethodCallsSTCTest.groovy
+++ b/src/test/groovy/groovy/transform/stc/MethodCallsSTCTest.groovy
@@ -2229,6 +2229,33 @@ import static groovy.test.GroovyAssert.shouldFail
'''
}
+ // GROOVY-12230
+ @Test
+ void testStringOrGStringArgumentForStringParameter() {
+ assertScript '''
+ Class m(String s) { s.class }
+
+ boolean flag = true
+ assert m(flag ? "a${1}" : 'b') === String
+ assert m(flag ? 'b' : "a${1}") === String
+ assert m(flag ? "a${1}" : null) === String
+ '''
+ }
+
+ // GROOVY-12230
+ @Test
+ void testStringOrGStringArgumentForStringParameter2() {
+ assertScript '''
+ class StringHolder {
+ String p
+ StringHolder(String s) { p = s }
+ }
+
+ boolean flag = false
+ assert new StringHolder(flag ? "a${1}" : 'b').p.class === String
+ '''
+ }
+
//--------------------------------------------------------------------------
static class MyMethodCallTestClass {