scala / scala3

The Scala 3 compiler, also known as Dotty.
https://dotty.epfl.ch
Apache License 2.0
5.89k stars 1.06k forks source link

Method calls should not be allowed on paths #21788

Open lrytz opened 1 month ago

lrytz commented 1 month ago

3.5.1

scala> class A { class B }
scala> def f(using a: A): a.type = { println("f"); a }
scala> given a: A = new A
scala> new f.B
val res0: a.B = A$B@3921135e

Via https://discord.com/channels/632150470000902164/632628489719382036/1296041645900562455

In the quotes API it's a transparent inline def: https://github.com/scala/scala3/blob/3.5.1/library/src/scala/quoted/Quotes.scala#L18.

@sjrd said

I thought that was only allowed for transparent inline defs that inline themselves to a path

which makes sense; is it specced?

som-snytt commented 1 month ago

I'm OK with that, but:

Welcome to Scala 3.5.1 (22.0.2, Java OpenJDK 64-Bit Server VM).
Type in expressions for evaluation. Or try :help.

scala> class A { class B }
// defined class A

scala> def f(using a: A): a.type = { println("f"); a }
def f(using a: A): a.type

scala> given a[X]: A = new A
def a[X]: A

scala> new f.B
java.lang.AssertionError: assertion failed: val <none>
        at scala.runtime.Scala3RunTime$.assertFailed(Scala3RunTime.scala:8)
        at dotty.tools.backend.jvm.BCodeHelpers$BCInnerClassGen.assertClassNotArray(BCodeHelpers.scala:151)
        at dotty.tools.backend.jvm.BCodeHelpers$BCInnerClassGen.assertClassNotArrayNotPrimitive(BCodeHelpers.scala:156)
        at dotty.tools.backend.jvm.BCodeHelpers$BCInnerClassGen.getClassBType(BCodeHelpers.scala:174)
EugeneFlesselle commented 1 month ago

This can also exploited to bypass bound realizability checks:

@main def Test =

  trait A:
    type B >: Int <: Boolean

  def f(using a: A): a.type = a
  given a[X]: A = a

  val _: Boolean = 1 : f.B // ClassCastException