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

STC inferred type lost in nested closure

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Major
    • Resolution: Fixed
    • 4.0.3
    • 2.5.18, 3.0.12, 4.0.4
    • None
    • None

    Description

      Groovy 4.0.3 fails to compile this code:
       

      @Grab('org.mockito:mockito-core:4.5.1')
      @Grab('org.junit.jupiter:junit-jupiter-api:5.8.2')
      
      import groovy.transform.CompileStatic
      import org.junit.jupiter.api.Test
      import org.mockito.Mockito
      
      import static org.mockito.ArgumentMatchers.anyString
      import static org.mockito.Mockito.when
      
      @CompileStatic
      class MockitoAnswerTest {
      
        @Test
        void stubbing() {
          def a = Mockito.mock(A)
      
          when(a.f(anyString())).thenAnswer { i ->
            new B().tap {
              b = 'Hallo' + i.arguments[0]
            }
          }
      
          assert a.f('x') != null
        }
      
      
        static class B {
          String b
        }
      
        interface A {
          B f(String x)
        }
      }
      

      It produces an error message like the following:

      org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
      /home/jh/Code/groovy-bugs/src/test/groovy/de/jhunovis/fluentapi/MockitoAnswerTest.groovy: 20: [Static type checking] - No such property: arguments for class: java.lang.Object
       @ line 20, column 24.
                 b = 'Hallo' + i.arguments[0]
                                ^
      
      /home/jh/Code/groovy-bugs/src/test/groovy/de/jhunovis/fluentapi/MockitoAnswerTest.groovy: 20: [Static type checking] - Cannot find matching method java.lang.Object#getAt(int). Please check if the declared type is correct and if the method exists.
       @ line 20, column 23.
                 b = 'Hallo' + i.arguments[0]
                               ^
      
      2 errors
      

       
      Explicitly typing i as InvocationMock makes the code compile.

      Attachments

        Activity

          People

            emilles Eric Milles
            jhunovis Jan Hackel
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: