Skip to content

ClassCastException for computed fields returning context functions #23415

Open
@TomasMikula

Description

@TomasMikula

Compiler version

3.7.1

Minimized code

//> using scala 3.7.1

trait Ctx

object Foo extends Selectable:
  type Fields = (x: Ctx ?=> Int, y: Ctx ?=> Int)

  def selectDynamic(name: String): Ctx ?=> Int =
    (ctx: Ctx) ?=> 1

@main def test =
  given Ctx = new Ctx {}
  val x: Int = Foo.x
  println(x)

Output

% scala test.scala
Compiling project (Scala 3.7.1, JVM (21))
Compiled project (Scala 3.7.1, JVM (21))
Exception in thread "main" java.lang.ClassCastException: class java.lang.Integer cannot be cast to class scala.Function1 (java.lang.Integer is in module java.base of loader 'bootstrap'; scala.Function1 is in unnamed module of loader 'app')
	at test$package$.test(test.scala:13)
	at test.main(test.scala:11)

Expectation

Should work.

For reference, regular functions work
//> using scala 3.7.1

trait Ctx

object Foo extends Selectable:
  type Fields = (x: Ctx => Int, y: Ctx => Int)

  def selectDynamic(name: String): Ctx => Int =
    (ctx: Ctx) => 1

@main def test =
  val ctx = new Ctx {}
  val x: Int = Foo.x(ctx)
  println(x)
% scala test2.scala        
Compiling project (Scala 3.7.1, JVM (21))
Compiled project (Scala 3.7.1, JVM (21))
1

Metadata

Metadata

Assignees

No one assigned

    Labels

    area:context functionsIssues tied to context functions.itype:bugitype:soundnessSoundness bug (it lets us compile code that crashes at runtime with a ClassCastException)

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions