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

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

Closed
devlaam opened this issue Feb 7, 2023 · 2 comments · Fixed by #16889
Closed

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

devlaam opened this issue Feb 7, 2023 · 2 comments · Fixed by #16889
Assignees
Labels
area:typer itype:bug itype:soundness Soundness bug (it lets us compile code that crashes at runtime with a ClassCastException) regression This worked in a previous version but doesn't anymore
Milestone

Comments

@devlaam
Copy link

devlaam commented Feb 7, 2023

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.

@devlaam devlaam added the stat:needs triage Every issue needs to have an "area" and "itype" label label Feb 7, 2023
@jchyb jchyb added itype:bug area:typer itype:soundness Soundness bug (it lets us compile code that crashes at runtime with a ClassCastException) and removed stat:needs triage Every issue needs to have an "area" and "itype" label labels Feb 7, 2023
@jchyb
Copy link
Contributor

jchyb commented Feb 7, 2023

Thank you for submitting. This appears to also be a pretty severe soundness issue, as I try to show in the reproduction below:

trait Trait:
  type Y <: String
  var list: List[Y] = Nil
  def listSubstr() =
    list.map(_.substring(0))

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

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

@main def main() =
  println(Object.list)

compiles "successfully" on main (21729d2) and 3.2.2, but when run will error out with a ClassCastException:

Exception in thread "main" java.lang.ExceptionInInitializerError
        at main$package$.main(main.scala:15)
        at main.main(main.scala:14)
Caused by: java.lang.ClassCastException: class java.lang.Integer cannot be cast to class java.lang.String (java.lang.Integer and java.lang.String are in module java.base of loader 'bootstrap')
        at scala.collection.immutable.List.map(List.scala:246)
        at Trait.listSubstr(main.scala:5)
        at Trait.listSubstr$(main.scala:1)
        at Class.listSubstr(main.scala:7)
        at Object$.<clinit>(main.scala:12)
        ... 2 more

@jchyb jchyb added the regression This worked in a previous version but doesn't anymore label Feb 7, 2023
@Kordyjan Kordyjan added this to the 3.3.0 backports milestone Feb 8, 2023
@KacperFKorban KacperFKorban added the stat:needs bisection Need to use nightly builds and git bisect to find out the commit where this issue was introduced label Feb 8, 2023
@jchyb
Copy link
Contributor

jchyb commented Feb 8, 2023

Bisect shows 66bdfb8 as "the first bad commit"

@jchyb jchyb removed the stat:needs bisection Need to use nightly builds and git bisect to find out the commit where this issue was introduced label Feb 8, 2023
@odersky odersky self-assigned this Feb 8, 2023
Kordyjan pushed a commit to dotty-staging/dotty that referenced this issue Feb 17, 2023
@Kordyjan Kordyjan modified the milestones: 3.3.0 backports, 3.3.1, 3.3.0 Aug 1, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area:typer itype:bug itype:soundness Soundness bug (it lets us compile code that crashes at runtime with a ClassCastException) regression This worked in a previous version but doesn't anymore
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants