Skip to content

Commit 8300806

Browse files
committed
add sources
1 parent c83d50c commit 8300806

File tree

8 files changed

+178
-0
lines changed

8 files changed

+178
-0
lines changed

.github/workflows/ci.yml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
name: ci
2+
on:
3+
push:
4+
pull_request:
5+
jobs:
6+
test:
7+
runs-on: ubuntu-latest
8+
timeout-minutes: 10
9+
steps:
10+
- uses: actions/checkout@v3
11+
- uses: actions/setup-java@v3
12+
with:
13+
java-version: 8
14+
distribution: 'adopt'
15+
- run: sbt "+ run"

LICENSE

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
This is free and unencumbered software released into the public domain.
2+
3+
Anyone is free to copy, modify, publish, use, compile, sell, or
4+
distribute this software, either in source code form or as a compiled
5+
binary, for any purpose, commercial or non-commercial, and by any
6+
means.
7+
8+
In jurisdictions that recognize copyright laws, the author or authors
9+
of this software dedicate any and all copyright interest in the
10+
software to the public domain. We make this dedication for the benefit
11+
of the public at large and to the detriment of our heirs and
12+
successors. We intend this dedication to be an overt act of
13+
relinquishment in perpetuity of all present and future rights to this
14+
software under copyright law.
15+
16+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19+
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20+
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21+
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22+
OTHER DEALINGS IN THE SOFTWARE.
23+
24+
For more information, please refer to <https://unlicense.org>

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Scala asSeenFrom
2+
3+
- https://github.com/scala/scala/blob/18ce8d410b57f30f4161896b49b03d2d3e7b0401/src/reflect/scala/reflect/api/Types.scala#L246
4+
- https://github.com/lampepfl/dotty/blob/53f5531e7e31eca02b3db158c8c384e713b4542a/compiler/src/dotty/tools/dotc/core/TypeOps.scala#L30

build.sbt

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
def Scala3 = "3.1.3-RC2"
2+
3+
run / fork := true
4+
5+
scalaVersion := Scala3
6+
7+
crossScalaVersions := Seq("2.13.8", Scala3)
8+
9+
scalacOptions ++= {
10+
if (scalaBinaryVersion.value == "3") {
11+
Nil
12+
} else {
13+
Seq("-Xsource:3")
14+
}
15+
}
16+
17+
libraryDependencies += {
18+
if (scalaBinaryVersion.value == "3") {
19+
"org.scala-lang" %% "scala3-tasty-inspector" % scalaVersion.value
20+
} else {
21+
"org.scala-lang" % "scala-reflect" % scalaVersion.value
22+
}
23+
}

project/build.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
sbt.version=1.6.2

src/main/scala-2/Main.scala

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package example
2+
3+
import scala.reflect.runtime.universe._
4+
5+
object Main {
6+
def main(args: Array[String]): Unit = {
7+
val c = rootMirror.staticClass("example.C")
8+
val x = c.info.member(TermName("x")).asMethod
9+
val Apply(Select(New(t), _), Literal(Constant(arg: Type)) :: Nil) = x.annotations.head.tree
10+
val a = rootMirror.staticClass("example.A")
11+
val result = arg.asSeenFrom(typeOf[C], a)
12+
println(result)
13+
assert(result =:= typeOf[D[Int]])
14+
}
15+
}

src/main/scala-3/Main.scala

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package example
2+
3+
import java.io.File
4+
import scala.quoted.Quotes
5+
import scala.tasty.inspector.Inspector
6+
import scala.tasty.inspector.Tasty
7+
import scala.tasty.inspector.TastyInspector
8+
import dotty.tools.dotc.core.Contexts.Context
9+
import dotty.tools.dotc.core.Symbols.{Symbol => DottySymbol}
10+
import dotty.tools.dotc.core.Types.{Type => DottyType}
11+
import dotty.tools.dotc.core.TypeOps.asSeenFrom
12+
13+
object Main {
14+
15+
@annotation.experimental // typeRef is experimental
16+
def main(args: Array[String]): Unit = withQuotes {
17+
val q: Quotes = summon[Quotes]
18+
import q.reflect._
19+
20+
extension (self: TypeRepr) {
21+
def asDottyType: DottyType = self.asInstanceOf[DottyType]
22+
}
23+
24+
extension (self: Symbol) {
25+
def asDottySymbol: DottySymbol = self.asInstanceOf[DottySymbol]
26+
}
27+
28+
extension (self: DottyType) {
29+
def fromDottyType: TypeRepr = self.asInstanceOf[TypeRepr]
30+
}
31+
32+
implicit val context: Context = q.asInstanceOf[scala.quoted.runtime.impl.QuotesImpl].ctx
33+
34+
val c = Symbol.requiredClass("example.C")
35+
val a = Symbol.classSymbol("example.A")
36+
val List(x1: Symbol) = c.methodMember("x")
37+
val x = x1.tree.asInstanceOf[DefDef].returnTpt.tpe
38+
val Apply(_, NamedArg("clazz", TypeApply(Ident("classOf"), clazz :: Nil)) :: Nil) = x1.annotations.head
39+
val result = asSeenFrom(clazz.tpe.asDottyType, c.typeRef.asDottyType, a.asDottySymbol).fromDottyType
40+
println(result.show)
41+
assert(result =:= TypeRepr.of[D[Int]], result)
42+
}
43+
44+
def getTastyFileOrJar(clazz: Class[?]): Either[File, File] = {
45+
val base = new File(clazz.getProtectionDomain.getCodeSource.getLocation.getFile)
46+
if (base.isDirectory) {
47+
val name = clazz.getName.replace('.', '/') + ".tasty"
48+
Right(new File(base, name))
49+
} else if (base.isFile && base.getName.endsWith(".jar")) {
50+
Left(base)
51+
} else {
52+
sys.error("not found " + clazz)
53+
}
54+
}
55+
56+
def runInspector(clazz: Class[?], inspector: Inspector): Unit = {
57+
getTastyFileOrJar(clazz) match {
58+
case Right(tasty) =>
59+
TastyInspector.inspectAllTastyFiles(
60+
tastyFiles = tasty.getAbsolutePath :: Nil,
61+
jars = Nil,
62+
dependenciesClasspath = Nil
63+
)(inspector)
64+
case Left(jar) =>
65+
TastyInspector.inspectAllTastyFiles(
66+
tastyFiles = Nil,
67+
jars = jar.getAbsolutePath :: Nil,
68+
dependenciesClasspath = Nil
69+
)(inspector)
70+
}
71+
}
72+
73+
def withQuotes[A](f: Quotes ?=> A): Unit = {
74+
val inspector = new Inspector {
75+
def inspect(using q: Quotes)(tastys: List[Tasty[q.type]]): Unit = {
76+
import q.reflect.*
77+
f
78+
}
79+
}
80+
runInspector(classOf[C], inspector)
81+
}
82+
}

src/main/scala/A.scala

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package example
2+
3+
import scala.annotation.StaticAnnotation
4+
5+
class MyAnnotation(clazz: Class[?]) extends StaticAnnotation
6+
7+
trait D[E]
8+
9+
class A[B] {
10+
@MyAnnotation(clazz = classOf[D[B]])
11+
def x: B = ???
12+
}
13+
14+
class C extends A[Int]

0 commit comments

Comments
 (0)