-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Fix #23224: Optimize simple tuple extraction #23373
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
base: main
Are you sure you want to change the base?
Changes from all commits
6e466de
0463b76
6841cd1
74e2b76
310e20b
5f79048
568b115
545dd69
d614d0b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1606,6 +1606,21 @@ class DottyBytecodeTests extends DottyBytecodeTest { | |
} | ||
} | ||
|
||
@Test | ||
def simpleTupleExtraction(): Unit = { | ||
val code = | ||
"""class C { | ||
| def f1(t: (Int, String)) = | ||
| val (i, s) = t | ||
| i + s.length | ||
|} | ||
""".stripMargin | ||
checkBCode(code) { dir => | ||
val c = loadClassNode(dir.lookupName("C.class", directory = false).input) | ||
assertNoInvoke(getMethod(c, "f1"), "scala/Tuple2$", "apply") // no Tuple2.apply call | ||
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. We should also make sure that there is no |
||
} | ||
} | ||
|
||
@Test | ||
def deprecation(): Unit = { | ||
val code = | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,9 @@ | ||
-- [E007] Type Mismatch Error: tests/neg/i7294.scala:7:15 -------------------------------------------------------------- | ||
-- [E007] Type Mismatch Error: tests/neg/i7294.scala:7:18 -------------------------------------------------------------- | ||
7 | case x: T => x.g(10) // error | ||
| ^ | ||
| Found: (x : Nothing) | ||
| Required: ?{ g: ? } | ||
| Note that implicit conversions were not tried because the result of an implicit conversion | ||
| must be more specific than ?{ g: [applied to (10) returning T] } | ||
| ^^^^^^^ | ||
| Found: Any | ||
| Required: T | ||
| | ||
| where: T is a type in given instance f with bounds <: foo.Foo | ||
| | ||
| longer explanation available when compiling with `-explain` |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
|
||
class 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. What is the purpose of this test? As a 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. It just tests and explains different cases for desugaring, by different number of variables in the pattern. |
||
def f1: (Int, String, AnyRef) = (1, "2", "3") | ||
def f2: (x: Int, y: String) = (0, "y") | ||
|
||
def test1 = | ||
val (a, b, c) = f1 | ||
// Desugared to: | ||
// val $2$: (Int, String, AnyRef) = | ||
// this.f1:(Int, String, AnyRef) @unchecked match | ||
// { | ||
// case $1$ @ Tuple3.unapply[Int, String, Object](_, _, _) => | ||
// $1$:(Int, String, AnyRef) | ||
// } | ||
// val a: Int = $2$._1 | ||
// val b: String = $2$._2 | ||
// val c: AnyRef = $2$._3 | ||
a + b.length() + c.toString.length() | ||
|
||
// This pattern will not be optimized: | ||
// val (a1, b1, c1: String) = f1 | ||
|
||
def test2 = | ||
val (_, b, c) = f1 | ||
b.length() + c.toString.length() | ||
|
||
val (a2, _, c2) = f1 | ||
a2 + c2.toString.length() | ||
|
||
val (a3, _, _) = f1 | ||
a3 + 1 | ||
|
||
def test3 = | ||
val (_, b, _) = f1 | ||
b.length() + 1 | ||
|
||
def test4 = | ||
val (x, y) = f2 | ||
x + y.length() | ||
|
||
def test5 = | ||
val (_, b) = f2 | ||
b.length() + 1 |
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.
Could you comment on why the bottom-related changes are necessary? I find them suspicious. This change should be nothing but an optimization, so it shouldn't change typing.
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.
We have tests like:
val (a, b) = ???
. Since we now bind the tuple pattern to a variable, I want to give the bind variable a tuple type instead of a bottom type.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.
Is that necessary for correctness? There is really no point in improving the code for such an extraction. By definition, the code will throw before it gets to the tuple extraction.