Skip to content
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

strange behavior List#corresponds #695

Closed
xuwei-k opened this issue May 3, 2017 · 6 comments
Closed

strange behavior List#corresponds #695

xuwei-k opened this issue May 3, 2017 · 6 comments

Comments

@xuwei-k
Copy link
Contributor

xuwei-k commented May 3, 2017

version 0.2

trait Equal[A] {
  def equal(x: A, y: A): Boolean
}

object Equal {
  implicit def int: Equal[Int] =
    new Equal[Int] {
      def equal(x: Int, y: Int): Boolean = x == y
    }

  implicit def string: Equal[String] =
    new Equal[String] {
      def equal(x: String, y: String): Boolean = x == y
    }

  implicit def list[A](implicit A: Equal[A]): Equal[List[A]] =
    new Equal[List[A]] {
      def equal(x: List[A], y: List[A]): Boolean =
        (x corresponds y)(A.equal(_, _))
    }
}

object Main {
  def main(args: Array[String]): Unit = {
    println(implicitly[Equal[Int]].equal(42, 42)) // true

    println(implicitly[Equal[String]].equal("a", "a")) // true

    val a = 42 :: Nil
    // false in scala-native!? true in JVM.
    println(implicitly[Equal[List[Int]]].equal(a, a))

    val b = "xyz" :: Nil
    println(implicitly[Equal[List[String]]].equal(b, b)) // false !?
  }
}
@densh
Copy link
Member

densh commented May 4, 2017

Definitely a bug, thanks for reporting @xuwei-k !

@densh densh added the bug label May 4, 2017
@densh densh added this to the 0.3 milestone May 4, 2017
@xuwei-k
Copy link
Contributor Author

xuwei-k commented May 6, 2017

more simple example

object Main {
  def main(args: Array[String]): Unit = {
    val a = List(1, 2, 3)

    // false in scala-native, true in JVM
    println(a.corresponds(a)(_ == _))
  }
}

@xuwei-k xuwei-k changed the title strange behavior with Equal type-class strange behavior List#corresponds May 6, 2017
@xuwei-k
Copy link
Contributor Author

xuwei-k commented May 6, 2017

https://github.com/scala/scala/blob/v2.11.11/src/library/scala/collection/LinearSeqLike.scala#L65-L68

object Main {
  def main(args: Array[String]): Unit = {
    val a = List(1, 2, 3)

    val result = a.corresponds(a){ (x, y) =>
      println((x, y))
      true
    }

    println(result)
  }
}
(1,1)
(1,2)
(1,3)
false

😱

@densh
Copy link
Member

densh commented May 18, 2017

@xuwei-k Thanks for the minimized example!

@densh densh modified the milestones: Backlog, 0.3 May 18, 2017
@densh
Copy link
Member

densh commented May 20, 2017

I was able to find the root cause for this one: front-end incorrectly resolves symbols to SSA names for methods that were transformed by tailrec phase. Instead of using the internal loop's version of this variable, it always uses the original this, never advancing to the tail. Should be easy to fix in 0.3.

@sjrd
Copy link
Collaborator

sjrd commented May 20, 2017

densh added a commit to densh/scala-native that referenced this issue Jun 13, 2017
Previously, front-end would incorrectly resolve symbols to SSA names for
methods that were transformed by tailrec phase. Instead of using the internal
loop's version of this variable, it always uses the original this, never
advancing to the tail.
densh added a commit to densh/scala-native that referenced this issue Jun 13, 2017
Previously, front-end would incorrectly resolve symbols to SSA names for
methods that were transformed by tailrec phase. Instead of using the internal
loop's version of this variable, it always uses the original this, never
advancing to the tail.
@densh densh closed this as completed in 3f39f9f Jun 13, 2017
asoltysik pushed a commit to asoltysik/scala-native that referenced this issue Jul 14, 2017
…rec (scala-native#826)

Previously, front-end would incorrectly resolve symbols to SSA names for
methods that were transformed by tailrec phase. Instead of using the internal
loop's version of this variable, it always uses the original this, never
advancing to the tail.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants