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

Typechecking regression in v3.1.2 #15178

Closed
tnielens opened this issue May 13, 2022 · 8 comments · Fixed by #15343
Closed

Typechecking regression in v3.1.2 #15178

tnielens opened this issue May 13, 2022 · 8 comments · Fixed by #15343
Assignees
Labels
area:typer itype:bug regression This worked in a previous version but doesn't anymore
Milestone

Comments

@tnielens
Copy link

tnielens commented May 13, 2022

Compiler version

Works with v3.1.1, fails with v3.1.2 and v3.1.3-RC3.

First bad commit 3ab18a9

Minimized code

//> using scala "3.1.1"
//> using lib "org.scalacheck::scalacheck:1.16.0"
import scala.reflect.ClassTag
import org.scalacheck.Arbitrary
import org.scalacheck.Gen

trait E[F[_]] {
  type T
  val value: F[T]
  override def toString(): String = s"E($value)"
}

object E {
  def apply[F[_], T1](value1: F[T1]) = new E[F] {
    type T = T1
    val value = value1
  }
}

trait TaggedGen[T] {
  val tag: ClassTag[T]
  val gen: Gen[T]
  override def toString(): String = s"TaggedGen($tag, $gen)"
}
type ETaggedGen = E[TaggedGen]

object TaggedGen {
  def apply[T: ClassTag: Arbitrary] = new TaggedGen[T] {
    val tag = implicitly
    val gen = implicitly[Arbitrary[T]].arbitrary
  }

  def apply[T](tag1: ClassTag[T], gen1: Gen[T]) = new TaggedGen[T] {
    val tag = tag1
    val gen = gen1
  }

  val gen: Gen[ETaggedGen] =
    Gen.oneOf(
      E(apply[Boolean]),
      E(apply[Byte]),
      E(apply[String])
    )

  val gen2: Gen[ETaggedGen] =
    gen.map { etg =>
      implicit val tag = etg.value.tag
      E(TaggedGen(tag, etg.value.gen.filter(_ => true))) // <-- failure here
    }
}

Output

[error] ./test.sc:54:19: Found:    (tag : reflect.ClassTag[etg.T])
[error] Required: reflect.ClassTag[Any]
[error]       E(TaggedGen(tag, etg.value.gen.filter(_ => true)))
[error]                   ^^^
Error compiling project (Scala 3.1.2, JVM)

Expectation

This snippet should continue compiling with scala v3.1.2.

@tnielens tnielens added itype:bug stat:needs triage Every issue needs to have an "area" and "itype" label labels May 13, 2022
@bishabosha
Copy link
Member

still fails with 3.2.0-RC1-bin-20220511-7c446ce-NIGHTLY

@bishabosha bishabosha added area:typer and removed stat:needs triage Every issue needs to have an "area" and "itype" label labels May 13, 2022
@odersky
Copy link
Contributor

odersky commented May 13, 2022

Can we find out where this regressed first? For the moment I am not even sure this is an error. I

@odersky odersky removed their assignment May 13, 2022
@bishabosha bishabosha added the stat:needs minimization Needs a self contained minimization label May 13, 2022
@bishabosha
Copy link
Member

can you reproduce this without scalacheck?

@tnielens
Copy link
Author

I'll try to minimize the example and remove scalacheck.
As a short note, adding explicit type args to the failing expression makes it compile:
E[TaggedGen, etg.T](TaggedGen(tag, etg.value.gen.filter(_ => true))) // <-- failure here. With explicit type, compiles fine.

@tnielens
Copy link
Author

Here is a short repro:

trait E[F[_]] {
  type T
  val value: F[T]
}

object E {
  def apply[F[_], T1](value1: F[T1]) = new E[F] {
    type T = T1
    val value = value1
  }
}

val a: Option[E[Ordering]] = Option(E(Ordering[Int]))
a.map(it => E(it.value))

@griggt
Copy link
Contributor

griggt commented May 13, 2022

Regressed in 3ab18a9.

@bishabosha bishabosha removed the stat:needs minimization Needs a self contained minimization label May 14, 2022
@bishabosha bishabosha assigned smarter and unassigned smarter May 14, 2022
@smarter
Copy link
Member

smarter commented May 16, 2022

Workaround: give an explicit result type to E.apply:

object E {
  def apply[F[_], T1](value1: F[T1]): E[F] = new E[F] {
    type T = T1
    val value = value1
  }
}

Otherwise the inferred type is E[F] { type T = T1 } which complicates inference since the type E[F] { type T = it.T } is not valid outside the lambda where it is defined.

@prolativ prolativ added the regression This worked in a previous version but doesn't anymore label May 17, 2022
@anatoliykmetyuk anatoliykmetyuk added this to the 3.1.3 milestone May 30, 2022
@odersky odersky removed this from the 3.1.3 milestone May 31, 2022
@odersky
Copy link
Contributor

odersky commented May 31, 2022

So, can we close this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area:typer itype:bug regression This worked in a previous version but doesn't anymore
Projects
None yet
Development

Successfully merging a pull request may close this issue.

8 participants