-
Notifications
You must be signed in to change notification settings - Fork 1.1k
[Deprecated] exhaustivity & redundancy check for pattern matching #1261
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
Closed
Changes from 1 commit
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
5cdd42f
initial works
liufengyun 9a0952c
support check java enum from classfile and source file
liufengyun fd1b286
fix false warning about Nil
liufengyun d681a78
suppress checking of for, try and partial functions
liufengyun da9511d
add GADT test
liufengyun 89e8ff9
refine patmat check for type erasure
liufengyun b25e952
add tuple test to patmat check
liufengyun e2db834
add test t5440 for patmat check
liufengyun 2d6e803
better warning for higher-kinded types and alias types
liufengyun f69b305
fix SI-9657 about type bounds
liufengyun c79181d
address counter-example of two unrelated traits
liufengyun File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
fix SI-9657 about type bounds
- Loading branch information
commit f69b305e56cac0836baaed88982660fe40070aed
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,10 +14,14 @@ import core.StdNames._ | |
| import core.NameOps._ | ||
| import core.Constants._ | ||
|
|
||
| /** Space logic for checking exhaustivity and unreachability of pattern matching. | ||
| /** Space logic for checking exhaustivity and unreachability of pattern matching | ||
| * | ||
| * The core idea of the algorithm is that patterns and types are value | ||
| * spaces, which is recursively defined as follows: | ||
| * Space can be thought of as a set of possible values. A type or a pattern | ||
| * both refer to spaces. The space of a type is the values that inhabit the | ||
| * type. The space of a pattern is the values that can be covered by the | ||
| * pattern. | ||
| * | ||
| * Space is recursively defined as follows: | ||
| * | ||
| * 1. `Empty` is a space | ||
| * 2. For a type T, `Typ(T)` is a space | ||
|
|
@@ -279,7 +283,11 @@ class SpaceEngine(implicit ctx: Context) extends SpaceLogic { | |
| } | ||
|
|
||
| /** Is `tp1` a subtype of `tp2`? */ | ||
| def isSubType(tp1: Type, tp2: Type): Boolean = tp1 <:< tp2 | ||
| def isSubType(tp1: Type, tp2: Type): Boolean = { | ||
| // expose is important due to type bounds | ||
| // check SI-9657 and tests/patmat/gadt.scala | ||
| tp1 <:< expose(tp2) | ||
| } | ||
|
|
||
| def isEqualType(tp1: Type, tp2: Type): Boolean = tp1 =:= tp2 | ||
|
|
||
|
|
@@ -472,6 +480,59 @@ class SpaceEngine(implicit ctx: Context) extends SpaceLogic { | |
| isCheckable(sel.tpe.widen.elimAnonymousClass) | ||
| } | ||
|
|
||
|
|
||
| /** Expose refined type to eliminate reference to type variables | ||
| * | ||
| * A = B M { type T = A } ~~> M { type T = B } | ||
| * | ||
| * A <: X :> Y M { type T = A } ~~> M { type T <: X :> Y } | ||
| * | ||
| * A <: X :> Y B <: U :> V M { type T <: A :> B } ~~> M { type T <: X :> B } | ||
|
||
| * | ||
| * A = X B = Y M { type T <: A :> B } ~~> M { type T <: X :> Y } | ||
| */ | ||
| def expose(tp: Type): Type = { | ||
| def follow(tp: Type, up: Boolean): Type = tp match { | ||
| case tp: TypeProxy => | ||
| tp.underlying match { | ||
| case TypeBounds(lo, hi) => | ||
| follow(if (up) hi else lo, up) | ||
| case _ => | ||
| tp | ||
| } | ||
| case OrType(tp1, tp2) => | ||
| OrType(follow(tp1, up), follow(tp2, up)) | ||
| case AndType(tp1, tp2) => | ||
| AndType(follow(tp1, up), follow(tp2, up)) | ||
| } | ||
|
|
||
| tp match { | ||
| case tp: RefinedType => | ||
| tp.refinedInfo match { | ||
| case tpa : TypeAlias => | ||
| val hi = follow(tpa.alias, true) | ||
| val lo = follow(tpa.alias, false) | ||
| val refined = if (hi =:= lo) | ||
| tpa.derivedTypeAlias(hi) | ||
| else | ||
| tpa.derivedTypeBounds(lo, hi) | ||
|
|
||
| tp.derivedRefinedType( | ||
| expose(tp.parent), | ||
| tp.refinedName, | ||
| refined | ||
| ) | ||
| case tpb @ TypeBounds(lo, hi) => | ||
| tp.derivedRefinedType( | ||
| expose(tp.parent), | ||
| tp.refinedName, | ||
| tpb.derivedTypeBounds(follow(lo, false), follow(hi, true)) | ||
| ) | ||
| } | ||
| case _ => tp | ||
| } | ||
| } | ||
|
|
||
| def checkExhaustivity(_match: Match): Unit = { | ||
| val Match(sel, cases) = _match | ||
| val selTyp = sel.tpe.widen.elimAnonymousClass.dealias | ||
|
|
@@ -491,7 +552,8 @@ class SpaceEngine(implicit ctx: Context) extends SpaceLogic { | |
|
|
||
| def checkRedundancy(_match: Match): Unit = { | ||
| val Match(sel, cases) = _match | ||
| val selTyp = sel.tpe.widen.elimAnonymousClass | ||
| // ignore selector type for now | ||
| // val selTyp = sel.tpe.widen.elimAnonymousClass.dealias | ||
|
|
||
| // starts from the second, the first can't be redundant | ||
| (1 until cases.length).foreach { i => | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| ./tests/patmat/t9657.scala:29: warning: match may not be exhaustive. | ||
| It would fail on the following input: Bus(_) | ||
| def refuel2[P <: Petrol.type](vehicle: Vehicle {type A = P} ): Vehicle = vehicle match { | ||
| ^ | ||
| ./tests/patmat/t9657.scala:38: warning: match may not be exhaustive. | ||
| It would fail on the following input: Bus(_) | ||
| def foo2(vehicle: Vehicle {type A <: Petrol.type} ): Vehicle = vehicle match { | ||
| ^ | ||
| ./tests/patmat/t9657.scala:49: warning: match may not be exhaustive. | ||
| It would fail on the following input: Bus(_) | ||
| def bar2(vehicle: Vehicle {type A <: P} ): Vehicle = vehicle match { | ||
| ^ | ||
| ./tests/patmat/t9657.scala:58: warning: match may not be exhaustive. | ||
| It would fail on the following input: Bus(_) | ||
| def qux2[P <: Petrol.type](vehicle: Vehicle {type A <: P} ): Vehicle = vehicle match { | ||
| ^ | ||
| four warnings found |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| sealed trait PowerSource | ||
|
|
||
| case object Petrol extends PowerSource | ||
|
|
||
| case object Pedal extends PowerSource | ||
|
|
||
| sealed abstract class Vehicle { | ||
| type A <: PowerSource | ||
| } | ||
|
|
||
| case object Bicycle extends Vehicle { | ||
| type A = Pedal.type | ||
| } | ||
|
|
||
| case class Bus(fuel: Int) extends Vehicle { | ||
| type A = Petrol.type | ||
| } | ||
|
|
||
| case class Car(fuel: Int) extends Vehicle { | ||
| type A = Petrol.type | ||
| } | ||
|
|
||
| class Test { | ||
| def refuel[P <: Petrol.type](vehicle: Vehicle {type A = P} ): Vehicle = vehicle match { | ||
| case Car(_) => Car(100) | ||
| case Bus(_) => Bus(100) | ||
| } | ||
|
|
||
| def refuel2[P <: Petrol.type](vehicle: Vehicle {type A = P} ): Vehicle = vehicle match { | ||
| case Car(_) => Car(100) | ||
| } | ||
|
|
||
| def foo1(vehicle: Vehicle {type A <: Petrol.type} ): Vehicle = vehicle match { | ||
| case Car(_) => Car(100) | ||
| case Bus(_) => Bus(100) | ||
| } | ||
|
|
||
| def foo2(vehicle: Vehicle {type A <: Petrol.type} ): Vehicle = vehicle match { | ||
| case Car(_) => Car(100) | ||
| } | ||
|
|
||
| type P = Petrol.type | ||
|
|
||
| def bar1(vehicle: Vehicle {type A <: P} ): Vehicle = vehicle match { | ||
| case Car(_) => Car(100) | ||
| case Bus(_) => Bus(100) | ||
| } | ||
|
|
||
| def bar2(vehicle: Vehicle {type A <: P} ): Vehicle = vehicle match { | ||
| case Car(_) => Car(100) | ||
| } | ||
|
|
||
| def qux1[P <: Petrol.type](vehicle: Vehicle {type A <: P} ): Vehicle = vehicle match { | ||
| case Car(_) => Car(100) | ||
| case Bus(_) => Bus(100) | ||
| } | ||
|
|
||
| def qux2[P <: Petrol.type](vehicle: Vehicle {type A <: P} ): Vehicle = vehicle match { | ||
| case Car(_) => Car(100) | ||
| } | ||
|
|
||
| } |
This file was deleted.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this seems like a hack to me. You are trying to mimic erazed semantics of instanceOf checks by non-erazed types.
If you'll go full-erasure you shouldn't have this problem.
What I'm proposing is more-or-less
tp1.typeSymbol.derivesFrom(tp2.typeSymbol), but you'll need to additionally take OrTypes into account.I believe you can handle Or Types on a
Spacelevel by hadling inprojectthe caseproject(tree with type (A | B)) == Space(A.erasure) union Space(A.erasure)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems here we can't just reuse the existing type erasure by splitting or-types to or-spaces, e.g.
M[Int | Boolean]may not necessarily the same asM[Int] | M[Boolean].