Skip to content

Commit

Permalink
Set proper position for ValDefs generated from tuples
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
tgodzik authored and olsdavis committed Apr 4, 2022
1 parent 0afcd76 commit 19fb504
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 14 deletions.
17 changes: 13 additions & 4 deletions compiler/src/dotty/tools/dotc/ast/Desugar.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1143,10 +1143,11 @@ object desugar {
) // no `_`

val ids = for ((named, _) <- vars) yield Ident(named.name)
val caseDef = CaseDef(pat, EmptyTree, makeTuple(ids))
val matchExpr =
if (tupleOptimizable) rhs
else Match(makeSelector(rhs, MatchCheck.IrrefutablePatDef), caseDef :: Nil)
else
val caseDef = CaseDef(pat, EmptyTree, makeTuple(ids))
Match(makeSelector(rhs, MatchCheck.IrrefutablePatDef), caseDef :: Nil)
vars match {
case Nil if !mods.is(Lazy) =>
matchExpr
Expand All @@ -1166,8 +1167,16 @@ object desugar {
val restDefs =
for (((named, tpt), n) <- vars.zipWithIndex if named.name != nme.WILDCARD)
yield
if (mods.is(Lazy)) derivedDefDef(original, named, tpt, selector(n), mods &~ Lazy)
else derivedValDef(original, named, tpt, selector(n), mods)
if mods.is(Lazy) then
DefDef(named.name.asTermName, Nil, tpt, selector(n))
.withMods(mods &~ Lazy)
.withSpan(named.span)
else
valDef(
ValDef(named.name.asTermName, tpt, selector(n))
.withMods(mods)
.withSpan(named.span)
)
flatTree(firstDef :: restDefs)
}
}
Expand Down
10 changes: 8 additions & 2 deletions language-server/test/dotty/tools/languageserver/HoverTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,13 @@ class HoverTest {

@Test def tuple: Unit = {
code"""|object A:
| val (${m1}first${m2}, second) = (1, 2)""".withSource
.hover(m1 to m2, hoverContent("Int"))
| val (${m1}first${m2}, second) = (1.0, 2)""".withSource
.hover(m1 to m2, hoverContent("Double"))
}

@Test def multiAssigment: Unit = {
code"""|val ${m1}x${m2}, ${m3}y${m4} = 42.0""".withSource
.hover(m1 to m2, hoverContent("Double"))
.hover(m3 to m4, hoverContent("Double"))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -93,12 +93,6 @@ class WorksheetTest {
((m1 to m2), "val res0: String = odd"))
}

@Test def patternMatching1: Unit = {
ws"""${m1}val (foo, bar) = (1, 2)${m2}""".withSource
.run(m1,
((m1 to m2), s"val foo: Int = 1${nl}val bar: Int = 2"))
}

@Test def evaluationException: Unit = {
ws"""${m1}val foo = 1 / 0${m2}
${m3}val bar = 2${m4}""".withSource
Expand Down
8 changes: 7 additions & 1 deletion tests/neg/t5702-neg-bad-and-wild.check
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,17 @@
|
| longer explanation available when compiling with `-explain`
-- [E032] Syntax Error: tests/neg/t5702-neg-bad-and-wild.scala:23:17 ---------------------------------------------------
23 | val K(ns @ _*, x) = k // error: pattern expected
23 | val K(ns @ _*, xx) = k // error: pattern expected
| ^
| pattern expected
|
| longer explanation available when compiling with `-explain`
-- [E161] Naming Error: tests/neg/t5702-neg-bad-and-wild.scala:24:10 ---------------------------------------------------
24 | val K(x) = k // error: x is already defined as value x
| ^^^^^^^^^^^^
| x is already defined as value x
|
| Note that overloaded methods must all be defined in the same group of toplevel definitions
-- Error: tests/neg/t5702-neg-bad-and-wild.scala:10:21 -----------------------------------------------------------------
10 | case List(1, _*,) => // error: pattern expected // error
| ^
Expand Down
3 changes: 2 additions & 1 deletion tests/neg/t5702-neg-bad-and-wild.scala
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ object Test {
// good syntax, bad semantics, detected by typer
//gowild.scala:14: error: star patterns must correspond with varargs parameters
val K(x @ _*) = k
val K(ns @ _*, x) = k // error: pattern expected
val K(ns @ _*, xx) = k // error: pattern expected
val K(x) = k // error: x is already defined as value x
val (b, _ * ) = (5,6) // ok
// no longer complains
//bad-and-wild.scala:15: error: ')' expected but '}' found.
Expand Down

0 comments on commit 19fb504

Please sign in to comment.