Skip to content

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

Closed
@SrTobi

Description

@SrTobi
Contributor

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.

Activity

added
area:reportingError reporting including formatting, implicit suggestions, etc
better-errorsIssues concerned with improving confusing/unhelpful diagnostic messages
stat:needs triageEvery issue needs to have an "area" and "itype" label
on Apr 1, 2025
SrTobi

SrTobi commented on Apr 1, 2025

@SrTobi
ContributorAuthor

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)

mbovel

mbovel commented on Jun 9, 2025

@mbovel
Member

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.

jan-pieter

jan-pieter commented on Jun 9, 2025

@jan-pieter
Contributor
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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

      Participants

      @mbovel@jan-pieter@SrTobi@Gedochao

      Issue actions

        Unhelpful error message when using named patterns with custom selector type in extractor match · Issue #22903 · scala/scala3