From e6f267c026daf855dde179da6b516c1cc18b2d94 Mon Sep 17 00:00:00 2001 From: Liu Fengyun Date: Mon, 20 Jan 2020 14:05:22 +0100 Subject: [PATCH] Fix #8042: add forgotten Java constructor type params For primary constructor, we always add the class parameters during parsing. For secondary constructors, we add class parameters during desugaring. --- compiler/src/dotty/tools/dotc/ast/Desugar.scala | 2 +- compiler/src/dotty/tools/dotc/parsing/JavaParsers.scala | 7 +++++-- tests/pos/i8042/Exception.java | 6 ++++++ 3 files changed, 12 insertions(+), 3 deletions(-) create mode 100644 tests/pos/i8042/Exception.java diff --git a/compiler/src/dotty/tools/dotc/ast/Desugar.scala b/compiler/src/dotty/tools/dotc/ast/Desugar.scala index c1e31140f411..4f149cb4e48c 100644 --- a/compiler/src/dotty/tools/dotc/ast/Desugar.scala +++ b/compiler/src/dotty/tools/dotc/ast/Desugar.scala @@ -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 diff --git a/compiler/src/dotty/tools/dotc/parsing/JavaParsers.scala b/compiler/src/dotty/tools/dotc/parsing/JavaParsers.scala index 203fa0c91b84..cbff9f44e16b 100644 --- a/compiler/src/dotty/tools/dotc/parsing/JavaParsers.scala +++ b/compiler/src/dotty/tools/dotc/parsing/JavaParsers.scala @@ -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) } @@ -506,7 +509,7 @@ object JavaParsers { optThrows() List { atSpan(start) { - DefDef(nme.CONSTRUCTOR, parentTParams, + DefDef(nme.CONSTRUCTOR, tparams, List(vparams), TypeTree(), methodBody()).withMods(mods) } } diff --git a/tests/pos/i8042/Exception.java b/tests/pos/i8042/Exception.java new file mode 100644 index 000000000000..6fa7745346bb --- /dev/null +++ b/tests/pos/i8042/Exception.java @@ -0,0 +1,6 @@ +public class Exception { + public Exception(T actual, T matcher) { } + + public Object foo(T actual, T matcher) { return null; } + +}