-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Avoid inference getting stuck when the expected type contains a union/intersection #8635
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
Merged
Merged
Changes from all commits
Commits
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -484,9 +484,10 @@ trait ConstraintHandling[AbstractContext] { | |
* recording an isLess relationship instead (even though this is not implied | ||
* by the bound). | ||
* | ||
* Narrowing a constraint is better than widening it, because narrowing leads | ||
* to incompleteness (which we face anyway, see for instance eitherIsSubType) | ||
* but widening leads to unsoundness. | ||
* Normally, narrowing a constraint is better than widening it, because | ||
* narrowing leads to incompleteness (which we face anyway, see for | ||
* instance `TypeComparer#either`) but widening leads to unsoundness, | ||
* but note the special handling in `ConstrainResult` mode below. | ||
* | ||
* A test case that demonstrates the problem is i864.scala. | ||
* Turn Config.checkConstraintsSeparated on to get an accurate diagnostic | ||
|
@@ -544,10 +545,23 @@ trait ConstraintHandling[AbstractContext] { | |
case bound: TypeParamRef if constraint contains bound => | ||
addParamBound(bound) | ||
case _ => | ||
val savedConstraint = constraint | ||
val pbound = prune(bound) | ||
pbound.exists | ||
&& kindCompatible(param, pbound) | ||
&& (if fromBelow then addLowerBound(param, pbound) else addUpperBound(param, pbound)) | ||
val constraintsNarrowed = constraint ne savedConstraint | ||
|
||
val res = | ||
pbound.exists | ||
&& kindCompatible(param, pbound) | ||
&& (if fromBelow then addLowerBound(param, pbound) else addUpperBound(param, pbound)) | ||
// If we're in `ConstrainResult` mode, we don't want to commit to a | ||
// set of constraints that would later prevent us from typechecking | ||
// arguments, so if `pruneParams` had to narrow the constraints, we | ||
// simply do not record any new constraint. | ||
// Unlike in `TypeComparer#either`, the same reasoning does not apply | ||
// to GADT mode because this code is never run on GADT constraints. | ||
if ctx.mode.is(Mode.ConstrainResult) && constraintsNarrowed then | ||
constraint = savedConstraint | ||
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. That looks good to me. It would be even nicer to have a prune that does not narrow. But let's save this for another day. |
||
res | ||
} | ||
finally addConstraintInvocations -= 1 | ||
} | ||
|
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
File renamed without changes.
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,13 @@ | ||
class A | ||
class B | ||
|
||
class Inv[T] | ||
class Contra[-T] | ||
|
||
class Test { | ||
def foo[T, S](x: T, y: S): Contra[Inv[T] & Inv[S]] = ??? | ||
val a: A = new A | ||
val b: B = new B | ||
|
||
val x: Contra[Inv[A] & Inv[B]] = foo(a, b) | ||
} |
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,27 @@ | ||
class X | ||
class Y | ||
|
||
object Test { | ||
type Id[T] = T | ||
|
||
val a: 1 = identity(1) | ||
val b: Id[1] = identity(1) | ||
|
||
val c: X | Y = identity(if (true) new X else new Y) | ||
val d: Id[X | Y] = identity(if (true) new X else new Y) | ||
|
||
def impUnion: Unit = { | ||
class Base | ||
class A extends Base | ||
class B extends Base | ||
class Inv[T] | ||
|
||
implicit def invBase: Inv[Base] = new Inv[Base] | ||
|
||
def getInv[T](x: T)(implicit inv: Inv[T]): Int = 1 | ||
|
||
val a: Int = getInv(if (true) new A else new B) | ||
// If we keep unions when doing the implicit search, this would give us: "no implicit argument of type Inv[X | Y]" | ||
val b: Int | Any = getInv(if (true) new A else new B) | ||
} | ||
} |
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 @@ | ||
trait Has[A] | ||
|
||
trait A | ||
trait B | ||
trait C | ||
|
||
trait ZLayer[-RIn, +E, +ROut] | ||
|
||
object ZLayer { | ||
def fromServices[A0, A1, B](f: (A0, A1) => B): ZLayer[Has[A0] with Has[A1], Nothing, Has[B]] = | ||
??? | ||
} | ||
|
||
val live: ZLayer[Has[A] & Has[B], Nothing, Has[C]] = | ||
ZLayer.fromServices { (a: A, b: B) => | ||
new C {} | ||
} |
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,14 @@ | ||
object Test { | ||
|
||
def a(lis: Set[Int] | Set[String]) = {} | ||
a(Set(1)) | ||
a(Set("")) | ||
|
||
def b(lis: List[Set[Int] | Set[String]]) = {} | ||
b(List(Set(1))) | ||
b(List(Set(""))) | ||
|
||
def c(x: Set[Any] | Array[Any]) = {} | ||
c(Set(1)) | ||
c(Array(1)) | ||
} |
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.
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.
@AleksanderBG Can you confirm that this comment is correct ?
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.
@smarter missed your comment! GADT code only goes through
addToConstraint
/addUpperBound
/addLowerBound
. AFAIT none of these invokeaddBound
.