Skip to content

Pattern binding using abstract type unapply gets wider type when @unchecked #14821

Closed
@griggt

Description

@griggt

Compiler version

3.1.2-RC3 and 3.1.3-RC1-bin-SNAPSHOT-git-616308d

Minimized code

trait Statement
trait Definition extends Statement

trait ClassDef extends Definition:
  def constructor: DefDef

object ClassDef:
  def copy(constr: DefDef): ClassDef = ???

// >>> This abstract implementation of DefDef causes a compilation error in transform...
type DefDef <: Definition
val DefDef: DefDefModule = ???
trait DefDefModule:
  def unapply(ddef: DefDef): (String, List[AnyRef])
// ...unless this given TypeTest is commented out, in which case we get only a type test warning
given scala.reflect.TypeTest[Statement, DefDef] = ???

// >>> This alternative works
// trait DefDef extends Definition
// object DefDef:
//   def unapply(ddef: DefDef): (String, List[AnyRef]) = ???

// >>> This alternative also works
// case class DefDef(name: String, paramss: List[AnyRef]) extends Definition

def transform(tree: Statement): Statement = tree match
  case tree: ClassDef =>
    val constructor @ DefDef(_, _) = transform(tree.constructor): @unchecked
    ClassDef.copy(constructor)

Output

-- [E007] Type Mismatch Error: test.scala:29:18 -----------------------------------------------
29 |    ClassDef.copy(constructor)
   |                  ^^^^^^^^^^^
   |                  Found:    (constructor : Statement)
   |                  Required: DefDef
   |
   | longer explanation available when compiling with `-explain`

Expectation

Without the @unchecked annotation, constructor is typed as DefDef, I expected the same here.
It compiles without the TypeTest given (but emits a type test warning instead).

Context: this arose while rebasing #14294 on latest main; @unchecked was added (as per advice) to

https://github.com/lampepfl/dotty/blob/616308d34912f1a42bee256bf77b85db3194c46b/library/src/scala/quoted/Quotes.scala#L4716

to suppress the refutable pattern warning, but instead caused the above error and a failure to bootstrap.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions