Skip to content

Cannot reference member of refined type in macro #18721

Closed
@WojciechMazur

Description

@WojciechMazur

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

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions