-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Take expected type into account when typing a sequence argument #8669
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
Conversation
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
smarter
added a commit
to lampepfl/xml-interpolator
that referenced
this pull request
Apr 5, 2020
Implicit conversions are not normally applied when splicing, it happened to work in this particular case before scala/scala3#8669 because of the peculiar way in which we typed repeated arguments.
a6c7300
to
917bc6a
Compare
122f045
to
82fb3d2
Compare
82fb3d2
to
b122756
Compare
8061f6f
to
d42ca2e
Compare
Hmmph, somehow this is causing scalatest to get stuck in an infinite loop, I'll investigate. |
d42ca2e
to
6fbcc17
Compare
Fixed. I had written |
odersky
approved these changes
Apr 13, 2020
val expr1 = typedExpr(tree.expr, ptArg) | ||
val fromCls = if expr1.tpe.derivesFrom(defn.ArrayClass) then defn.ArrayClass else defn.SeqClass | ||
val tpt1 = TypeTree(expr1.tpe.widen.translateToRepeated(fromCls)).withSpan(tree.tpt.span) | ||
assignType(cpy.Typed(tree)(expr1, tpt1), tpt1) |
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.
nice!
The previous name was slightly misleading: we can pass Array sequence arguments to Scala methods and Seq sequence argument to Java methods, so we may want to translate a repeated parameter type to either an Array or a Seq irrespective of what it erases to (we don't right now but we will in the next commit).
6fbcc17
to
f01f307
Compare
Given an expected type `*T`, we allow a sequence argument `xs: _*` to be either a `Seq[T]` or an `Array[_ <: T]`, irrespective of whether the method we're calling is a Java or Scala method. So far we typed sequence arguments without an expected type, meaning that adaptation did not take place. But thanks to scala#8635, we can type them with an expected type of `Seq[T] | Array[_ <: T]` and type inference works out. This is what this commit does.
I don't know of a case where this matters currently but it's more correct.
f01f307
to
03d5d9b
Compare
smarter
added a commit
to dotty-staging/dotty
that referenced
this pull request
Apr 13, 2020
A Java generic array `T[]` is unpickled in ClassfileParser to `Array[T & Object]`, because it will not accept primitive arrays. However, Java varargs were special-cased to drop this intersection. Erasure contained some code to compensate for this, but this is still unsound and lead to tests/run/t1360.scala failing with a ClassCastException. Thanks to scala#8669 we can drop this special-casing of varargs without affecting typing too much: it's still possible to pass `Array(1, 2): _*` where `(T & Object)*` is expected because adaptation will take care of boxing. One test case had to be adapted, I replaced: def apply[X](xs : X*): java.util.List[X] java.util.Arrays.asList(xs: _*) with: def apply[X <: AnyRef](xs : X*): java.util.List[X] = java.util.Arrays.asList(xs: _*) I don't think that's too constraining. Note: after this commit, tests/run/i533 started failing, this is fixed in the next commit.
smarter
added a commit
to dotty-staging/dotty
that referenced
this pull request
Apr 13, 2020
A Java generic array `T[]` is unpickled in ClassfileParser to `Array[T & Object]`, because it will not accept primitive arrays. However, Java varargs were special-cased to drop this intersection. Erasure contained some code to compensate for this, but this is still unsound and lead to tests/run/t1360.scala failing with a ClassCastException. Thanks to scala#8669 we can drop this special-casing of varargs without affecting typing too much: it's still possible to pass `Array(1, 2): _*` where `(T & Object)*` is expected because adaptation will take care of boxing. One test case had to be adapted, I replaced: def apply[X](xs : X*): java.util.List[X] java.util.Arrays.asList(xs: _*) with: def apply[X <: AnyRef](xs : X*): java.util.List[X] = java.util.Arrays.asList(xs: _*) I don't think that's too constraining. Note: after this commit, tests/run/i533 started failing, this is fixed in the next commit.
smarter
added a commit
to dotty-staging/dotty
that referenced
this pull request
May 18, 2020
A Java generic array `T[]` is unpickled in ClassfileParser to `Array[T & Object]`, because it will not accept primitive arrays. However, Java varargs were special-cased to drop this intersection. Erasure contained some code to compensate for this, but this is still unsound and lead to tests/run/t1360.scala failing with a ClassCastException. Thanks to scala#8669 we can drop this special-casing of varargs without affecting typing too much: it's still possible to pass `Array(1, 2): _*` where `(T & Object)*` is expected because adaptation will take care of boxing. One test case had to be adapted, I replaced: def apply[X](xs : X*): java.util.List[X] java.util.Arrays.asList(xs: _*) with: def apply[X <: AnyRef](xs : X*): java.util.List[X] = java.util.Arrays.asList(xs: _*) I don't think that's too constraining. Note: after this commit, tests/run/i533 started failing, this is fixed in the next commit.
rethab
pushed a commit
to rethab/dotty
that referenced
this pull request
May 27, 2020
A Java generic array `T[]` is unpickled in ClassfileParser to `Array[T & Object]`, because it will not accept primitive arrays. However, Java varargs were special-cased to drop this intersection. Erasure contained some code to compensate for this, but this is still unsound and lead to tests/run/t1360.scala failing with a ClassCastException. Thanks to scala#8669 we can drop this special-casing of varargs without affecting typing too much: it's still possible to pass `Array(1, 2): _*` where `(T & Object)*` is expected because adaptation will take care of boxing. One test case had to be adapted, I replaced: def apply[X](xs : X*): java.util.List[X] java.util.Arrays.asList(xs: _*) with: def apply[X <: AnyRef](xs : X*): java.util.List[X] = java.util.Arrays.asList(xs: _*) I don't think that's too constraining. Note: after this commit, tests/run/i533 started failing, this is fixed in the next commit.
odersky
pushed a commit
to lampepfl/xml-interpolator
that referenced
this pull request
May 29, 2020
Implicit conversions are not normally applied when splicing, it happened to work in this particular case before scala/scala3#8669 because of the peculiar way in which we typed repeated arguments.
gorilskij
pushed a commit
to gorilskij/xml-interpolator
that referenced
this pull request
Jun 13, 2022
Implicit conversions are not normally applied when splicing, it happened to work in this particular case before scala/scala3#8669 because of the peculiar way in which we typed repeated arguments.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
No description provided.