Skip to content

Quoted patterns regression: match with Underlying type fails #16522

Closed
@hmf

Description

@hmf

Compiler version

Compiler version 3.2.1

Minimized code

This is the code that worked when discussed in issue #10358. I am reporting it here so that it does not slip through the cracks (discussed in November 2020):

  import scala.quoted.*

  sealed trait HList
  case class HCons[+HD, TL <: HList](hd: HD, tl: TL) extends HList
  case object HNil extends HList

  def showFirstTwoImpl(e: Expr[HList])(using Quotes): Expr[String] = {
    e match {
      // Ok. No match 
      case '{HCons($h1, HCons($h2, $_))} => '{$h1.toString ++ $h2.toString}
      // Fails. Does not compile 
      case '{HCons($h1: hd1, HCons($h2: hd2, $_ : tl))} => '{$h1.toString ++ $h2.toString}
      // Ok
      case '{HCons[hd, HCons[sd, tl]]($h1, HCons($h2, $_))} => '{$h1.toString ++ $h2.toString}
      case _ => '{""}
    }
  }

  transparent inline def showFirstTwo(inline xs: HList) = ${ showFirstTwoImpl('xs) }  

Output

error] -- [E007] Type Mismatch Error: /home/hmf/VSCodeProjects/sploty/meta/src/data/Macros3.scala:1184:45 
[error] 1184 |      case '{HCons($h1: hd1, HCons($h2: hd2, $_ : tl))} => '{$h1.toString ++ $h2.toString}
[error]      |                                             ^^^^^^^
[error]      |                                      Found:    tl$given2.Underlying
[error]      |                                      Required: data.Macros3.HList
[error]      |--------------------------------------------------------------------------
[error]      | Explanation (enabled by `-explain`)
Compiler explanation:
error]      |
[error]      | Tree: ${_}:(tl$given2 @ _).Underlying
[error]      | I tried to show that
[error]      |   tl$given2.Underlying
[error]      | conforms to
[error]      |   TL
[error]      | but the comparison trace ended with `false`:
[error]      |
[error]      |   ==> tl$given2.Underlying  <:  TL
[error]      |     ==> tl$given2.Underlying  <:  TL
[error]      |       ==> tl  <:  TL
[error]      |         ==> tl  <:  TL in frozen constraint
[error]      |           ==> tl  <:  Nothing in frozen constraint
[error]      |             ==> Any  <:  Nothing (left is approximated) in frozen constraint
[error]      |             <== Any  <:  Nothing (left is approximated) in frozen constraint = false
[error]      |           <== tl  <:  Nothing in frozen constraint = false
[error]      |           ==> Any  <:  TL (left is approximated) in frozen constraint
[error]      |             ==> Any  <:  Nothing (left is approximated) in frozen constraint
[error]      |             <== Any  <:  Nothing (left is approximated) in frozen constraint = false
[error]      |           <== Any  <:  TL (left is approximated) in frozen constraint = false
[error]      |         <== tl  <:  TL in frozen constraint = false
[error]      |         ==> add constraint TL >: tl , constraint =  uninstantiated variables: TL, HD, TL, HD, TL, HD, TL, HD, TL  constrained types:    [HD, TL <: data.Macros3.HList](hd: HD, tl: TL): data.Macros3.HCons[HD, TL] , [HD, TL <: data.Macros3.HList](hd: HD, tl: TL): data.Macros3.HCons[HD, TL],    [HD, TL <: data.Macros3.HList](hd: HD, tl: TL): data.Macros3.HCons[HD, TL] , [HD, TL <: data.Macros3.HList](hd: HD, tl: TL): data.Macros3.HCons[HD, TL],    [HD, TL <: data.Macros3.HList](hd: HD, tl: TL): data.Macros3.HCons[HD, TL]  bounds:       HD := Any      TL >: data.Macros3.HCons[Any, data.Macros3.HList] <: data.Macros3.HList      HD      TL <: data.Macros3.HList      HD >: hd1      TL >: data.Macros3.HCons[HD, TL] <: data.Macros3.HList      HD      TL <: data.Macros3.HList      HD >: hd2      TL <: data.Macros3.HList  ordering: 
[error]      |           ==> lub(Nothing, tl, canConstrain=false, isSoft=true)
[error]      |           <== lub(Nothing, tl, canConstrain=false, isSoft=true) = tl
[error]      |           ==> tl  <:  data.Macros3.HList
[error]      |             ==> Any  <:  data.Macros3.HList (left is approximated)
[error]      |             <== Any  <:  data.Macros3.HList (left is approximated) = false
[error]      |           <== tl  <:  data.Macros3.HList = false
[error]      |         <== add constraint TL >: tl , constraint =  uninstantiated variables: TL, HD, TL, HD, TL, HD, TL, HD, TL  constrained types:    [HD, TL <: data.Macros3.HList](hd: HD, tl: TL): data.Macros3.HCons[HD, TL] , [HD, TL <: data.Macros3.HList](hd: HD, tl: TL): data.Macros3.HCons[HD, TL],    [HD, TL <: data.Macros3.HList](hd: HD, tl: TL): data.Macros3.HCons[HD, TL] , [HD, TL <: data.Macros3.HList](hd: HD, tl: TL): data.Macros3.HCons[HD, TL],    [HD, TL <: data.Macros3.HList](hd: HD, tl: TL): data.Macros3.HCons[HD, TL]  bounds:       HD := Any      TL >: data.Macros3.HCons[Any, data.Macros3.HList] <: data.Macros3.HList      HD      TL <: data.Macros3.HList      HD >: hd1      TL >: data.Macros3.HCons[HD, TL] <: data.Macros3.HList      HD      TL <: data.Macros3.HList      HD >: hd2      TL <: data.Macros3.HList  ordering:  = false
[error]      |       <== tl  <:  TL = false
[error]      |     <== tl$given2.Underlying  <:  TL = false
[error]      |   <== tl$given2.Underlying  <:  TL = false
[error]      |
[error]      | The tests were made under a constraint with:
[error]      |  uninstantiated variables: TL, HD, TL, HD, TL, HD, TL, HD, TL
[error]      |  constrained types: 
[error]      |   [HD, TL <: data.Macros3.HList](hd: HD, tl: TL): data.Macros3.HCons[HD, TL]
[error]      | , [HD, TL <: data.Macros3.HList](hd: HD, tl: TL): data.Macros3.HCons[HD, TL], 
[error]      |   [HD, TL <: data.Macros3.HList](hd: HD, tl: TL): data.Macros3.HCons[HD, TL]
[error]      | , [HD, TL <: data.Macros3.HList](hd: HD, tl: TL): data.Macros3.HCons[HD, TL], 
[error]      |   [HD, TL <: data.Macros3.HList](hd: HD, tl: TL): data.Macros3.HCons[HD, TL]
[error]      |  bounds: 
[error]      |      HD := Any
[error]      |      TL >: data.Macros3.HCons[Any, data.Macros3.HList] <: data.Macros3.HList
[error]      |      HD
[error]      |      TL <: data.Macros3.HList
[error]      |      HD >: hd1
[error]      |      TL >: data.Macros3.HCons[HD, TL] <: data.Macros3.HList
[error]      |      HD
[error]      |      TL <: data.Macros3.HList
[error]      |      HD >: hd2
[error]      |      TL <: data.Macros3.HList
[error]      |  ordering: 
[error]       --------------------------------------------------------------------------

Expectation

Expect the code in #10358 to keep working on the latest compiler.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions