Skip to content

Commit 85ed796

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 92e7e59 commit 85ed796

File tree

4 files changed

+81
-13
lines changed

4 files changed

+81
-13
lines changed

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

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

232232
implicit def TermDeco(t: Term): AbstractTerm = new AbstractTerm {
233233
def pos(implicit ctx: Context): Position = new TastyPosition(t.pos)
234-
def tpe: Types.Type = t.tpe
234+
def tpe(implicit ctx: Context): Types.Type = t.tpe
235235
}
236236

237237
def termClassTag: ClassTag[Term] = implicitly[ClassTag[Term]]
@@ -378,7 +378,7 @@ object TastyImpl extends scala.tasty.Tasty {
378378
def unapply(x: Term)(implicit ctx: Context): Option[(Term, Int, Type)] = x match {
379379
case x: tpd.Select @unchecked =>
380380
x.name match {
381-
case NameKinds.OuterSelectName(_, levels) => Some((x.qualifier, levels, x.tpe))
381+
case NameKinds.OuterSelectName(_, levels) => Some((x.qualifier, levels, x.tpe.stripTypeVar))
382382
case _ => None
383383
}
384384
case _ => None
@@ -405,7 +405,7 @@ object TastyImpl extends scala.tasty.Tasty {
405405

406406
implicit def PatternDeco(x: Pattern): AbstractPattern = new AbstractPattern {
407407
def pos(implicit ctx: Context): Position = new TastyPosition(x.pos)
408-
def tpe: Types.Type = x.tpe
408+
def tpe(implicit ctx: Context): Types.Type = x.tpe.stripTypeVar
409409
}
410410

411411
def patternClassTag: ClassTag[Pattern] = implicitly[ClassTag[Pattern]]
@@ -451,7 +451,7 @@ object TastyImpl extends scala.tasty.Tasty {
451451
type MaybeTypeTree = tpd.Tree
452452

453453
implicit def MaybeTypeTreeDeco(x: MaybeTypeTree): AbstractMaybeTypeTree = new AbstractMaybeTypeTree {
454-
def tpe: Type = x.tpe
454+
def tpe(implicit ctx: Context): Type = x.tpe.stripTypeVar
455455
}
456456

457457
// ----- TypeTrees ------------------------------------------------
@@ -462,7 +462,7 @@ object TastyImpl extends scala.tasty.Tasty {
462462

463463
implicit def TypeTreeDeco(x: TypeTree): AbstractTypeTree = new AbstractTypeTree {
464464
def pos(implicit ctx: Context): Position = new TastyPosition(x.pos)
465-
def tpe: Types.Type = x.tpe
465+
def tpe(implicit ctx: Context): Types.Type = x.tpe.stripTypeVar
466466
}
467467

468468
val Synthetic: SyntheticExtractor = new SyntheticExtractor {
@@ -605,35 +605,35 @@ object TastyImpl extends scala.tasty.Tasty {
605605

606606
val AppliedType: AppliedTypeExtractor = new AppliedTypeExtractor {
607607
def unapply(x: Type)(implicit ctx: Context): Option[(Type, List[MaybeType /* Type | TypeBounds */])] = x match {
608-
case Types.AppliedType(tycon, args) => Some((tycon, args))
608+
case Types.AppliedType(tycon, args) => Some((tycon.stripTypeVar, args.map(_.stripTypeVar)))
609609
case _ => None
610610
}
611611
}
612612

613613
val AnnotatedType: AnnotatedTypeExtractor = new AnnotatedTypeExtractor {
614614
def unapply(x: Type)(implicit ctx: Context): Option[(Type, Term)] = x match {
615-
case Types.AnnotatedType(underlying, annot) => Some((underlying, annot.tree))
615+
case Types.AnnotatedType(underlying, annot) => Some((underlying.stripTypeVar, annot.tree))
616616
case _ => None
617617
}
618618
}
619619

620620
val AndType: AndTypeExtractor = new AndTypeExtractor {
621621
def unapply(x: Type)(implicit ctx: Context): Option[(Type, Type)] = x match {
622-
case Types.AndType(left, right) => Some(left, right)
622+
case Types.AndType(left, right) => Some(left.stripTypeVar, right.stripTypeVar)
623623
case _ => None
624624
}
625625
}
626626

627627
val OrType: OrTypeExtractor = new OrTypeExtractor {
628628
def unapply(x: Type)(implicit ctx: Context): Option[(Type, Type)] = x match {
629-
case Types.OrType(left, right) => Some(left, right)
629+
case Types.OrType(left, right) => Some(left.stripTypeVar, right.stripTypeVar)
630630
case _ => None
631631
}
632632
}
633633

634634
val ByNameType: ByNameTypeExtractor = new ByNameTypeExtractor {
635635
def unapply(x: Type)(implicit ctx: Context): Option[Type] = x match {
636-
case Types.ExprType(resType) => Some(resType)
636+
case Types.ExprType(resType) => Some(resType.stripTypeVar)
637637
case _ => None
638638
}
639639
}
@@ -668,7 +668,7 @@ object TastyImpl extends scala.tasty.Tasty {
668668

669669
val RecursiveType: RecursiveTypeExtractor = new RecursiveTypeExtractor {
670670
def unapply(x: RecursiveType)(implicit ctx: Context): Option[Type] = x match {
671-
case tp: Types.RecType => Some(tp.underlying)
671+
case tp: Types.RecType => Some(tp.underlying.stripTypeVar)
672672
case _ => None
673673
}
674674
}

library/src/scala/tasty/Tasty.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,7 @@ abstract class Tasty {
335335
type MaybeTypeTree
336336

337337
trait AbstractMaybeTypeTree {
338-
def tpe: MaybeType
338+
def tpe(implicit ctx: Context): MaybeType
339339
}
340340
implicit def MaybeTypeTreeDeco(x: MaybeTypeTree): AbstractMaybeTypeTree
341341

@@ -420,7 +420,7 @@ abstract class Tasty {
420420
type MaybeType
421421

422422
trait Typed {
423-
def tpe: Type
423+
def tpe(implicit ctx: Context): Type
424424
}
425425

426426
// ----- Types ----------------------------------------------------
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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+
trait Size[Keys] { def size: Int }
17+
object Size {
18+
def apply[Keys](v: Int): Size[Keys] = new Size { def size = v }
19+
implicit val unit: Size[Unit] = Size(0)
20+
implicit def cons[H, T](implicit s: Size[T]): Size[(H, T)] = Size(s.size + 1)
21+
}
22+
23+
class Index[K, Keys](val index: Int) extends AnyVal
24+
object Index {
25+
26+
implicit def zero[K, T]: Index[K, (K, T)] = new Index(0)
27+
28+
implicit inline def succ[K, H, T](implicit prev: => Index[K, T]): Index[K, (H, T)] = ~succImpl(Universe.compilationUniverse)('[K], '[H], '[T])
29+
30+
def succImpl[K, H, T](u: Universe)(implicit k: Type[K], h: Type[H], t: Type[T]): Expr[Index[K, (H, T)]] = {
31+
import u._
32+
import u.tasty._
33+
34+
def name(tp: MaybeType): String = tp match {
35+
case ConstantType(StringConstant(str)) => str
36+
}
37+
38+
def names(tp: MaybeType): List[String] = tp match {
39+
case AppliedType(_, x1 :: x2 :: Nil) => name(x1) :: names(x2)
40+
case _ => Nil
41+
}
42+
43+
val key = name(k.toTasty.tpe)
44+
val keys = name(h.toTasty.tpe) :: names(t.toTasty.tpe)
45+
46+
val index = keys.indexOf(key)
47+
48+
'(new Index(~index.toExpr))
49+
}
50+
}
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)