-
Notifications
You must be signed in to change notification settings - Fork 1.1k
[Experiment] Do not widen singletons in hard unions #14347
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 all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1266,9 +1266,10 @@ object Types { | |
/** Widen this type and if the result contains embedded soft union types, replace | ||
* them by their joins. | ||
* "Embedded" means: inside type lambdas, intersections or recursive types, | ||
* in prefixes of refined types, or in hard union types. | ||
* or in prefixes of refined types. Softs unions inside hard unions are | ||
* left untouched. | ||
* If an embedded soft union is found, we first try to simplify or eliminate it by | ||
* re-lubbing it while allowing type parameters to be constrained further. | ||
* re-lubing it while allowing type parameters to be constrained further. | ||
* Any remaining union types are replaced by their joins. | ||
* | ||
* For instance, if `A` is an unconstrained type variable, then | ||
|
@@ -1303,8 +1304,8 @@ object Types { | |
case tp => | ||
tp | ||
|
||
/** Widen all top-level singletons reachable by dealiasing | ||
* and going to the operands of & and |. | ||
/** Widen all top-level singletons reachable by dealiasing and going to the | ||
* operands of intersections and soft unions. | ||
* Overridden and cached in OrType. | ||
*/ | ||
def widenSingletons(using Context): Type = dealias match { | ||
|
@@ -3266,7 +3267,7 @@ object Types { | |
TypeComparer.lub(tp1.widenUnionWithoutNull, tp2.widenUnionWithoutNull, canConstrain = true) match | ||
case union: OrType => union.join | ||
case res => res | ||
else derivedOrType(tp1.widenUnionWithoutNull, tp2.widenUnionWithoutNull) | ||
else this | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Might be worth flipping the order with your change: if isSoft then
if myUnionPeriod != ctx.period then
...
myUnion
else this |
||
if !isProvisional then myUnionPeriod = ctx.period | ||
myUnion | ||
|
||
|
@@ -3280,11 +3281,16 @@ object Types { | |
if tp1.hasClassSymbol(defn.NothingClass) then tp2.atoms | ||
else if tp2.hasClassSymbol(defn.NothingClass) then tp1.atoms | ||
else tp1.atoms | tp2.atoms | ||
val tp1w = tp1.widenSingletons | ||
val tp2w = tp2.widenSingletons | ||
myWidened = if ((tp1 eq tp1w) && (tp2 eq tp2w)) this else tp1w | tp2w | ||
atomsRunId = ctx.runId | ||
|
||
myWidened = | ||
if isSoft then | ||
val tp1w = tp1.widenSingletons | ||
val tp2w = tp2.widenSingletons | ||
if ((tp1 eq tp1w) && (tp2 eq tp2w)) this else tp1w | tp2w | ||
else | ||
this | ||
mbovel marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
override def atoms(using Context): Atoms = | ||
ensureAtomsComputed() | ||
myAtoms | ||
|
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 |
---|---|---|
|
@@ -13,12 +13,21 @@ object Test2: | |
|| xs.corresponds(ys)(consistent(_, _)) // error, found: Any, required: Int | String | ||
|
||
object Test3: | ||
|
||
def g[X](x: X | String): Int = ??? | ||
def y: Boolean | String = ??? | ||
g[Boolean](y) | ||
g(y) | ||
g[Boolean](identity(y)) | ||
g(identity(y)) | ||
|
||
object Test4: | ||
def f(a: 2 | 3) = a | ||
|
||
def test() = | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider adding a test where the union is the result type of a |
||
val x: 2 | 3 = 2 | ||
val y = x | ||
f(y) | ||
|
||
def g: 2 | 3 = 2 | ||
val z = g | ||
f(z) |
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.