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

Typer regression in multiple Open CB projects #17434

Closed
WojciechMazur opened this issue May 8, 2023 · 5 comments · Fixed by #17541
Closed

Typer regression in multiple Open CB projects #17434

WojciechMazur opened this issue May 8, 2023 · 5 comments · Fixed by #17541
Assignees
Labels
area:metaprogramming:quotes Issues related to quotes and splices itype:bug regression This worked in a previous version but doesn't anymore
Milestone

Comments

@WojciechMazur
Copy link
Contributor

WojciechMazur commented May 8, 2023

Compiler version

3.3.1-RC1-bin-20230504-0e00420-NIGHTLY

Needs minimization based on Open Community Build projects

Project Version Build URL Notes
dwickern/scala-nameof 4.0.0 Open CB logs typer, mismatch
scalikejdbc/scalikejdbc 4.0.0 Open CB logs typer, mismatch, macro
scalikejdbc/scalikejdbc-async 0.18.0 Open CB logs typer, mismatch, macro
bitlap/scalikejdbc-helper 0.2.3 -> 0.2.6 Open CB logs typer, mismatch, macro

Output

Example failure from dwickern/scala-nameof

Error:  -- [E007] Type Mismatch Error: /build/repo/src/test/scala/com/github/dwickern/macros/NameOfTest.scala:54:11 
Error:  54 |    nameOf(func1 _) should equal ("func1")
Error:     |           ^^^^^
Error:     |           Found:    Int => String
Error:     |           Required: T => Any

Expectation

@WojciechMazur WojciechMazur added itype:bug stat:needs minimization Needs a self contained minimization regression This worked in a previous version but doesn't anymore stat:needs triage Every issue needs to have an "area" and "itype" label labels May 8, 2023
@nicolasstucki nicolasstucki removed the stat:needs triage Every issue needs to have an "area" and "itype" label label May 9, 2023
@WojciechMazur
Copy link
Contributor Author

Reproducer for dwickern/scala-nameof

