Skip to content

Commit 26578ca

Browse files
committed
Invent given pattern name in for comprehension
1 parent 606268f commit 26578ca

File tree

2 files changed

+38
-8
lines changed

2 files changed

+38
-8
lines changed

compiler/src/dotty/tools/dotc/ast/Desugar.scala

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1345,7 +1345,7 @@ object desugar {
13451345
)).withSpan(tree.span)
13461346
end makePolyFunctionType
13471347

1348-
/** Invent a name for an anonympus given of type or template `impl`. */
1348+
/** Invent a name for an anonymous given of type or template `impl`. */
13491349
def inventGivenName(impl: Tree)(using Context): SimpleName =
13501350
val str = impl match
13511351
case impl: Template =>
@@ -2067,18 +2067,19 @@ object desugar {
20672067
* that refers to the bound variable for the pattern. Wildcard Binds are
20682068
* also replaced by Binds with fresh names.
20692069
*/
2070-
def makeIdPat(pat: Tree): (Tree, Ident) = pat match {
2071-
case bind @ Bind(name, pat1) =>
2072-
if name == nme.WILDCARD then
2073-
val name = UniqueName.fresh()
2074-
(cpy.Bind(pat)(name, pat1).withMods(bind.mods), Ident(name))
2075-
else (pat, Ident(name))
2070+
def makeIdPat(pat: Tree): (Tree, Ident) = pat match
2071+
case pat @ Bind(nme.WILDCARD, body) =>
2072+
val name =
2073+
body match
2074+
case Typed(Ident(nme.WILDCARD), tpt) if pat.mods.is(Given) => inventGivenName(tpt)
2075+
case _ => UniqueName.fresh()
2076+
(cpy.Bind(pat)(name, body).withMods(pat.mods), Ident(name))
2077+
case Bind(name, _) => (pat, Ident(name))
20762078
case id: Ident if isVarPattern(id) && id.name != nme.WILDCARD => (id, id)
20772079
case Typed(id: Ident, _) if isVarPattern(id) && id.name != nme.WILDCARD => (pat, id)
20782080
case _ =>
20792081
val name = UniqueName.fresh()
20802082
(Bind(name, pat), Ident(name))
2081-
}
20822083

20832084
/** Make a pattern filter:
20842085
* rhs.withFilter { case pat => true case _ => false }

tests/pos/i23119.scala

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
//> using options -Wunused:patvars -Werror
2+
3+
def make: IndexedSeq[FalsePositive] =
4+
for {
5+
i <- 1 to 2
6+
given Int = i
7+
fp = FalsePositive()
8+
} yield fp
9+
10+
def broken =
11+
for
12+
i <- List(42)
13+
(x, y) = "hello" -> "world"
14+
yield
15+
s"$x, $y" * i
16+
17+
def alt: IndexedSeq[FalsePositive] =
18+
given String = "hi"
19+
for
20+
given Int <- 1 to 2
21+
j: Int = summon[Int] // simple assign because irrefutable
22+
_ = j + 1
23+
k :: Nil = j :: Nil : @unchecked // pattern in one var
24+
fp = FalsePositive(using k)
25+
yield fp
26+
27+
class FalsePositive(using Int):
28+
def usage(): Unit =
29+
println(summon[Int])

0 commit comments

Comments
 (0)