scala3 icon indicating copy to clipboard operation
scala3 copied to clipboard

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

Open SrTobi opened this issue 8 months ago • 3 comments

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.

SrTobi avatar Apr 01 '25 16:04 SrTobi

I found this while implementing the logic in IntelliJ. Pls ping me if some underlying concept changes (like allowing named patterns on custom selector types)

SrTobi avatar Apr 01 '25 16:04 SrTobi

This issue was picked for today's Scala Issue Spree. @jan-pieter, @nmcb, @RoccoMathijn, Thijs and Wessel will be working on it! If you have any insight into the issue or guidance on how to fix it, please leave it here.

mbovel avatar Jun 09 '25 09:06 mbovel

@thijsnissen @wwbakker ^

jan-pieter avatar Jun 09 '25 10:06 jan-pieter