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

Cannot reference member of refined type in macro #18721

Closed
WojciechMazur opened this issue Oct 18, 2023 · 2 comments · Fixed by #18731
Closed

Cannot reference member of refined type in macro #18721

WojciechMazur opened this issue Oct 18, 2023 · 2 comments · Fixed by #18731
Assignees
Labels
area:metaprogramming:quotes Issues related to quotes and splices area:typer itype:bug regression This worked in a previous version but doesn't anymore

Comments

@WojciechMazur
Copy link
Contributor

WojciechMazur commented Oct 18, 2023

Based on OpenCB failure in augustnagro/magnum, see build logs here

Compiler version

3.4.0-RC1-bin-20231006-b1ccc6a-NIGHTLY
Bisect points to #18527

Works in 3.3.1

Minimized code

// core.scala
import scala.quoted.*

trait SqlLiteral:
  def queryRepr: String
   
class ColumnName(
    val name: String,
    val queryRepr: String
) extends SqlLiteral

class TableInfo[EC] extends Selectable {
  def selectDynamic(scalaName: String): ColumnName = ???
}

extension (inline sc: StringContext)
  inline def sql(inline args: Any*): Any =
    ${ sqlImpl('{ sc }, '{ args }) }


private def sqlImpl(sc: Expr[StringContext], args: Expr[Seq[Any]])(using
    Quotes
): Expr[Any] =
  import quotes.reflect.*
  val argsExprs: Seq[Expr[Any]] = args match
    case Varargs(ae) => ae

  val interpolatedVarargs = Varargs(argsExprs.map {
    case '{ $arg: SqlLiteral } => '{ $arg.queryRepr }
    case '{ $arg: tp } => ???
  })
  interpolatedVarargs
  
end sqlImpl
// main.scala
case class Foo(id: Int)

@main def Test = {
  val foo: TableInfo[Foo]{ val id: ColumnName } = ???
  sql"${foo.id}"
}

Output

  exception while retyping foo.selectDynamic("id").$asInstanceOf[(foo.id : ColumnName)].queryRepr of class Inlined # -1

  An unhandled exception was thrown in the compiler.

Stacktrace (click arrow to expand)

