Skip to content

Commit b9356f1

Browse files
authored
add missing context scope in Interactive for pattern guards (#15807)
1 parent b0f1c0f commit b9356f1

File tree

3 files changed

+47
-7
lines changed

3 files changed

+47
-7
lines changed

compiler/src/dotty/tools/dotc/interactive/Completion.scala

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -232,10 +232,8 @@ object Completion {
232232
val mappings = collection.mutable.Map.empty[Name, List[ScopedDenotations]].withDefaultValue(List.empty)
233233
def addMapping(name: Name, denots: ScopedDenotations) =
234234
mappings(name) = mappings(name) :+ denots
235-
var ctx = context
236235

237-
while ctx ne NoContext do
238-
given Context = ctx
236+
ctx.outersIterator.foreach { case ctx @ given Context =>
239237
if ctx.isImportContext then
240238
importedCompletions.foreach { (name, denots) =>
241239
addMapping(name, ScopedDenotations(denots, ctx))
@@ -251,9 +249,7 @@ object Completion {
251249
.groupByName.foreach { (name, denots) =>
252250
addMapping(name, ScopedDenotations(denots, ctx))
253251
}
254-
255-
ctx = ctx.outer
256-
end while
252+
}
257253

258254
var resultMappings = Map.empty[Name, Seq[SingleDenotation]]
259255

compiler/src/dotty/tools/dotc/interactive/Interactive.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,7 @@ object Interactive {
306306
case _ =>
307307
}
308308
contextOfStat(stats, nested, ctx.owner, localCtx)
309-
case tree @ CaseDef(pat, guard, rhs) if nested `eq` rhs =>
309+
case tree @ CaseDef(pat, _, _) =>
310310
val localCtx = outer.fresh.setNewScope
311311
pat.foreachSubTree {
312312
case bind: Bind => localCtx.enter(bind.symbol)

language-server/test/dotty/tools/languageserver/CompletionTest.scala

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1474,4 +1474,48 @@ class CompletionTest {
14741474
("xClass", Module, "Foo.xClass"),
14751475
("xClass", Class, "Foo.xClass")))
14761476
}
1477+
1478+
@Test def patternGuardCompletions: Unit = {
1479+
code"""object Foo:
1480+
| 1 match { case foo if fo${m1} => }
1481+
| 1 match { case foo => fo${m2} }
1482+
"""
1483+
.completion(m1, Set(("foo", Field, "Int")))
1484+
.completion(m2, Set(("foo", Field, "Int")))
1485+
}
1486+
1487+
@Test def patternGuardCompletionsUnApply: Unit = {
1488+
code"""object Foo:
1489+
| Some(1) match { case Some(foo) if fo${m1} => }
1490+
| Some(1) match { case Some(foo) => fo${m2} }
1491+
"""
1492+
.completion(m1, Set(("foo", Field, "Int")))
1493+
.completion(m2, Set(("foo", Field, "Int")))
1494+
}
1495+
1496+
@Test def patternGuardCompletionsNested: Unit = {
1497+
code"""object Foo:
1498+
| ((1, 2), 3) match { case ((foo1, foo2), foo3) if fo${m1} => }
1499+
| ((1, 2), 3) match { case ((foo1, foo2), foo3) => fo${m2} }
1500+
"""
1501+
.completion(m1, Set(("foo1", Field, "Int"), ("foo2", Field, "Int"), ("foo3", Field, "Int")))
1502+
.completion(m2, Set(("foo1", Field, "Int"), ("foo2", Field, "Int"), ("foo3", Field, "Int")))
1503+
}
1504+
1505+
@Test def patternGuardCompletionsSeq: Unit = {
1506+
code"""object Foo:
1507+
| Seq(1, 2) match { case foo1 :: foo2 if fo${m1} => }
1508+
| Seq(1, 2) match { case foo1 :: foo2 => fo${m2} }
1509+
"""
1510+
.completion(m1, Set(("foo1", Field, "Int"), ("foo2", Field, "List[Int]")))
1511+
.completion(m2, Set(("foo1", Field, "Int"), ("foo2", Field, "List[Int]")))
1512+
}
1513+
1514+
@Test def noCompletionsInsidePatternBind: Unit = {
1515+
code"""object Foo:
1516+
| (1, 2) match { case (foo, fo${m1}
1517+
"""
1518+
.noCompletions()
1519+
}
1520+
14771521
}

0 commit comments

Comments
 (0)