@@ -5,7 +5,7 @@ import scala.tools.nsc.Global
5
5
6
6
class ExplicitGenerics (g : Global ) extends AnalyzerRule (g, " explicitGenerics" ) {
7
7
8
- import global ._
8
+ import global .*
9
9
10
10
lazy val explicitGenericsAnnotTpe = classType(" com.avsystem.commons.annotation.explicitGenerics" )
11
11
@@ -17,16 +17,29 @@ class ExplicitGenerics(g: Global) extends AnalyzerRule(g, "explicitGenerics") {
17
17
def requiresExplicitGenerics (sym : Symbol ): Boolean =
18
18
sym != NoSymbol && (sym :: sym.overrides).flatMap(_.annotations).exists(_.tree.tpe <:< explicitGenericsAnnotTpe)
19
19
20
+ def applyOfAnnotatedCompanion (preSym : Symbol ): Boolean = {
21
+ if (preSym != NoSymbol && preSym.isMethod && preSym.name == TermName (" apply" )) {
22
+ val owner = preSym.owner
23
+ val companionCls =
24
+ if (owner.isModuleClass) owner.companionClass
25
+ else if (owner.isModule) owner.moduleClass.companionClass
26
+ else NoSymbol
27
+ requiresExplicitGenerics(companionCls)
28
+ } else false
29
+ }
30
+
20
31
def analyzeTree (tree : Tree ): Unit = analyzer.macroExpandee(tree) match {
21
32
case `tree` | EmptyTree =>
22
33
tree match {
23
- case t@ TypeApply (pre, args) if requiresExplicitGenerics(pre.symbol) =>
34
+ case t@ TypeApply (pre, args) if requiresExplicitGenerics(pre.symbol) || applyOfAnnotatedCompanion(pre.symbol) =>
24
35
val inferredTypeParams = args.forall {
25
36
case tt : TypeTree => tt.original == null || tt.original == EmptyTree
26
37
case _ => false
27
38
}
28
39
if (inferredTypeParams) {
29
- fail(t.pos, pre.symbol)
40
+ // If we're on companion.apply, report on the class symbol for clearer message
41
+ val targetSym = if (applyOfAnnotatedCompanion(pre.symbol)) pre.symbol.owner.companionClass else pre.symbol
42
+ fail(t.pos, targetSym)
30
43
}
31
44
case n@ New (tpt) if requiresExplicitGenerics(tpt.tpe.typeSymbol) =>
32
45
val explicitTypeArgsProvided = tpt match {
@@ -45,6 +58,7 @@ class ExplicitGenerics(g: Global) extends AnalyzerRule(g, "explicitGenerics") {
45
58
case prevTree =>
46
59
analyzeTree(prevTree)
47
60
}
61
+
48
62
analyzeTree(unit.body)
49
63
}
50
64
}
0 commit comments