-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Backport "Some fixes for AnnotatedTypes mapping" to LTS (#22124)
Backports #19957 to the 3.3.5. PR submitted by the release tooling. [skip ci]
- Loading branch information
Showing
23 changed files
with
242 additions
and
18 deletions.
There are no files selected for viewing
71 changes: 71 additions & 0 deletions
71
bench-micro/src/main/scala/dotty/tools/benchmarks/AnnotationsMappingBenchmark.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,71 @@ | ||
package dotty.tools.benchmarks | ||
|
||
import org.openjdk.jmh.annotations.{Benchmark, BenchmarkMode, Fork, Level, Measurement, Mode as JMHMode, Param, Scope, Setup, State, Warmup} | ||
import java.util.concurrent.TimeUnit.SECONDS | ||
|
||
import dotty.tools.dotc.{Driver, Run, Compiler} | ||
import dotty.tools.dotc.ast.{tpd, TreeTypeMap}, tpd.{Apply, Block, Tree, TreeAccumulator, TypeApply} | ||
import dotty.tools.dotc.core.Annotations.{Annotation, ConcreteAnnotation, EmptyAnnotation} | ||
import dotty.tools.dotc.core.Contexts.{ContextBase, Context, ctx, withMode} | ||
import dotty.tools.dotc.core.Mode | ||
import dotty.tools.dotc.core.Phases.Phase | ||
import dotty.tools.dotc.core.Symbols.{defn, mapSymbols, Symbol} | ||
import dotty.tools.dotc.core.Types.{AnnotatedType, NoType, SkolemType, TermRef, Type, TypeMap} | ||
import dotty.tools.dotc.parsing.Parser | ||
import dotty.tools.dotc.typer.TyperPhase | ||
|
||
/** Measures the performance of mapping over annotated types. | ||
* | ||
* Run with: scala3-bench-micro / Jmh / run AnnotationsMappingBenchmark | ||
*/ | ||
@Fork(value = 4) | ||
@Warmup(iterations = 4, time = 1, timeUnit = SECONDS) | ||
@Measurement(iterations = 4, time = 1, timeUnit = SECONDS) | ||
@BenchmarkMode(Array(JMHMode.Throughput)) | ||
@State(Scope.Thread) | ||
class AnnotationsMappingBenchmark: | ||
var tp: Type = null | ||
var specialIntTp: Type = null | ||
var context: Context = null | ||
var typeFunction: Context ?=> Type => Type = null | ||
var typeMap: TypeMap = null | ||
|
||
@Param(Array("v1", "v2", "v3", "v4")) | ||
var valName: String = null | ||
|
||
@Param(Array("id", "mapInts")) | ||
var typeFunctionName: String = null | ||
|
||
@Setup(Level.Iteration) | ||
def setup(): Unit = | ||
val testPhase = | ||
new Phase: | ||
final override def phaseName = "testPhase" | ||
final override def run(using ctx: Context): Unit = | ||
val pkg = ctx.compilationUnit.tpdTree.symbol | ||
tp = pkg.requiredClass("Test").requiredValueRef(valName).underlying | ||
specialIntTp = pkg.requiredClass("Test").requiredType("SpecialInt").typeRef | ||
context = ctx | ||
|
||
val compiler = | ||
new Compiler: | ||
private final val baseCompiler = new Compiler() | ||
final override def phases = List(List(Parser()), List(TyperPhase()), List(testPhase)) | ||
|
||
val driver = | ||
new Driver: | ||
final override def newCompiler(using Context): Compiler = compiler | ||
|
||
driver.process(Array("-classpath", System.getProperty("BENCH_CLASS_PATH"), "tests/someAnnotatedTypes.scala")) | ||
|
||
typeFunction = | ||
typeFunctionName match | ||
case "id" => tp => tp | ||
case "mapInts" => tp => (if tp frozen_=:= defn.IntType then specialIntTp else tp) | ||
case _ => throw new IllegalArgumentException(s"Unknown type function: $typeFunctionName") | ||
|
||
typeMap = | ||
new TypeMap(using context): | ||
final override def apply(tp: Type): Type = typeFunction(mapOver(tp)) | ||
|
||
@Benchmark def applyTypeMap() = typeMap.apply(tp) |
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
class Test: | ||
class FlagAnnot extends annotation.StaticAnnotation | ||
class StringAnnot(val s: String) extends annotation.StaticAnnotation | ||
class LambdaAnnot(val f: Int => Boolean) extends annotation.StaticAnnotation | ||
|
||
type SpecialInt <: Int | ||
|
||
val v1: Int @FlagAnnot = 42 | ||
|
||
val v2: Int @StringAnnot("hello") = 42 | ||
|
||
val v3: Int @LambdaAnnot(it => it == 42) = 42 | ||
|
||
val v4: Int @LambdaAnnot(it => { | ||
def g(x: Int, y: Int) = x - y + 5 | ||
g(it, 7) * 2 == 80 | ||
}) = 42 | ||
|
||
/*val v5: Int @LambdaAnnot(it => { | ||
class Foo(x: Int): | ||
def xPlus10 = x + 10 | ||
def xPlus20 = x + 20 | ||
def xPlus(y: Int) = x + y | ||
val foo = Foo(it) | ||
foo.xPlus10 - foo.xPlus20 + foo.xPlus(30) == 62 | ||
}) = 42*/ | ||
|
||
def main(args: Array[String]): Unit = ??? |
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import scala.annotation.Annotation | ||
class myRefined(f: ? => Boolean) extends Annotation | ||
|
||
def test(axes: Int) = true | ||
|
||
trait Tensor: | ||
def mean(axes: Int): Int @myRefined(_ => test(axes)) | ||
|
||
class TensorImpl() extends Tensor: | ||
def mean(axes: Int) = ??? |
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,9 @@ | ||
//> using options "-Xprint:typer" | ||
|
||
class myAnnot[T]() extends annotation.Annotation | ||
|
||
trait Tensor[T]: | ||
def add: Tensor[T] @myAnnot[T]() | ||
|
||
class TensorImpl[A]() extends Tensor[A]: | ||
def add /* : Tensor[A] @myAnnot[A] */ = this |
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,10 @@ | ||
class Annot[T] extends scala.annotation.Annotation | ||
|
||
class D[T](val f: Int@Annot[T]) | ||
|
||
object A{ | ||
def main(a:Array[String]) = { | ||
val c = new D[Int](1) | ||
c.f | ||
} | ||
} |
Oops, something went wrong.