From 5f1c3c2b43cead73a51aed68273b80c1641cb425 Mon Sep 17 00:00:00 2001 From: Jan Chyb Date: Fri, 24 May 2024 16:12:19 +0200 Subject: [PATCH] Do not expose ClassInfo in widenTermRefByName Previously ClassInfo could be easily be exposed with calls like `TypeRepr.of[T].termRef.widenTermRefByName`. [Cherry-picked 500906592c7298f54d6696cf7b7173cf30e43ac1] --- .../src/scala/quoted/runtime/impl/QuotesImpl.scala | 5 ++++- tests/pos-macros/i20458/Macro_1.scala | 12 ++++++++++++ tests/pos-macros/i20458/Test_2.scala | 1 + 3 files changed, 17 insertions(+), 1 deletion(-) create mode 100644 tests/pos-macros/i20458/Macro_1.scala create mode 100644 tests/pos-macros/i20458/Test_2.scala diff --git a/compiler/src/scala/quoted/runtime/impl/QuotesImpl.scala b/compiler/src/scala/quoted/runtime/impl/QuotesImpl.scala index 16df7d096192..c2cc3acca018 100644 --- a/compiler/src/scala/quoted/runtime/impl/QuotesImpl.scala +++ b/compiler/src/scala/quoted/runtime/impl/QuotesImpl.scala @@ -1781,7 +1781,10 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler def =:=(that: TypeRepr): Boolean = self =:= that def <:<(that: TypeRepr): Boolean = self <:< that def widen: TypeRepr = self.widen - def widenTermRefByName: TypeRepr = self.widenTermRefExpr + def widenTermRefByName: TypeRepr = + self.widenTermRefExpr match + case dotc.core.Types.ClassInfo(prefix, sym, _, _, _) => prefix.select(sym) + case other => other def widenByName: TypeRepr = self.widenExpr def dealias: TypeRepr = self.dealias def simplified: TypeRepr = self.simplified diff --git a/tests/pos-macros/i20458/Macro_1.scala b/tests/pos-macros/i20458/Macro_1.scala new file mode 100644 index 000000000000..803eff68062a --- /dev/null +++ b/tests/pos-macros/i20458/Macro_1.scala @@ -0,0 +1,12 @@ +import scala.quoted._ + +inline def matchCustom[F](): Unit = ${ matchCustomImpl[F] } + +private def matchCustomImpl[F: Type](using q: Quotes): Expr[Unit] = { + import q.reflect.* + val any = TypeRepr.of[Any].typeSymbol + assert(!any.termRef.widenTermRefByName.toString.contains("ClassInfo")) + any.termRef.widenTermRefByName.asType match + case '[t] => () + '{ () } +} diff --git a/tests/pos-macros/i20458/Test_2.scala b/tests/pos-macros/i20458/Test_2.scala new file mode 100644 index 000000000000..1118f4483e23 --- /dev/null +++ b/tests/pos-macros/i20458/Test_2.scala @@ -0,0 +1 @@ +def main() = matchCustom()