Skip to content
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

Regression for pattern match over dependant sealed traits #21845

Closed
WojciechMazur opened this issue Oct 25, 2024 · 4 comments · Fixed by #21848
Closed

Regression for pattern match over dependant sealed traits #21845

WojciechMazur opened this issue Oct 25, 2024 · 4 comments · Fixed by #21848
Assignees
Labels
area:pattern-matching itype:bug regression This worked in a previous version but doesn't anymore
Milestone

Comments

@WojciechMazur
Copy link
Contributor

Based on compilation failure found in OpenCB for sbt/sbt for version 2.0.0-M2

Compiler version

Introduced in 3.4.0
Backported to 3.3.4

Last good release: 3.4.0-RC1-bin-20231020-89a8fce-NIGHTLY
First bad release: 3.4.0-RC1-bin-20231023-44a537b-NIGHTLY

Bisect points to 2be1b41 however these changes were not backported to LTS.
Bisect on LTS commits only failed to build compiler, but issue could have been introduced in any of: c1dfcef
f87a634

Minimized code

//> using options -Werror
trait Init[ScopeType]:
  sealed trait Initialize[A1]
  final class Bind[S, A1](val f: S => Initialize[A1], val in: Initialize[S])
    extends Initialize[A1]
  final class Value[A1](val value: () => A1) extends Initialize[A1]
  final class ValidationCapture[A1](val key: ScopedKey[A1], val selfRefOk: Boolean)
        extends Initialize[ScopedKey[A1]]
  final class TransformCapture(val f: [x] => Initialize[x] => Initialize[x])
        extends Initialize[[x] => Initialize[x] => Initialize[x]]
  final class Optional[S, A1](val a: Option[Initialize[S]], val f: Option[S] => A1)
        extends Initialize[A1]
  object StaticScopes extends Initialize[Set[ScopeType]]

  sealed trait Keyed[S, A1] extends Initialize[A1]
  trait KeyedInitialize[A1] extends Keyed[A1, A1]
  sealed case class ScopedKey[A](scope: ScopeType, key: AttributeKey[A]) extends KeyedInitialize[A]
  sealed trait AttributeKey[A]

abstract class EvaluateSettings[ScopeType]:
    protected val init: Init[ScopeType]
    import init._

    val transform: [A] => Initialize[A] => Unit = [A] =>
        (fa: Initialize[A]) =>
          fa match
            case k: Keyed[s, A]          => ???
            case b: Bind[s, A]           => ???
            case v: Value[A]             => ???
            case v: ValidationCapture[a] => ??? // unrearchable warning
            case t: TransformCapture     => ??? // unrearchable warning
            case o: Optional[s, A]       => ??? // unrearchable warning
            case StaticScopes            => ??? // unrearchable warning 
        

Output

-- [E029] Pattern Match Exhaustivity Warning: /Users/wmazur/projects/sandbox/test.scala:26:10 --------------------------------------------------------------------------------------------------------------
26 |          fa match
   |          ^^
   |          match may not be exhaustive.
   |
   |          It would fail on pattern case: _: (Init[<?>] & (EvaluateSettings#init : Init[EvaluateSettings#ScopeType]))#TransformCapture
   |
   | longer explanation available when compiling with `-explain`
-- [E030] Match case Unreachable Warning: /Users/wmazur/projects/sandbox/test.scala:30:17 ------------------------------------------------------------------------------------------------------------------
30 |            case v: ValidationCapture[a] => ??? // warn
   |                 ^^^^^^^^^^^^^^^^^^^^^^^
   |                 Unreachable case
-- [E030] Match case Unreachable Warning: /Users/wmazur/projects/sandbox/test.scala:31:17 ------------------------------------------------------------------------------------------------------------------
31 |            case t: TransformCapture     => ??? // warn
   |                 ^^^^^^^^^^^^^^^^^^^
   |                 Unreachable case
-- [E030] Match case Unreachable Warning: /Users/wmazur/projects/sandbox/test.scala:32:17 ------------------------------------------------------------------------------------------------------------------
32 |            case o: Optional[s, A]       => ??? // warn
   |                 ^^^^^^^^^^^^^^^^^
   |                 Unreachable case
-- [E030] Match case Unreachable Warning: /Users/wmazur/projects/sandbox/test.scala:33:17 ------------------------------------------------------------------------------------------------------------------
33 |            case StaticScopes            => ??? // warn
   |                 ^^^^^^^^^^^^
   |                 Unreachable case
No warnings can be incurred under -Werror (or -Xfatal-warnings)

Expectation

Should not warn about unreachable cases or being non-exhaustive

@WojciechMazur WojciechMazur added itype:bug area:pattern-matching regression This worked in a previous version but doesn't anymore labels Oct 25, 2024
@dwijnand dwijnand self-assigned this Oct 25, 2024
@ivan-klass
Copy link

@dwijnand A much simpler example that I think is very relevant

https://scastie.scala-lang.org/7iPISGKwTfOBxOKWIRIiaw

trait Figure

sealed trait Corners {
  self: Figure =>
}


enum Shape extends Figure:
  case Triange extends Shape with Corners
  case Square extends Shape with Corners
  case Circle extends Shape
  case Ellipsis extends Shape


def hasCorners(s: Shape): Boolean = s match
  case hasCorners: Corners => true // unreachable warning
  case _ => false

println(hasCorners(Shape.Circle))

@dwijnand
Copy link
Member

That seems to be a different issue.

@ivan-klass
Copy link

@dwijnand thanks, I'll create a new issue then

@ivan-klass
Copy link

@dwijnand thanks, I'll create a new issue then

Reported #21860

@WojciechMazur WojciechMazur added this to the 3.6.3 milestone Dec 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area:pattern-matching itype:bug regression This worked in a previous version but doesn't anymore
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants