Skip to content

i16601 Fix class name in error message #16635

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 5 commits into from
Jan 9, 2023
Merged
Show file tree
Hide file tree
Changes from 4 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
4 changes: 2 additions & 2 deletions compiler/src/dotty/tools/dotc/transform/PostTyper.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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(tree.tpe, nu.tpe, nu.srcPos)
withNoCheckNews(nu :: Nil)(app1)
case _ =>
app1
Expand Down Expand Up @@ -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")
Expand Down
12 changes: 9 additions & 3 deletions compiler/src/dotty/tools/dotc/typer/Checking.scala
Original file line number Diff line number Diff line change
Expand Up @@ -185,12 +185,18 @@ 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)) {
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
// it conforms to the self type of the class as seen from that instance.
Expand Down
1 change: 1 addition & 0 deletions compiler/test/dotty/tools/dotc/CompilationTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down
18 changes: 18 additions & 0 deletions tests/neg-custom-args/i16601a.check
Original file line number Diff line number Diff line change
@@ -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.
---------------------------------------------------------------------------------------------------------------------
1 change: 1 addition & 0 deletions tests/neg-custom-args/i16601a.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
@main def Test: Unit = new concurrent.ExecutionContext // error
6 changes: 6 additions & 0 deletions tests/neg/i16601.check
Original file line number Diff line number Diff line change
@@ -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`
1 change: 1 addition & 0 deletions tests/neg/i16601.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
@main def Test: Unit = new concurrent.ExecutionContext // error