Skip to content

Unhelpful error message when using named patterns with custom selector type in extractor match #22903

Open
@SrTobi

Description

@SrTobi

Compiler version

3.7.1-RC1-bin-20250328-d519790-NIGHTLY

Minimized example

class CustomProduct(x: Int) extends Product {
  def _1 = someName
  def _2 = blub

  val someName = x + 5
  val blub = "blub"

  override def canEqual(that: Any): Boolean = ???
}

object ProductMatch {
  def unapply(x: Int): CustomProduct = new CustomProduct(x)
}

@main
def run = {
  3 match {
    case ProductMatch(someName = x) => println (x)
  }
}

Output Error/Warning message

No element named `someName` is defined in selector type CustomProduct
    case ProductMatch(someName = x) => println (x)

Why this Error/Warning was not helpful

The message was unhelpful because there is actually an element called someName on CustomProduct.

Suggested improvement

I think the message should be something more like

CustomProduct is not a valid selector type for the use with named patterns.
Only named tuples and case classes can be used with named patterns.

Metadata

Metadata

Assignees

No one assigned

    Labels

    area:pattern-matchingarea:reportingError reporting including formatting, implicit suggestions, etcbetter-errorsIssues concerned with improving confusing/unhelpful diagnostic messagesitype:enhancement

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions