Skip to content

Regression: "Unreachable case" warning in pattern match on StringContext #18601

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
sirthias opened this issue Sep 26, 2023 · 2 comments · Fixed by #18642
Closed

Regression: "Unreachable case" warning in pattern match on StringContext #18601

sirthias opened this issue Sep 26, 2023 · 2 comments · Fixed by #18642
Labels

Comments

@sirthias
Copy link
Contributor

Compiler version

3.3.1

Minimized code

extension (sc: StringContext)
  def m: StringContext = sc
  def unapply(string: String): Option[String] =
    val pattern = sc.parts.head
    if (string.length == pattern.length) Some(string) else None

def parse(x: PartialFunction[String, String]) = x

val pf = parse {
  case m"x$s" => s
  case m"xx$s" => s
}

pf.applyOrElse("a", _ => ".") // "a"
pf.applyOrElse("ab", _ => ".") // "ab"
pf.applyOrElse("abc", _ => ".") // "."

Output

-- [E030] Match case Unreachable Warning: --------------------------------------
17 |  case m"xx$s" => s
   |       ^^^^^^^
   |       Unreachable case

Expectation

No warning, as with compiler version 3.3.0

Notes

There is no regression (and no warning) here:

extension (sc: StringContext) def m: SC = SC(sc)

class SC(sc: StringContext):
  def unapply(string: String): Option[String] =
    val pattern = sc.parts.head
    if (string.length == pattern.length) Some(string) else None

def parse(x: PartialFunction[String, String]) = x

val pf = parse {
  case m"x$s" => s
  case m"xx$s" => s
}

pf.applyOrElse("a", _ => ".") // "a"
pf.applyOrElse("ab", _ => ".") // "ab"
pf.applyOrElse("abc", _ => ".") // "."
@sirthias sirthias added itype:bug stat:needs triage Every issue needs to have an "area" and "itype" label labels Sep 26, 2023
@mbovel mbovel added area:pattern-matching Spree Suitable for a future Spree and removed stat:needs triage Every issue needs to have an "area" and "itype" label labels Sep 26, 2023
@scala-center-bot
Copy link

This issue was picked for the Issue Spree No. 37 of 03 October 2023 which takes place in 7 days. @dwijnand, @KuceraMartin, @natsukagami, @yilinwei will be working on it. If you have any insight into the issue or guidance on how to fix it, please leave it here.

@dwijnand dwijnand linked a pull request Oct 3, 2023 that will close this issue
@sirthias
Copy link
Contributor Author

Awesome! Thank you, @dwijnand ! 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants