-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Precise apply for enum companion objects #9728
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 4 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
4ec5446
fix #3935: widen inferred enum types, precise factory method
bishabosha db22123
add version of Nat to int using enums
bishabosha 3110a00
separate widenEnumClass logic to own method
bishabosha a89998e
preserve module and enumValue in widenTermRefExpr
bishabosha 4b9de0d
explicit bounds for dispatch on enums
bishabosha 3139176
update scodec
bishabosha e25f912
deeply embed enum values in union types
bishabosha 19fc8fe
handle modules like enum values
bishabosha a71dac9
add macro test, preserve singleton types
bishabosha e78a6e4
fix #6781: add regression test
bishabosha 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
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
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
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,10 @@ | ||
enum Foo3[T](x: T) { | ||
case Bar[S, T](y: T) extends Foo3[y.type](y) | ||
} | ||
|
||
val foo: Foo3.Bar[Nothing, 3] = Foo3.Bar(3) | ||
val bar = foo | ||
|
||
def baz[T](f: Foo3[T]): f.type = f | ||
|
||
val qux = baz(bar) // existentials are back in Dotty? |
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 |
---|---|---|
|
@@ -11,5 +11,9 @@ true | |
|
||
true | ||
|
||
true | ||
|
||
true | ||
|
||
false | ||
|
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,34 @@ | ||
import Nat._ | ||
import compiletime._ | ||
|
||
enum Nat: | ||
case Zero | ||
case Succ[N <: Nat](n: N) | ||
|
||
inline def toIntTypeLevel[N <: Nat]: Int = inline erasedValue[N] match | ||
case _: Zero.type => 0 | ||
case _: Succ[n] => toIntTypeLevel[n] + 1 | ||
|
||
inline def toInt(inline nat: Nat): Int = inline nat match | ||
case nat: Zero.type => 0 | ||
case nat: Succ[n] => toInt(nat.n) + 1 | ||
|
||
inline def toIntUnapply(inline nat: Nat): Int = inline nat match | ||
case Zero => 0 | ||
case Succ(n) => toIntUnapply(n) + 1 | ||
|
||
inline def toIntTypeTailRec[N <: Nat, Acc <: Int]: Int = inline erasedValue[N] match | ||
case _: Zero.type => constValue[Acc] | ||
case _: Succ[n] => toIntTypeTailRec[n, S[Acc]] | ||
|
||
inline def toIntErased[N <: Nat](inline nat: N): Int = toIntTypeTailRec[N, 0] | ||
|
||
@main def Test: Unit = | ||
println("erased value:") | ||
assert(toIntTypeLevel[Succ[Succ[Succ[Zero.type]]]] == 3) | ||
println("type test:") | ||
assert(toInt(Succ(Succ(Succ(Zero)))) == 3) | ||
println("unapply:") | ||
assert(toIntUnapply(Succ(Succ(Succ(Zero)))) == 3) | ||
println("infer erased:") | ||
assert(toIntErased(Succ(Succ(Succ(Zero)))) == 3) |
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,31 @@ | ||
enum NonEmptyList[+T]: | ||
case Many[+U](head: U, tail: NonEmptyList[U]) extends NonEmptyList[U] | ||
case One [+U](value: U) extends NonEmptyList[U] | ||
|
||
enum Ast: | ||
case Binding(name: String, tpe: String) | ||
case Lambda(args: NonEmptyList[Binding], rhs: Ast) // reference to another case of the enum | ||
case Ident(name: String) | ||
case Apply(fn: Ast, args: NonEmptyList[Ast]) | ||
|
||
import NonEmptyList._ | ||
import Ast._ | ||
|
||
// This example showcases the widening when inferring enum case types. | ||
// With scala 2 case class hierarchies, if One.apply(1) returns One[Int] and Many.apply(2, One(3)) returns Many[Int] | ||
// then the `foldRight` expression below would complain that Many[Binding] is not One[Binding]. With Scala 3 enums, | ||
// .apply on the companion returns the precise class, but type inference will widen to NonEmptyList[Binding] unless | ||
// the precise class is expected. | ||
def Bindings(arg: (String, String), args: (String, String)*): NonEmptyList[Binding] = | ||
def Bind(arg: (String, String)): Binding = | ||
val (name, tpe) = arg | ||
Binding(name, tpe) | ||
|
||
args.foldRight(One[Binding](Bind(arg)))((arg, acc) => Many(Bind(arg), acc)) | ||
|
||
@main def Test: Unit = | ||
val OneOfOne: One[1] = One[1](1) | ||
val True = Lambda(Bindings("x" -> "T", "y" -> "T"), Ident("x")) | ||
val Const = Lambda(One(Binding("x", "T")), Lambda(One(Binding("y", "U")), Ident("x"))) // precise type is forwarded | ||
|
||
assert(OneOfOne.value == 1) |
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.
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.
I think that's still not quite right: if the upper bound is an enum, a singleton should still be widened unless it's the type of an enum case.
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.
do you mean remove singletons except the term ref of a singleton enum case?
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.
yeah
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 to get a lot trickier when unions of singletons are involved
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.
new steps are
widenSingle
>widenOr
>[widenEnumCase]
>dropSuperTraits
, wherewidenOr
is intercepted so that singletons of module or enum value do not widen