Skip to content

Compilation error when using opaque type alias methods with same name #23841

@lucasmdjl

Description

@lucasmdjl

Compiler version

3.3.6

This bug is fixed in 3.6.4+, but given that 3.3.6 is the current LTS, could the fix be backported?

Minimized code

//> using scala 3.3.6
//> using options -Wall -Werror
//> using dep com.outr::scribe:3.16.1

import scala.util.{Failure, Success, Try}

class JavaFraction(val value: java.math.BigDecimal) {
  def asBigDecimal(): java.math.BigDecimal = value
}

opaque type Fraction = JavaFraction

object Fraction:
  def fromDecimal(value: BigDecimal): Fraction =
    JavaFraction(value.bigDecimal)

extension (f: Fraction)
  def asBigDecimal(): Try[BigDecimal] = Try(BigDecimal(f.asBigDecimal()))

extension (v: BigDecimal) def asFraction: Fraction = Fraction.fromDecimal(v)

class Bug {
  def compilerBug(): Boolean = {
    val v = BigDecimal("1.5")

    // This should work but the compiler sees x as being of type Any
    val f = v.asFraction
    f.asBigDecimal() match {
      case Success(x) => x.bigDecimal == v.bigDecimal
      case Failure(e) => false
    }
  }

  def workingWorkaround(): Boolean = {
    val v = BigDecimal("1.5")

    // This works with explicit typing
    val f: Fraction = v.asFraction
    f.asBigDecimal() match {
      case Success(x) => x.bigDecimal == v.bigDecimal
      case Failure(e) => false
    }
  }
}

Output

[error] -- [E008] Not Found Error: /home/xxxx/compiler-bug/src/main/scala/JavaApi.scala:25:27 
[error] 25 |      case Success(x) => x.bigDecimal == v.bigDecimal
[error]    |                         ^^^^^^^^^^^^
[error]    |                         value bigDecimal is not a member of Any
[error] one error found
[error] (Compile / compileIncremental) Compilation failed

Expectation

x should be resolved as a scala.math.BigDecimal and the code should compile correctly.

Metadata

Metadata

Assignees

No one assigned

    Labels

    itype:bugstat:needs triageEvery issue needs to have an "area" and "itype" label

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions