From 643ff4d0be601c2ff95d33c2795a9cc70ed9511d Mon Sep 17 00:00:00 2001 From: nizhikov Date: Sun, 8 Jan 2023 13:10:06 +0300 Subject: [PATCH 1/5] i16601 Fix class name in error message --- compiler/src/dotty/tools/dotc/transform/PostTyper.scala | 2 +- compiler/test/dotty/tools/dotc/CompilationTests.scala | 1 + tests/neg/i16601.check | 6 ++++++ tests/neg/i16601.scala | 1 + 4 files changed, 9 insertions(+), 1 deletion(-) create mode 100644 tests/neg/i16601.check create mode 100644 tests/neg/i16601.scala diff --git a/compiler/src/dotty/tools/dotc/transform/PostTyper.scala b/compiler/src/dotty/tools/dotc/transform/PostTyper.scala index b435baba23f1..0886f3a1714b 100644 --- a/compiler/src/dotty/tools/dotc/transform/PostTyper.scala +++ b/compiler/src/dotty/tools/dotc/transform/PostTyper.scala @@ -325,7 +325,7 @@ class PostTyper extends MacroTransform with IdentityDenotTransformer { thisPhase // Check the constructor type as well; it could be an illegal singleton type // which would not be reflected as `tree.tpe` ctx.typer.checkClassType(nu.tpe, tree.srcPos, traitReq = false, stablePrefixReq = false) - Checking.checkInstantiable(tree.tpe, nu.srcPos) + Checking.checkInstantiable(nu.tpe, nu.srcPos) withNoCheckNews(nu :: Nil)(app1) case _ => app1 diff --git a/compiler/test/dotty/tools/dotc/CompilationTests.scala b/compiler/test/dotty/tools/dotc/CompilationTests.scala index 20daa2d24406..decc22ecfda9 100644 --- a/compiler/test/dotty/tools/dotc/CompilationTests.scala +++ b/compiler/test/dotty/tools/dotc/CompilationTests.scala @@ -159,6 +159,7 @@ class CompilationTests { compileFile("tests/neg-custom-args/i9517.scala", defaultOptions.and("-Xprint-types")), compileFile("tests/neg-custom-args/i11637.scala", defaultOptions.and("-explain")), compileFile("tests/neg-custom-args/i15575.scala", defaultOptions.and("-explain")), + compileFile("tests/neg-custom-args/i16601a.scala", defaultOptions.and("-explain")), compileFile("tests/neg-custom-args/interop-polytypes.scala", allowDeepSubtypes.and("-Yexplicit-nulls")), compileFile("tests/neg-custom-args/conditionalWarnings.scala", allowDeepSubtypes.and("-deprecation").and("-Xfatal-warnings")), compileFilesInDir("tests/neg-custom-args/isInstanceOf", allowDeepSubtypes and "-Xfatal-warnings"), diff --git a/tests/neg/i16601.check b/tests/neg/i16601.check new file mode 100644 index 000000000000..25baef04e479 --- /dev/null +++ b/tests/neg/i16601.check @@ -0,0 +1,6 @@ +-- [E042] Type Error: tests/neg/i16601.scala:1:27 ---------------------------------------------------------------------- +1 |@main def Test: Unit = new concurrent.ExecutionContext // error + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | ExecutionContext is a trait; it cannot be instantiated + | + | longer explanation available when compiling with `-explain` diff --git a/tests/neg/i16601.scala b/tests/neg/i16601.scala new file mode 100644 index 000000000000..2e058db0093c --- /dev/null +++ b/tests/neg/i16601.scala @@ -0,0 +1 @@ +@main def Test: Unit = new concurrent.ExecutionContext // error \ No newline at end of file From 460d01ec1be763fde43c9620b6f1a1e6a34af796 Mon Sep 17 00:00:00 2001 From: nizhikov Date: Sun, 8 Jan 2023 13:11:03 +0300 Subject: [PATCH 2/5] Additional checks --- tests/neg-custom-args/i16601a.check | 18 ++++++++++++++++++ tests/neg-custom-args/i16601a.scala | 1 + 2 files changed, 19 insertions(+) create mode 100644 tests/neg-custom-args/i16601a.check create mode 100644 tests/neg-custom-args/i16601a.scala diff --git a/tests/neg-custom-args/i16601a.check b/tests/neg-custom-args/i16601a.check new file mode 100644 index 000000000000..604f71993ada --- /dev/null +++ b/tests/neg-custom-args/i16601a.check @@ -0,0 +1,18 @@ +-- [E042] Type Error: tests/neg-custom-args/i16601a.scala:1:27 --------------------------------------------------------- +1 |@main def Test: Unit = new concurrent.ExecutionContext // error + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | ExecutionContext is a trait; it cannot be instantiated + |--------------------------------------------------------------------------------------------------------------------- + | Explanation (enabled by `-explain`) + |- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + | Abstract classes and traits need to be extended by a concrete class or object + | to make their functionality accessible. + | + | You may want to create an anonymous class extending ExecutionContext with + | class ExecutionContext { } + | + | or add a companion object with + | object ExecutionContext extends ExecutionContext + | + | You need to implement any abstract members in both cases. + --------------------------------------------------------------------------------------------------------------------- diff --git a/tests/neg-custom-args/i16601a.scala b/tests/neg-custom-args/i16601a.scala new file mode 100644 index 000000000000..2e058db0093c --- /dev/null +++ b/tests/neg-custom-args/i16601a.scala @@ -0,0 +1 @@ +@main def Test: Unit = new concurrent.ExecutionContext // error \ No newline at end of file From c3c17effe0bb68d5601f3367ac90b0eca6d96606 Mon Sep 17 00:00:00 2001 From: nizhikov Date: Sun, 8 Jan 2023 23:25:39 +0300 Subject: [PATCH 3/5] Fix tests --- compiler/src/dotty/tools/dotc/transform/PostTyper.scala | 4 ++-- compiler/src/dotty/tools/dotc/typer/Checking.scala | 8 +++++--- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/compiler/src/dotty/tools/dotc/transform/PostTyper.scala b/compiler/src/dotty/tools/dotc/transform/PostTyper.scala index 0886f3a1714b..4c252d572d86 100644 --- a/compiler/src/dotty/tools/dotc/transform/PostTyper.scala +++ b/compiler/src/dotty/tools/dotc/transform/PostTyper.scala @@ -325,7 +325,7 @@ class PostTyper extends MacroTransform with IdentityDenotTransformer { thisPhase // Check the constructor type as well; it could be an illegal singleton type // which would not be reflected as `tree.tpe` ctx.typer.checkClassType(nu.tpe, tree.srcPos, traitReq = false, stablePrefixReq = false) - Checking.checkInstantiable(nu.tpe, nu.srcPos) + Checking.checkInstantiable(tree.tpe, nu.tpe, nu.srcPos) withNoCheckNews(nu :: Nil)(app1) case _ => app1 @@ -413,7 +413,7 @@ class PostTyper extends MacroTransform with IdentityDenotTransformer { thisPhase Checking.checkGoodBounds(tree.symbol) super.transform(tree) case tree: New if isCheckable(tree) => - Checking.checkInstantiable(tree.tpe, tree.srcPos) + Checking.checkInstantiable(tree.tpe, tree.tpe, tree.srcPos) super.transform(tree) case tree: Closure if !tree.tpt.isEmpty => Checking.checkRealizable(tree.tpt.tpe, tree.srcPos, "SAM type") diff --git a/compiler/src/dotty/tools/dotc/typer/Checking.scala b/compiler/src/dotty/tools/dotc/typer/Checking.scala index a1ee2c539622..d9d537fcd345 100644 --- a/compiler/src/dotty/tools/dotc/typer/Checking.scala +++ b/compiler/src/dotty/tools/dotc/typer/Checking.scala @@ -185,12 +185,14 @@ object Checking { /** Check that `tp` refers to a nonAbstract class * and that the instance conforms to the self type of the created class. */ - def checkInstantiable(tp: Type, pos: SrcPos)(using Context): Unit = + def checkInstantiable(tp: Type, srcTp: Type, pos: SrcPos)(using Context): Unit = tp.underlyingClassRef(refinementOK = false) match case tref: TypeRef => val cls = tref.symbol - if (cls.isOneOf(AbstractOrTrait)) - report.error(CantInstantiateAbstractClassOrTrait(cls, isTrait = cls.is(Trait)), pos) + if (cls.isOneOf(AbstractOrTrait)) { + val srcCls = srcTp.underlyingClassRef(refinementOK = false).asInstanceOf[TypeRef].symbol + report.error(CantInstantiateAbstractClassOrTrait(srcCls, isTrait = srcCls.is(Trait)), pos) + } if !cls.is(Module) then // Create a synthetic singleton type instance, and check whether // it conforms to the self type of the class as seen from that instance. From 2e2558f76abf2149221ec68dd60a1d7fab45e9d5 Mon Sep 17 00:00:00 2001 From: nizhikov Date: Mon, 9 Jan 2023 11:24:59 +0300 Subject: [PATCH 4/5] Fix review comments --- compiler/src/dotty/tools/dotc/typer/Checking.scala | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/compiler/src/dotty/tools/dotc/typer/Checking.scala b/compiler/src/dotty/tools/dotc/typer/Checking.scala index d9d537fcd345..835cbb25fab3 100644 --- a/compiler/src/dotty/tools/dotc/typer/Checking.scala +++ b/compiler/src/dotty/tools/dotc/typer/Checking.scala @@ -190,8 +190,12 @@ object Checking { case tref: TypeRef => val cls = tref.symbol if (cls.isOneOf(AbstractOrTrait)) { - val srcCls = srcTp.underlyingClassRef(refinementOK = false).asInstanceOf[TypeRef].symbol - report.error(CantInstantiateAbstractClassOrTrait(srcCls, isTrait = srcCls.is(Trait)), pos) + srcTp.underlyingClassRef(refinementOK = false) match + case tref0: TypeRef => + val srcCls = tref0.symbol + report.error(CantInstantiateAbstractClassOrTrait(srcCls, isTrait = srcCls.is(Trait)), pos) + case _ => + report.error(CantInstantiateAbstractClassOrTrait(cls, isTrait = cls.is(Trait)), pos) } if !cls.is(Module) then // Create a synthetic singleton type instance, and check whether From 1161dc0613e9b5b42ee7d75237d4ad77165392de Mon Sep 17 00:00:00 2001 From: nizhikov Date: Mon, 9 Jan 2023 17:01:18 +0300 Subject: [PATCH 5/5] Review fixes --- compiler/src/dotty/tools/dotc/typer/Checking.scala | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/compiler/src/dotty/tools/dotc/typer/Checking.scala b/compiler/src/dotty/tools/dotc/typer/Checking.scala index 835cbb25fab3..4c3819e9872c 100644 --- a/compiler/src/dotty/tools/dotc/typer/Checking.scala +++ b/compiler/src/dotty/tools/dotc/typer/Checking.scala @@ -190,12 +190,8 @@ object Checking { case tref: TypeRef => val cls = tref.symbol if (cls.isOneOf(AbstractOrTrait)) { - srcTp.underlyingClassRef(refinementOK = false) match - case tref0: TypeRef => - val srcCls = tref0.symbol - report.error(CantInstantiateAbstractClassOrTrait(srcCls, isTrait = srcCls.is(Trait)), pos) - case _ => - report.error(CantInstantiateAbstractClassOrTrait(cls, isTrait = cls.is(Trait)), pos) + val srcCls = srcTp.underlyingClassRef(refinementOK = false).typeSymbol + report.error(CantInstantiateAbstractClassOrTrait(srcCls, isTrait = srcCls.is(Trait)), pos) } if !cls.is(Module) then // Create a synthetic singleton type instance, and check whether