From 40c083dad6360a80d3b1c151d255a271128deda3 Mon Sep 17 00:00:00 2001 From: Joel Wilsson Date: Thu, 29 Aug 2024 23:02:23 +0200 Subject: [PATCH] Remove empty argument lists for classes with only context bounds Closes #21418 --- .../dotty/tools/dotc/typer/Migrations.scala | 6 +++++ .../dotty/tools/dotc/CompilationTests.scala | 1 + tests/rewrites/i21418.check | 26 +++++++++++++++++++ tests/rewrites/i21418.scala | 26 +++++++++++++++++++ 4 files changed, 59 insertions(+) create mode 100644 tests/rewrites/i21418.check create mode 100644 tests/rewrites/i21418.scala diff --git a/compiler/src/dotty/tools/dotc/typer/Migrations.scala b/compiler/src/dotty/tools/dotc/typer/Migrations.scala index 8d468fd68bba..d6b95ceb93dc 100644 --- a/compiler/src/dotty/tools/dotc/typer/Migrations.scala +++ b/compiler/src/dotty/tools/dotc/typer/Migrations.scala @@ -113,6 +113,12 @@ trait Migrations: em"""Context bounds will map to context parameters. |A `using` clause is needed to pass explicit arguments to them.$rewriteMsg""", tree.srcPos, mversion) + tree match + case Apply(ta @ TypeApply(Select(New(_), _), _), Nil) => + // Remove empty arguments for calls to new that may precede the context bound. + // They are no longer necessary. + patch(Span(ta.span.end, pt.args.head.span.start - 1), "") + case _ => () if mversion.needsPatch && pt.args.nonEmpty then patch(Span(pt.args.head.span.start), "using ") end contextBoundParams diff --git a/compiler/test/dotty/tools/dotc/CompilationTests.scala b/compiler/test/dotty/tools/dotc/CompilationTests.scala index dd722403723a..0c5d5764949a 100644 --- a/compiler/test/dotty/tools/dotc/CompilationTests.scala +++ b/compiler/test/dotty/tools/dotc/CompilationTests.scala @@ -77,6 +77,7 @@ class CompilationTests { compileFile("tests/rewrites/i17399.scala", unindentOptions.and("-rewrite")), compileFile("tests/rewrites/i20002.scala", defaultOptions.and("-indent", "-rewrite")), compileDir("tests/rewrites/annotation-named-pararamters", defaultOptions.and("-rewrite", "-source:3.6-migration")), + compileFile("tests/rewrites/i21418.scala", unindentOptions.and("-rewrite", "-source:3.5-migration")), ).checkRewrites() } diff --git a/tests/rewrites/i21418.check b/tests/rewrites/i21418.check new file mode 100644 index 000000000000..9ad54a6134df --- /dev/null +++ b/tests/rewrites/i21418.check @@ -0,0 +1,26 @@ +trait Effect[F[_]] +class Countdown[F[_]: Effect] +class Countdown1[F[_]: Effect](howMany: Int) +class Countdown2[F[_]: Effect, F2[_]: Effect] + +def foo[F[_]: Effect]() = + "foo" + +@main def Test = { + val a = new Countdown[Option](using ???) + Countdown[Option](using ???) + val b = Countdown[Option](using ???) + new Countdown[Option](using ???) + val c = Countdown[List](using ???) + new Countdown2[List, Option](using ???, ???) + new Countdown2[List, Option] (using ???, ???) + Countdown2[List, Option](using ???, ???) + Countdown2[List, Option] (using ???, ???) + new Countdown1[Option](10)(using ???) + new Array[Int](10) + new scala.collection.immutable.HashSet[Int] + new scala.collection.immutable.HashSet[Int]() + new scala.collection.immutable.HashSet[Int] () + foo()(using ???) + foo() (using ???) +} diff --git a/tests/rewrites/i21418.scala b/tests/rewrites/i21418.scala new file mode 100644 index 000000000000..88fdb22ea177 --- /dev/null +++ b/tests/rewrites/i21418.scala @@ -0,0 +1,26 @@ +trait Effect[F[_]] +class Countdown[F[_]: Effect] +class Countdown1[F[_]: Effect](howMany: Int) +class Countdown2[F[_]: Effect, F2[_]: Effect] + +def foo[F[_]: Effect]() = + "foo" + +@main def Test = { + val a = new Countdown[Option]()(???) + Countdown[Option]()(???) + val b = Countdown[Option]()(???) + new Countdown[Option] ()(???) + val c = Countdown[List] () (???) + new Countdown2[List, Option] () (???, ???) + new Countdown2[List, Option] (using ???, ???) + Countdown2[List, Option] () (???, ???) + Countdown2[List, Option] (using ???, ???) + new Countdown1[Option](10)(???) + new Array[Int](10) + new scala.collection.immutable.HashSet[Int] + new scala.collection.immutable.HashSet[Int]() + new scala.collection.immutable.HashSet[Int] () + foo()(???) + foo() (???) +}