Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a check for correct Array shape in quotes.reflect.ClassOfConstant #22033

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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