Skip to content

Commit cc77e28

Browse files
authored
Merge pull request #399 from scala/backport-lts-3.3-22722
Backport "fix: make pc actions work for re-exported symbols" to 3.3 LTS
2 parents 51cfb07 + a0ad379 commit cc77e28

File tree

11 files changed

+119
-13
lines changed

11 files changed

+119
-13
lines changed

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -1518,7 +1518,7 @@ object tpd extends Trees.Instance[Type] with TypedTreeInfo {
15181518
* @param selectorPredicate A test to find the selector to use.
15191519
* @return The symbols imported.
15201520
*/
1521-
def importedSymbols(imp: Import,
1521+
def importedSymbols(imp: ImportOrExport,
15221522
selectorPredicate: untpd.ImportSelector => Boolean = util.common.alwaysTrue)
15231523
(using Context): List[Symbol] =
15241524
imp.selectors.find(selectorPredicate) match

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -1296,7 +1296,7 @@ class Namer { typer: Typer =>
12961296
val ddef = tpd.DefDef(forwarder.asTerm, prefss => {
12971297
val forwarderCtx = ctx.withOwner(forwarder)
12981298
val (pathRefss, methRefss) = prefss.splitAt(extensionParamsCount(path.tpe.widen))
1299-
val ref = path.appliedToArgss(pathRefss).select(sym.asTerm)
1299+
val ref = path.appliedToArgss(pathRefss).select(sym.asTerm).withSpan(span.focus)
13001300
val rhs = ref.appliedToArgss(adaptForwarderParams(Nil, sym.info, methRefss))
13011301
.etaExpandCFT(using forwarderCtx)
13021302
if forwarder.isInlineMethod then

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ object MetalsInteractive:
135135
(sym, sym.info, None)
136136
)
137137

138-
case (imp: Import) :: _ =>
138+
case (imp: ImportOrExport) :: _ =>
139139
importedSymbols(imp, _.span.contains(pos.span)).map(sym =>
140140
(sym, sym.info, None)
141141
)

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

+8-5
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,8 @@ trait PcCollector[T]:
6363
o.span.exists && o.span.point == named.symbol.owner.span.point
6464
)
6565

66-
def soughtOrOverride(sym: Symbol) =
66+
def soughtOrOverride(sym0: Symbol) =
67+
val sym = if sym0.is(Flags.Exported) then sym0.sourceSymbol else sym0
6768
sought(sym) || sym.allOverriddenSymbols.exists(sought(_))
6869

6970
def soughtTreeFilter(tree: Tree): Boolean =
@@ -76,7 +77,7 @@ trait PcCollector[T]:
7677
case df: NamedDefTree
7778
if soughtOrOverride(df.symbol) && !df.symbol.isSetter =>
7879
true
79-
case imp: Import if owners(imp.expr.symbol) => true
80+
case imp: ImportOrExport if owners(imp.expr.symbol) => true
8081
case _ => false
8182

8283
def soughtFilter(f: Symbol => Boolean): Boolean =
@@ -115,11 +116,13 @@ trait PcCollector[T]:
115116
*/
116117
case ident: Ident if ident.isCorrectSpan && filter(ident) =>
117118
// symbols will differ for params in different ext methods, but source pos will be the same
118-
if soughtFilter(_.sourcePos == ident.symbol.sourcePos)
119+
val symbol = if ident.symbol.is(Flags.Exported) then ident.symbol.sourceSymbol else ident.symbol
120+
if soughtFilter(_.sourcePos == symbol.sourcePos)
119121
then
120122
occurrences + collect(
121123
ident,
122-
ident.sourcePos
124+
ident.sourcePos,
125+
Some(symbol)
123126
)
124127
else occurrences
125128
/**
@@ -228,7 +231,7 @@ trait PcCollector[T]:
228231
* For traversing import selectors:
229232
* import scala.util.<<Try>>
230233
*/
231-
case imp: Import if filter(imp) =>
234+
case imp: ImportOrExport if filter(imp) =>
232235
imp.selectors
233236
.collect {
234237
case sel: ImportSelector

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ trait PcSymbolSearch:
4949
lazy val soughtSymbols: Option[(Set[Symbol], SourcePosition)] =
5050
soughtSymbols(path)
5151

52-
def soughtSymbols(path: List[Tree]): Option[(Set[Symbol], SourcePosition)] =
52+
private def soughtSymbols(path: List[Tree]): Option[(Set[Symbol], SourcePosition)] =
5353
val sought = path match
5454
/* reference of an extension paramter
5555
* extension [EF](<<xs>>: List[EF])
@@ -148,7 +148,7 @@ trait PcSymbolSearch:
148148
/* Import selectors:
149149
* import scala.util.Tr@@y
150150
*/
151-
case (imp: Import) :: _ if imp.span.contains(pos.span) =>
151+
case (imp: ImportOrExport) :: _ if imp.span.contains(pos.span) =>
152152
imp
153153
.selector(pos.span)
154154
.map(sym => (symbolAlternatives(sym), sym.sourcePos))

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

+3-1
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,9 @@ class WithCompilationUnit(
7676
}
7777
else Set.empty
7878
val all =
79-
if sym.is(Flags.ModuleClass) then
79+
if sym.is(Flags.Exported) then
80+
Set(sym, sym.sourceSymbol)
81+
else if sym.is(Flags.ModuleClass) then
8082
Set(sym, sym.companionModule, sym.companionModule.companion)
8183
else if sym.isClass then
8284
Set(sym, sym.companionModule, sym.companion.moduleClass)

presentation-compiler/src/main/dotty/tools/pc/utils/InteractiveEnrichments.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,7 @@ object InteractiveEnrichments extends CommonMtagsEnrichments:
339339
end enclosedChildren
340340
end extension
341341

342-
extension (imp: Import)
342+
extension (imp: ImportOrExport)
343343
def selector(span: Span)(using Context): Option[Symbol] =
344344
for sel <- imp.selectors.find(_.span.contains(span))
345345
yield imp.expr.symbol.info.member(sel.name).symbol

presentation-compiler/test/dotty/tools/pc/tests/definition/PcDefinitionSuite.scala

+37-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ class PcDefinitionSuite extends BasePcDefinitionSuite:
2828
MockLocation("scala/Predef.Ensuring#ensuring(+2).", "Predef.scala"),
2929
MockLocation("scala/Predef.Ensuring#ensuring(+3).", "Predef.scala"),
3030
MockLocation("scala/collection/immutable/List#`::`().", "List.scala"),
31-
MockLocation("scala/package.List.", "package.scala")
31+
MockLocation("scala/package.List.", "package.scala"),
32+
MockLocation("scala/collection/immutable/Vector.", "Vector.scala"),
3233
)
3334

3435
override def definitions(offsetParams: OffsetParams): List[Location] =
@@ -603,3 +604,38 @@ class PcDefinitionSuite extends BasePcDefinitionSuite:
603604
|""".stripMargin
604605
)
605606

607+
@Test def i7256 =
608+
check(
609+
"""|object Test:
610+
| def <<methodA>>: Unit = ???
611+
|export Test.me@@thodA
612+
|""".stripMargin
613+
)
614+
615+
@Test def `i7256-2` =
616+
check(
617+
"""|object Test:
618+
| def <<methodA>>: Unit = ???
619+
| def methodB: Unit = ???
620+
|export Test.{me@@thodA, methodB}
621+
|""".stripMargin
622+
)
623+
624+
@Test def `i7256-3` =
625+
check(
626+
"""|object Test:
627+
| def <<methodA>>: Unit = ???
628+
| def methodB: Unit = ???
629+
|export Test.{methodA, methodB}
630+
|
631+
|val i = met@@hodA
632+
|""".stripMargin
633+
)
634+
635+
@Test def i7427 =
636+
check(
637+
"""|package a
638+
|object Repro:
639+
| export scala.collection.immutable.V/*scala/collection/immutable/Vector. Vector.scala*/@@ector
640+
|""".stripMargin
641+
)

presentation-compiler/test/dotty/tools/pc/tests/highlight/DocumentHighlightSuite.scala

+40
Original file line numberDiff line numberDiff line change
@@ -1486,5 +1486,45 @@ class DocumentHighlightSuite extends BaseDocumentHighlightSuite:
14861486
|""".stripMargin
14871487
)
14881488

1489+
@Test def i7256 =
1490+
check(
1491+
"""|package a
1492+
|object Test:
1493+
| def <<methodA>>: Unit = ???
1494+
|export Test.<<me@@thodA>>
1495+
|val i = <<methodA>>
1496+
|""".stripMargin
1497+
)
1498+
1499+
@Test def `i7256-2` =
1500+
check(
1501+
"""|object Test:
1502+
| def <<methodA>>: Unit = ???
1503+
| def methodB: Unit = ???
1504+
|export Test.{<<me@@thodA>>, methodB}
1505+
|val i = <<methodA>>
1506+
|""".stripMargin
1507+
)
1508+
1509+
1510+
@Test def `i7256-3` =
1511+
check(
1512+
"""|object Test:
1513+
| def <<methodA>>: Unit = ???
1514+
| def methodB: Unit = ???
1515+
|export Test.<<methodA>>
1516+
|val i = <<met@@hodA>>
1517+
|val j = <<methodA>>
1518+
|""".stripMargin
1519+
)
1520+
1521+
@Test def `i7256-4` =
1522+
check(
1523+
"""|object Test:
1524+
| class <<Foo>>
1525+
| object <<Foo>>
1526+
|export Test.<<F@@oo>>
1527+
|""".stripMargin
1528+
)
14891529

14901530
end DocumentHighlightSuite

presentation-compiler/test/dotty/tools/pc/tests/hover/HoverDefnSuite.scala

+17
Original file line numberDiff line numberDiff line change
@@ -238,3 +238,20 @@ class HoverDefnSuite extends BaseHoverSuite:
238238
|""".stripMargin,
239239
"val foo: Int".hover
240240
)
241+
242+
@Test def i7256 =
243+
check(
244+
"""|object Test:
245+
| def methodA: Unit = ???
246+
|export Test.me@@thodA
247+
|""".stripMargin,
248+
"""|**Expression type**:
249+
|```scala
250+
|=> Unit
251+
|```
252+
|**Symbol signature**:
253+
|```scala
254+
|def methodA: Unit
255+
|```
256+
|""".stripMargin
257+
)

presentation-compiler/test/dotty/tools/pc/tests/tokens/SemanticTokensSuite.scala

+8
Original file line numberDiff line numberDiff line change
@@ -459,3 +459,11 @@ class SemanticTokensSuite extends BaseSemanticTokensSuite:
459459
| end <<foo>>/*class,definition*/
460460
|""".stripMargin
461461
)
462+
463+
@Test def i7256 =
464+
check(
465+
"""|object <<Test>>/*class*/:
466+
| def <<methodA>>/*method,definition*/: <<Unit>>/*class,abstract*/ = <<???>>/*method*/
467+
|export <<Test>>/*class*/.<<methodA>>/*method*/
468+
|""".stripMargin
469+
)

0 commit comments

Comments
 (0)