Skip to content

Commit 80ae408

Browse files
committed
Add a check for correct Array shape in quotes.reflect.ClassOfConstant
1 parent 1f0c576 commit 80ae408

File tree

4 files changed

+38
-1
lines changed

4 files changed

+38
-1
lines changed

compiler/src/scala/quoted/runtime/impl/QuotesImpl.scala

+11-1
Original file line numberDiff line numberDiff line change
@@ -2519,7 +2519,17 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler
25192519

25202520
object ClassOfConstant extends ClassOfConstantModule:
25212521
def apply(x: TypeRepr): ClassOfConstant =
2522-
// TODO check that the type is a valid class when creating this constant or let Ycheck do it?
2522+
// We only check if the supplied TypeRepr is valid if it contains an Array,
2523+
// as so far only that Array could cause issues
2524+
def correctTypeApplicationForArray(typeRepr: TypeRepr): Boolean =
2525+
val isArray = typeRepr.typeSymbol != dotc.core.Symbols.defn.ArrayClass
2526+
typeRepr match
2527+
case AppliedType(_, targs) if !targs.isEmpty => true
2528+
case _ => isArray
2529+
xCheckMacroAssert(
2530+
correctTypeApplicationForArray(x),
2531+
"Illegal empty Array type constructor. Please supply a type parameter."
2532+
)
25232533
dotc.core.Constants.Constant(x)
25242534
def unapply(constant: ClassOfConstant): Some[TypeRepr] = Some(constant.typeValue)
25252535
end ClassOfConstant

tests/neg-macros/i21916.check

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
2+
-- Error: tests/neg-macros/i21916/Test_2.scala:3:27 --------------------------------------------------------------------
3+
3 |@main def Test = Macro.test() // error
4+
| ^^^^^^^^^^^^
5+
| Exception occurred while executing macro expansion.
6+
| java.lang.AssertionError: Illegal empty Array type constructor. Please supply a type parameter.
7+
| at Macro$.testImpl(Macro_1.scala:8)
8+
|
9+
|---------------------------------------------------------------------------------------------------------------------
10+
|Inline stack trace
11+
|- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
12+
|This location contains code that was inlined from Macro_1.scala:3
13+
3 | inline def test() = ${testImpl}
14+
| ^^^^^^^^^^^
15+
---------------------------------------------------------------------------------------------------------------------

tests/neg-macros/i21916/Macro_1.scala

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import scala.quoted._
2+
object Macro:
3+
inline def test() = ${testImpl}
4+
def testImpl(using Quotes): Expr[Any] = {
5+
import quotes.reflect._
6+
val tpe = TypeRepr.of[Array[Byte]] match
7+
case AppliedType(tycons, _) => tycons
8+
Literal(ClassOfConstant(tpe)).asExpr
9+
}

tests/neg-macros/i21916/Test_2.scala

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// lack of type ascription is on purpose,
2+
// as that was part of what would cause the crash before.
3+
@main def Test = Macro.test() // error

0 commit comments

Comments
 (0)