Skip to content

Type parameter defines type member under inheritance with Scala 3.2.2 ? #16850

Closed
@devlaam

Description

@devlaam

Observation

trait Trait :
  type Y
  var list: List[Y] = Nil

class Class[Y] extends Trait :
  def add(elm: Y): Unit = list = elm :: list

object Object extends Class[Int] :
  add(42)

println(Object.list)

compiles under 3.2.2 but not under 3.2.1. The latter gives the error:

Found:    (elm : Y)
Required: Class.this.Y²

where:    Y  is a type in class Class
          Y² is a type in trait Trait

So it looks like under 3.2.2 it is possible to define the type member from Trait with the type parameter from Class. That would be nice if this would fail to compile:

trait Trait :
  type Y <: String
  var list: List[Y] = Nil

class Class[Y] extends Trait :
  def add(elm: Y): Unit = list = elm :: list

object Object extends Class[Int] :
  add(42)

println(Object.list)

where I changed type Y to type Y <: String but it does not. This still produces List(42).

Expectation

Compiler error on the latter code OR the same behaviour as in Scala 3.2.1 of Scala 3.2.2 on the former code .

Discussion

I observed this behaviour on the Jan, 13 and we discussed it om the scala users forum under the title:
Int <: String in Scala 3.2.2?
. I had to wait though until Scastie with 3.2.2 was released for easy reference. I tested the latter code also under Scala 3.3.0-RC2 and 3.3.1-RC1-bin-20230204-a356581-NIGHTLY and they behave as 3.2.2 does.

Metadata

Metadata

Assignees

Labels

area:typeritype:bugitype:soundnessSoundness bug (it lets us compile code that crashes at runtime with a ClassCastException)regressionThis worked in a previous version but doesn't anymore

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions