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

in CompileStatic mode, "a[i] = a[j]" won't work

    XMLWordPrintableJSON

Details

    Description

      import groovy.transform.CompileStatic
      import org.junit.Test
      
      import static groovy.util.GroovyTestCase.assertEquals
      
      
      class TestGroovyListBug {
      
          @CompileStatic
          def swapUseStatic(List<Integer> a, int i, int j) {
              int temp = a[i]
              a[i] = a[j]  //BUG HERE, a[i] = ... doesn't call "putAt"
              a[j] = temp
          }
      
          @CompileStatic
          def swapUseStaticPut(List<Integer> a, int i, int j) {
              int temp = a[i]
              a.putAt(i, a[j])
              a[j] = temp
          }
      
          def swapNonStatic(a, i, j) {
              def temp = a[i]
              a[i] = a[j]
              a[j] = temp
          }
      
          @Test
          def void testCompileStaticListPut() {
              List a = new ArrayList()
      
              a.clear(); a.add(0); a.add(1);
              swapNonStatic(a, 0, 1)
              assertEquals(1, a[0])
              assertEquals(0, a[1])
      
              a.clear(); a.add(0); a.add(1);
              swapUseStaticPut(a, 0, 1)
              assertEquals(1, a[0])
              assertEquals(0, a[1])
      
              a.clear(); a.add(0); a.add(1);
              swapUseStatic(a, 0, 1)
              assertEquals(1, a[0])
              assertEquals(0, a[1])
          }
      }
      

      Attachments

        Activity

          People

            melix Cédric Champeau
            chice Xiaoguang WANG
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: