Skip to content

Commit 361b5b2

Browse files
committed
Fix #4498: Go to definition in lifted expressions
Consider the following snippet: ``` val lst = new A :: Nil ``` This is rewritten as: ``` val lst = { val x$0 = new A Nil.::(x$0) } ``` During lifting, we need to assign positions to the trees. We were assigning the position of `new A` in the original code to `x$0` in `Nil.::(x$0)`, and the original position of `new A` to `val x$0 = new A` with a zero-extent. Still considering this snippet, this means that when trying to go to the definition on `new A`, the IDE would think that the user tried to go to the definition of `x$0`, because the IDE would look for the matching tree by position. This commit changes this so that the lifted definition (`val x$0 = new A`) gets the position of the original expression, and the reference gets a synthetic, zero-extent position. With this change, the IDE correctly finds the right tree when selecting `new A` in the original code. Fixes #4498
1 parent d97bce0 commit 361b5b2

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

compiler/src/dotty/tools/dotc/typer/EtaExpansion.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ abstract class Lifter {
4949
var liftedType = fullyDefinedType(expr.tpe.widen, "lifted expression", expr.pos)
5050
if (liftedFlags.is(Method)) liftedType = ExprType(liftedType)
5151
val lifted = ctx.newSymbol(ctx.owner, name, liftedFlags, liftedType, coord = positionCoord(expr.pos))
52-
defs += liftedDef(lifted, expr).withPos(expr.pos.focus)
53-
ref(lifted.termRef).withPos(expr.pos)
52+
defs += liftedDef(lifted, expr).withPos(expr.pos)
53+
ref(lifted.termRef).withPos(expr.pos.focus)
5454
}
5555

5656
/** Lift out common part of lhs tree taking part in an operator assignment such as

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,11 @@ class DefinitionTest {
3636
.definition(m5 to m6, List(m1 to m2))
3737
}
3838

39+
@Test def liftedExpression: Unit = {
40+
withSources(
41+
code"class ${m1}A${m2}",
42+
code"object B { val lst = new ${m3}A${m4} :: Nil }"
43+
).definition(m3 to m4, List(m1 to m2))
44+
}
45+
3946
}

0 commit comments

Comments
 (0)