Skip to content

Support type references to types within quotes #7405

Closed
@nicolasstucki

Description

@nicolasstucki

minimized code

import scala.quoted._
class Foo {
  def f(given QuoteContext): Expr[Any] = {
    '{
      type X = Int // Level 1
      // val x: X = ???
      ${
        val t: Type[X] = '[X] // Level 0
        '{ /*val y: $t = x*/ } 
      }
    }
  }
}

fails with

9 |        val t: Type[X] = '[X] // Level 0
  |                    ^
  |        access to type X from wrong staging level:
  |         - the definition is at level 1,
  |         - but the access is at level 0.
  |        
  |         The access would be accepted with an implict scala.quoted.Type[X]

expectation

This code should work.

To be able to have references to type tags of types defined in a higher level we need to generalize the concept of type healing. In this case we instead of needing to keep a type tag for the next stage it should be possible to refer to types for a next level. This is already the case inside of top level splices in inline methods (macros) but it should be possible to do in any code.

To make this change we will need to:

  • Adapt the PCP check to not complain for types if the definition is in a higher level than the use site.
  • Adapt types that may not be in scope after ReifyQuotes. This could be in PCPChecks or ReifyQuotes.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions