Skip to content

Commit 1b0be01

Browse files
authored
fix: don't search for members in pc info when irrelevant (#22674)
resolves: scalameta/metals#7251 `membersBasedOnFlags` on type alias without rhs was throwing an error
2 parents abaae76 + 53b2786 commit 1b0be01

File tree

2 files changed

+54
-1
lines changed

2 files changed

+54
-1
lines changed

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

+7-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,13 @@ class SymbolInformationProvider(using Context):
5555
val classOwner =
5656
sym.ownersIterator.drop(1).find(s => s.isClass || s.is(Flags.Module))
5757
val overridden = sym.denot.allOverriddenSymbols.toList
58-
val memberDefAnnots = sym.info.membersBasedOnFlags(Flags.Method, Flags.EmptyFlags).flatMap(_.allSymbols).flatMap(_.denot.annotations)
58+
val memberDefAnnots =
59+
if classSym.exists then
60+
classSym.info
61+
.membersBasedOnFlags(Flags.Method, Flags.EmptyFlags)
62+
.flatMap(_.allSymbols)
63+
.flatMap(_.denot.annotations)
64+
else Nil
5965

6066
val pcSymbolInformation =
6167
PcSymbolInformation(

presentation-compiler/test/dotty/tools/pc/tests/info/InfoSuite.scala

+47
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ import scala.meta.pc.PcSymbolInformation
88
import dotty.tools.pc.base.BasePCSuite
99
import scala.language.unsafeNulls
1010
import org.junit.Test
11+
import scala.meta.internal.metals.CompilerOffsetParams
12+
import java.nio.file.Paths
13+
import scala.annotation.nowarn
1114

1215
class InfoSuite extends BasePCSuite {
1316

@@ -53,4 +56,48 @@ class InfoSuite extends BasePCSuite {
5356
|scala/collection/IterableOnceOps#flatMap().
5457
|""".stripMargin
5558
)
59+
60+
@Test def i7251 =
61+
withSource(
62+
"""|package a
63+
|sealed trait TA:
64+
| type SomeType
65+
|trait TB extends TA:
66+
| type SomeType = Int
67+
|""".stripMargin
68+
)
69+
val info = presentationCompiler.info("a/TA#SomeType#").get()
70+
assertNoDiff(info.get().symbol(), "a/TA#SomeType#")
71+
72+
@Test def memberDefsAnnotations =
73+
def assertMemberDefsAnnotations(symbol: String, expected: String) =
74+
val info = presentationCompiler.info(symbol).get()
75+
assertNoDiff(info.get().memberDefsAnnotations().asScala.mkString("\n"), expected, Some(symbol))
76+
withSource(
77+
"""|package a
78+
|import scala.annotation.nowarn
79+
|sealed trait TA:
80+
| @nowarn
81+
| def aaa = 1
82+
|
83+
|object O:
84+
| @nowarn
85+
| def aaa = 1
86+
|
87+
|class D:
88+
| @nowarn
89+
| def bbb = 1
90+
|""".stripMargin
91+
)
92+
assertMemberDefsAnnotations("a/TA#", "scala.annotation.nowarn")
93+
assertMemberDefsAnnotations("a/O.", "scala.annotation.nowarn")
94+
assertMemberDefsAnnotations("a/D#", "scala.annotation.nowarn")
95+
assertMemberDefsAnnotations("a/D#bbb().", "")
96+
97+
// hacky way to add a source file to the presentation compiler sources
98+
private def withSource(code: String) =
99+
val filename = "Hover.scala"
100+
val pcParams = CompilerOffsetParams(Paths.get(filename).toUri(), code, 0)
101+
presentationCompiler.hover(pcParams).get()
102+
56103
}

0 commit comments

Comments
 (0)