Uploaded image for project: 'Groovy'
  1. Groovy
  2. GROOVY-10114

Wrong type argument inference when using the result of ternary operator directly.

    XMLWordPrintableJSON

    Details

    • Type: Bug
    • Status: Open
    • Priority: Major
    • Resolution: Unresolved
    • Affects Version/s: 4.0.0-alpha-2, 3.0.8
    • Fix Version/s: None
    • Labels:
      None

      Description

      I have the following Groovy program.

      @groovy.transform.TypeChecked
      class Test {
        public static void main(String[] args) {
          def x = ((false) ? new B<>(new A()) : new B<>(new A()))
          x.bar.foo()
          ((false) ? new B<A>(new A()) : new B<>(new A())).bar.foo()
          ((false) ? new B<>(new A()) : new B<>(new A())).bar.foo()
        }
      }
      
      final class A {
        public A() {
        }
      
        final Character foo() {
          (Character) 'g'
        }
      }
      
      class B<V> {
        public V bar
      
        public B(V bar) {
          this.bar = bar
        }
      }
      
      

      Actual Behavior

      The program does not compile, and I get the following error.

      org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
      Test.groovy: 7: [Static type checking] - Cannot find matching method java.lang.Object#foo(). Please check if the declared type is correct and if the method exists.
       @ line 7, column 5.
             ((false) ? new B<>(new A()) : new B<>(new A())).bar.foo()
             ^
      
      1 error
      
      

      Expected Behavior

      Compile successfully.

      Comment

      This bug seems similar to GROOVY-9983, but it doesn't use subtyping. Moreover, in versions 3.0.8 and 4.0.0-alpha-2, the compiler produces the following error (in addition to the previous error message).

      org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
      Test.groovy: 5: [Static type checking] - Cannot find matching method java.lang.Object#foo(). Please check if the declared type is correct and if the method exists.
       @ line 5, column 5.
             x.bar.foo()
             ^
      

      Affected Version

      This programs have been tested with the compiler from the master (commit: c36c8bf3d24ce41e972e3f3e7a5763acf96f46b1), 4.0.0-alpha-2, and 3.0.8.

        Attachments

          Activity

            People

            • Assignee:
              Unassigned
              Reporter:
              schaliasos Stefanos Chaliasos
            • Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

              Dates

              • Created:
                Updated: