Skip to content

Invent given pattern name in for comprehension #23121

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 9 additions & 8 deletions compiler/src/dotty/tools/dotc/ast/Desugar.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1345,7 +1345,7 @@ object desugar {
)).withSpan(tree.span)
end makePolyFunctionType

/** Invent a name for an anonympus given of type or template `impl`. */
/** Invent a name for an anonymous given of type or template `impl`. */
def inventGivenName(impl: Tree)(using Context): SimpleName =
val str = impl match
case impl: Template =>
Expand Down Expand Up @@ -2067,18 +2067,19 @@ object desugar {
* that refers to the bound variable for the pattern. Wildcard Binds are
* also replaced by Binds with fresh names.
*/
def makeIdPat(pat: Tree): (Tree, Ident) = pat match {
case bind @ Bind(name, pat1) =>
if name == nme.WILDCARD then
val name = UniqueName.fresh()
(cpy.Bind(pat)(name, pat1).withMods(bind.mods), Ident(name))
else (pat, Ident(name))
def makeIdPat(pat: Tree): (Tree, Ident) = pat match
case pat @ Bind(nme.WILDCARD, body) =>
val name =
body match
case Typed(Ident(nme.WILDCARD), tpt) if pat.mods.is(Given) => inventGivenName(tpt)
case _ => UniqueName.fresh()
(cpy.Bind(pat)(name, body).withMods(pat.mods), Ident(name))
case Bind(name, _) => (pat, Ident(name))
case id: Ident if isVarPattern(id) && id.name != nme.WILDCARD => (id, id)
case Typed(id: Ident, _) if isVarPattern(id) && id.name != nme.WILDCARD => (pat, id)
case _ =>
val name = UniqueName.fresh()
(Bind(name, pat), Ident(name))
}

/** Make a pattern filter:
* rhs.withFilter { case pat => true case _ => false }
Expand Down
29 changes: 29 additions & 0 deletions tests/pos/i23119.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
//> using options -Wunused:patvars -Werror

def make: IndexedSeq[FalsePositive] =
for {
i <- 1 to 2
given Int = i
fp = FalsePositive()
} yield fp

def broken =
for
i <- List(42)
(x, y) = "hello" -> "world"
yield
s"$x, $y" * i

def alt: IndexedSeq[FalsePositive] =
given String = "hi"
for
given Int <- 1 to 2
j: Int = summon[Int] // simple assign because irrefutable
_ = j + 1
k :: Nil = j :: Nil : @unchecked // pattern in one var
fp = FalsePositive(using k)
yield fp

class FalsePositive(using Int):
def usage(): Unit =
println(summon[Int])
Loading