#macros.scala
trait NameOf:
  transparent inline def nameOf(inline expr: Any): String = ${NameOfImpl.nameOf('expr)}
  transparent inline def nameOf[T](inline expr: T => Any): String = ${NameOfImpl.nameOf('expr)}
object NameOf extends NameOf

import scala.compiletime.*

import scala.annotation.tailrec
import scala.quoted.*

object NameOfImpl {
  def nameOf(expr: Expr[Any])(using Quotes): Expr[String] = {
    import quotes.reflect.*
    @tailrec def extract(tree: Tree): String = tree match {
      case Ident(name) => name
      case Select(_, name) => name
      case Block(List(stmt), term) => extract(stmt)
      case DefDef("$anonfun", _, _, Some(term)) => extract(term)
      case Block(_, term) => extract(term)
      case Apply(term, _) if term.symbol.fullName != "<special-ops>.throw" => extract(term)
      case TypeApply(term, _) => extract(term)
      case Inlined(_, _, term) => extract(term)
      case Typed(term, _) => extract(term)
      case _ => throw new MatchError(s"Unsupported expression: ${expr.show}")
    }
    val name = extract(expr.asTerm)
    Expr(name)
  }
}
# test.scala

import NameOf._
def test() = 
    def func1(x: Int): String = ???
    val funcVal = func1 _
    assert(nameOf(funcVal) == "funcVal")
    assert(nameOf(func1 _) == "func1")

Output

-- [E007] Type Mismatch Error: /workspace/bisect/test.scala:7:18 ---------------
7 |    assert(nameOf(funcVal) == "funcVal")
  |                  ^^^^^^^
  |                  Found:    (funcVal : Int => String)
  |                  Required: T => Any
  |
  | longer explanation available when compiling with `-explain`
-- [E007] Type Mismatch Error: /workspace/bisect/test.scala:8:18 ---------------
8 |    assert(nameOf(func1 _) == "func1")
  |                  ^^^^^
  |                  Found:    Int => String
  |                  Required: T => Any
  |
  | longer explanation available when compiling with `-explain`

Last good release: 3.3.1-RC1-bin-20230501-1aa3372-NIGHTLY
First bad release: 3.3.1-RC1-bin-20230502-dee0065-NIGHTLY

Further bisect based commit was not possible due to problems with very strange compile errors not related to that issue, run following command to see the list of potential commits:

git log 1aa3372..dee0065

@nicolasstucki @smarter I can see that most of potential commits that could introduce that regression was commit by you. Can you please take a look at that code snippet?

@WojciechMazur
Copy link
Contributor Author

The reproducer for scalikejdbc/scalikejdbc, remaining projects are probably related to same issue

// macro.scala
import scala.quoted.*

object SelectDynamicMacroImpl {
  def selectImpl[E: Type](
    ref: Expr[SQLSyntaxProvider[_]],
    name: Expr[String]
  )(using Quotes): Expr[SQLSyntax] = '{SQLSyntax("foo")}
}
// test.scala
import scala.language.dynamics

trait SQLSyntaxProvider[A] extends Dynamic{
  def field(name: String): SQLSyntax = ???
 
  inline def selectDynamic(inline name: String): SQLSyntax = 
  select[A](this, name)

  inline def select[E](ref: SQLSyntaxProvider[A], inline name: String): SQLSyntax = 
    ${ SelectDynamicMacroImpl.selectImpl[E]('ref, 'name) }
}

class SQLSyntax(value: String)
trait SQLSyntaxSupport[A]
case class ColumnSQLSyntaxProvider[S <: SQLSyntaxSupport[A], A](support: S) extends SQLSyntaxProvider[A]

case class Account(id: Long, name: String)
object Account extends SQLSyntaxSupport[Account]

def Test() =
  val p = ColumnSQLSyntaxProvider[Account.type, Account](Account)
  assert(p.name == SQLSyntax("name"))

Output

Compiling project (Scala 3.3.1-RC1-bin-20230510-d6c643c-NIGHTLY, JVM)
[error] ./test.scala:22:10
[error] Found:    (SQLSyntaxProvider_this : (p : ColumnSQLSyntaxProvider[Account.type, Account]))
[error] Required: SQLSyntaxProvider[A]
[error]   assert(p.name == SQLSyntax("name"))
[error]       

Both reproducers have in common usage of quotes and started to fail in the same commit range

@WojciechMazur WojciechMazur added area:metaprogramming:quotes Issues related to quotes and splices and removed stat:needs minimization Needs a self contained minimization labels May 11, 2023
@WojciechMazur
Copy link
Contributor Author

Bisect points to 5ae7861

@nicolasstucki
Copy link
Contributor

Minimized

import scala.quoted.*
def impl[E: Type](ref: Expr[Foo[_]])(using Quotes): Expr[Unit] = '{ }
trait Foo[A]:
  inline def foo(): Unit = bar[this.type](this)
  inline def bar[E](ref: Foo[A]): Unit = ${ impl[E]('ref) }
def test(p: Foo[Int]) = p.foo()

@nicolasstucki
Copy link
Contributor

Other minimization

import scala.quoted.*
inline def foo[T](expr: T => Any): Unit = ${impl('expr)}
def impl(expr: Expr[Any])(using Quotes): Expr[Unit] = '{}
def test(f: Int => Any) = foo(f)

nicolasstucki added a commit to dotty-staging/dotty that referenced this issue May 19, 2023
nicolasstucki added a commit to dotty-staging/dotty that referenced this issue May 19, 2023
nicolasstucki added a commit to dotty-staging/dotty that referenced this issue May 19, 2023
Before 5ae7861 we used to find those
types withing the `tpt`.

Fixes scala#17434
nicolasstucki added a commit to dotty-staging/dotty that referenced this issue May 19, 2023
Before 5ae7861 we used to find those
types withing the `tpt`.

Fixes scala#17434
nicolasstucki added a commit that referenced this issue May 22, 2023
Before 5ae7861 we used to find those
types withing the `tpt`.

Fixes #17434

#### Other CI tests

| Project | Version | Build URL | Notes |
| ------- | ------- | --------- | ----- |
| dwickern/scala-nameof | 4.0.0 | [Open CB
logs](https://github.com/VirtusLab/community-build3/actions/runs/5025314096)
| passed |
| scalikejdbc/scalikejdbc | 4.0.0 | [Open CB
logs](https://github.com/VirtusLab/community-build3/actions/runs/5025351221)
| passed |
| scalikejdbc/scalikejdbc-async | 0.18.0 | [Open CB
logs](https://github.com/VirtusLab/community-build3/actions/runs/5025363289)
| passed |
| bitlap/scalikejdbc-helper | 0.2.3 -> 0.2.6 | [Open CB
logs](https://github.com/VirtusLab/community-build3/actions/runs/5025363289)
([old
failure](https://github.com/VirtusLab/community-build3/actions/runs/4897105783/jobs/8746001478)
| failed with other issue |
@Kordyjan Kordyjan added this to the 3.3.1 milestone Aug 2, 2023
G1ng3r pushed a commit to G1ng3r/dotty that referenced this issue Sep 10, 2023
Before 5ae7861 we used to find those
types withing the `tpt`.

Fixes scala#17434
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area:metaprogramming:quotes Issues related to quotes and splices 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.

3 participants