Skip to content

Refinements lost in inferred type #19749

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
wz7982 opened this issue Feb 21, 2024 · 2 comments · Fixed by #20177
Closed

Refinements lost in inferred type #19749

wz7982 opened this issue Feb 21, 2024 · 2 comments · Fixed by #20177
Assignees
Labels
area:infer itype:bug regression This worked in a previous version but doesn't anymore stat:needs bisection Need to use nightly builds and git bisect to find out the commit where this issue was introduced
Milestone

Comments

@wz7982
Copy link

wz7982 commented Feb 21, 2024

Compiler version

3.4.0

Minimized code

import scala.deriving.Mirror

case class A(x: Int, y: String)

trait SomeTrait[T]

object SomeTrait:
    given [T]: SomeTrait[T] with {}

def f1[T](using p: Mirror.ProductOf[T]): Tuple.Elem[p.MirroredElemTypes, 0] = ???

def f2[T, R](f: T => R)(using SomeTrait[R]) = ???

// Scala3.3 is fine, 3.4 has compilation errors, p MirroredElemTypes type is missing and has been changed to Nothing
val x = f2(_ => f1[A])

Output

-- [E172] Type Error: ----------------------------------------------------------
13 |val x = f2(_ => f1[A])
   |                      ^
   |            No given instance of type SomeTrait[
   |              <error Match type reduction failed since selector Nothing
   |            matches none of the cases
   |
   |                case x *: xs => (0 : Int) match {
   |              case (0 : Int) => x
   |              case scala.compiletime.ops.int.S[n1] => Tuple.Elem[xs, n1]
   |            }>
   |              ] was found for parameter x$2 of method f2
1 error found

Expectation

The code is working properly

@wz7982 wz7982 added itype:bug stat:needs triage Every issue needs to have an "area" and "itype" label labels Feb 21, 2024
@bishabosha
Copy link
Member

changing some definitions around, it seems like the refinements to the mirror are not being kept:

scala> case class A(x: Int, y: String)
     |
     | trait SomeTrait[T]
     |
     | object SomeTrait:
     |     given [T]: SomeTrait[T] with {}
     |
     | def f1[T](using p: Mirror.ProductOf[T]): p.type = p
     |
     | def f2[T, R](f: T => R)(using SomeTrait[R]): R = ???
     |
     | def x = f2(_ => f1[A])
// defined case class A
// defined trait SomeTrait
// defined object SomeTrait
def f1[T](using p: scala.deriving.Mirror.ProductOf[T]): p.type
def f2[T, R](f: T => R)(using x$2: SomeTrait[R]): R
def x: scala.deriving.Mirror.ProductOf[A]

@bishabosha
Copy link
Member

more verbosely:

scala -S 3 -Xprint:typer
Welcome to Scala 3.4.0 (21.0.1, Java OpenJDK 64-Bit Server VM).
Type in expressions for evaluation. Or try :help.

scala> import scala.deriving.Mirror
     |
     | case class A(x: Int, y: String)
     |
     | trait SomeTrait[T]
     |
     | object SomeTrait:
     |     given [T]: SomeTrait[T] with {}
     |
     | def f1[T](using p: Mirror.ProductOf[T]): p.type = p
     |
     | def f2[T, R](f: T => R)(using SomeTrait[R]): R = ???
     |
     | def x = f2(_ => f1[A])
[[syntax trees at end of                     typer]] // rs$line$1
package <empty> {
  final lazy module val rs$line$1: rs$line$1 = new rs$line$1()
  final module class rs$line$1() extends Object() { this: rs$line$1.type =>
    import scala.deriving.Mirror
    case class A(x: Int, y: String) extends Object(), _root_.scala.Product,
      _root_.scala.Serializable {
      val x: Int
      val y: String
      def copy(x: Int, y: String): A = new A(x, y)
      def copy$default$1: Int @uncheckedVariance = A.this.x
      def copy$default$2: String @uncheckedVariance = A.this.y
      def _1: Int = this.x
      def _2: String = this.y
    }
    final lazy module val A: A = new A()
    final module class A() extends AnyRef() { this: A.type =>
      def apply(x: Int, y: String): A = new A(x, y)
      def unapply(x$1: A): A = x$1
      override def toString: String = "A"
    }
    trait SomeTrait[T >: Nothing <: Any]() extends Object {
      T
    }
    final lazy module val SomeTrait: SomeTrait = new SomeTrait()
    final module class SomeTrait() extends Object() { this: SomeTrait.type =>
      given class given_SomeTrait_T[T >: Nothing <: Any]() extends Object(),
        SomeTrait[given_SomeTrait_T.this.T] {
        T
      }
      final given def given_SomeTrait_T[T >: Nothing <: Any]:
        SomeTrait.given_SomeTrait_T[T] = new SomeTrait.given_SomeTrait_T[T]()
    }
    def f1[T >: Nothing <: Any](using p: scala.deriving.Mirror.ProductOf[T]):
      p.type = p
    def f2[T >: Nothing <: Any, R >: Nothing <: Any](f: T => R)(using
      x$2: SomeTrait[R]): R = ???
    def x: scala.deriving.Mirror.ProductOf[A] =
      f2[Any, scala.deriving.Mirror.ProductOf[A]](
        {
          def $anonfun(_$1: Any): scala.deriving.Mirror.ProductOf[A] =
            f1[A](
              A.$asInstanceOf[

                  scala.deriving.Mirror.Product{
                    type MirroredMonoType = A; type MirroredType = A;
                      type MirroredLabel = ("A" : String);
                      type MirroredElemTypes = (Int, String);
                      type MirroredElemLabels = (("x" : String), ("y" : String))
                    }

              ]
            )
          closure($anonfun)
        }
      )(
        SomeTrait.given_SomeTrait_T[

            scala.deriving.Mirror.Product{
              type MirroredType = A; type MirroredMonoType = A;
                type MirroredElemTypes <: Tuple
            }

        ]
      )
  }
}

// defined case class A
// defined trait SomeTrait
// defined object SomeTrait
def f1[T](using p: scala.deriving.Mirror.ProductOf[T]): p.type
def f2[T, R](f: T => R)(using x$2: SomeTrait[R]): R
def x: scala.deriving.Mirror.ProductOf[A]

@bishabosha bishabosha changed the title I tried to upgrade my code to 3.4.0, but there was an error Refinements lost in inferred type Feb 21, 2024
@bishabosha bishabosha added area: type inference regression This worked in a previous version but doesn't anymore stat:needs bisection Need to use nightly builds and git bisect to find out the commit where this issue was introduced and removed stat:needs triage Every issue needs to have an "area" and "itype" label labels Feb 21, 2024
EugeneFlesselle added a commit to dotty-staging/dotty that referenced this issue Apr 13, 2024
EugeneFlesselle added a commit to dotty-staging/dotty that referenced this issue Apr 14, 2024
hamzaremmal added a commit that referenced this issue Apr 14, 2024
@Kordyjan Kordyjan added this to the 3.5.0 milestone May 10, 2024
WojciechMazur pushed a commit that referenced this issue Jul 5, 2024
Closes #19749
Closes #16208

[Cherry-picked 2afcf39]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area:infer itype:bug regression This worked in a previous version but doesn't anymore stat:needs bisection Need to use nightly builds and git bisect to find out the commit where this issue was introduced
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants