Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

2.8 Enumeration regression with "private Object escapes its defining scope" #3174

Closed
scabug opened this issue Mar 13, 2010 · 7 comments
Closed
Assignees

Comments

@scabug
Copy link

scabug commented Mar 13, 2010

In 2.7.7 this use of an enumeration compiles, but in 2.8b1 it does not

$$ cat test.scala
object Color extends Enumeration {
  val Blue = Value
  val Green = Value
}

class Board {
  val grid = Array(Color.Blue, Color.Green)
}

println((new Board).grid)
$$ /usr/lib/scala-2.8.0.Beta1-prerelease/bin/scala test.scala
(fragment of test.scala):7: error: private object Color escapes its defining scope as part of type Array[this.Color.Value]
  val grid = Array(Color.Blue, Color.Green)
       ^
one error found
!!!
discarding <script preamble>
$$ /usr/lib/scala-2.7.7.final/bin/scala test.scala
[Lscala.Enumeration$$Value;@366025e7
@scabug
Copy link
Author

scabug commented Mar 13, 2010

Imported From: https://issues.scala-lang.org/browse/SI-3174?orig=1
Reporter: @JamesIry
See #5211

@scabug
Copy link
Author

scabug commented Mar 13, 2010

@JamesIry said:
Ah, this appears to be a REPL problem.

$$ /usr/lib/scala-2.8.0.Beta1-prerelease/bin/scalac test.scala
$$ /usr/lib/scala-2.8.0.Beta1-prerelease/bin/scala Test
[Lscala.Enumeration$$Value;@7f5663a2

@scabug
Copy link
Author

scabug commented Mar 13, 2010

@JamesIry said:
Arg, I wish I could edit.

Previous comment should have been preceeded with modified code showing a wrapper object with main defined

$$ cat test.scala
object Test {
object Color extends Enumeration {
  val Blue = Value
  val Green = Value
}

class Board {
  val grid = Array(Color.Blue, Color.Green)
}

def main(args : Array[String]) {
   println((new Board).grid)
}
}

@scabug
Copy link
Author

scabug commented Jun 6, 2010

@lexspoon said:
Is this type error intended? Here is a smaller example showing the situation:

object test {
  def method() {
    class Foo extends AnyRef {
      object Color {
        object Blue
      }

      class Board {
        val grid = Color.Blue
      }
    }
    new Foo
  }
}

Where does a name escape its scope? Within the method, Color and Blue are in scope. Outside of the method, neither is visible. Where is the escape?

As a clue to what's happening, changing "new Foo" causes it to compile without error. Either deleting it or replacing it by "val x = new Foo" causes the type error to go away.

What seems to be happening is that Foo is temporarily inferred as the return type of the method's block. That's not desired, though, is it? The block should type check with an expected type of Unit. That means the last statement of the block should also type check with an expected type of Unit. Thus the type of new Foo should not ever make it out of the block's interior.

If anyone wants to defend this situation, then please also say what is different about this example, which does not trigger an error:

object test {
  def method() {
    class Foo extends AnyRef {
    }
    new Foo
  }
}

In this example, type Foo escapes its method in the same way as my first example, but now there is no type error. Why should there be an error about Foo's members but not about Foo itself?

If this needs to be fixed in the repl it can be. However, can the type checker's check for escaping values be made less aggressive? In this case it is raising a false alarm.

@scabug
Copy link
Author

scabug commented Jun 7, 2010

@odersky said:
(In r22184) Fixed #3174. Review by spoon

@scabug
Copy link
Author

scabug commented Jun 13, 2010

@odersky said:
(In r22253) Closes #3174 and #3560. Review by moors.

@scabug
Copy link
Author

scabug commented Jun 13, 2010

@odersky said:
(In r22254) Applied patch that closes #3560 and still fixes #3174.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants