Uploaded image for project: 'Scala Programming Language'
  1. Scala Programming Language
  2. SI-5210

Wrong type inferred for variables with this.type

    Details

    • Type: Bug
    • Status: CLOSED
    • Priority: Critical
    • Resolution: Fixed
    • Affects Version/s: Scala 2.10.0
    • Fix Version/s: Scala 2.10.0-RC1
    • Component/s: Compiler (Misc)
    • Labels:
      None
    • Environment:

      scala-2.10.0.r26037-b20111121023229

      Description

      The following works in 2.9.1 (and earlier versions) but fails in trunk (2011-11-21):

      object WithOpTest {
       
      trait WithOp extends Cloneable {
       
        def f: this.type = this
       
        def g1: this.type = {
          f
        }
       
        def g2: this.type = {
          val t = f
          t
        }
       
      }
       
      }
      

      Using f directly in g1 is OK but assigning it to an intermediate variable in g2 fails:

      C:\Users\szeiger\Desktop>scalac Test.scala
       
      C:\Users\szeiger\Desktop>c:\StandaloneApps\scala-2.10.0.r26037-b20111121023229\bin\scalac Test.scala
      Test.scala:13: error: type mismatch;
       found   : t.type (with underlying type WithOpTest.WithOp)
       required: WithOp.this.type
          t
          ^
      one error found
      

      Adding an explicit type annotation ": this.type" to t also makes it work.

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                Unassigned
                Reporter:
                szeiger Stefan Zeiger
              • Votes:
                0 Vote for this issue
                Watchers:
                6 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved: