Skip to content

Commit

Permalink
Fix records with type parameters (scala#19578).
Browse files Browse the repository at this point in the history
This fixes a whole host of subtle issues.

 - The type parameter was not stamped correctly on the constructor
   causing the original error
 - The parsed record was not stamped with `JavaDefined`, which meant
   the duplicate constructors in the case of overrides were not
   removed.
  • Loading branch information
yilinwei committed Feb 1, 2024
1 parent 64a8865 commit f7eb589
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
4 changes: 2 additions & 2 deletions compiler/src/dotty/tools/dotc/parsing/JavaParsers.scala
Original file line number Diff line number Diff line change
Expand Up @@ -860,7 +860,7 @@ object JavaParsers {

// generate the canonical constructor
val canonicalConstructor =
DefDef(nme.CONSTRUCTOR, joinParams(tparams, List(header)), TypeTree(), EmptyTree)
DefDef(nme.CONSTRUCTOR, joinParams(Nil, List(header)), TypeTree(), EmptyTree)
.withMods(Modifiers(Flags.JavaDefined | Flags.Synthetic, mods.privateWithin))

// return the trees
Expand All @@ -872,7 +872,7 @@ object JavaParsers {
tparams = tparams,
needsDummyConstr = true
)
).withMods(mods)
).withMods(mods.withFlags(Flags.JavaDefined | Flags.Final))
}
addCompanionObject(statics, recordTypeDef)
end recordDecl
Expand Down
6 changes: 5 additions & 1 deletion compiler/src/dotty/tools/dotc/typer/Namer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -879,6 +879,10 @@ class Namer { typer: Typer =>
* with a user-defined method in the same scope with a matching type.
*/
private def invalidateIfClashingSynthetic(denot: SymDenotation): Unit =

def isJavaRecord(owner: Symbol) =
owner.is(JavaDefined) && owner.derivesFrom(defn.JavaRecordClass)

def isCaseClassOrCompanion(owner: Symbol) =
owner.isClass && {
if (owner.is(Module)) owner.linkedClass.is(CaseClass)
Expand All @@ -904,7 +908,7 @@ class Namer { typer: Typer =>
||
// remove synthetic constructor of a java Record if it clashes with a non-synthetic constructor
(denot.isConstructor
&& denot.owner.is(JavaDefined) && denot.owner.derivesFrom(defn.JavaRecordClass)
&& isJavaRecord(denot.owner)
&& denot.owner.unforcedDecls.lookupAll(denot.name).exists(c => c != denot.symbol && c.info.matches(denot.info))
)
)
Expand Down

0 comments on commit f7eb589

Please sign in to comment.