Skip to content

Add annotations to semanticdb #13818

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions compiler/src/dotty/tools/dotc/semanticdb/PPrint.scala
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ class SymbolInformationPrinter (symtab: PrinterSymtab):
case Reference, Definition
def pprint(info: SymbolInformation): String =
val sb = new StringBuilder()
val annotStr = info.annotations.map(pprint).mkString(" ")
if annotStr.nonEmpty then
sb.append(annotStr + " ")
sb.append(accessString(info.access))
if info.isAbstract then sb.append("abstract ")
if info.isFinal then sb.append("final ")
Expand Down
5 changes: 5 additions & 0 deletions compiler/src/dotty/tools/dotc/semanticdb/Scala3.scala
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,10 @@ object Scala3:
val kind = s.symbolKind(symkinds)
val sname = sym.symbolName
val signature = s.info.toSemanticSig(s)
val symbolAnnotations = s.annotations.collect{
case annot if annot.symbol != defn.BodyAnnot && annot.symbol != defn.ChildAnnot =>
Annotation(annot.tree.tpe.toSemanticType(annot.symbol))
}
SymbolInformation(
symbol = sname,
language = Language.SCALA,
Expand All @@ -104,6 +108,7 @@ object Scala3:
signature = signature,
access = s.symbolAccess(kind),
overriddenSymbols = s.overriddenSymbols,
annotations = symbolAnnotations
)
case s: WildcardTypeSymbol =>
SymbolInformation(
Expand Down
3 changes: 3 additions & 0 deletions tests/semanticdb/expect/Annotations.expect.scala
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ class Annotations/*<-annot::Annotations#*/[@TypeParameterAnnotation/*->com::java
class B/*<-annot::B#*/ @ConstructorAnnotation/*->com::javacp::annot::ConstructorAnnotation#*/()(x/*<-annot::B#x.*/: Int/*->scala::Int#*/) {
@ConstructorAnnotation/*->com::javacp::annot::ConstructorAnnotation#*/
def this/*<-annot::B#`<init>`(+1).*/() = this(42)

@throws/*->scala::throws#*/[Exception/*->scala::package.Exception#*/]
def throwing/*<-annot::B#throwing().*/ = throw new Exception/*->scala::package.Exception#*/("")
}

@ObjectAnnotation/*->com::javacp::annot::ObjectAnnotation#*/
Expand Down
3 changes: 3 additions & 0 deletions tests/semanticdb/expect/Annotations.scala
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ class Annotations[@TypeParameterAnnotation T](@ParameterAnnotation x: T) { self:
class B @ConstructorAnnotation()(x: Int) {
@ConstructorAnnotation
def this() = this(42)

@throws[Exception]
def throwing = throw new Exception("")
}

@ObjectAnnotation
Expand Down
75 changes: 40 additions & 35 deletions tests/semanticdb/metac.expect
Original file line number Diff line number Diff line change
Expand Up @@ -222,32 +222,33 @@ Schema => SemanticDB v4
Uri => Annotations.scala
Text => empty
Language => Scala
Symbols => 22 entries
Occurrences => 48 entries
Symbols => 23 entries
Occurrences => 52 entries

Symbols:
annot/Alias. => final object Alias extends Object { self: Alias.type => +2 decls }
annot/Alias.A# => type A = ClassAnnotation @param
annot/Annotations# => class Annotations [typeparam T ] extends Object { self: AnyRef & Annotations[T] => +6 decls }
annot/Annotations#S# => type S
annot/Annotations#[T] => typeparam T
annot/Annotations#`<init>`(). => primary ctor <init> [typeparam T ](param x: T): Annotations[T]
annot/Annotations#`<init>`().(x) => param x: T
annot/Annotations#field. => val method field Int
annot/Annotations#method(). => method method => Int
annot/Annotations#x. => private[this] val method x T
annot/B# => class B extends Object { self: B => +3 decls }
annot/B#`<init>`(). => primary ctor <init> (param x: Int): B
annot/Annotations# => @ClassAnnotation class Annotations [@TypeParameterAnnotation typeparam T ] extends Object { self: AnyRef & Annotations[T] => +6 decls }
annot/Annotations#S# => @TypeAnnotation type S
annot/Annotations#[T] => @TypeParameterAnnotation typeparam T
annot/Annotations#`<init>`(). => primary ctor <init> [@TypeParameterAnnotation typeparam T ](@ParameterAnnotation param x: T): Annotations[T]
annot/Annotations#`<init>`().(x) => @ParameterAnnotation param x: T
annot/Annotations#field. => @FieldAnnotation val method field Int
annot/Annotations#method(). => @MethodAnnotation method method => Int
annot/Annotations#x. => @ParameterAnnotation private[this] val method x T
annot/B# => class B extends Object { self: B => +4 decls }
annot/B#`<init>`(). => @ConstructorAnnotation primary ctor <init> (param x: Int): B
annot/B#`<init>`().(x) => param x: Int
annot/B#`<init>`(+1). => ctor <init> (): B
annot/B#`<init>`(+1). => @ConstructorAnnotation ctor <init> (): B
annot/B#throwing(). => @throws[Exception] method throwing => Nothing
annot/B#x. => private[this] val method x Int
annot/M. => final object M extends Object { self: M.type => +1 decls }
annot/M.m(). => macro m [typeparam TT ]: Int
annot/M. => @ObjectAnnotation final object M extends Object { self: M.type => +1 decls }
annot/M.m(). => @MacroAnnotation macro m [typeparam TT ]: Int
annot/M.m().[TT] => typeparam TT
annot/T# => trait T extends Object { self: T => +1 decls }
annot/T# => @TraitAnnotation trait T extends Object { self: T => +1 decls }
annot/T#`<init>`(). => primary ctor <init> (): T
local0 => selfparam self: AnyRef
local1 => val local local: Int
local1 => @LocalAnnotation val local local: Int

Occurrences:
[0:8..0:13): annot <- annot/
Expand Down Expand Up @@ -285,19 +286,23 @@ Occurrences:
[21:36..21:39): Int -> scala/Int#
[22:3..22:24): ConstructorAnnotation -> com/javacp/annot/ConstructorAnnotation#
[23:6..23:10): <- annot/B#`<init>`(+1).
[26:1..26:17): ObjectAnnotation -> com/javacp/annot/ObjectAnnotation#
[27:7..27:8): M <- annot/M.
[28:3..28:18): MacroAnnotation -> com/javacp/annot/MacroAnnotation#
[29:6..29:7): m <- annot/M.m().
[29:8..29:10): TT <- annot/M.m().[TT]
[29:13..29:16): Int -> scala/Int#
[29:25..29:28): ??? -> scala/Predef.`???`().
[32:1..32:16): TraitAnnotation -> com/javacp/annot/TraitAnnotation#
[33:6..33:7): T <- annot/T#
[35:7..35:12): Alias <- annot/Alias.
[36:7..36:8): A <- annot/Alias.A#
[36:11..36:26): ClassAnnotation -> com/javacp/annot/ClassAnnotation#
[36:28..36:33): param -> scala/annotation/meta/param#
[25:3..25:9): throws -> scala/throws#
[25:10..25:19): Exception -> scala/package.Exception#
[26:6..26:14): throwing <- annot/B#throwing().
[26:27..26:36): Exception -> scala/package.Exception#
[29:1..29:17): ObjectAnnotation -> com/javacp/annot/ObjectAnnotation#
[30:7..30:8): M <- annot/M.
[31:3..31:18): MacroAnnotation -> com/javacp/annot/MacroAnnotation#
[32:6..32:7): m <- annot/M.m().
[32:8..32:10): TT <- annot/M.m().[TT]
[32:13..32:16): Int -> scala/Int#
[32:25..32:28): ??? -> scala/Predef.`???`().
[35:1..35:16): TraitAnnotation -> com/javacp/annot/TraitAnnotation#
[36:6..36:7): T <- annot/T#
[38:7..38:12): Alias <- annot/Alias.
[39:7..39:8): A <- annot/Alias.A#
[39:11..39:26): ClassAnnotation -> com/javacp/annot/ClassAnnotation#
[39:28..39:33): param -> scala/annotation/meta/param#

expect/Anonymous.scala
----------------------
Expand Down Expand Up @@ -4082,9 +4087,9 @@ flags/p/package.C#w(). => abstract method w => Int
flags/p/package.C#x. => private[this] val method x T
flags/p/package.C#y. => private[this] val method y U
flags/p/package.C#z. => private[this] val method z V
flags/p/package.S# => class S [typeparam T ] extends Object { self: S[T] => +2 decls }
flags/p/package.S#[T] => typeparam T
flags/p/package.S#`<init>`(). => primary ctor <init> [typeparam T ](): S[T]
flags/p/package.S# => class S [@specialized typeparam T ] extends Object { self: S[T] => +2 decls }
flags/p/package.S#[T] => @specialized typeparam T
flags/p/package.S#`<init>`(). => primary ctor <init> [@specialized typeparam T ](): S[T]
flags/p/package.T1# => type T1 = Int
flags/p/package.T2# => type T2 [typeparam T ] = S[T]
flags/p/package.T2#[T] => typeparam T
Expand Down Expand Up @@ -4635,7 +4640,7 @@ Symbols:
_empty_/MyProgram# => final class MyProgram extends Object { self: MyProgram => +2 decls }
_empty_/readInts# => final class readInts extends Object { self: readInts => +2 decls }
_empty_/toplevel$package. => final package object _empty_ extends Object { self: _empty_.type => +9 decls }
_empty_/toplevel$package.MyProgram(). => method MyProgram (param times: Int): Unit
_empty_/toplevel$package.MyProgram(). => @main method MyProgram (param times: Int): Unit
_empty_/toplevel$package.MyProgram().(times) => param times: Int
_empty_/toplevel$package.a. => val inline method a ""
_empty_/toplevel$package.combine(). => method combine (param x: Int)(param y: Int): Int
Expand All @@ -4648,7 +4653,7 @@ _empty_/toplevel$package.combine(+1).(z) => param z: Int
_empty_/toplevel$package.combine(+2). => method combine => Int
_empty_/toplevel$package.foo(). => method foo => String
_empty_/toplevel$package.fooRef(). => method fooRef => String
_empty_/toplevel$package.readInts(). => method readInts (param ints: Int*): Unit
_empty_/toplevel$package.readInts(). => @main method readInts (param ints: Int*): Unit
_empty_/toplevel$package.readInts().(ints) => param ints: Int*

Occurrences:
Expand Down