Skip to content

inside Conversion methods, resolve extension wrong #15474

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wangzaixiang opened this issue Jun 19, 2022 · 1 comment · Fixed by #15481
Closed

inside Conversion methods, resolve extension wrong #15474

wangzaixiang opened this issue Jun 19, 2022 · 1 comment · Fixed by #15481

Comments

@wangzaixiang
Copy link

wangzaixiang commented Jun 19, 2022

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.

@wangzaixiang wangzaixiang added itype:bug stat:needs triage Every issue needs to have an "area" and "itype" label labels Jun 19, 2022
@som-snytt
Copy link
Contributor

Duplicates #10947 where there are links to duplicates, recursively.

odersky added a commit to dotty-staging/dotty that referenced this issue Jun 19, 2022
 1. Also check apply methods of companions of implicit or given classes.
    Specifically apply methods of implicit Conversions.

 2. Look inside Inlined nodes to detect loops.

Fixes scala#15474
Fixes scala#10947
@odersky odersky added itype:enhancement and removed itype:bug stat:needs triage Every issue needs to have an "area" and "itype" label labels Jun 19, 2022
@Kordyjan Kordyjan added this to the 3.2.1 milestone Aug 1, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants