Skip to content

Commit e1930d1

Browse files
authored
Merge pull request #13818 from tgodzik/add-annotations
Add annotations to semanticdb
2 parents bd57121 + 8fa8c36 commit e1930d1

File tree

5 files changed

+54
-35
lines changed

5 files changed

+54
-35
lines changed

compiler/src/dotty/tools/dotc/semanticdb/PPrint.scala

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ class SymbolInformationPrinter (symtab: PrinterSymtab):
3535
case Reference, Definition
3636
def pprint(info: SymbolInformation): String =
3737
val sb = new StringBuilder()
38+
val annotStr = info.annotations.map(pprint).mkString(" ")
39+
if annotStr.nonEmpty then
40+
sb.append(annotStr + " ")
3841
sb.append(accessString(info.access))
3942
if info.isAbstract then sb.append("abstract ")
4043
if info.isFinal then sb.append("final ")

compiler/src/dotty/tools/dotc/semanticdb/Scala3.scala

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,10 @@ object Scala3:
9595
val kind = s.symbolKind(symkinds)
9696
val sname = sym.symbolName
9797
val signature = s.info.toSemanticSig(s)
98+
val symbolAnnotations = s.annotations.collect{
99+
case annot if annot.symbol != defn.BodyAnnot && annot.symbol != defn.ChildAnnot =>
100+
Annotation(annot.tree.tpe.toSemanticType(annot.symbol))
101+
}
98102
SymbolInformation(
99103
symbol = sname,
100104
language = Language.SCALA,
@@ -104,6 +108,7 @@ object Scala3:
104108
signature = signature,
105109
access = s.symbolAccess(kind),
106110
overriddenSymbols = s.overriddenSymbols,
111+
annotations = symbolAnnotations
107112
)
108113
case s: WildcardTypeSymbol =>
109114
SymbolInformation(

tests/semanticdb/expect/Annotations.expect.scala

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ class Annotations/*<-annot::Annotations#*/[@TypeParameterAnnotation/*->com::java
2222
class B/*<-annot::B#*/ @ConstructorAnnotation/*->com::javacp::annot::ConstructorAnnotation#*/()(x/*<-annot::B#x.*/: Int/*->scala::Int#*/) {
2323
@ConstructorAnnotation/*->com::javacp::annot::ConstructorAnnotation#*/
2424
def this/*<-annot::B#`<init>`(+1).*/() = this(42)
25+
26+
@throws/*->scala::throws#*/[Exception/*->scala::package.Exception#*/]
27+
def throwing/*<-annot::B#throwing().*/ = throw new Exception/*->scala::package.Exception#*/("")
2528
}
2629

2730
@ObjectAnnotation/*->com::javacp::annot::ObjectAnnotation#*/

tests/semanticdb/expect/Annotations.scala

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ class Annotations[@TypeParameterAnnotation T](@ParameterAnnotation x: T) { self:
2222
class B @ConstructorAnnotation()(x: Int) {
2323
@ConstructorAnnotation
2424
def this() = this(42)
25+
26+
@throws[Exception]
27+
def throwing = throw new Exception("")
2528
}
2629

2730
@ObjectAnnotation

tests/semanticdb/metac.expect

Lines changed: 40 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -222,32 +222,33 @@ Schema => SemanticDB v4
222222
Uri => Annotations.scala
223223
Text => empty
224224
Language => Scala
225-
Symbols => 22 entries
226-
Occurrences => 48 entries
225+
Symbols => 23 entries
226+
Occurrences => 52 entries
227227

228228
Symbols:
229229
annot/Alias. => final object Alias extends Object { self: Alias.type => +2 decls }
230230
annot/Alias.A# => type A = ClassAnnotation @param
231-
annot/Annotations# => class Annotations [typeparam T ] extends Object { self: AnyRef & Annotations[T] => +6 decls }
232-
annot/Annotations#S# => type S
233-
annot/Annotations#[T] => typeparam T
234-
annot/Annotations#`<init>`(). => primary ctor <init> [typeparam T ](param x: T): Annotations[T]
235-
annot/Annotations#`<init>`().(x) => param x: T
236-
annot/Annotations#field. => val method field Int
237-
annot/Annotations#method(). => method method => Int
238-
annot/Annotations#x. => private[this] val method x T
239-
annot/B# => class B extends Object { self: B => +3 decls }
240-
annot/B#`<init>`(). => primary ctor <init> (param x: Int): B
231+
annot/Annotations# => @ClassAnnotation class Annotations [@TypeParameterAnnotation typeparam T ] extends Object { self: AnyRef & Annotations[T] => +6 decls }
232+
annot/Annotations#S# => @TypeAnnotation type S
233+
annot/Annotations#[T] => @TypeParameterAnnotation typeparam T
234+
annot/Annotations#`<init>`(). => primary ctor <init> [@TypeParameterAnnotation typeparam T ](@ParameterAnnotation param x: T): Annotations[T]
235+
annot/Annotations#`<init>`().(x) => @ParameterAnnotation param x: T
236+
annot/Annotations#field. => @FieldAnnotation val method field Int
237+
annot/Annotations#method(). => @MethodAnnotation method method => Int
238+
annot/Annotations#x. => @ParameterAnnotation private[this] val method x T
239+
annot/B# => class B extends Object { self: B => +4 decls }
240+
annot/B#`<init>`(). => @ConstructorAnnotation primary ctor <init> (param x: Int): B
241241
annot/B#`<init>`().(x) => param x: Int
242-
annot/B#`<init>`(+1). => ctor <init> (): B
242+
annot/B#`<init>`(+1). => @ConstructorAnnotation ctor <init> (): B
243+
annot/B#throwing(). => @throws[Exception] method throwing => Nothing
243244
annot/B#x. => private[this] val method x Int
244-
annot/M. => final object M extends Object { self: M.type => +1 decls }
245-
annot/M.m(). => macro m [typeparam TT ]: Int
245+
annot/M. => @ObjectAnnotation final object M extends Object { self: M.type => +1 decls }
246+
annot/M.m(). => @MacroAnnotation macro m [typeparam TT ]: Int
246247
annot/M.m().[TT] => typeparam TT
247-
annot/T# => trait T extends Object { self: T => +1 decls }
248+
annot/T# => @TraitAnnotation trait T extends Object { self: T => +1 decls }
248249
annot/T#`<init>`(). => primary ctor <init> (): T
249250
local0 => selfparam self: AnyRef
250-
local1 => val local local: Int
251+
local1 => @LocalAnnotation val local local: Int
251252

252253
Occurrences:
253254
[0:8..0:13): annot <- annot/
@@ -285,19 +286,23 @@ Occurrences:
285286
[21:36..21:39): Int -> scala/Int#
286287
[22:3..22:24): ConstructorAnnotation -> com/javacp/annot/ConstructorAnnotation#
287288
[23:6..23:10): <- annot/B#`<init>`(+1).
288-
[26:1..26:17): ObjectAnnotation -> com/javacp/annot/ObjectAnnotation#
289-
[27:7..27:8): M <- annot/M.
290-
[28:3..28:18): MacroAnnotation -> com/javacp/annot/MacroAnnotation#
291-
[29:6..29:7): m <- annot/M.m().
292-
[29:8..29:10): TT <- annot/M.m().[TT]
293-
[29:13..29:16): Int -> scala/Int#
294-
[29:25..29:28): ??? -> scala/Predef.`???`().
295-
[32:1..32:16): TraitAnnotation -> com/javacp/annot/TraitAnnotation#
296-
[33:6..33:7): T <- annot/T#
297-
[35:7..35:12): Alias <- annot/Alias.
298-
[36:7..36:8): A <- annot/Alias.A#
299-
[36:11..36:26): ClassAnnotation -> com/javacp/annot/ClassAnnotation#
300-
[36:28..36:33): param -> scala/annotation/meta/param#
289+
[25:3..25:9): throws -> scala/throws#
290+
[25:10..25:19): Exception -> scala/package.Exception#
291+
[26:6..26:14): throwing <- annot/B#throwing().
292+
[26:27..26:36): Exception -> scala/package.Exception#
293+
[29:1..29:17): ObjectAnnotation -> com/javacp/annot/ObjectAnnotation#
294+
[30:7..30:8): M <- annot/M.
295+
[31:3..31:18): MacroAnnotation -> com/javacp/annot/MacroAnnotation#
296+
[32:6..32:7): m <- annot/M.m().
297+
[32:8..32:10): TT <- annot/M.m().[TT]
298+
[32:13..32:16): Int -> scala/Int#
299+
[32:25..32:28): ??? -> scala/Predef.`???`().
300+
[35:1..35:16): TraitAnnotation -> com/javacp/annot/TraitAnnotation#
301+
[36:6..36:7): T <- annot/T#
302+
[38:7..38:12): Alias <- annot/Alias.
303+
[39:7..39:8): A <- annot/Alias.A#
304+
[39:11..39:26): ClassAnnotation -> com/javacp/annot/ClassAnnotation#
305+
[39:28..39:33): param -> scala/annotation/meta/param#
301306

302307
expect/Anonymous.scala
303308
----------------------
@@ -4082,9 +4087,9 @@ flags/p/package.C#w(). => abstract method w => Int
40824087
flags/p/package.C#x. => private[this] val method x T
40834088
flags/p/package.C#y. => private[this] val method y U
40844089
flags/p/package.C#z. => private[this] val method z V
4085-
flags/p/package.S# => class S [typeparam T ] extends Object { self: S[T] => +2 decls }
4086-
flags/p/package.S#[T] => typeparam T
4087-
flags/p/package.S#`<init>`(). => primary ctor <init> [typeparam T ](): S[T]
4090+
flags/p/package.S# => class S [@specialized typeparam T ] extends Object { self: S[T] => +2 decls }
4091+
flags/p/package.S#[T] => @specialized typeparam T
4092+
flags/p/package.S#`<init>`(). => primary ctor <init> [@specialized typeparam T ](): S[T]
40884093
flags/p/package.T1# => type T1 = Int
40894094
flags/p/package.T2# => type T2 [typeparam T ] = S[T]
40904095
flags/p/package.T2#[T] => typeparam T
@@ -4635,7 +4640,7 @@ Symbols:
46354640
_empty_/MyProgram# => final class MyProgram extends Object { self: MyProgram => +2 decls }
46364641
_empty_/readInts# => final class readInts extends Object { self: readInts => +2 decls }
46374642
_empty_/toplevel$package. => final package object _empty_ extends Object { self: _empty_.type => +9 decls }
4638-
_empty_/toplevel$package.MyProgram(). => method MyProgram (param times: Int): Unit
4643+
_empty_/toplevel$package.MyProgram(). => @main method MyProgram (param times: Int): Unit
46394644
_empty_/toplevel$package.MyProgram().(times) => param times: Int
46404645
_empty_/toplevel$package.a. => val inline method a ""
46414646
_empty_/toplevel$package.combine(). => method combine (param x: Int)(param y: Int): Int
@@ -4648,7 +4653,7 @@ _empty_/toplevel$package.combine(+1).(z) => param z: Int
46484653
_empty_/toplevel$package.combine(+2). => method combine => Int
46494654
_empty_/toplevel$package.foo(). => method foo => String
46504655
_empty_/toplevel$package.fooRef(). => method fooRef => String
4651-
_empty_/toplevel$package.readInts(). => method readInts (param ints: Int*): Unit
4656+
_empty_/toplevel$package.readInts(). => @main method readInts (param ints: Int*): Unit
46524657
_empty_/toplevel$package.readInts().(ints) => param ints: Int*
46534658

46544659
Occurrences:

0 commit comments

Comments
 (0)