Skip to content

Commit 765582c

Browse files
committed
force info before reading symbol's annotations
1 parent da6246e commit 765582c

File tree

3 files changed

+24
-15
lines changed

3 files changed

+24
-15
lines changed

commons-core/src/test/scala/com/avsystem/commons/rest/openapi/RestSchemaTest.scala

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,12 @@
11
package com.avsystem.commons
22
package rest.openapi
33

4-
import com.avsystem.commons.annotation.AnnotationAggregate
54
import com.avsystem.commons.rest.RestDataCompanion
65
import com.avsystem.commons.rest.openapi.adjusters.description
76
import com.avsystem.commons.serialization.json.JsonStringOutput
8-
import com.avsystem.commons.serialization.{GenCodec, name, transparent, whenAbsent}
7+
import com.avsystem.commons.serialization.{GenCodec, name, transparent}
98
import org.scalatest.FunSuite
109

11-
class customWa[+T](value: => T) extends AnnotationAggregate {
12-
@whenAbsent(value) type Implied
13-
}
14-
1510
class Fuu[T](thing: T)
1611

1712
class RestSchemaTest extends FunSuite {
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.avsystem.commons
2+
package rest.openapi
3+
4+
import com.avsystem.commons.annotation.AnnotationAggregate
5+
import com.avsystem.commons.serialization.whenAbsent
6+
7+
class customWa[+T](value: => T) extends AnnotationAggregate {
8+
@whenAbsent(value) type Implied
9+
}

commons-macros/src/main/scala/com/avsystem/commons/macros/MacroCommons.scala

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,11 @@ trait MacroCommons { bundle =>
9999
def indent(str: String, indent: String): String =
100100
str.replaceAllLiterally("\n", s"\n$indent")
101101

102+
private def annotations(s: Symbol): List[Annotation] = {
103+
s.info // srsly scalac, load these goddamned annotations
104+
s.annotations
105+
}
106+
102107
def treeAsSeenFrom(tree: Tree, seenFrom: Type): Tree = seenFrom match {
103108
case NoType => tree
104109
case TypeRef(_, sym, Nil) if sym.isStatic => tree
@@ -191,7 +196,7 @@ trait MacroCommons { bundle =>
191196
lazy val aggregated: List[Annot] = {
192197
if (tpe <:< AnnotationAggregateType) {
193198
val impliedMember = tpe.member(TypeName("Implied"))
194-
impliedMember.annotations.map { a =>
199+
annotations(impliedMember).map { a =>
195200
new Annot(argsInliner.transform(treeAsSeenFrom(a.tree, tpe)), subject, impliedMember, Some(this))
196201
}
197202
} else Nil
@@ -226,7 +231,7 @@ trait MacroCommons { bundle =>
226231
!(superSym != initSym && isSealedHierarchyRoot(superSym) && annot.tree.tpe <:< NotInheritedFromSealedTypes)
227232

228233
val nonFallback = maybeWithSuperSymbols(initSym, withInherited)
229-
.flatMap(ss => ss.annotations.filter(inherited(_, ss))
234+
.flatMap(ss => annotations(ss).filter(inherited(_, ss))
230235
.map(a => new Annot(treeAsSeenFrom(a.tree, seenFrom), s, ss, None)))
231236

232237
(nonFallback ++ fallback.iterator.map(t => new Annot(t, s, s, None)))
@@ -258,7 +263,7 @@ trait MacroCommons { bundle =>
258263
!(superSym != initSym && isSealedHierarchyRoot(superSym) && annot.tree.tpe <:< NotInheritedFromSealedTypes)
259264

260265
maybeWithSuperSymbols(initSym, withInherited)
261-
.map(ss => find(ss.annotations.filter(inherited(_, ss))
266+
.map(ss => find(annotations(ss).filter(inherited(_, ss))
262267
.map(a => new Annot(treeAsSeenFrom(a.tree, seenFrom), s, ss, None))))
263268
.collectFirst { case Some(annot) => annot }
264269
.orElse(find(fallback.map(t => new Annot(t, s, s, None))))
@@ -407,7 +412,7 @@ trait MacroCommons { bundle =>
407412
symbolImplicitNotFoundMsg(tpe, tpe.typeSymbol, tpe.typeSymbol.typeSignature.typeParams, tpe.typeArgs)
408413

409414
private def symbolImplicitNotFoundMsg(tpe: Type, sym: Symbol, tparams: List[Symbol], typeArgs: List[Type]): String =
410-
sym.annotations.find(_.tree.tpe <:< ImplicitNotFoundAT)
415+
annotations(sym).find(_.tree.tpe <:< ImplicitNotFoundAT)
411416
.map(_.tree.children.tail.head).collect { case StringLiteral(error) => error }
412417
.map { error =>
413418
val tpNames = tparams.map(_.name.decodedName.toString)
@@ -773,19 +778,19 @@ trait MacroCommons { bundle =>
773778
def paramSymbolToValDef(sym: Symbol): ValDef = {
774779
val ts = sym.asTerm
775780
val implicitFlag = if (sym.isImplicit) Flag.IMPLICIT else NoFlags
776-
val mods = Modifiers(Flag.PARAM | implicitFlag, typeNames.EMPTY, ts.annotations.map(_.tree))
781+
val mods = Modifiers(Flag.PARAM | implicitFlag, typeNames.EMPTY, annotations(ts).map(_.tree))
777782
ValDef(mods, ts.name, treeForType(sym.typeSignature), EmptyTree)
778783
}
779784

780785
def getterSymbolToValDef(sym: Symbol): ValDef = {
781786
val ms = sym.asMethod
782787
val mutableFlag = if (ms.isVar) Flag.MUTABLE else NoFlags
783-
val mods = Modifiers(Flag.DEFERRED | mutableFlag, typeNames.EMPTY, ms.annotations.map(_.tree))
788+
val mods = Modifiers(Flag.DEFERRED | mutableFlag, typeNames.EMPTY, annotations(ms).map(_.tree))
784789
ValDef(mods, ms.name, treeForType(sym.typeSignature), EmptyTree)
785790
}
786791

787792
def existentialSingletonToValDef(sym: Symbol, name: TermName, tpe: Type): ValDef = {
788-
val mods = Modifiers(Flag.DEFERRED, typeNames.EMPTY, sym.annotations.map(_.tree))
793+
val mods = Modifiers(Flag.DEFERRED, typeNames.EMPTY, annotations(sym).map(_.tree))
789794
ValDef(mods, name, treeForType(tpe), EmptyTree)
790795
}
791796

@@ -804,7 +809,7 @@ trait MacroCommons { bundle =>
804809
else NoFlags
805810

806811
val flags = paramOrDeferredFlag | syntheticFlag | varianceFlag
807-
val mods = Modifiers(flags, typeNames.EMPTY, ts.annotations.map(_.tree))
812+
val mods = Modifiers(flags, typeNames.EMPTY, annotations(ts).map(_.tree))
808813
val (typeParams, signature) = sym.typeSignature match {
809814
case PolyType(polyParams, resultType) => (polyParams, resultType)
810815
case sig => (ts.typeParams, sig)
@@ -1038,7 +1043,7 @@ trait MacroCommons { bundle =>
10381043
def positionPoint(sym: Symbol): Int =
10391044
if (c.enclosingPosition.source == sym.pos.source) sym.pos.point
10401045
else positionCache.getOrElseUpdate(sym,
1041-
sym.annotations.find(_.tree.tpe <:< PositionedAT).map(_.tree).map {
1046+
annotations(sym).find(_.tree.tpe <:< PositionedAT).map(_.tree).map {
10421047
case Apply(_, List(MaybeTyped(Lit(point: Int), _))) => point
10431048
case t => abort(s"expected literal int as argument of @positioned annotation on $sym, got $t")
10441049
} getOrElse {

0 commit comments

Comments
 (0)