Skip to content

Commit 0b57a87

Browse files
committed
Add indexed map test and couple of fixes
* Remove type vars from tasty types * Fix impure by name arguments to macros
1 parent c9ccb21 commit 0b57a87

File tree

5 files changed

+97
-14
lines changed

5 files changed

+97
-14
lines changed

compiler/src/dotty/tools/dotc/tasty/TastyImpl.scala

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ object TastyImpl extends scala.tasty.Tasty {
234234

235235
implicit def TermDeco(t: Term): AbstractTerm = new AbstractTerm {
236236
def pos(implicit ctx: Context): Position = new TastyPosition(t.pos)
237-
def tpe: Types.Type = t.tpe
237+
def tpe(implicit ctx: Context): Types.Type = t.tpe.stripTypeVar
238238

239239
def toExpr[T: quoted.Type](implicit ctx: Context): quoted.Expr[T] = {
240240
def typecheck(implicit ctx0: FreshContext) = {
@@ -410,7 +410,7 @@ object TastyImpl extends scala.tasty.Tasty {
410410
def unapply(x: Term)(implicit ctx: Context): Option[(Term, Int, Type)] = x match {
411411
case x: tpd.Select @unchecked =>
412412
x.name match {
413-
case NameKinds.OuterSelectName(_, levels) => Some((x.qualifier, levels, x.tpe))
413+
case NameKinds.OuterSelectName(_, levels) => Some((x.qualifier, levels, x.tpe.stripTypeVar))
414414
case _ => None
415415
}
416416
case _ => None
@@ -437,7 +437,7 @@ object TastyImpl extends scala.tasty.Tasty {
437437

438438
implicit def PatternDeco(x: Pattern): AbstractPattern = new AbstractPattern {
439439
def pos(implicit ctx: Context): Position = new TastyPosition(x.pos)
440-
def tpe: Types.Type = x.tpe
440+
def tpe(implicit ctx: Context): Types.Type = x.tpe.stripTypeVar
441441
}
442442

443443
def patternClassTag: ClassTag[Pattern] = implicitly[ClassTag[Pattern]]
@@ -483,7 +483,7 @@ object TastyImpl extends scala.tasty.Tasty {
483483
type MaybeTypeTree = tpd.Tree
484484

485485
implicit def MaybeTypeTreeDeco(x: MaybeTypeTree): AbstractMaybeTypeTree = new AbstractMaybeTypeTree {
486-
def tpe: Type = x.tpe
486+
def tpe(implicit ctx: Context): Type = x.tpe.stripTypeVar
487487
}
488488

489489
// ----- TypeTrees ------------------------------------------------
@@ -494,7 +494,7 @@ object TastyImpl extends scala.tasty.Tasty {
494494

495495
implicit def TypeTreeDeco(x: TypeTree): AbstractTypeTree = new AbstractTypeTree {
496496
def pos(implicit ctx: Context): Position = new TastyPosition(x.pos)
497-
def tpe: Types.Type = x.tpe
497+
def tpe(implicit ctx: Context): Types.Type = x.tpe.stripTypeVar
498498
def toType(implicit ctx: Context): quoted.Type[_] = new quoted.Types.TreeType(x)
499499
}
500500

@@ -638,35 +638,35 @@ object TastyImpl extends scala.tasty.Tasty {
638638

639639
val AppliedType: AppliedTypeExtractor = new AppliedTypeExtractor {
640640
def unapply(x: Type)(implicit ctx: Context): Option[(Type, List[MaybeType /* Type | TypeBounds */])] = x match {
641-
case Types.AppliedType(tycon, args) => Some((tycon, args))
641+
case Types.AppliedType(tycon, args) => Some((tycon.stripTypeVar, args.map(_.stripTypeVar)))
642642
case _ => None
643643
}
644644
}
645645

646646
val AnnotatedType: AnnotatedTypeExtractor = new AnnotatedTypeExtractor {
647647
def unapply(x: Type)(implicit ctx: Context): Option[(Type, Term)] = x match {
648-
case Types.AnnotatedType(underlying, annot) => Some((underlying, annot.tree))
648+
case Types.AnnotatedType(underlying, annot) => Some((underlying.stripTypeVar, annot.tree))
649649
case _ => None
650650
}
651651
}
652652

653653
val AndType: AndTypeExtractor = new AndTypeExtractor {
654654
def unapply(x: Type)(implicit ctx: Context): Option[(Type, Type)] = x match {
655-
case Types.AndType(left, right) => Some(left, right)
655+
case Types.AndType(left, right) => Some(left.stripTypeVar, right.stripTypeVar)
656656
case _ => None
657657
}
658658
}
659659

660660
val OrType: OrTypeExtractor = new OrTypeExtractor {
661661
def unapply(x: Type)(implicit ctx: Context): Option[(Type, Type)] = x match {
662-
case Types.OrType(left, right) => Some(left, right)
662+
case Types.OrType(left, right) => Some(left.stripTypeVar, right.stripTypeVar)
663663
case _ => None
664664
}
665665
}
666666

667667
val ByNameType: ByNameTypeExtractor = new ByNameTypeExtractor {
668668
def unapply(x: Type)(implicit ctx: Context): Option[Type] = x match {
669-
case Types.ExprType(resType) => Some(resType)
669+
case Types.ExprType(resType) => Some(resType.stripTypeVar)
670670
case _ => None
671671
}
672672
}
@@ -701,7 +701,7 @@ object TastyImpl extends scala.tasty.Tasty {
701701

702702
val RecursiveType: RecursiveTypeExtractor = new RecursiveTypeExtractor {
703703
def unapply(x: RecursiveType)(implicit ctx: Context): Option[Type] = x match {
704-
case tp: Types.RecType => Some(tp.underlying)
704+
case tp: Types.RecType => Some(tp.underlying.stripTypeVar)
705705
case _ => None
706706
}
707707
}

compiler/src/dotty/tools/dotc/transform/Splicer.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ object Splicer {
6363
*/
6464
private def getLiftedArgs(call: Tree, bindings: List[Tree])(implicit ctx: Context): List[Any] = {
6565
val bindMap = bindings.map {
66-
case vdef: ValDef => (vdef.rhs, ref(vdef.symbol).withPos(vdef.rhs.pos))
66+
case vdef: ValOrDefDef => (vdef.rhs, ref(vdef.symbol).withPos(vdef.rhs.pos))
6767
}.toMap
6868
def allArgs(call: Tree, acc: List[List[Tree]]): List[List[Tree]] = call match {
6969
case call: Apply => allArgs(call.fun, call.args :: acc)

library/src/scala/tasty/Tasty.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,7 @@ abstract class Tasty {
338338
type MaybeTypeTree
339339

340340
trait AbstractMaybeTypeTree {
341-
def tpe: MaybeType
341+
def tpe(implicit ctx: Context): MaybeType
342342
}
343343
implicit def MaybeTypeTreeDeco(x: MaybeTypeTree): AbstractMaybeTypeTree
344344

@@ -425,7 +425,7 @@ abstract class Tasty {
425425
type MaybeType
426426

427427
trait Typed {
428-
def tpe: Type
428+
def tpe(implicit ctx: Context): Type
429429
}
430430

431431
// ----- Types ----------------------------------------------------
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
2+
import scala.quoted._
3+
4+
import scala.tasty.Universe
5+
import scala.tasty.util.TastyPrinter
6+
7+
class MyMap[Keys](private val underlying: Array[Int]) extends AnyVal {
8+
def get[K <: String](implicit i: Index[K, Keys]): Int = underlying(i.index)
9+
def set[K <: String](value: Int)(implicit i: Index[K, Keys]): Unit = underlying(i.index) = value
10+
}
11+
12+
object MyMap {
13+
def create[Keys](implicit s: Size[Keys]): MyMap[Keys] = new MyMap[Keys](new Array[Int](s.size))
14+
}
15+
16+
class Size[HList](val size: Int) extends AnyVal
17+
object Size {
18+
type HList = Cons[_, _] | Unit
19+
type Cons[H, T <: HList] = (H, T)
20+
21+
implicit inline def getSize[HL <: HList]: Size[HL] = ~sizeImpl(Universe.compilationUniverse)('[HL])
22+
23+
def sizeImpl[HL <: HList](u: Universe)(implicit keys: Type[HL]): Expr[Size[HL]] = {
24+
import u._
25+
import u.tasty._
26+
27+
def sizeOf(tp: MaybeType): Int = tp match {
28+
case AppliedType(_, h :: t :: Nil) => 1 + sizeOf(t)
29+
case _ => 0
30+
}
31+
32+
val size = sizeOf(keys.toTasty.tpe)
33+
34+
'(new Size(~size.toExpr))
35+
}
36+
}
37+
38+
class Index[K, Keys](val index: Int) extends AnyVal
39+
object Index {
40+
41+
implicit inline def zero[K, T]: Index[K, (K, T)] = new Index(0)
42+
43+
implicit inline def succ[K, H, T](implicit prev: => Index[K, T]): Index[K, (H, T)] = ~succImpl(Universe.compilationUniverse)('[K], '[H], '[T])
44+
45+
def succImpl[K, H, T](u: Universe)(implicit k: Type[K], h: Type[H], t: Type[T]): Expr[Index[K, (H, T)]] = {
46+
import u._
47+
import u.tasty._
48+
49+
def name(tp: MaybeType): String = tp match {
50+
case ConstantType(StringConstant(str)) => str
51+
}
52+
53+
def names(tp: MaybeType): List[String] = tp match {
54+
case AppliedType(_, x1 :: x2 :: Nil) => name(x1) :: names(x2)
55+
case _ => Nil
56+
}
57+
58+
val key = name(k.toTasty.tpe)
59+
val keys = name(h.toTasty.tpe) :: names(t.toTasty.tpe)
60+
61+
val index = keys.indexOf(key)
62+
63+
'(new Index(~index.toExpr))
64+
}
65+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
object Test {
2+
3+
def main(args: Array[String]): Unit = {
4+
val map = MyMap.create[("foo", ("bar", ("baz", Unit)))]
5+
6+
map.set["foo"](9)
7+
assert(map.get["foo"] == 9)
8+
9+
map.set["bar"](42)
10+
assert(map.get["bar"] == 42)
11+
12+
map.set["baz"](42)
13+
assert(map.get["baz"] == 42)
14+
15+
// map.set["banana"](42) // error
16+
// assert(map.get["banana"] == 42) // error
17+
}
18+
}

0 commit comments

Comments
 (0)