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

Do not return java outline dummy constructor in primaryConstructor #22104

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
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]])
Loading