-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Improve type inference for functions like fold #18780
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 3 commits
Commits
Show all changes
4 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 |
---|---|---|
|
@@ -4908,6 +4908,9 @@ object Types { | |
tp | ||
} | ||
|
||
def typeToInstantiateWith(fromBelow: Boolean)(using Context): Type = | ||
TypeComparer.instanceType(origin, fromBelow, widenUnions, nestingLevel) | ||
|
||
/** Instantiate variable from the constraints over its `origin`. | ||
* If `fromBelow` is true, the variable is instantiated to the lub | ||
* of its lower bounds in the current constraint; otherwise it is | ||
|
@@ -4916,8 +4919,9 @@ object Types { | |
* is also a singleton type. | ||
*/ | ||
def instantiate(fromBelow: Boolean)(using Context): Type = | ||
val tp = TypeComparer.instanceType(origin, fromBelow, widenUnions, nestingLevel) | ||
val tp = typeToInstantiateWith(fromBelow) | ||
if myInst.exists then // The line above might have triggered instantiation of the current type variable | ||
Member | ||
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. stray line?
odersky marked this conversation as resolved.
Show resolved
Hide resolved
|
||
myInst | ||
else | ||
instantiateWith(tp) | ||
|
@@ -5812,11 +5816,13 @@ object Types { | |
protected def derivedLambdaType(tp: LambdaType)(formals: List[tp.PInfo], restpe: Type): Type = | ||
tp.derivedLambdaType(tp.paramNames, formals, restpe) | ||
|
||
protected def mapArg(arg: Type, tparam: ParamInfo): Type = arg match | ||
case arg: TypeBounds => this(arg) | ||
case arg => atVariance(variance * tparam.paramVarianceSign)(this(arg)) | ||
|
||
protected def mapArgs(args: List[Type], tparams: List[ParamInfo]): List[Type] = args match | ||
case arg :: otherArgs if tparams.nonEmpty => | ||
val arg1 = arg match | ||
case arg: TypeBounds => this(arg) | ||
case arg => atVariance(variance * tparams.head.paramVarianceSign)(this(arg)) | ||
val arg1 = mapArg(arg, tparams.head) | ||
val otherArgs1 = mapArgs(otherArgs, tparams.tail) | ||
if ((arg1 eq arg) && (otherArgs1 eq otherArgs)) args | ||
else arg1 :: otherArgs1 | ||
|
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,7 @@ | ||
-- [E007] Type Mismatch Error: tests/neg/foldinf-ill-kinded.scala:9:16 ------------------------------------------------- | ||
9 | ys.combine(x) // error | ||
| ^^^^^^^^^^^^^ | ||
| Found: Foo[List] | ||
| Required: Foo[Nothing] | ||
| | ||
| longer explanation available when compiling with `-explain` |
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 @@ | ||
class Foo[+T[_]]: | ||
def combine[T1[x] >: T[x]](x: T1[Int]): Foo[T1] = new Foo | ||
object Foo: | ||
def empty: Foo[Nothing] = new Foo | ||
|
||
object X: | ||
def test(xs: List[List[Int]]): Unit = | ||
xs.foldLeft(Foo.empty)((ys, x) => | ||
ys.combine(x) // error | ||
) |
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 @@ | ||
|
||
object Test: | ||
extension [A](xs: List[A]) | ||
def foldl[B](acc: B)(f: (A, B) => B): B = ??? | ||
|
||
val xs = List(1, 2, 3) | ||
|
||
val _ = xs.foldl(List())((y, ys) => y :: ys) | ||
|
||
val _ = xs.foldl(Nil)((y, ys) => y :: ys) | ||
|
||
def partition[a](xs: List[a], pred: a => Boolean): Tuple2[List[a], List[a]] = { | ||
xs.foldRight/*[Tuple2[List[a], List[a]]]*/((List(), List())) { | ||
(x, p) => if (pred (x)) (x :: p._1, p._2) else (p._1, x :: p._2) | ||
} | ||
} | ||
|
||
def snoc[A](xs: List[A], x: A) = x :: xs | ||
|
||
def reverse[A](xs: List[A]) = | ||
xs.foldLeft(Nil)(snoc) | ||
|
||
def reverse2[A](xs: List[A]) = | ||
xs.foldLeft(List())(snoc) | ||
|
||
val ys: Seq[Int] = xs | ||
ys.foldLeft(Seq())((ys, y) => y +: ys) | ||
ys.foldLeft(Nil)((ys, y) => y +: ys) | ||
|
||
def dup[A](xs: List[A]) = | ||
xs.foldRight(Nil)((x, xs) => x :: x :: xs) | ||
|
||
def toSet[A](xs: Seq[A]) = | ||
xs.foldLeft(Set.empty)(_ + _) |
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.
This check used to be necessary so I would prefer to keep it now that the
assert
ininstantiateWith
has been added back, unless we have some strong reason to believe it no longer is.