Skip to content

Type holes in protected[this] abstract types #370

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
acrylic-origami opened this issue Apr 8, 2017 · 2 comments
Closed

Type holes in protected[this] abstract types #370

acrylic-origami opened this issue Apr 8, 2017 · 2 comments

Comments

@acrylic-origami
Copy link

Variant types can take invariant positions in object-protected (protected[this]) abstract type, but these abstract types can also be exposed publicly [sometimes*]. For example, both of the following are legal, although they shouldn't be:

// 1. Type bound with +T in invariant position, then exposed publicly:
abstract class InvariantBoundAbstractType[+T] {
  protected[this] type TSeq <: MutableList[T]
  var v: TSeq
}

// 2. Locally, the type is bound properly, but not for subclass:
abstract class VariantBoundAbstractType[+T] {
  protected[this] type TSeq <: Seq[T]
  protected[this] val v: TSeq
  def get(): TSeq = v
  
  // None of this is immediately erroneous,
  // but can be if subclassed as below:
}
class InvariantConcreteType[+T](protected[this] val v: MutableList[T]) extends VariantBoundAbstractType[T] {
  protected[this] type TSeq = MutableList[T] // no complaints for this assignment
}

The violations are classic:

// 1.
val wrapper = (new InvariantBoundAbstractType[Int] {
  type TSeq = MutableList[Int]
  var v = MutableList(42)
})
(wrapper: InvariantBoundAbstractType[Any]).v += "string!"
wrapper.v.last + 42
// java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer

//2.
var A = MutableList(42)
(new InvariantConcreteType[Int](A): VariantBoundAbstractType[Any]).get() += "string!"
A.last + 42
// java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer

*The typechecker catches this hole occasionally when the violation is simpler. For example, the following fails correctly:

class DirectInvariantConcreteType[+T] {
  protected[this] type TSeq = MutableList[T] // assignment makes set() a no-go, whereas lower-bounding would be fine
  def set(v: TSeq): Unit // "covariant type T occurs in invariant position"
}
@acrylic-origami acrylic-origami changed the title Type holes in protected[this] abstract type constructors Type holes in protected[this] abstract types Apr 8, 2017
@paulp
Copy link

paulp commented May 10, 2017

Also known as scala/bug#7093.

@SethTisue
Copy link
Member

SethTisue commented May 10, 2017

offhand it looks to me like Paul is right and this is 7093

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

3 participants