Commit 69e97e9
authored
Invent given pattern name in for comprehension (#23121)
Fixes #23119
A given pattern in a for comprehension results in a fresh val in the
body of the mapping function, but it should have the same (arbitrary)
name as in the rest of the expansion.
This commit gives the given its usual given name (in `makeIdPat`) so
that the unused check can check it.
Currently, without `-preview`, showing that `$1$` is called `given_Int`
by typer:
```
➜ scala-cli repl --server=false -S 3.7.2-RC1 -Vprint:typer,refchecks -Wunused:all
Welcome to Scala 3.7.2-RC1 (17.0.15, Java OpenJDK 64-Bit Server VM).
Type in expressions for evaluation. Or try :help.
scala> for given Int <- 1 to 2; j: Int = summon[Int] yield j
1 warning found
[[syntax trees at end of typer]] // rs$line$1
package <empty> {
final lazy module val rs$line$1: rs$line$1 = new rs$line$1()
final module class rs$line$1() extends Object() { this: rs$line$1.type =>
val res0: IndexedSeq[Int] =
intWrapper(1).to(2).map[(Int, Int)]((x$1: Int) =>
x$1:Int @unchecked match
{
case given $1$ @ _:Int =>
val j: Int = $1$
Tuple2.apply[Int, Int]($1$, j)
}
).map[Int]((x$1: (Int, Int)) =>
x$1:(x$1 : (Int, Int)) @unchecked match
{
case Tuple2.unapply[Int, Int](given given_Int @ _:Int, j @ _:Int)
=> j:Int
}
)
}
}
```
Without `-preview`, this commit:
```
scala> for given Int <- 1 to 2; j: Int = summon[Int] yield j
[[syntax trees at end of typer]] // rs$line$1
package <empty> {
final lazy module val rs$line$1: rs$line$1 = new rs$line$1()
final module class rs$line$1() extends Object() { this: rs$line$1.type =>
val res0: IndexedSeq[Int] =
intWrapper(1).to(2).map[(Int, Int)]((x$1: Int) =>
x$1:Int @unchecked match
{
case given given_Int @ _:Int =>
val j: Int = given_Int
Tuple2.apply[Int, Int](given_Int, j)
}
).map[Int]((x$1: (Int, Int)) =>
x$1:(x$1 : (Int, Int)) @unchecked match
{
case Tuple2.unapply[Int, Int](given given_Int @ _:Int, j @ _:Int)
=> j:Int
}
)
}
}
```
With `-preview`, the tupling map is eliminated early:
```
scala> for given Int <- 1 to 2; j: Int = summon[Int] yield j
[[syntax trees at end of typer]] // rs$line$1
package <empty> {
final lazy module val rs$line$1: rs$line$1 = new rs$line$1()
final module class rs$line$1() extends Object() { this: rs$line$1.type =>
val res0: IndexedSeq[Int] =
intWrapper(1).to(2).map[Int]((x$1: Int) =>
x$1:Int @unchecked match
{
case given given_Int @ _:Int =>
val j: Int = given_Int
j:Int
}
)
}
}
```File tree
5 files changed
+97
-9
lines changed- compiler/src/dotty/tools/dotc
- ast
- reporting
- tests
- neg
- pos
5 files changed
+97
-9
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1347 | 1347 | | |
1348 | 1348 | | |
1349 | 1349 | | |
1350 | | - | |
| 1350 | + | |
1351 | 1351 | | |
1352 | 1352 | | |
1353 | 1353 | | |
| |||
2136 | 2136 | | |
2137 | 2137 | | |
2138 | 2138 | | |
2139 | | - | |
2140 | | - | |
2141 | | - | |
2142 | | - | |
2143 | | - | |
2144 | | - | |
| 2139 | + | |
| 2140 | + | |
| 2141 | + | |
| 2142 | + | |
| 2143 | + | |
| 2144 | + | |
| 2145 | + | |
| 2146 | + | |
2145 | 2147 | | |
2146 | 2148 | | |
2147 | 2149 | | |
2148 | 2150 | | |
2149 | 2151 | | |
2150 | | - | |
2151 | 2152 | | |
2152 | 2153 | | |
2153 | 2154 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2125 | 2125 | | |
2126 | 2126 | | |
2127 | 2127 | | |
| 2128 | + | |
| 2129 | + | |
| 2130 | + | |
| 2131 | + | |
| 2132 | + | |
| 2133 | + | |
| 2134 | + | |
| 2135 | + | |
| 2136 | + | |
| 2137 | + | |
2128 | 2138 | | |
2129 | | - | |
| 2139 | + | |
| 2140 | + | |
| 2141 | + | |
| 2142 | + | |
| 2143 | + | |
| 2144 | + | |
| 2145 | + | |
| 2146 | + | |
| 2147 | + | |
| 2148 | + | |
2130 | 2149 | | |
2131 | 2150 | | |
2132 | 2151 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
0 commit comments