[ https://issues.apache.org/jira/browse/GROOVY-9074?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17897920#comment-17897920 ]
Eric Milles edited comment on GROOVY-9074 at 11/18/24 9:05 PM: --------------------------------------------------------------- https://github.com/apache/groovy/commit/71490fbbc2dc63cdaa70b2f61b77f7f5f94b82af https://github.com/apache/groovy/commit/9435d9613e912d018efe567fd4f9e600f55b69a0 was (Author: emilles): https://github.com/apache/groovy/commit/71490fbbc2dc63cdaa70b2f61b77f7f5f94b82af > CompileStatic does not throw expected exception > ----------------------------------------------- > > Key: GROOVY-9074 > URL: https://issues.apache.org/jira/browse/GROOVY-9074 > Project: Groovy > Issue Type: Bug > Components: Static Type Checker > Affects Versions: 2.5.5 > Reporter: Robert Stagner > Assignee: Eric Milles > Priority: Major > Labels: breaking > Fix For: 5.0.0-alpha-12 > > Time Spent: 50m > Remaining Estimate: 0h > > While reading about Java Generics and wildcards (see > [https://docs.oracle.com/javase/tutorial/extra/generics/wildcards.html]), I > came across the following code in the referenced oracle document > {code:java} > Collection<?> c = new ArrayList<String>(); > c.add(new Object()); // Compile time error{code} > Now, I decided to wrap this in a class and try it out in Java and then in > Groovy. > Here is the Java code > > {code:java} > import java.util.*; > public class TestCollection { > static Collection<?> c = new ArrayList<String>(); > public static void main(String[] args) { > c.add(new Object()); // Compile time error > } > } > {code} > > > and here is the groovy code > > {code:java} > import java.util.*; > import groovy.transform.CompileStatic; > @CompileStatic > public class TestCollection { > static Collection<?> c = new ArrayList<String>(); > public static void main(String[] args) { > c.add(new Object()); // Compile time error > println c.first(); > } > } > {code} > > When I attempt to compile the Java code I get the following expected error > {code:java} > $ javac TestCollection.java > TestCollection.java:7: error: incompatible types: Object cannot be converted > to CAP#1 > c.add(new Object()); // Compile time error > ^ > where CAP#1 is a fresh type-variable: > CAP#1 extends Object from capture of ?{code} > But, when I run the code through Groovy, I do not get a compile-time error. > Instead, I get the following output > {code:java} > java.lang.Object@41a0aa7d{code} -- This message was sent by Atlassian Jira (v8.20.10#820010)