diff --git a/compiler/src/dotty/tools/dotc/transform/Constructors.scala b/compiler/src/dotty/tools/dotc/transform/Constructors.scala index 49e3c4805d67..4e0f29d999c3 100644 --- a/compiler/src/dotty/tools/dotc/transform/Constructors.scala +++ b/compiler/src/dotty/tools/dotc/transform/Constructors.scala @@ -212,7 +212,6 @@ class Constructors extends MiniPhase with IdentityDenotTransformer { thisPhase = } splitStats(stats1) case Nil => - (Nil, Nil) } splitStats(tree.body) @@ -222,6 +221,10 @@ class Constructors extends MiniPhase with IdentityDenotTransformer { thisPhase = dropped += acc Nil } + else if (!isRetained(acc.field)) { // It may happen for unit fields, tests/run/i6987.scala + dropped += acc.field + Nil + } else { if (acc.hasAnnotation(defn.TransientParamAnnot)) ctx.error(em"transient parameter $acc is retained as field in class ${acc.owner}", acc.sourcePos) diff --git a/tests/run/i6987.scala b/tests/run/i6987.scala new file mode 100644 index 000000000000..288a13b981f3 --- /dev/null +++ b/tests/run/i6987.scala @@ -0,0 +1,3 @@ +class A(val u: Unit) + +@main def Test = A(()) \ No newline at end of file diff --git a/tests/run/i6987b.scala b/tests/run/i6987b.scala new file mode 100644 index 000000000000..52424532bd74 --- /dev/null +++ b/tests/run/i6987b.scala @@ -0,0 +1,18 @@ +enum SingleCase { + case TheCase1(u: Unit) +} + +case class TheCase2(u: Unit) + +case class TheCase3(s: String, u: Unit) + +class TheCase4(val u: Unit) + +abstract class TheCase5(val u: Unit) + +@main def Test = + SingleCase.TheCase1(()) + TheCase2(()) + TheCase3("", ()) + TheCase4(()) + new TheCase5(()) {}