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

Covariant overrides with value class parameters produce broken forwarders #6500

Closed
scabug opened this issue Oct 10, 2012 · 7 comments
Closed
Assignees
Milestone

Comments

@scabug
Copy link

scabug commented Oct 10, 2012

Overriding a method C => A with a method C => B, where C is some value class and A and B are regular classes with B <: A, generates a broken forwarder. The forwarder attempts to box to the value class erasure's box and then cast to the value class box, producing a ClassCastException at runtime. Naturally, this affects overrides returning this.type as well. Tested with nightly Scala-2.11.0-SNAPSHOT.

object ValueClassForwarderVariance extends App {
  class Box(val value: Int) extends AnyVal
  
  trait Foo {
    def append(box: Box): Foo
  }
  
  class Bar extends Foo {
    override def append(box: Box): Bar = this // produces bad forwarder
  }
  
  // throws ClassCastException: java.lang.Integer cannot be cast to _$Box
  ((new Bar): Foo).append(new Box(0))
}
// boxes to value class erasure's box,
// casts to value class box (fail),
// then unboxes and forwards.
public ValueClassForwarderVariance$Foo append(int);
Code:
   0: aload_0       
   1: iload_1       
   2: invokestatic  #20 // Method scala/runtime/BoxesRunTime.boxToInteger:(I)Ljava/lang/Integer;
   5: checkcast     #22 // class ValueClassForwarderVariance$Box
   8: invokevirtual #26 // Method ValueClassForwarderVariance$Box.value:()I
  11: invokevirtual #28 // Method append:(I)LValueClassForwarderVariance$Bar;
  14: areturn            
@scabug
Copy link
Author

scabug commented Oct 10, 2012

Imported From: https://issues.scala-lang.org/browse/SI-6500?orig=1
Reporter: Chris Sachs (c9r)
Affected Versions: 2.10.0-M7
Other Milestones: 2.10.0

@scabug
Copy link
Author

scabug commented Oct 10, 2012

Chris Sachs (c9r) said:
Simplified test case.

@scabug
Copy link
Author

scabug commented Oct 15, 2012

@adriaanm said:
Paul, I know you love variance and value classes. Please re-assign to Grzegorz or some other person who loves them more.

@scabug
Copy link
Author

scabug commented Oct 15, 2012

@paulp said:
Terminology note to submitter: that's a bridge method, not a forwarder. (It is true that it forwards, so it's not like it's a crazy choice of word, but it's confusing because we have other things called forwarders.)

@scabug
Copy link
Author

scabug commented Oct 22, 2012

Chris Sachs (c9r) said:
Good to know definitively about the bridge method terminology, I'd wondered. I was about to report that value class methods also forget their type parameter bounds, but I see you already found and fixed that one :) Thanks.

@scabug
Copy link
Author

scabug commented Oct 30, 2012

@odersky said:
scala/scala#1539

(Sigh of relief: this turned out to be a silly omission; I feared a deeper design flaw).

@scabug
Copy link
Author

scabug commented Oct 30, 2012

@adriaanm said:
it's actually scala/scala#1540

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