From 53b2786ab9351d852e0352629bcc698c44ead15b Mon Sep 17 00:00:00 2001 From: kasiaMarek Date: Thu, 27 Feb 2025 12:33:18 +0100 Subject: [PATCH] fix: don't search for members in pc info when irrelevant --- .../tools/pc/SymbolInformationProvider.scala | 8 +++- .../dotty/tools/pc/tests/info/InfoSuite.scala | 47 +++++++++++++++++++ 2 files changed, 54 insertions(+), 1 deletion(-) diff --git a/presentation-compiler/src/main/dotty/tools/pc/SymbolInformationProvider.scala b/presentation-compiler/src/main/dotty/tools/pc/SymbolInformationProvider.scala index ccda618078b8..8bed605a87f8 100644 --- a/presentation-compiler/src/main/dotty/tools/pc/SymbolInformationProvider.scala +++ b/presentation-compiler/src/main/dotty/tools/pc/SymbolInformationProvider.scala @@ -55,7 +55,13 @@ class SymbolInformationProvider(using Context): val classOwner = sym.ownersIterator.drop(1).find(s => s.isClass || s.is(Flags.Module)) val overridden = sym.denot.allOverriddenSymbols.toList - val memberDefAnnots = sym.info.membersBasedOnFlags(Flags.Method, Flags.EmptyFlags).flatMap(_.allSymbols).flatMap(_.denot.annotations) + val memberDefAnnots = + if classSym.exists then + classSym.info + .membersBasedOnFlags(Flags.Method, Flags.EmptyFlags) + .flatMap(_.allSymbols) + .flatMap(_.denot.annotations) + else Nil val pcSymbolInformation = PcSymbolInformation( diff --git a/presentation-compiler/test/dotty/tools/pc/tests/info/InfoSuite.scala b/presentation-compiler/test/dotty/tools/pc/tests/info/InfoSuite.scala index 8464c4d69da4..57ceb7a099d7 100644 --- a/presentation-compiler/test/dotty/tools/pc/tests/info/InfoSuite.scala +++ b/presentation-compiler/test/dotty/tools/pc/tests/info/InfoSuite.scala @@ -8,6 +8,9 @@ import scala.meta.pc.PcSymbolInformation import dotty.tools.pc.base.BasePCSuite import scala.language.unsafeNulls import org.junit.Test +import scala.meta.internal.metals.CompilerOffsetParams +import java.nio.file.Paths +import scala.annotation.nowarn class InfoSuite extends BasePCSuite { @@ -53,4 +56,48 @@ class InfoSuite extends BasePCSuite { |scala/collection/IterableOnceOps#flatMap(). |""".stripMargin ) + + @Test def i7251 = + withSource( + """|package a + |sealed trait TA: + | type SomeType + |trait TB extends TA: + | type SomeType = Int + |""".stripMargin + ) + val info = presentationCompiler.info("a/TA#SomeType#").get() + assertNoDiff(info.get().symbol(), "a/TA#SomeType#") + + @Test def memberDefsAnnotations = + def assertMemberDefsAnnotations(symbol: String, expected: String) = + val info = presentationCompiler.info(symbol).get() + assertNoDiff(info.get().memberDefsAnnotations().asScala.mkString("\n"), expected, Some(symbol)) + withSource( + """|package a + |import scala.annotation.nowarn + |sealed trait TA: + | @nowarn + | def aaa = 1 + | + |object O: + | @nowarn + | def aaa = 1 + | + |class D: + | @nowarn + | def bbb = 1 + |""".stripMargin + ) + assertMemberDefsAnnotations("a/TA#", "scala.annotation.nowarn") + assertMemberDefsAnnotations("a/O.", "scala.annotation.nowarn") + assertMemberDefsAnnotations("a/D#", "scala.annotation.nowarn") + assertMemberDefsAnnotations("a/D#bbb().", "") + + // hacky way to add a source file to the presentation compiler sources + private def withSource(code: String) = + val filename = "Hover.scala" + val pcParams = CompilerOffsetParams(Paths.get(filename).toUri(), code, 0) + presentationCompiler.hover(pcParams).get() + }