Skip to content

erasure(Array[Int] | Array[Float]) == Array[Object] #1065

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

Closed
sjrd opened this issue Feb 8, 2016 · 3 comments
Closed

erasure(Array[Int] | Array[Float]) == Array[Object] #1065

sjrd opened this issue Feb 8, 2016 · 3 comments

Comments

@sjrd
Copy link
Member

sjrd commented Feb 8, 2016

I minimized the bug in the problematic test t3833.scala from #1061 to something that does not even require a Match. It can be reproduced with an If:

package hello

object world {
  def mkArray(atype: Int): Array[_ <: AnyVal] = {
    (if (atype == 1) new Array[Int](10) else new Array[Float](10))
  }

  def main(args: Array[String]): Unit = {
    println(mkArray(1))
  }
}

This causes an assertion in the back-end. We can see that the types are wrong as early as after erasure: (grep for HERE)

> run -Xprint:erasure -Xprint-types examples/hello.scala
[warn] Multiple main classes detected.  Run 'show discoveredMainClasses' to see the list
[info] Packaging /localhome/doeraene/projects/dotty/target/scala-2.11/dotty_2.11-0.1-SNAPSHOT.jar ...
[info] Done packaging.
[info] Running dotty.tools.dotc.Main -Xprint:erasure -Xprint-types examples/hello.scala
[error] result of examples/hello.scala after erasure:
[error] <package <hello:hello> {
[error]   final lazy module val world: hello.world$ = 
[error]     <<<new hello.world$:hello.world$>:(()hello.world$)>():hello.world$>
[error]   final module class world$() extends <<<new Object:Object>:(()Object)>():Object
[error]     >
[error]    { this: <notype> => 
[error]     def mkArray(atype: Int): Object = <{
[error]       <if <<<atype:Int>.==:((x: Int)Boolean)>(<1:Int>):Boolean> then 
[error]         <
[error]           <<dotty.runtime.Arrays:dotty.runtime.Arrays$>.newIntArray:
[error]             ((length: Int)Int[])
[error]           >
[error]         (<10:Int>):Int[]>
[error]        else 
[error]         <
[error]           <<dotty.runtime.Arrays:dotty.runtime.Arrays$>.newFloatArray:
[error]             ((length: Int)Float[])
[error]           >
[error]         (<10:Int>):Float[]>
[error]       :Object[]> // <-- HERE, not good, should be Object
[error]     }:Object[]> // <-- Same here
[error]     def main(args: String[]): Unit = <{
[error]       <<println:((x: Object)Unit)>(
[error]         <<hello.world.mkArray:((atype: Int)Object)>(<1:Int>):Object>
[error]       ):Unit>
[error]     }:Unit>
[error]   }
[error] }:hello.type>

The complete stack trace of the error is:

