Skip to content

Commit 583a4c6

Browse files
kasiaMarektgodzik
authored andcommitted
fix: don't look for overshadow conflicts for symbols not in the scope
[Cherry-picked 6187ac0]
1 parent 91ceb8e commit 583a4c6

File tree

2 files changed

+73
-11
lines changed

2 files changed

+73
-11
lines changed

presentation-compiler/src/main/dotty/tools/pc/PcInlineValueProviderImpl.scala

+12-11
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import dotty.tools.dotc.interactive.Interactive
1818
import dotty.tools.dotc.interactive.InteractiveDriver
1919
import dotty.tools.dotc.util.SourcePosition
2020
import dotty.tools.pc.utils.InteractiveEnrichments.*
21+
import dotty.tools.pc.IndexedContext.Result
2122

2223
import org.eclipse.lsp4j as l
2324

@@ -49,7 +50,9 @@ final class PcInlineValueProviderImpl(
4950
DefinitionTree(defn, pos)
5051
}
5152
.toRight(Errors.didNotFindDefinition)
52-
symbols = symbolsUsedInDefn(definition.tree.rhs)
53+
path = Interactive.pathTo(unit.tpdTree, definition.tree.rhs.span)(using newctx)
54+
indexedContext = IndexedContext(Interactive.contextOfPath(path)(using newctx))
55+
symbols = symbolsUsedInDefn(definition.tree.rhs).filter(indexedContext.lookupSym(_) == Result.InScope)
5356
references <- getReferencesToInline(definition, allOccurences, symbols)
5457
yield
5558
val (deleteDefinition, refsEdits) = references
@@ -111,27 +114,25 @@ final class PcInlineValueProviderImpl(
111114
val adjustedEnd = extend(pos.end - 1, ')', 1) + 1
112115
text.slice(adjustedStart, adjustedEnd).mkString
113116

114-
private def symbolsUsedInDefn(
115-
rhs: Tree
116-
): List[Symbol] =
117+
private def symbolsUsedInDefn(rhs: Tree): Set[Symbol] =
117118
def collectNames(
118-
symbols: List[Symbol],
119+
symbols: Set[Symbol],
119120
tree: Tree
120-
): List[Symbol] =
121+
): Set[Symbol] =
121122
tree match
122123
case id: (Ident | Select)
123124
if !id.symbol.is(Synthetic) && !id.symbol.is(Implicit) =>
124-
tree.symbol :: symbols
125+
symbols + tree.symbol
125126
case _ => symbols
126127

127-
val traverser = new DeepFolder[List[Symbol]](collectNames)
128-
traverser(List(), rhs)
128+
val traverser = new DeepFolder[Set[Symbol]](collectNames)
129+
traverser(Set(), rhs)
129130
end symbolsUsedInDefn
130131

131132
private def getReferencesToInline(
132133
definition: DefinitionTree,
133134
allOccurences: List[Occurence],
134-
symbols: List[Symbol]
135+
symbols: Set[Symbol]
135136
): Either[String, (Boolean, List[Reference])] =
136137
val defIsLocal = definition.tree.symbol.ownersIterator
137138
.drop(1)
@@ -156,7 +157,7 @@ final class PcInlineValueProviderImpl(
156157

157158
private def makeRefsEdits(
158159
refs: List[Occurence],
159-
symbols: List[Symbol]
160+
symbols: Set[Symbol]
160161
): Either[String, List[Reference]] =
161162
val newctx = driver.currentCtx.fresh.setCompilationUnit(unit)
162163
def buildRef(occurrence: Occurence): Either[String, Reference] =

presentation-compiler/test/dotty/tools/pc/tests/edit/InlineValueSuite.scala

+61
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,47 @@ class InlineValueSuite extends BaseCodeActionSuite with CommonMtagsEnrichments:
301301
|}""".stripMargin
302302
)
303303

304+
305+
@Test def `i6924` =
306+
checkEdit(
307+
"""|object O {
308+
| def test(n: Int) = {
309+
| val isOne = n == 1
310+
| <<i>>sOne
311+
| }
312+
|}
313+
|""".stripMargin,
314+
"""|object O {
315+
| def test(n: Int) = {
316+
| n == 1
317+
| }
318+
|}
319+
|""".stripMargin
320+
)
321+
322+
@Test def `i6924-2` =
323+
checkEdit(
324+
"""|object O {
325+
| def ==(o: O) = false
326+
|}
327+
|object P {
328+
| def test() = {
329+
| val isOne = O == O
330+
| <<i>>sOne
331+
| }
332+
|}
333+
|""".stripMargin,
334+
"""|object O {
335+
| def ==(o: O) = false
336+
|}
337+
|object P {
338+
| def test() = {
339+
| O == O
340+
| }
341+
|}
342+
|""".stripMargin
343+
)
344+
304345
@Test def `scoping-packages` =
305346
checkError(
306347
"""|package a
@@ -346,6 +387,26 @@ class InlineValueSuite extends BaseCodeActionSuite with CommonMtagsEnrichments:
346387
InlineErrors.variablesAreShadowed("A.k")
347388
)
348389

390+
@Test def `bad-scoping-3` =
391+
checkError(
392+
"""|class T {
393+
| val a = 1
394+
|}
395+
|
396+
|class O {
397+
| val t = new T()
398+
| import t._
399+
| val bb = a + a
400+
|
401+
| class Inner {
402+
| val a = 123
403+
| val cc = <<b>>b
404+
| }
405+
|}
406+
|""".stripMargin,
407+
InlineErrors.variablesAreShadowed("T.a")
408+
)
409+
349410
def checkEdit(
350411
original: String,
351412
expected: String,

0 commit comments

Comments
 (0)