Groovy
  1. Groovy
  2. GROOVY-5160

Assignments in if/else/for/while/ternary are not type checked properly

    Details

    • Type: Bug Bug
    • Status: Closed
    • Priority: Major Major
    • Resolution: Fixed
    • Affects Version/s: 2.0-beta-1
    • Fix Version/s: 2.0-beta-2
    • Component/s: Static Type Checker
    • Labels:
      None

      Description

      The following code will pass, though the return type of x after the if/else statement may differ:

      def x
      if (cond) {
        x = new Date()
      } else {
        x = 123
      }
      x.toInteger()
      

      In a similar manner:

      def x = '123'
      for (int i=0; i<5;i++) { x = new HashSet() }
      x.toInteger()
      
      def x = '123'
      while (false) { x = new HashSet() }
      x.toInteger()
      
      def x = '123'
      def cond = false
      cond?(x = new HashSet()):3
      x.toInteger()
      

        Activity

        Cédric Champeau created issue -
        Cédric Champeau made changes -
        Field Original Value New Value
        Summary Assignments in if/else are not type checked properly Assignments in if/else/for/while/ternary are not type checked properly
        Description The following code will pass, though the return type of x after the if/else statement may differ:

        {code}
        def x
        if (cond) {
          x = new Date()
        } else {
          x = 123
        }
        x.toInteger()
        {code}
        The following code will pass, though the return type of x after the if/else statement may differ:

        {code}
        def x
        if (cond) {
          x = new Date()
        } else {
          x = 123
        }
        x.toInteger()
        {code}

        In a similar manner:
        {code}
        def x = '123'
        for (int i=0; i<5;i++) { x = new HashSet() }
        x.toInteger()
        {code}

        {code}
        def x = '123'
        while (false) { x = new HashSet() }
        x.toInteger()
        {code}

        {code}
        def x = '123'
        def cond = false
        cond?(x = new HashSet()):3
        x.toInteger()
        {code}
        Cédric Champeau made changes -
        Resolution Fixed [ 1 ]
        Status Open [ 1 ] Resolved [ 5 ]
        Paul King made changes -
        Status Resolved [ 5 ] Closed [ 6 ]
        Mark Thomas made changes -
        Project Import Sun Apr 05 13:32:57 UTC 2015 [ 1428240777691 ]
        Mark Thomas made changes -
        Workflow jira [ 12734084 ] Default workflow, editable Closed status [ 12745733 ]
        Mark Thomas made changes -
        Project Import Mon Apr 06 02:11:23 UTC 2015 [ 1428286283443 ]
        Mark Thomas made changes -
        Workflow jira [ 12971461 ] Default workflow, editable Closed status [ 12979270 ]

          People

          • Assignee:
            Cédric Champeau
            Reporter:
            Cédric Champeau
          • Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved:

              Development