Skip to content

Commit

Permalink
Do not return java outline dummy constructor in primaryConstructor
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
jchyb committed Dec 4, 2024
1 parent 896965c commit 898f952
Show file tree
Hide file tree
Showing 9 changed files with 62 additions and 1 deletion.
21 changes: 20 additions & 1 deletion compiler/src/scala/quoted/runtime/impl/QuotesImpl.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
5 changes: 5 additions & 0 deletions tests/run-macros/i20052.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
method <init> (Flags.JavaDefined | Flags.Method) List(List((x$0,scala.Int)))
method <init> (Flags.JavaDefined | Flags.Method) List(List())
method <init> (Flags.JavaDefined | Flags.Method | Flags.Private) List(List())
method <init> (Flags.JavaDefined | Flags.Method | Flags.Private) List(List())
method <init> (Flags.JavaDefined | Flags.Method) List(List((A,_ >: scala.Nothing <: <special-ops>.<FromJavaObject>)), List((x$0,A)))
4 changes: 4 additions & 0 deletions tests/run-macros/i20052/JavaClass.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
public class JavaClass {
public JavaClass(int a) {}
public JavaClass(float a) {}
}
1 change: 1 addition & 0 deletions tests/run-macros/i20052/JavaClassEmpty.java
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
public class JavaClassEmpty {}
3 changes: 3 additions & 0 deletions tests/run-macros/i20052/JavaClassParam.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
public class JavaClassParam<A> {
public JavaClassParam(A a) {}
}
3 changes: 3 additions & 0 deletions tests/run-macros/i20052/JavaClassPrivate.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class JavaClassPrivate {
private JavaClassPrivate() {}
}
4 changes: 4 additions & 0 deletions tests/run-macros/i20052/JavaClassStartsWithPrivate.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class JavaClassStartsWithPrivate {
private JavaClassStartsWithPrivate() {}
public JavaClassStartsWithPrivate(int a) {}
}
16 changes: 16 additions & 0 deletions tests/run-macros/i20052/Macro.scala
Original file line number Diff line number Diff line change
@@ -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)
}
}
6 changes: 6 additions & 0 deletions tests/run-macros/i20052/Test_2.scala
Original file line number Diff line number Diff line change
@@ -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]])

0 comments on commit 898f952

Please sign in to comment.