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

Generics failed to compile with Groovy 3.0.9 when overriding plus operator

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Major
    • Resolution: Fixed
    • 3.0.9
    • 4.0.0-beta-1, 3.0.10
    • Compiler
    • $ uname -a
      Linux auo4ever 5.14.13-desktop-1.mga9 #1 SMP Sun Oct 17 09:53:03 UTC 2021 x86_64 x86_64 x86_64 GNU/Linux
      $ java -fullversion
      openjdk full version "11.0.6+9-jvmci-19.3-b07"

    Description

      When compiling with Groovy 3.0.9 the following test-case, I get an error:

      $ ./gradlew assemble
      [ . . . ]
      startup failed:
      .../generics-testcase/src/main/groovy/test/GenericsTestCase.groovy: 37: [Static type checking] - Cannot call LC#plus(T) with arguments [LC] 
       @ line 37, column 22.
                         else lineContent += it
                              ^
      
      1 error
      [ . . . ]

      No errors with previous Groovy compiler.

      package test
      
      import groovy.transform.CompileStatic
      
      @CompileStatic
      class GenericsTestCase {
          interface ILineContent<T extends ILineContent> {
              T plus(T lineContent)
          }
      
          @CompileStatic
          private static class LineContent implements ILineContent<LineContent> {
              final BigDecimal sales
      
              LineContent(final BigDecimal commission) {
                  this.sales = sales
              }
      
              LineContent() {
                  this.sales = 0.0
              }
      
              @Override
              LineContent plus(LineContent lineContent) {
                  if (!lineContent) return this
                  else new LineContent((sales ?: 0.0) + (lineContent.sales ?: 0.0))
              }
          }
      
          private static class ClassThatCauseCompilationError<LC extends ILineContent> {
              List<LC> someLines
      
              LC getTotal() {
                  LC lineContent = null
                  for (LC it in someLines) {
                      if (!lineContent) lineContent = it
                      else lineContent += it
                  }
                  lineContent as LC
              }
          }
      }
      

       

      I attached to this report the Gradle test-case to switch quickly between Groovy compiler releases.

       

      Thanks for your support,
      Adrien

      Attachments

        1. generics-testcase.tar.gz
          57 kB
          Adrien Guichard

        Activity

          People

            emilles Eric Milles
            AdrienGuichard Adrien Guichard
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: