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 committed Feb 18, 2022
1 parent d466f9f commit 2dfcd8c
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 6 deletions.
11 changes: 7 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,10 @@ 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)
val valDef =
if (mods.is(Lazy)) derivedDefDef(original, named, tpt, selector(n), mods &~ Lazy)
else derivedValDef(original, named, tpt, selector(n), mods)
valDef.withSpan(named.span)
flatTree(firstDef :: restDefs)
}
}
Expand Down
2 changes: 2 additions & 0 deletions compiler/src/dotty/tools/dotc/ast/NavigateAST.scala
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ object NavigateAST {
case xs: List[?] => childPath(xs.iterator, path)
case _ => path
}
// println(path1.head)
// println(path1.head.span)
if ((path1 ne path) &&
((bestFit eq path) ||
bestFit.head.span != path1.head.span &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ 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"))
}
}

0 comments on commit 2dfcd8c

Please sign in to comment.