You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
}
)
}
}
```
0 commit comments