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

companions in package object trip assertion #3437

Closed
scabug opened this issue May 14, 2010 · 3 comments
Closed

companions in package object trip assertion #3437

scabug opened this issue May 14, 2010 · 3 comments
Assignees

Comments

@scabug
Copy link

scabug commented May 14, 2010

Using scala 2.8.0.RC2 with Java 1.6.0_20 (on Ubuntu Linux), doing the following provokes a crash of the scala console.

Put the following code into a file called Pacakge.scala

package object expr {

  import scala.collection.immutable.Map

  object LExpr {
    val zero: LExpr = new LExpr(Map())
    def apply(m: (Var,Int)*): LExpr = {
      val n = m filter {case (_,c) => c != 0}
      if (n isEmpty)
        zero
      else 
        new LExpr(Map(n:_*))
    }
  }

  class LExpr private (val m: Map[Var,Int])  {
    def + (other: LExpr): LExpr = {
      val keySeq = (m.keySet ++ other.m.keySet) toSeq;
      new LExpr(Map(keySeq map (v => (v, m.getOrElse(v,0) + other.m.getOrElse(v,0))):_*))
    }
    def * (factor: Int): LExpr = new LExpr((m mapValues (_ * factor)))
  }

  class Var (private val idx: Int) {
    
  }

  class Scalar (val i: Int) {
    
  }

  implicit def var2LExpr (v: Var): LExpr = LExpr(v -> 1)
  implicit def int2Scalar (i: Int): Scalar = new Scalar(i)

}

Compile it and start a console session

peter@notebook:~/tmp/bug$$ scalac Package.scala 
peter@notebook:~/tmp/bug$$ scala -cp .
Welcome to Scala version 2.8.0.RC2 (Java HotSpot(TM) 64-Bit Server VM, Java 1.6.0_20).
Type in expressions to have them evaluated.
Type :help for more information.

scala> import expr._
import expr._

scala> val v = new Var(1)
v: expr.package.Var = expr.package$$Var@1a659078

scala> val w = new Var(2)
w: expr.package.Var = expr.package$$Var@1d74f478

scala> v+w
Exception in thread "main" java.lang.AssertionError: assertion failed: List(object package$$LExpr, object package$$LExpr)
	at scala.tools.nsc.symtab.Symbols$$Symbol.suchThat(Symbols.scala:1051)
	at scala.tools.nsc.symtab.Symbols$$Symbol.companionModule0(Symbols.scala:1257)
	at scala.tools.nsc.symtab.Symbols$$Symbol.companionModule(Symbols.scala:1265)
	at scala.tools.nsc.backend.jvm.GenJVM$$BytecodeGenerator.javaName(GenJVM.scala:1751)
	at scala.tools.nsc.backend.jvm.GenJVM$$BytecodeGenerator.javaType(GenJVM.scala:1841)
	at scala.tools.nsc.backend.jvm.GenJVM$$BytecodeGenerator.javaType(GenJVM.scala:1845)
	at scala.tools.nsc.backend.jvm.GenJVM$$BytecodeGenerator.genField(GenJVM.scala:600)
	at scala.tools.nsc.backend.jvm.GenJVM$$BytecodeGenerator$$$$anonfun$$genClass$$3.apply(GenJVM.scala:257)
	at scala.tools.nsc.backend.jvm.GenJVM$$BytecodeGenerator$$$$anonfun$$genClass$$3.apply(GenJVM.scala:257)
	at scala.collection.LinearSeqOptimized$$class.foreach(LinearSeqOptimized.scala:62)
	at scala.collection.immutable.List.foreach(List.scala:46)
	at scala.tools.nsc.backend.jvm.GenJVM$$BytecodeGenerator.genClass(GenJVM.scala:257)
	at scala.tools.nsc.backend.jvm.GenJVM$$JvmPhase.apply(GenJVM.scala:57)
	at scala.tools.nsc.backend.jvm.GenJVM$$JvmPhase$$$$anonfun$$run$$3.apply(GenJVM.scala:53)
	at scala.tools.nsc.backend.jvm.GenJVM$$JvmPhase$$$$anonfun$$run$$3.apply(GenJVM.scala:53)
	at scala.collection.mutable.HashMap$$$$anon$$2$$$$anonfun$$foreach$$3.apply(HashMap.scala:90)
	at scala.collection.mutable.HashMap$$$$anon$$2$$$$anonfun$$foreach$$3.apply(HashMap.scala:90)
	at scala.collection.Iterator$$class.foreach(Iterator.scala:632)
	at scala.collection.mutable.HashTable$$$$anon$$1.foreach(HashTable.scala:162)
	at scala.collection.mutable.HashTable$$class.foreachEntry(HashTable.scala:195)
	at scala.collection.mutable.HashMap.foreachEntry(HashMap.scala:40)
	at scala.collection.mutable.HashMap$$$$anon$$2.foreach(HashMap.scala:90)
	at scala.tools.nsc.backend.jvm.GenJVM$$JvmPhase.run(GenJVM.scala:53)
	at scala.tools.nsc.Global$$Run.compileSources(Global.scala:729)
	at scala.tools.nsc.Interpreter.compileAndSaveRun(Interpreter.scala:521)
	at scala.tools.nsc.Interpreter$$Request.objRun(Interpreter.scala:893)
	at scala.tools.nsc.Interpreter$$Request.compile(Interpreter.scala:916)
	at scala.tools.nsc.Interpreter.interpret(Interpreter.scala:588)
	at scala.tools.nsc.Interpreter.interpret(Interpreter.scala:580)
	at scala.tools.nsc.InterpreterLoop.reallyInterpret$$1(InterpreterLoop.scala:481)
	at scala.tools.nsc.InterpreterLoop.interpretStartingWith(InterpreterLoop.scala:518)
	at scala.tools.nsc.InterpreterLoop.command(InterpreterLoop.scala:371)
	at scala.tools.nsc.InterpreterLoop.processLine$$1(InterpreterLoop.scala:243)
	at scala.tools.nsc.InterpreterLoop.repl(InterpreterLoop.scala:256)
	at scala.tools.nsc.InterpreterLoop.main(InterpreterLoop.scala:555)
	at scala.tools.nsc.MainGenericRunner$$.main(MainGenericRunner.scala:67)
	at scala.tools.nsc.MainGenericRunner.main(MainGenericRunner.scala)

The same code. however, runs fine when compiled.

@scabug
Copy link
Author

scabug commented May 14, 2010

Imported From: https://issues.scala-lang.org/browse/SI-3437?orig=1
Reporter: peter.lietz

@scabug
Copy link
Author

scabug commented May 23, 2010

@paulp said:
This is all it takes to hit that assertion:

package object expr {
  object LExpr { }
  class LExpr { }
}

And this also crashes the compiler. You can't overload in package objects and apparently you can't stash companions either. These limitations are no big deal, but it is a problem that the main route to finding out about them is crashing and/or things not compiling for no obvious reason.

See also #1987.

@scabug
Copy link
Author

scabug commented Feb 20, 2011

@paulp said:
(In r24311) Observed that some historical issues with package objects no longer
seem so issuey. In the interests of keeping the arbitrary limitations
to a minimum, re-enabled case classes in package objects (see #2130, #3437.)
Closes #3437, review by odersky.

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

No branches or pull requests

2 participants