Scala Programming Language
  1. Scala Programming Language
  2. SI-5210

Wrong type inferred for variables with this.type

    Details

    • Type: Bug Bug
    • Status: Open
    • Priority: Critical Critical
    • Resolution: Unresolved
    • Affects Version/s: Scala 2.10.0
    • Fix Version/s: None
    • Component/s: Misc Compiler
    • 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.

        Issue Links

          Activity

          Hide
          Paul Phillips added a comment -

          Regression in r25879, caused by me.

          I'm kind of surprised it ever worked (especially as there are no tests defending it.)

          Show
          Paul Phillips added a comment - Regression in r25879, caused by me. I'm kind of surprised it ever worked (especially as there are no tests defending it.)
          Hide
          Paul Phillips added a comment -

          Fixed in 09f380dbda .

          Show
          Paul Phillips added a comment - Fixed in 09f380dbda .
          Hide
          Martin Odersky added a comment -

          I believe the original behavior was buggy. The test case should not compile.

          Show
          Martin Odersky added a comment - I believe the original behavior was buggy. The test case should not compile.

            People

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

              Dates

              • Created:
                Updated:

                Development