[error] java.lang.AssertionError: assertion failed: Expected primitive types I - Ljava/lang/Object;
[error]         at scala.Predef$.assert(Predef.scala:165)
[error]         at scala.tools.nsc.backend.jvm.BTypes$BType$class.conformsTo(BTypes.scala:157)
[error]         at scala.tools.nsc.backend.jvm.BTypes$ArrayBType.conformsTo(BTypes.scala:847)
[error]         at scala.tools.nsc.backend.jvm.BCodeBodyBuilder$PlainBodyBuilder.adapt(BCodeBodyBuilder.scala:896)
[error]         at scala.tools.nsc.backend.jvm.BCodeBodyBuilder$PlainBodyBuilder.genLoad(BCodeBodyBuilder.scala:436)
[error]         at scala.tools.nsc.backend.jvm.BCodeBodyBuilder$PlainBodyBuilder.genLoadIf(BCodeBodyBuilder.scala:217)
[error]         at scala.tools.nsc.backend.jvm.BCodeBodyBuilder$PlainBodyBuilder.genLoad(BCodeBodyBuilder.scala:300)
[error]         at scala.tools.nsc.backend.jvm.BCodeSkelBuilder$PlainSkelBuilder.emitNormalMethodBody$1(BCodeSkelBuilder.scala:607)
[error]         at scala.tools.nsc.backend.jvm.BCodeSkelBuilder$PlainSkelBuilder.genDefDef(BCodeSkelBuilder.scala:641)
[error]         at scala.tools.nsc.backend.jvm.BCodeSkelBuilder$PlainSkelBuilder.gen(BCodeSkelBuilder.scala:510)
[error]         at scala.tools.nsc.backend.jvm.BCodeSkelBuilder$PlainSkelBuilder$$anonfun$gen$1.apply(BCodeSkelBuilder.scala:512)
[error]         at scala.tools.nsc.backend.jvm.BCodeSkelBuilder$PlainSkelBuilder$$anonfun$gen$1.apply(BCodeSkelBuilder.scala:512)
[error]         at scala.collection.immutable.List.foreach(List.scala:381)
[error]         at scala.tools.nsc.backend.jvm.BCodeSkelBuilder$PlainSkelBuilder.gen(BCodeSkelBuilder.scala:512)
[error]         at scala.tools.nsc.backend.jvm.BCodeSkelBuilder$PlainSkelBuilder.genPlainClass(BCodeSkelBuilder.scala:119)
[error]         at dotty.tools.backend.jvm.GenBCodePipeline$Worker1.visit(GenBCode.scala:177)
[error]         at dotty.tools.backend.jvm.GenBCodePipeline$Worker1.run(GenBCode.scala:130)
[error]         at dotty.tools.backend.jvm.GenBCodePipeline.buildAndSendToDisk(GenBCode.scala:334)
[error]         at dotty.tools.backend.jvm.GenBCodePipeline.run(GenBCode.scala:300)
[error]         at dotty.tools.backend.jvm.GenBCode.run(GenBCode.scala:42)
[error]         at dotty.tools.dotc.core.Phases$Phase$$anonfun$runOn$1.apply(Phases.scala:271)
[error]         at dotty.tools.dotc.core.Phases$Phase$$anonfun$runOn$1.apply(Phases.scala:269)
[error]         at scala.collection.immutable.List.map(List.scala:273)
[error]         at dotty.tools.dotc.core.Phases$Phase$class.runOn(Phases.scala:269)
[error]         at dotty.tools.backend.jvm.GenBCode.runOn(GenBCode.scala:35)
[error]         at dotty.tools.dotc.Run$$anonfun$compileUnits$1$$anonfun$apply$mcV$sp$1.apply(Run.scala:60)
[error]         at dotty.tools.dotc.Run$$anonfun$compileUnits$1$$anonfun$apply$mcV$sp$1.apply(Run.scala:57)
[error]         at scala.collection.IndexedSeqOptimized$class.foreach(IndexedSeqOptimized.scala:33)
[error]         at scala.collection.mutable.ArrayOps$ofRef.foreach(ArrayOps.scala:186)
[error]         at dotty.tools.dotc.Run$$anonfun$compileUnits$1.apply$mcV$sp(Run.scala:57)
[error]         at dotty.tools.dotc.Run$$anonfun$compileUnits$1.apply(Run.scala:52)
[error]         at dotty.tools.dotc.Run$$anonfun$compileUnits$1.apply(Run.scala:52)
[error]         at dotty.tools.dotc.util.Stats$.monitorHeartBeat(Stats.scala:69)
[error]         at dotty.tools.dotc.Run.compileUnits(Run.scala:52)
[error]         at dotty.tools.dotc.Run.compileSources(Run.scala:49)
[error]         at dotty.tools.dotc.Run.compile(Run.scala:33)
[error]         at dotty.tools.dotc.Driver.doCompile(Driver.scala:21)
[error]         at dotty.tools.dotc.Driver.process(Driver.scala:47)
[error]         at dotty.tools.dotc.Driver.process(Driver.scala:57)
[error]         at dotty.tools.dotc.Driver.main(Driver.scala:65)
[error]         at dotty.tools.dotc.Main.main(Main.scala)
[error] error: Error while emitting hello.scala
[error] assertion failed: Expected primitive types I - Ljava/lang/Object;
[error] one error found
@sjrd sjrd added the itype:bug label Feb 8, 2016
@DarkDimius DarkDimius self-assigned this Feb 8, 2016
@DarkDimius DarkDimius changed the title Wrong erasure of Array[_ <: AnyVal] for branches of an If/Match erasure(Array[Int] | Array[Float]) == Array[Object] Feb 8, 2016
@DarkDimius
Copy link
Contributor

Errasure of ((Array[Int] | Array[Float])) is currently Array[Object].
I'm making a fix.

DarkDimius added a commit to dotty-staging/dotty that referenced this issue Feb 8, 2016
Unlike arrays of objects, that can be accessed as an array
of a super type of this object,
int[] cannot be accessed as an array of primitives.
DarkDimius added a commit to dotty-staging/dotty that referenced this issue Feb 8, 2016
odersky added a commit to dotty-staging/dotty that referenced this issue Feb 8, 2016
The lub of Int[] and Float[] is Object, not Object[].
Fixes scala#1065.
DarkDimius added a commit to dotty-staging/dotty that referenced this issue Feb 8, 2016
@DarkDimius
Copy link
Contributor

@sjrd you have two competing fixes to choose from, that do more-or-less the same in different ways.

@sjrd
Copy link
Member Author

sjrd commented Feb 8, 2016

Both the available fixes indeed solve the original issue I was struggling with.

I don't think I have the competence to choose either. If you really want my opinion, I'd vote for @DarkDimius' version, as I'm guess it would be slightly faster (it doesn't need additional subtyping checks)

@odersky odersky closed this as completed in 692cd5a Feb 8, 2016
odersky added a commit that referenced this issue Feb 8, 2016
Fix #1065 erasedLub for arrays of primitives.
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