Skip to content

Extension method name conflict produces Lambda toString with Java 17 bytecode instead of compile error #24254

@carueda

Description

@carueda

Compiler version

Scala 3.3.4 and 3.7.0 (likely other 3.x)

Minimized code

extension (s: String)
  def indent: String = s.replaceAll("\n", "\n  ")
  def preIndent: String = "  " + s.indent  // Calls extension method on same receiver

@main def test(): Unit =
  val result = "foo\nbar".preIndent
  println(result)

Output

Bug$package$$$Lambda$1/0x0000000800001800@17ed40e0

Using scala-cli:

Scala 3.3.4:

❯ scala-cli run Bug.scala --scala-version 3.3.4 -O -java-output-version:17
Compiling project (Scala 3.3.4, JVM (17))
Compiled project (Scala 3.3.4, JVM (17))
  Bug$package$$$Lambda$1/0x0000000800001800@17ed40e0

Scala 3.7.0:

❯ scala-cli run Bug.scala --scala-version 3.7.0 -O -java-output-version:17
Compiling project (Scala 3.7.0, JVM (17))
[warn] ./Bug.scala:2:7
[warn] Extension method indent will never be selected from type String
[warn] because String already has a member with the same name and compatible parameter types.
[warn]   def indent: String = s.replaceAll("\n", "\n  ")
[warn]       ^
Compiled project (Scala 3.7.0, JVM (17))
  Bug$package$$$Lambda$1/0x0000000800001800@17ed40e0

Expectation

foo
bar

When an extension method conflicts with an existing type member (e.g., String.indent), the compiler warns that it "will never be selected" but then generates broken code (Lambda toString) when another extension method tries to call it, instead of either making it work or failing with a compile error.

Workaround: Inline the logic instead of calling the other extension method:

  def preIndent: String = "  " + s.replaceAll("\n", "\n  ")

Note: Works correctly with -java-output-version:8:

❯ scala-cli run Bug.scala --scala-version 3.7.0 -O -java-output-version:8
Compiling project (Scala 3.7.0, JVM (17))
Compiled project (Scala 3.7.0, JVM (17))
  foo
  bar

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions