Skip to content

Fix #8042: add forgotten Java constructor type params #8075

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

Merged
merged 1 commit into from
Jan 23, 2020
Merged
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
2 changes: 1 addition & 1 deletion compiler/src/dotty/tools/dotc/ast/Desugar.scala
Original file line number Diff line number Diff line change
Expand Up @@ -473,7 +473,7 @@ object desugar {
decompose(
defDef(
addEvidenceParams(
cpy.DefDef(ddef)(tparams = constrTparams),
cpy.DefDef(ddef)(tparams = constrTparams ++ ddef.tparams),
evidenceParams(constr1).map(toDefParam(_, keepAnnotations = false)))))
case stat =>
stat
Expand Down
7 changes: 5 additions & 2 deletions compiler/src/dotty/tools/dotc/parsing/JavaParsers.scala
Original file line number Diff line number Diff line change
Expand Up @@ -112,15 +112,18 @@ object JavaParsers {
case nil => (EmptyTree, nil)
}
var (constr1, stats1) = pullOutFirstConstr(stats)
if (constr1 == EmptyTree) constr1 = makeConstructor(List(), tparams)
// A dummy first constructor is needed for Java classes so that the real constructors see the
// import of the companion object. The constructor has parameter of type Unit so no Java code
// can call it.
// This also avoids clashes between the constructor parameter names and member names.
if (needsDummyConstr) {
if (constr1 == EmptyTree) constr1 = makeConstructor(List(), Nil)
stats1 = constr1 :: stats1
constr1 = makeConstructor(List(scalaDot(tpnme.Unit)), tparams, Flags.JavaDefined | Flags.PrivateLocal)
}
else if (constr1 == EmptyTree) {
constr1 = makeConstructor(List(), tparams)
}
Template(constr1.asInstanceOf[DefDef], parents, Nil, EmptyValDef, stats1)
}

Expand Down Expand Up @@ -506,7 +509,7 @@ object JavaParsers {
optThrows()
List {
atSpan(start) {
DefDef(nme.CONSTRUCTOR, parentTParams,
DefDef(nme.CONSTRUCTOR, tparams,
List(vparams), TypeTree(), methodBody()).withMods(mods)
}
}
Expand Down
6 changes: 6 additions & 0 deletions tests/pos/i8042/Exception.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
public class Exception {
public <T> Exception(T actual, T matcher) { }

public <T> Object foo(T actual, T matcher) { return null; }

}