Skip to content

Commit bba708f

Browse files
committed
Refactor direct usages of throw QuoteError
1 parent 5c1785e commit bba708f

File tree

13 files changed

+230
-173
lines changed

13 files changed

+230
-173
lines changed

compiler/src/dotty/tools/dotc/printing/PlainPrinter.scala

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,11 @@ class PlainPrinter(_ctx: Context) extends Printer {
4545
protected def debugPos: Boolean = ctx.settings.YdebugPos.value
4646

4747
def homogenize(tp: Type): Type =
48-
if (homogenizedView)
49-
tp match {
48+
if (homogenizedView) {
49+
val a = tp.tryNormalize
50+
if (a.exists) {
51+
homogenize(a)
52+
} else tp match {
5053
case tp: ThisType if tp.cls.is(Package) && !tp.cls.isEffectiveRoot =>
5154
ctx.requiredPackage(tp.cls.fullName).termRef
5255
case tp: TypeVar if tp.isInstantiated =>
@@ -62,9 +65,9 @@ class PlainPrinter(_ctx: Context) extends Printer {
6265
case AppliedType(tycon, args) =>
6366
tycon.dealias.appliedTo(args)
6467
case _ =>
65-
tp
68+
tp
6669
}
67-
else tp
70+
} else tp
6871

6972
private def sameBound(lo: Type, hi: Type): Boolean =
7073
try lo frozen_=:= hi catch { case NonFatal(ex) => false }

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,14 @@ object Splicer {
4646
}
4747
catch {
4848
case ex: scala.quoted.QuoteError =>
49-
ctx.error(ex.getMessage, pos)
49+
val pos1 = ex.from match {
50+
case None => pos
51+
case Some(expr) =>
52+
val reflect: scala.tasty.Reflection = ReflectionImpl(ctx)
53+
import reflect._
54+
expr.unseal.underlyingArgument.pos.asInstanceOf[SourcePosition]
55+
}
56+
ctx.error(ex.getMessage, pos1)
5057
EmptyTree
5158
case NonFatal(ex) =>
5259
val msg =

docs/docs/reference/other-new-features/tasty-reflect.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,10 @@ def natConstImpl(x: Expr[Int])(implicit reflection: Reflection): Expr[Int] = {
4242
xTree match {
4343
case Term.Literal(Constant.Int(n)) =>
4444
if (n <= 0)
45-
throw new QuoteError("Parameter must be natural number")
45+
QuoteError("Parameter must be natural number")
4646
n.toExpr
4747
case _ =>
48-
throw new QuoteError("Parameter must be a known constant")
48+
QuoteError("Parameter must be a known constant")
4949
}
5050
}
5151
```

library/src-bootstrapped/scala/Tuple.scala

Lines changed: 18 additions & 149 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package scala
22
import annotation.showAsInfix
33
import compiletime._
4-
import scala.StagedTuple
54

65
sealed trait Tuple extends Any {
76
import Tuple._
@@ -10,7 +9,7 @@ sealed trait Tuple extends Any {
109
if (stageIt) toArrayStaged
1110
else inline constValueOpt[BoundedSize[this.type]] match {
1211
case Some(0) =>
13-
empty$Array
12+
scala.runtime.DynamicTuple.empty$Array
1413
case Some(1) =>
1514
val t = asInstanceOf[Tuple1[Object]]
1615
Array(t._1)
@@ -23,12 +22,12 @@ sealed trait Tuple extends Any {
2322
case Some(4) =>
2423
val t = asInstanceOf[Tuple4[Object, Object, Object, Object]]
2524
Array(t._1, t._2, t._3, t._4)
26-
case Some(n) if n <= MaxSpecialized =>
25+
case Some(n) if n <= scala.runtime.DynamicTuple.MaxSpecialized =>
2726
to$Array(this, n)
2827
case Some(n) =>
2928
asInstanceOf[TupleXXL].elems
3029
case None =>
31-
dynamicToArray(this)
30+
runtime.DynamicTuple.dynamicToArray(this)
3231
}
3332

3433
inline def toArrayStaged: Array[Object] =
@@ -55,7 +54,7 @@ sealed trait Tuple extends Any {
5554
case Some(n) =>
5655
fromArray[H *: this.type](cons$Array(x, toArray))
5756
case _ =>
58-
dynamic_*:[This, H](this, x)
57+
runtime.DynamicTuple.dynamic_*:[This, H](this, x)
5958
}
6059
}
6160

@@ -99,7 +98,7 @@ sealed trait Tuple extends Any {
9998
if (constValue[BoundedSize[that.type]] == 0) this.asInstanceOf[Result]
10099
else genericConcat[Result](this, that).asInstanceOf[Result]
101100
case None =>
102-
dynamic_++[This, that.type](this, that)
101+
runtime.DynamicTuple.dynamic_++[This, that.type](this, that)
103102
}
104103
}
105104

@@ -116,7 +115,7 @@ sealed trait Tuple extends Any {
116115
type Result = Size[This]
117116
inline constValueOpt[BoundedSize[this.type]] match {
118117
case Some(n) => n.asInstanceOf[Result]
119-
case _ => dynamicSize(this)
118+
case _ => runtime.DynamicTuple.dynamicSize(this)
120119
}
121120
}
122121

@@ -125,9 +124,6 @@ sealed trait Tuple extends Any {
125124
}
126125

127126
object Tuple {
128-
inline val MaxSpecialized = 22
129-
inline private val XXL = MaxSpecialized + 1
130-
131127
final val stageIt = true
132128

133129
type Head[X <: NonEmptyTuple] = X match {
@@ -167,7 +163,7 @@ object Tuple {
167163

168164
private[scala] type BoundedSize[X] = BoundedSizeRecur[X, 23]
169165

170-
val empty$Array = Array[Object]()
166+
val $emptyArray = Array[Object]()
171167

172168
def to$Array(xs: Tuple, n: Int) = {
173169
val arr = new Array[Object](n)
@@ -217,99 +213,11 @@ object Tuple {
217213
}
218214

219215
inline def fromArrayStaged[T <: Tuple](xs: Array[Object]): T =
220-
${ StagedTuple.fromArrayStaged[T]('xs, constValueOpt[BoundedSize[this.type]]) }
221-
222-
def dynamicFromArray[T <: Tuple](xs: Array[Object]): T = xs.length match {
223-
case 0 => ().asInstanceOf[T]
224-
case 1 => Tuple1(xs(0)).asInstanceOf[T]
225-
case 2 => Tuple2(xs(0), xs(1)).asInstanceOf[T]
226-
case 3 => Tuple3(xs(0), xs(1), xs(2)).asInstanceOf[T]
227-
case 4 => Tuple4(xs(0), xs(1), xs(2), xs(3)).asInstanceOf[T]
228-
case 5 => Tuple5(xs(0), xs(1), xs(2), xs(3), xs(4)).asInstanceOf[T]
229-
case 6 => Tuple6(xs(0), xs(1), xs(2), xs(3), xs(4), xs(5)).asInstanceOf[T]
230-
case 7 => Tuple7(xs(0), xs(1), xs(2), xs(3), xs(4), xs(5), xs(6)).asInstanceOf[T]
231-
case 8 => Tuple8(xs(0), xs(1), xs(2), xs(3), xs(4), xs(5), xs(6), xs(7)).asInstanceOf[T]
232-
case 9 => Tuple9(xs(0), xs(1), xs(2), xs(3), xs(4), xs(5), xs(6), xs(7), xs(8)).asInstanceOf[T]
233-
case 10 => Tuple10(xs(0), xs(1), xs(2), xs(3), xs(4), xs(5), xs(6), xs(7), xs(8), xs(9)).asInstanceOf[T]
234-
case 11 => Tuple11(xs(0), xs(1), xs(2), xs(3), xs(4), xs(5), xs(6), xs(7), xs(8), xs(9), xs(10)).asInstanceOf[T]
235-
case 12 => Tuple12(xs(0), xs(1), xs(2), xs(3), xs(4), xs(5), xs(6), xs(7), xs(8), xs(9), xs(10), xs(11)).asInstanceOf[T]
236-
case 13 => Tuple13(xs(0), xs(1), xs(2), xs(3), xs(4), xs(5), xs(6), xs(7), xs(8), xs(9), xs(10), xs(11), xs(12)).asInstanceOf[T]
237-
case 14 => Tuple14(xs(0), xs(1), xs(2), xs(3), xs(4), xs(5), xs(6), xs(7), xs(8), xs(9), xs(10), xs(11), xs(12), xs(13)).asInstanceOf[T]
238-
case 15 => Tuple15(xs(0), xs(1), xs(2), xs(3), xs(4), xs(5), xs(6), xs(7), xs(8), xs(9), xs(10), xs(11), xs(12), xs(13), xs(14)).asInstanceOf[T]
239-
case 16 => Tuple16(xs(0), xs(1), xs(2), xs(3), xs(4), xs(5), xs(6), xs(7), xs(8), xs(9), xs(10), xs(11), xs(12), xs(13), xs(14), xs(15)).asInstanceOf[T]
240-
case 17 => Tuple17(xs(0), xs(1), xs(2), xs(3), xs(4), xs(5), xs(6), xs(7), xs(8), xs(9), xs(10), xs(11), xs(12), xs(13), xs(14), xs(15), xs(16)).asInstanceOf[T]
241-
case 18 => Tuple18(xs(0), xs(1), xs(2), xs(3), xs(4), xs(5), xs(6), xs(7), xs(8), xs(9), xs(10), xs(11), xs(12), xs(13), xs(14), xs(15), xs(16), xs(17)).asInstanceOf[T]
242-
case 19 => Tuple19(xs(0), xs(1), xs(2), xs(3), xs(4), xs(5), xs(6), xs(7), xs(8), xs(9), xs(10), xs(11), xs(12), xs(13), xs(14), xs(15), xs(16), xs(17), xs(18)).asInstanceOf[T]
243-
case 20 => Tuple20(xs(0), xs(1), xs(2), xs(3), xs(4), xs(5), xs(6), xs(7), xs(8), xs(9), xs(10), xs(11), xs(12), xs(13), xs(14), xs(15), xs(16), xs(17), xs(18), xs(19)).asInstanceOf[T]
244-
case 21 => Tuple21(xs(0), xs(1), xs(2), xs(3), xs(4), xs(5), xs(6), xs(7), xs(8), xs(9), xs(10), xs(11), xs(12), xs(13), xs(14), xs(15), xs(16), xs(17), xs(18), xs(19), xs(20)).asInstanceOf[T]
245-
case 22 => Tuple22(xs(0), xs(1), xs(2), xs(3), xs(4), xs(5), xs(6), xs(7), xs(8), xs(9), xs(10), xs(11), xs(12), xs(13), xs(14), xs(15), xs(16), xs(17), xs(18), xs(19), xs(20), xs(21)).asInstanceOf[T]
246-
case _ => TupleXXL(xs).asInstanceOf[T]
247-
}
248-
249-
def dynamicToArray(self: Tuple): Array[Object] = (self: Any) match {
250-
case self: Unit =>
251-
empty$Array
252-
case self: Tuple1[_] =>
253-
val t = self.asInstanceOf[Tuple1[Object]]
254-
Array(t._1)
255-
case self: Tuple2[_, _] =>
256-
val t = self.asInstanceOf[Tuple2[Object, Object]]
257-
Array(t._1, t._2)
258-
case self: Tuple3[_, _, _] =>
259-
val t = self.asInstanceOf[Tuple3[Object, Object, Object]]
260-
Array(t._1, t._2, t._3)
261-
case self: Tuple4[_, _, _, _] =>
262-
val t = self.asInstanceOf[Tuple4[Object, Object, Object, Object]]
263-
Array(t._1, t._2, t._3, t._4)
264-
case self: TupleXXL =>
265-
self.elems
266-
case self: Product =>
267-
val arr = new Array[Object](self.productArity)
268-
for (i <- 0 until arr.length) arr(i) = self.productElement(i).asInstanceOf[Object]
269-
arr
270-
}
271-
272-
def dynamic_*: [This <: Tuple, H] (self: Tuple, x: H): H *: This = {
273-
type Result = H *: This
274-
(self: Any) match {
275-
case Unit =>
276-
Tuple1(x).asInstanceOf[Result]
277-
case self: Tuple1[_] =>
278-
Tuple2(x, self._1).asInstanceOf[Result]
279-
case self: Tuple2[_, _] =>
280-
Tuple3(x, self._1, self._2).asInstanceOf[Result]
281-
case self: Tuple3[_, _, _] =>
282-
Tuple4(x, self._1, self._2, self._3).asInstanceOf[Result]
283-
case self: Tuple4[_, _, _, _] =>
284-
Tuple5(x, self._1, self._2, self._3, self._4).asInstanceOf[Result]
285-
case _ =>
286-
dynamicFromArray[Result](cons$Array(x, dynamicToArray(self)))
287-
}
288-
}
289-
290-
def dynamic_++[This <: Tuple, That <: Tuple](self: This, that: That): Concat[This, That] = {
291-
type Result = Concat[This, That]
292-
(this: Any) match {
293-
case self: Unit => return self.asInstanceOf[Result]
294-
case _ =>
295-
}
296-
(that: Any) match {
297-
case that: Unit => return self.asInstanceOf[Result]
298-
case _ =>
299-
}
300-
dynamicFromArray[Result](dynamicToArray(self) ++ dynamicToArray(that))
301-
}
302-
303-
def dynamicSize[This <: Tuple](self: This): Size[This] = (self: Any) match {
304-
case self: Unit => 0.asInstanceOf[Size[This]]
305-
case self: TupleXXL => self.elems.length.asInstanceOf[Size[This]]
306-
case self: Product => self.productArity.asInstanceOf[Size[This]]
307-
}
216+
${StagedTuple.fromArrayStaged[T]('xs, constValueOpt[BoundedSize[this.type]])}
308217
}
309218

310219
sealed trait NonEmptyTuple extends Tuple {
311220
import Tuple._
312-
import NonEmptyTuple._
313221

314222
inline def head[This >: this.type <: NonEmptyTuple]: Head[This] =
315223
if (stageIt) headStaged.asInstanceOf
@@ -328,13 +236,13 @@ sealed trait NonEmptyTuple extends Tuple {
328236
case Some(4) =>
329237
val t = asInstanceOf[Tuple4[_, _, _, _]]
330238
t._1
331-
case Some(n) if n > 4 && n <= MaxSpecialized =>
239+
case Some(n) if n > 4 && n <= scala.runtime.DynamicTuple.MaxSpecialized =>
332240
asInstanceOf[Product].productElement(0)
333-
case Some(n) if n > MaxSpecialized =>
241+
case Some(n) if n > scala.runtime.DynamicTuple.MaxSpecialized =>
334242
val t = asInstanceOf[TupleXXL]
335243
t.elems(0)
336244
case None =>
337-
dynamicHead[this.type](this)
245+
scala.runtime.DynamicTuple.dynamicHead[this.type](this)
338246
}
339247
resVal.asInstanceOf[Result]
340248
}
@@ -364,7 +272,7 @@ sealed trait NonEmptyTuple extends Tuple {
364272
case Some(n) if n > 5 =>
365273
fromArray[Result](toArray.tail)
366274
case None =>
367-
dynamicTail[This](this)
275+
runtime.DynamicTuple.dynamicTail[This](this)
368276
}
369277
}
370278

@@ -374,7 +282,7 @@ sealed trait NonEmptyTuple extends Tuple {
374282
inline def fallbackApply(n: Int) =
375283
inline constValueOpt[n.type] match {
376284
case Some(n: Int) => error("index out of bounds: ", n)
377-
case None => dynamicApply[this.type, n.type](this, n)
285+
case None => runtime.DynamicTuple.dynamicApply[this.type, n.type](this, n)
378286
}
379287

380288
inline def apply[This >: this.type <: NonEmptyTuple](n: Int): Elem[This, n.type] =
@@ -412,13 +320,13 @@ sealed trait NonEmptyTuple extends Tuple {
412320
case Some(3) => t._4.asInstanceOf[Result]
413321
case _ => fallbackApply(n).asInstanceOf[Result]
414322
}
415-
case Some(s) if s > 4 && s <= MaxSpecialized =>
323+
case Some(s) if s > 4 && s <= scala.runtime.DynamicTuple.MaxSpecialized =>
416324
val t = asInstanceOf[Product]
417325
inline constValueOpt[n.type] match {
418326
case Some(n) if n >= 0 && n < s => t.productElement(n).asInstanceOf[Result]
419327
case _ => fallbackApply(n).asInstanceOf[Result]
420328
}
421-
case Some(s) if s > MaxSpecialized =>
329+
case Some(s) if s > scala.runtime.DynamicTuple.MaxSpecialized =>
422330
val t = asInstanceOf[TupleXXL]
423331
inline constValueOpt[n.type] match {
424332
case Some(n) if n >= 0 && n < s => t.elems(n).asInstanceOf[Result]
@@ -429,53 +337,14 @@ sealed trait NonEmptyTuple extends Tuple {
429337
}
430338

431339
inline def applyStaged(n: Int): Elem[this.type, n.type] =
432-
${ StagedTuple.applyStaged[this.type, n.type](
433-
'this, constValueOpt[Size[this.type]],
434-
'n, constValueOpt[n.type]) }
435-
}
436-
437-
object NonEmptyTuple {
438-
import Tuple._
439-
440-
def dynamicHead[This <: NonEmptyTuple] (self: This): Head[This] = {
441-
type Result = Head[This]
442-
val res = (self: Any) match {
443-
case self: Tuple1[_] => self._1
444-
case self: Tuple2[_, _] => self._1
445-
case self: Tuple3[_, _, _] => self._1
446-
case self: Tuple4[_, _, _, _] => self._1
447-
case self: TupleXXL => self.elems(0)
448-
case self: Product => self.productElement(0)
449-
}
450-
res.asInstanceOf[Result]
451-
}
452-
453-
def dynamicTail[This <: NonEmptyTuple] (self: This): Tail[This] = {
454-
type Result = Tail[This]
455-
val res = (self: Any) match {
456-
case self: Tuple1[_] => ()
457-
case self: Tuple2[_, _] => Tuple1(self._2)
458-
case self: Tuple3[_, _, _] => Tuple2(self._2, self._3)
459-
case self: Tuple4[_, _, _, _] => Tuple3(self._2, self._3, self._4)
460-
case _ => dynamicFromArray[Result](dynamicToArray(self).tail)
461-
}
462-
res.asInstanceOf[Result]
463-
}
464-
465-
def dynamicApply[This <: NonEmptyTuple, N <: Int] (self: This, n: N): Elem[This, N] = {
466-
type Result = Elem[This, N]
467-
val res = (self: Any) match {
468-
case self: TupleXXL => self.elems(n)
469-
case self: Product => self.productElement(n)
470-
}
471-
res.asInstanceOf[Result]
472-
}
340+
${StagedTuple.applyStaged[this.type, n.type](
341+
'this, constValueOpt[Size[this.type]], 'n, constValueOpt[n.type])}
473342
}
474343

475344
@showAsInfix
476345
sealed class *:[+H, +T <: Tuple] extends NonEmptyTuple
477346

478347
object *: {
479348
inline def unapply[H, T <: Tuple](x: H *: T) =
480-
(NonEmptyTuple.dynamicHead(x), NonEmptyTuple.dynamicTail(x))
349+
(scala.runtime.DynamicTuple.dynamicHead(x), scala.runtime.DynamicTuple.dynamicTail(x))
481350
}

library/src-bootstrapped/scala/StagedTuple.scala renamed to library/src-bootstrapped/scala/compiletime/StagedTuple.scala

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,22 @@
1-
package scala
1+
package scala.compiletime
22

33
import scala.quoted._
44

55
object StagedTuple {
6-
import Tuple._
7-
import NonEmptyTuple._
6+
import Tuple.Concat
7+
import Tuple.Head
8+
import Tuple.Tail
9+
import Tuple.Size
10+
import Tuple.Elem
11+
import scala.runtime.DynamicTuple._
812

913
private final val specialize = true
1014

1115
def toArrayStaged(tup: Expr[Tuple], size: Option[Int]): Expr[Array[Object]] = {
1216
if (!specialize) '{dynamicToArray($tup)}
1317
else size match {
1418
case Some(0) =>
15-
'{empty$Array}
19+
'{scala.runtime.DynamicTuple.empty$Array}
1620
case Some(1) =>
1721
tup.as[Tuple1[Object]].bind(t => '{Array($t._1)})
1822
case Some(2) =>
@@ -121,11 +125,15 @@ object StagedTuple {
121125
}
122126
}
123127

124-
def applyStaged[Tup <: NonEmptyTuple : Type, N <: Int : Type](tup: Expr[Tup], size: Option[Int], n: Expr[N], nValue: Option[Int]): Expr[Elem[Tup, N]] = {
128+
implicit val toolbox: scala.quoted.Toolbox = scala.quoted.Toolbox.make
129+
130+
def applyStaged[Tup <: NonEmptyTuple : Type, N <: Int : Type](tup: Expr[Tup], size: Option[Int], n: Expr[N], nValue: Option[Int])(implicit reflect: tasty.Reflection): Expr[Elem[Tup, N]] = {
131+
import reflect._
132+
125133
if (!specialize) '{dynamicApply($tup, $n)}
126134
else {
127135
def fallbackApply(): Expr[Elem[Tup, N]] = nValue match {
128-
case Some(n) => quoted.QuoteError("index out of bounds: " + n)
136+
case Some(n) => quoted.QuoteError("index out of bounds: " + n, tup)
129137
case None => '{dynamicApply($tup, $n)}
130138
}
131139
val res = size match {
@@ -256,7 +264,5 @@ object StagedTuple {
256264
val t: U = $expr
257265
${in('t)}
258266
}
259-
260267
}
261-
262268
}

0 commit comments

Comments
 (0)