Skip to content

inside Conversion methods, resolve extension wrong #15474

Closed
@wangzaixiang

Description

@wangzaixiang

Compiler version

3.1.2

Demo1 1

  given Conversion[ String, Int ] with
    def apply(from: String): Int = from.toInt   // the generate code is def apply(from:String:Int) = apply(from)

Output

The generated code is:

 public int apply(java.lang.String);
    Code:
       0: aload_0
       1: aload_1
       2: invokevirtual #34                 // Method apply:(Ljava/lang/String;)I, calls apply recusive, so will StackOverflow.
       5: ireturn

Demo2

object Demo2:
  def arr2list[T](arr: Array[T]): List[T] = arr.toList

  given [T]: Conversion[Array[T], List[T]] with
    def apply(arr: Array[T]): List[T] = arr2list(arr)

  def main(args: Array[String]): Unit = 
    val arr = Array(1,2,3)
    val list: List[Int] = arr
    println(s"list = ${list}")
  

The code still compiled to

object Demo2:
  def arr2list[T](arr: Array[T]): List[T] = given_Conversion_Array_List.apply(arr) // instead arr.toList

  given [T]: Conversion[Array[T], List[T]] with
    def apply(arr: Array[T]): List[T] = arr2list(arr)

so run the code will got the StackOverflow error.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions