Skip to content

Commit

Permalink
Add a check for correct Array shape in quotes.reflect.ClassOfConstant
Browse files Browse the repository at this point in the history
  • Loading branch information
jchyb committed Nov 26, 2024
1 parent 7ba88ad commit 164a8ed
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 1 deletion.
12 changes: 11 additions & 1 deletion compiler/src/scala/quoted/runtime/impl/QuotesImpl.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2519,7 +2519,17 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler

object ClassOfConstant extends ClassOfConstantModule:
def apply(x: TypeRepr): ClassOfConstant =
// TODO check that the type is a valid class when creating this constant or let Ycheck do it?
// We only check if the supplied TypeRepr is valid if it contains an Array,
// as so far only that Array could cause issues
def correctTypeApplicationForArray(typeRepr: TypeRepr): Boolean =
val isArray = typeRepr.typeSymbol != dotc.core.Symbols.defn.ArrayClass
typeRepr match
case AppliedType(_, Nil) => isArray
case _ => isArray
xCheckMacroAssert(
correctTypeApplicationForArray(x),
"Illegal empty Array type constructor. Please supply a type parameter."
)
dotc.core.Constants.Constant(x)
def unapply(constant: ClassOfConstant): Some[TypeRepr] = Some(constant.typeValue)
end ClassOfConstant
Expand Down
15 changes: 15 additions & 0 deletions tests/neg-macros/i21916.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@

-- Error: tests/neg-macros/i21916/Test_2.scala:1:33 --------------------------------------------------------------------
1 |@main def Test: Unit = Macro.test() // error
| ^^^^^^^^^^^^
| Exception occurred while executing macro expansion.
| java.lang.AssertionError: Illegal empty Array type constructor. Please supply a type parameter.
| at Macro$.testImpl(Macro_1.scala:8)
|
|---------------------------------------------------------------------------------------------------------------------
|Inline stack trace
|- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|This location contains code that was inlined from Macro_1.scala:3
3 | inline def test() = ${testImpl}
| ^^^^^^^^^^^
---------------------------------------------------------------------------------------------------------------------
9 changes: 9 additions & 0 deletions tests/neg-macros/i21916/Macro_1.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import scala.quoted._
object Macro:
inline def test() = ${testImpl}
def testImpl(using Quotes): Expr[Any] = {
import quotes.reflect._
val tpe = TypeRepr.of[Array[Byte]] match
case AppliedType(tycons, _) => tycons
Literal(ClassOfConstant(tpe)).asExpr
}
1 change: 1 addition & 0 deletions tests/neg-macros/i21916/Test_2.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
@main def Test: Unit = Macro.test() // error

0 comments on commit 164a8ed

Please sign in to comment.