Skip to content

Commit f2e4e7a

Browse files
committed
Set proper position for ValDefs generated from tuples
Previously, the span for ValDefs generated for tuples would encompas the entire expression, which led to difficulties identifying the exact path to the current position. Now, we set the span to be the same as the name underneath. Not sure if this is a proper solution, since normally ValDefs ecompans the entire span, but in this case it makes 2 different ValDef have the same span. An alternative solution would be to find the one with point nearer to the current position. This popped up within the Nightly tests we do in Metals.
1 parent d466f9f commit f2e4e7a

File tree

5 files changed

+30
-14
lines changed

5 files changed

+30
-14
lines changed

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

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1143,10 +1143,11 @@ object desugar {
11431143
) // no `_`
11441144

11451145
val ids = for ((named, _) <- vars) yield Ident(named.name)
1146-
val caseDef = CaseDef(pat, EmptyTree, makeTuple(ids))
11471146
val matchExpr =
11481147
if (tupleOptimizable) rhs
1149-
else Match(makeSelector(rhs, MatchCheck.IrrefutablePatDef), caseDef :: Nil)
1148+
else
1149+
val caseDef = CaseDef(pat, EmptyTree, makeTuple(ids))
1150+
Match(makeSelector(rhs, MatchCheck.IrrefutablePatDef), caseDef :: Nil)
11501151
vars match {
11511152
case Nil if !mods.is(Lazy) =>
11521153
matchExpr
@@ -1166,8 +1167,16 @@ object desugar {
11661167
val restDefs =
11671168
for (((named, tpt), n) <- vars.zipWithIndex if named.name != nme.WILDCARD)
11681169
yield
1169-
if (mods.is(Lazy)) derivedDefDef(original, named, tpt, selector(n), mods &~ Lazy)
1170-
else derivedValDef(original, named, tpt, selector(n), mods)
1170+
if mods.is(Lazy) then
1171+
DefDef(named.name.asTermName, Nil, tpt, selector(n))
1172+
.withMods(mods &~ Lazy)
1173+
.withSpan(named.span)
1174+
else
1175+
valDef(
1176+
ValDef(named.name.asTermName, tpt, selector(n))
1177+
.withMods(mods)
1178+
.withSpan(named.span)
1179+
)
11711180
flatTree(firstDef :: restDefs)
11721181
}
11731182
}

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,13 @@ class HoverTest {
234234

235235
@Test def tuple: Unit = {
236236
code"""|object A:
237-
| val (${m1}first${m2}, second) = (1, 2)""".withSource
238-
.hover(m1 to m2, hoverContent("Int"))
237+
| val (${m1}first${m2}, second) = (1.0, 2)""".withSource
238+
.hover(m1 to m2, hoverContent("Double"))
239+
}
240+
241+
@Test def multiAssigment: Unit = {
242+
code"""|val ${m1}x${m2}, ${m3}y${m4} = 42.0""".withSource
243+
.hover(m1 to m2, hoverContent("Double"))
244+
.hover(m3 to m4, hoverContent("Double"))
239245
}
240246
}

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

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -93,12 +93,6 @@ class WorksheetTest {
9393
((m1 to m2), "val res0: String = odd"))
9494
}
9595

96-
@Test def patternMatching1: Unit = {
97-
ws"""${m1}val (foo, bar) = (1, 2)${m2}""".withSource
98-
.run(m1,
99-
((m1 to m2), s"val foo: Int = 1${nl}val bar: Int = 2"))
100-
}
101-
10296
@Test def evaluationException: Unit = {
10397
ws"""${m1}val foo = 1 / 0${m2}
10498
${m3}val bar = 2${m4}""".withSource

tests/neg/t5702-neg-bad-and-wild.check

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,17 @@
2323
|
2424
| longer explanation available when compiling with `-explain`
2525
-- [E032] Syntax Error: tests/neg/t5702-neg-bad-and-wild.scala:23:17 ---------------------------------------------------
26-
23 | val K(ns @ _*, x) = k // error: pattern expected
26+
23 | val K(ns @ _*, xx) = k // error: pattern expected
2727
| ^
2828
| pattern expected
2929
|
3030
| longer explanation available when compiling with `-explain`
31+
-- [E161] Naming Error: tests/neg/t5702-neg-bad-and-wild.scala:24:10 ---------------------------------------------------
32+
24 | val K(x) = k // error: x is already defined as value x
33+
| ^^^^^^^^^^^^
34+
| x is already defined as value x
35+
|
36+
| Note that overloaded methods must all be defined in the same group of toplevel definitions
3137
-- Error: tests/neg/t5702-neg-bad-and-wild.scala:10:21 -----------------------------------------------------------------
3238
10 | case List(1, _*,) => // error: pattern expected // error
3339
| ^

tests/neg/t5702-neg-bad-and-wild.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ object Test {
2020
// good syntax, bad semantics, detected by typer
2121
//gowild.scala:14: error: star patterns must correspond with varargs parameters
2222
val K(x @ _*) = k
23-
val K(ns @ _*, x) = k // error: pattern expected
23+
val K(ns @ _*, xx) = k // error: pattern expected
24+
val K(x) = k // error: x is already defined as value x
2425
val (b, _ * ) = (5,6) // ok
2526
// no longer complains
2627
//bad-and-wild.scala:15: error: ')' expected but '}' found.

0 commit comments

Comments
 (0)