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

Compiler error for use of variable expression within tap closure

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Major
    • Resolution: Fixed
    • 3.0.4, 2.5.12
    • 4.0.0-alpha-1, 3.0.5, 2.5.15
    • None
    • None

    Description

      Consider the following:

      @groovy.transform.ToString
      class A {
        A(a) {}
        def b
      }
      class C {
        C() {
          this(new A(null).tap { b = 42 })
        }
      C(x) {
          print x
        }
      }
      new C() // should output "A(42)"
      

      A has no default constructor, so properties are initialized in tap closure. Because the "tap" expression is passed as argument to special constructor call, extra static checking is performed and the error "Apparent variable 'b' was found in a static scope but doesn't refer to a local variable, static field or class." is emitted.

      Creation of A cannot be moved to a local variable because special constructor call must be the first statement/expression in the constructor body.

      This was discovered while investigating variations of GROOVY-8327, GROOVY-8389, and GROOVY-9587.

      Attachments

        Activity

          People

            emilles Eric Milles
            emilles Eric Milles
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Time Tracking

                Estimated:
                Original Estimate - Not Specified
                Not Specified
                Remaining:
                Remaining Estimate - 0h
                0h
                Logged:
                Time Spent - 0.5h
                0.5h