From 898f95226f6c0766293cfd223f2996fe7d70d503 Mon Sep 17 00:00:00 2001 From: Jan Chyb Date: Tue, 3 Dec 2024 19:27:32 +0100 Subject: [PATCH] Do not return java outline dummy constructor in `primaryConstructor` Java outline parser phase for various reasons adds a dummy constructor to java classes compiling simultenously from scalac. Since they provide no information to the user and are overall misleading (with always having the same fake flags and parameters), we filter them out and return the first constructor that can be found in the source. This is also what happened up to this point when running the macro with a java classfile on the classpath instead, since those dummy constructors cannot be found there. --- .../quoted/runtime/impl/QuotesImpl.scala | 21 ++++++++++++++++++- tests/run-macros/i20052.check | 5 +++++ tests/run-macros/i20052/JavaClass.java | 4 ++++ tests/run-macros/i20052/JavaClassEmpty.java | 1 + tests/run-macros/i20052/JavaClassParam.java | 3 +++ tests/run-macros/i20052/JavaClassPrivate.java | 3 +++ .../i20052/JavaClassStartsWithPrivate.java | 4 ++++ tests/run-macros/i20052/Macro.scala | 16 ++++++++++++++ tests/run-macros/i20052/Test_2.scala | 6 ++++++ 9 files changed, 62 insertions(+), 1 deletion(-) create mode 100644 tests/run-macros/i20052.check create mode 100644 tests/run-macros/i20052/JavaClass.java create mode 100644 tests/run-macros/i20052/JavaClassEmpty.java create mode 100644 tests/run-macros/i20052/JavaClassParam.java create mode 100644 tests/run-macros/i20052/JavaClassPrivate.java create mode 100644 tests/run-macros/i20052/JavaClassStartsWithPrivate.java create mode 100644 tests/run-macros/i20052/Macro.scala create mode 100644 tests/run-macros/i20052/Test_2.scala diff --git a/compiler/src/scala/quoted/runtime/impl/QuotesImpl.scala b/compiler/src/scala/quoted/runtime/impl/QuotesImpl.scala index 22be293c3562..fd5b635e11e2 100644 --- a/compiler/src/scala/quoted/runtime/impl/QuotesImpl.scala +++ b/compiler/src/scala/quoted/runtime/impl/QuotesImpl.scala @@ -2822,7 +2822,26 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler self.typeRef.info.decls.toList def paramSymss: List[List[Symbol]] = self.denot.paramSymss - def primaryConstructor: Symbol = self.denot.primaryConstructor + def primaryConstructor: Symbol = + val initialPrimary = self.denot.primaryConstructor + // Java outline parser creates a dummyConstructor. We want to avoid returning it here, + // instead returning the first non-dummy one, which is what happens when a java classfile + // is read from classpath instead of using the java outline parser. + // We check if the constructor is dummy if it has the same parameters as defined in JavaParsers.scala, + // incliding the private[this] flags and parameter shape with scala.Unit argument. + val isJavaDummyConstructor = + val paramSymss = initialPrimary.paramSymss + initialPrimary.flags.is(Flags.JavaDefined | Flags.Local | Flags.Method | Flags.Private | Flags.PrivateLocal) + && { + paramSymss match + case List(List(typeTree)) if self.typeRef.memberType(typeTree).typeSymbol == defn.UnitClass => true + case _ => false + } + if isJavaDummyConstructor then + declarations.filter(sym => sym != initialPrimary && sym.isConstructor).headOption.getOrElse(Symbol.noSymbol) + else + initialPrimary + def allOverriddenSymbols: Iterator[Symbol] = self.denot.allOverriddenSymbols def overridingSymbol(ofclazz: Symbol): Symbol = if ofclazz.isClass then self.denot.overridingSymbol(ofclazz.asClass) diff --git a/tests/run-macros/i20052.check b/tests/run-macros/i20052.check new file mode 100644 index 000000000000..ca45222cf4cf --- /dev/null +++ b/tests/run-macros/i20052.check @@ -0,0 +1,5 @@ +method (Flags.JavaDefined | Flags.Method) List(List((x$0,scala.Int))) +method (Flags.JavaDefined | Flags.Method) List(List()) +method (Flags.JavaDefined | Flags.Method | Flags.Private) List(List()) +method (Flags.JavaDefined | Flags.Method | Flags.Private) List(List()) +method (Flags.JavaDefined | Flags.Method) List(List((A,_ >: scala.Nothing <: .)), List((x$0,A))) diff --git a/tests/run-macros/i20052/JavaClass.java b/tests/run-macros/i20052/JavaClass.java new file mode 100644 index 000000000000..368bbf47830a --- /dev/null +++ b/tests/run-macros/i20052/JavaClass.java @@ -0,0 +1,4 @@ +public class JavaClass { + public JavaClass(int a) {} + public JavaClass(float a) {} +} diff --git a/tests/run-macros/i20052/JavaClassEmpty.java b/tests/run-macros/i20052/JavaClassEmpty.java new file mode 100644 index 000000000000..22710a8b7337 --- /dev/null +++ b/tests/run-macros/i20052/JavaClassEmpty.java @@ -0,0 +1 @@ +public class JavaClassEmpty {} diff --git a/tests/run-macros/i20052/JavaClassParam.java b/tests/run-macros/i20052/JavaClassParam.java new file mode 100644 index 000000000000..ddbb7b0a6a5f --- /dev/null +++ b/tests/run-macros/i20052/JavaClassParam.java @@ -0,0 +1,3 @@ +public class JavaClassParam { + public JavaClassParam(A a) {} +} diff --git a/tests/run-macros/i20052/JavaClassPrivate.java b/tests/run-macros/i20052/JavaClassPrivate.java new file mode 100644 index 000000000000..d2f0b3cda023 --- /dev/null +++ b/tests/run-macros/i20052/JavaClassPrivate.java @@ -0,0 +1,3 @@ +class JavaClassPrivate { + private JavaClassPrivate() {} +} diff --git a/tests/run-macros/i20052/JavaClassStartsWithPrivate.java b/tests/run-macros/i20052/JavaClassStartsWithPrivate.java new file mode 100644 index 000000000000..d27724424f34 --- /dev/null +++ b/tests/run-macros/i20052/JavaClassStartsWithPrivate.java @@ -0,0 +1,4 @@ +class JavaClassStartsWithPrivate { + private JavaClassStartsWithPrivate() {} + public JavaClassStartsWithPrivate(int a) {} +} diff --git a/tests/run-macros/i20052/Macro.scala b/tests/run-macros/i20052/Macro.scala new file mode 100644 index 000000000000..e38bb63257a3 --- /dev/null +++ b/tests/run-macros/i20052/Macro.scala @@ -0,0 +1,16 @@ +import scala.quoted.* + +object Macro { + inline def logPrimaryConstructor[A]: String = ${ logPrimaryConstructorImpl[A] } + + def logPrimaryConstructorImpl[A](using Type[A], Quotes): Expr[String] = { + import quotes.reflect.* + + val primaryConstructor = TypeRepr.of[A].typeSymbol.primaryConstructor + val flags = primaryConstructor.flags.show + val paramSymss = primaryConstructor.paramSymss + val clauses = paramSymss.map(_.map(param => (param.name, TypeRepr.of[A].memberType(param).show))) + val str = s"${primaryConstructor} (${primaryConstructor.flags.show}) ${clauses}" + Expr(str) + } +} diff --git a/tests/run-macros/i20052/Test_2.scala b/tests/run-macros/i20052/Test_2.scala new file mode 100644 index 000000000000..ef4fc8bd1887 --- /dev/null +++ b/tests/run-macros/i20052/Test_2.scala @@ -0,0 +1,6 @@ +@main def Test() = + println(Macro.logPrimaryConstructor[JavaClass]) + println(Macro.logPrimaryConstructor[JavaClassEmpty]) + println(Macro.logPrimaryConstructor[JavaClassPrivate]) + println(Macro.logPrimaryConstructor[JavaClassStartsWithPrivate]) + println(Macro.logPrimaryConstructor[JavaClassParam[Int]])