Exception in thread "main" java.lang.AssertionError: assertion failed: TermRef(TermRef(NoPrefix,val foo),id)
        at scala.runtime.Scala3RunTime$.assertFailed(Scala3RunTime.scala:8)
        at dotty.tools.dotc.core.TypeErasure$.erasedRef(TypeErasure.scala:243)
        at dotty.tools.dotc.core.TypeErasure$.erasedRef(TypeErasure.scala:246)
        at dotty.tools.dotc.transform.Erasure$Typer.erasedType(Erasure.scala:606)
        at dotty.tools.dotc.transform.Erasure$Typer.promote(Erasure.scala:611)
        at dotty.tools.dotc.typer.ReTyper.typedInlined(ReTyper.scala:100)
        at dotty.tools.dotc.transform.Erasure$Typer.typedInlined(Erasure.scala:899)
        at dotty.tools.dotc.typer.Typer.typedUnnamed$1(Typer.scala:3134)
        at dotty.tools.dotc.typer.Typer.typedUnadapted(Typer.scala:3179)
        at dotty.tools.dotc.typer.ReTyper.typedUnadapted(ReTyper.scala:174)
        at dotty.tools.dotc.typer.Typer.typed(Typer.scala:3255)
        at dotty.tools.dotc.typer.Typer.typed(Typer.scala:3252)
        at dotty.tools.dotc.typer.Typer.typed(Typer.scala:3259)
        at dotty.tools.dotc.typer.Typer.$anonfun$45(Typer.scala:2083)
        at dotty.tools.dotc.core.Decorators$.loop$1(Decorators.scala:94)
        at dotty.tools.dotc.core.Decorators$.mapconserve(Decorators.scala:110)
        at dotty.tools.dotc.typer.Typer.typedSeqLiteral(Typer.scala:2083)
        at dotty.tools.dotc.transform.Erasure$Typer.typedSeqLiteral(Erasure.scala:880)
        at dotty.tools.dotc.typer.Typer.typedUnnamed$1(Typer.scala:3133)
        at dotty.tools.dotc.typer.Typer.typedUnadapted(Typer.scala:3179)
        at dotty.tools.dotc.typer.ReTyper.typedUnadapted(ReTyper.scala:174)
        at dotty.tools.dotc.typer.Typer.typed(Typer.scala:3255)
        at dotty.tools.dotc.typer.Typer.typed(Typer.scala:3259)
        at dotty.tools.dotc.typer.Typer.typedExpr(Typer.scala:3371)
        at dotty.tools.dotc.transform.Erasure$Typer.$anonfun$7(Erasure.scala:851)
        at dotty.tools.dotc.core.Decorators$.zipWithConserve(Decorators.scala:155)
        at dotty.tools.dotc.transform.Erasure$Typer.typedApply(Erasure.scala:851)
        at dotty.tools.dotc.typer.Typer.typedUnnamed$1(Typer.scala:3111)
        at dotty.tools.dotc.typer.Typer.typedUnadapted(Typer.scala:3179)
        at dotty.tools.dotc.typer.ReTyper.typedUnadapted(ReTyper.scala:174)
        at dotty.tools.dotc.typer.Typer.typed(Typer.scala:3255)
        at dotty.tools.dotc.typer.Typer.typed(Typer.scala:3252)
        at dotty.tools.dotc.typer.Typer.typed(Typer.scala:3259)
        at dotty.tools.dotc.transform.Erasure$Typer.typedTyped(Erasure.scala:633)
        at dotty.tools.dotc.typer.Typer.typedUnnamed$1(Typer.scala:3116)
        at dotty.tools.dotc.typer.Typer.typedUnadapted(Typer.scala:3179)
        at dotty.tools.dotc.typer.ReTyper.typedUnadapted(ReTyper.scala:174)
        at dotty.tools.dotc.typer.Typer.typed(Typer.scala:3255)
        at dotty.tools.dotc.typer.Typer.typed(Typer.scala:3252)
        at dotty.tools.dotc.typer.Typer.typed(Typer.scala:3259)
        at dotty.tools.dotc.typer.ReTyper.typedInlined(ReTyper.scala:100)
        at dotty.tools.dotc.transform.Erasure$Typer.typedInlined(Erasure.scala:899)
        at dotty.tools.dotc.typer.Typer.typedUnnamed$1(Typer.scala:3134)
        at dotty.tools.dotc.typer.Typer.typedUnadapted(Typer.scala:3179)
        at dotty.tools.dotc.typer.ReTyper.typedUnadapted(ReTyper.scala:174)
        at dotty.tools.dotc.typer.Typer.typed(Typer.scala:3255)
        at dotty.tools.dotc.typer.Typer.typed(Typer.scala:3259)
        at dotty.tools.dotc.typer.Typer.typedExpr(Typer.scala:3371)
        at dotty.tools.dotc.typer.Typer.typedBlock(Typer.scala:1176)
        at dotty.tools.dotc.typer.Typer.typedUnnamed$1(Typer.scala:3119)
        at dotty.tools.dotc.typer.Typer.typedUnadapted(Typer.scala:3179)
        at dotty.tools.dotc.typer.ReTyper.typedUnadapted(ReTyper.scala:174)
        at dotty.tools.dotc.typer.Typer.typed(Typer.scala:3255)
        at dotty.tools.dotc.typer.Typer.typed(Typer.scala:3259)
        at dotty.tools.dotc.typer.Typer.typedExpr(Typer.scala:3371)
        at dotty.tools.dotc.typer.Typer.$anonfun$62(Typer.scala:2551)
        at dotty.tools.dotc.inlines.PrepareInlineable$.dropInlineIfError(PrepareInlineable.scala:243)
        at dotty.tools.dotc.typer.Typer.typedDefDef(Typer.scala:2551)
        at dotty.tools.dotc.transform.Erasure$Typer.typedDefDef(Erasure.scala:955)
        at dotty.tools.dotc.typer.Typer.typedNamed$1(Typer.scala:3087)
        at dotty.tools.dotc.typer.Typer.typedUnadapted(Typer.scala:3178)
        at dotty.tools.dotc.typer.ReTyper.typedUnadapted(ReTyper.scala:174)
        at dotty.tools.dotc.typer.Typer.typed(Typer.scala:3255)
        at dotty.tools.dotc.typer.Typer.typed(Typer.scala:3259)
        at dotty.tools.dotc.typer.Typer.traverse$1(Typer.scala:3281)
        at dotty.tools.dotc.typer.Typer.typedStats(Typer.scala:3327)
        at dotty.tools.dotc.transform.Erasure$Typer.typedStats(Erasure.scala:1054)
        at dotty.tools.dotc.typer.Typer.typedClassDef(Typer.scala:2738)
        at dotty.tools.dotc.transform.Erasure$Typer.typedClassDef(Erasure.scala:1043)
        at dotty.tools.dotc.typer.Typer.typedTypeOrClassDef$1(Typer.scala:3099)
        at dotty.tools.dotc.typer.Typer.typedNamed$1(Typer.scala:3103)
        at dotty.tools.dotc.typer.Typer.typedUnadapted(Typer.scala:3178)
        at dotty.tools.dotc.typer.ReTyper.typedUnadapted(ReTyper.scala:174)
        at dotty.tools.dotc.typer.Typer.typed(Typer.scala:3255)
        at dotty.tools.dotc.typer.Typer.typed(Typer.scala:3259)
        at dotty.tools.dotc.typer.Typer.traverse$1(Typer.scala:3281)
        at dotty.tools.dotc.typer.Typer.typedStats(Typer.scala:3327)
        at dotty.tools.dotc.transform.Erasure$Typer.typedStats(Erasure.scala:1054)
        at dotty.tools.dotc.typer.Typer.typedPackageDef(Typer.scala:2873)
        at dotty.tools.dotc.typer.Typer.typedUnnamed$1(Typer.scala:3145)
        at dotty.tools.dotc.typer.Typer.typedUnadapted(Typer.scala:3179)
        at dotty.tools.dotc.typer.ReTyper.typedUnadapted(ReTyper.scala:174)
        at dotty.tools.dotc.typer.Typer.typed(Typer.scala:3255)
        at dotty.tools.dotc.typer.Typer.typed(Typer.scala:3259)
        at dotty.tools.dotc.typer.Typer.typedExpr(Typer.scala:3371)
        at dotty.tools.dotc.transform.Erasure.run(Erasure.scala:144)
        at dotty.tools.dotc.core.Phases$Phase.runOn$$anonfun$1(Phases.scala:327)
        at scala.collection.immutable.List.map(List.scala:246)
        at dotty.tools.dotc.core.Phases$Phase.runOn(Phases.scala:331)
        at dotty.tools.dotc.Run.runPhases$1$$anonfun$1(Run.scala:246)
        at scala.runtime.function.JProcedure1.apply(JProcedure1.java:15)
        at scala.runtime.function.JProcedure1.apply(JProcedure1.java:10)
        at scala.collection.ArrayOps$.foreach$extension(ArrayOps.scala:1321)
        at dotty.tools.dotc.Run.runPhases$1(Run.scala:262)
        at dotty.tools.dotc.Run.compileUnits$$anonfun$1(Run.scala:270)
        at dotty.tools.dotc.Run.compileUnits$$anonfun$adapted$1(Run.scala:279)
        at dotty.tools.dotc.util.Stats$.maybeMonitored(Stats.scala:71)
        at dotty.tools.dotc.Run.compileUnits(Run.scala:279)
        at dotty.tools.dotc.Run.compileUnits(Run.scala:200)
        at dotty.tools.dotc.Driver.finish(Driver.scala:58)
        at dotty.tools.dotc.Driver.doCompile(Driver.scala:38)
        at dotty.tools.dotc.Driver.process(Driver.scala:197)
        at dotty.tools.dotc.Driver.process(Driver.scala:165)
        at dotty.tools.dotc.Driver.process(Driver.scala:177)
        at dotty.tools.dotc.Driver.main(Driver.scala:207)
        at dotty.tools.dotc.Main.main(Main.scala)

Expectation

Should allow to reference queryRepr on instance of SQLLiteral

@WojciechMazur WojciechMazur added itype:bug area:typer regression This worked in a previous version but doesn't anymore area:metaprogramming:quotes Issues related to quotes and splices labels Oct 18, 2023
@nicolasstucki
Copy link
Contributor

nicolasstucki commented Oct 19, 2023

Reverting this line fixes the issue: https://github.com/lampepfl/dotty/pull/18527/files#diff-2c37ce7afebb125dbeebfd9b2c61f4d042881d0369afea05142ed558be637ccbL245-R245

But this makes the #18527 stack overflow. Maybe we need to widen more than widenExpr but less than widen. @dwijnand what were you trying to avoid there?

@dwijnand
Copy link
Member

I'll look at this one soon.

@dwijnand dwijnand linked a pull request Oct 20, 2023 that will close this issue
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 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.

3 participants