-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
44e36aa
commit 595a880
Showing
11 changed files
with
131 additions
and
109 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 23 additions & 44 deletions
67
analyzer/src/main/scala/com/avsystem/commons/analyzer/ExplicitGenerics.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,54 +1,33 @@ | ||
package com.avsystem.commons | ||
package analyzer | ||
|
||
import dotty.tools.dotc.ast.tpd | ||
import dotty.tools.dotc.ast.tpd.* | ||
import dotty.tools.dotc.core.Contexts.Context | ||
import dotty.tools.dotc.core.{Contexts, Symbols} | ||
import dotty.tools.dotc.transform.PickleQuotes | ||
|
||
final class ExplicitGenerics extends AnalyzerRule("explicitGenerics"): | ||
import ExplicitGenerics.* | ||
|
||
import tpd.* | ||
|
||
// lazy val explicitGenericsAnnotTpe = classType("com.avsystem.commons.annotation.explicitGenerics") | ||
|
||
// def analyze(unit: CompilationUnit) = if (explicitGenericsAnnotTpe != NoType) { | ||
// def requiresExplicitGenerics(sym: Symbol): Boolean = | ||
// sym != NoSymbol && (sym :: sym.overrides).flatMap(_.annotations).exists(_.tree.tpe <:< explicitGenericsAnnotTpe) | ||
// | ||
// def analyzeTree(tree: Tree): Unit = analyzer.macroExpandee(tree) match { | ||
// case `tree` | EmptyTree => | ||
// tree match { | ||
// case t@TypeApply(pre, args) if requiresExplicitGenerics(pre.symbol) => | ||
// val inferredTypeParams = args.forall { | ||
// case tt: TypeTree => tt.original == null || tt.original == EmptyTree | ||
// case _ => false | ||
// } | ||
// if (inferredTypeParams) { | ||
// report(t.pos, s"${pre.symbol} requires that its type arguments are explicit (not inferred)") | ||
// } | ||
// case _ => | ||
// } | ||
// tree.children.foreach(analyzeTree) | ||
// case prevTree => | ||
// analyzeTree(prevTree) | ||
// } | ||
// analyzeTree(unit.body) | ||
// } | ||
|
||
override def transformTypeApply(tree: TypeApply)(using Context): Tree = | ||
val TypeApply(fun: Tree, args) = tree | ||
val explicitGenerics = Symbols.requiredClass("com.avsystem.commons.annotation.explicitGenerics") | ||
if fun.symbol.hasAnnotation(explicitGenerics) then | ||
args.foreach { | ||
case tt: TypeTree /*if tt.original == null || tt.original == EmptyTree */ => | ||
report(s"${fun.symbol} requires that its type arguments are explicit (not inferred)", tree) | ||
case _ => | ||
} | ||
|
||
end if | ||
tree | ||
|
||
end transformTypeApply | ||
override def transformTypeApply(tree: TypeApply)(using Context): Tree = tree.tap { | ||
case TypeApply(fun: Tree, args) | ||
if fun.symbol.hasAnnotation(explicitGenericsSymbol) && args.containsInferredTypeParameters => | ||
report(s"${fun.symbol} requires that its type arguments are explicit (not inferred)", tree) | ||
case _ => | ||
} | ||
|
||
override def transformInlined(tree: Inlined)(using Context): Tree = tree.tap { | ||
case Inlined(Apply(typeApply: TypeApply, _), _, _) => transformTypeApply(typeApply) | ||
case _ => | ||
} | ||
|
||
end ExplicitGenerics | ||
|
||
private object ExplicitGenerics: | ||
|
||
private final val explicitGenericsSymbol = | ||
(ctx: Context) ?=> Symbols.requiredClass("com.avsystem.commons.annotation.explicitGenerics") | ||
|
||
extension (args: List[Tree]) | ||
private def containsInferredTypeParameters: Boolean = args.exists(_.isInstanceOf[TypeTree]) | ||
|
||
end ExplicitGenerics |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 0 additions & 38 deletions
38
analyzer/src/test/scala/com/avsystem/commons/analyzer/Any2StringAddTest.scala
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
core3/src/main/scala/com/avsystem/commons/annotation/atLeast.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package com.avsystem.commons | ||
package annotation | ||
|
||
import scala.annotation.StaticAnnotation | ||
|
||
/** | ||
* When applied on varargs parameter, indicates that at least some number of parameters is required. | ||
* This is later checked by the static analyzer. | ||
* <br/> | ||
* WARNING: implementation of method which takes a varargs parameter may NOT assume that given number of | ||
* arguments will always be passed, because it's still possible to pass a `Seq` where | ||
* varargs parameter is required using the `: _*` ascription, e.g. | ||
* {{{ | ||
* varargsMethod(List(): _*) | ||
* }}} | ||
* and that is not checked by the static analyzer. | ||
*/ | ||
final class atLeast(n: Int) extends StaticAnnotation |