Skip to content

Commit 7fe700b

Browse files
authored
i16601 Fix class name in error message (#16635)
2 parents 52236dc + 1161dc0 commit 7fe700b

File tree

7 files changed

+34
-5
lines changed

7 files changed

+34
-5
lines changed

compiler/src/dotty/tools/dotc/transform/PostTyper.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,7 @@ class PostTyper extends MacroTransform with IdentityDenotTransformer { thisPhase
325325
// Check the constructor type as well; it could be an illegal singleton type
326326
// which would not be reflected as `tree.tpe`
327327
ctx.typer.checkClassType(nu.tpe, tree.srcPos, traitReq = false, stablePrefixReq = false)
328-
Checking.checkInstantiable(tree.tpe, nu.srcPos)
328+
Checking.checkInstantiable(tree.tpe, nu.tpe, nu.srcPos)
329329
withNoCheckNews(nu :: Nil)(app1)
330330
case _ =>
331331
app1
@@ -413,7 +413,7 @@ class PostTyper extends MacroTransform with IdentityDenotTransformer { thisPhase
413413
Checking.checkGoodBounds(tree.symbol)
414414
super.transform(tree)
415415
case tree: New if isCheckable(tree) =>
416-
Checking.checkInstantiable(tree.tpe, tree.srcPos)
416+
Checking.checkInstantiable(tree.tpe, tree.tpe, tree.srcPos)
417417
super.transform(tree)
418418
case tree: Closure if !tree.tpt.isEmpty =>
419419
Checking.checkRealizable(tree.tpt.tpe, tree.srcPos, "SAM type")

compiler/src/dotty/tools/dotc/typer/Checking.scala

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -185,12 +185,14 @@ object Checking {
185185
/** Check that `tp` refers to a nonAbstract class
186186
* and that the instance conforms to the self type of the created class.
187187
*/
188-
def checkInstantiable(tp: Type, pos: SrcPos)(using Context): Unit =
188+
def checkInstantiable(tp: Type, srcTp: Type, pos: SrcPos)(using Context): Unit =
189189
tp.underlyingClassRef(refinementOK = false) match
190190
case tref: TypeRef =>
191191
val cls = tref.symbol
192-
if (cls.isOneOf(AbstractOrTrait))
193-
report.error(CantInstantiateAbstractClassOrTrait(cls, isTrait = cls.is(Trait)), pos)
192+
if (cls.isOneOf(AbstractOrTrait)) {
193+
val srcCls = srcTp.underlyingClassRef(refinementOK = false).typeSymbol
194+
report.error(CantInstantiateAbstractClassOrTrait(srcCls, isTrait = srcCls.is(Trait)), pos)
195+
}
194196
if !cls.is(Module) then
195197
// Create a synthetic singleton type instance, and check whether
196198
// it conforms to the self type of the class as seen from that instance.

compiler/test/dotty/tools/dotc/CompilationTests.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@ class CompilationTests {
159159
compileFile("tests/neg-custom-args/i9517.scala", defaultOptions.and("-Xprint-types")),
160160
compileFile("tests/neg-custom-args/i11637.scala", defaultOptions.and("-explain")),
161161
compileFile("tests/neg-custom-args/i15575.scala", defaultOptions.and("-explain")),
162+
compileFile("tests/neg-custom-args/i16601a.scala", defaultOptions.and("-explain")),
162163
compileFile("tests/neg-custom-args/interop-polytypes.scala", allowDeepSubtypes.and("-Yexplicit-nulls")),
163164
compileFile("tests/neg-custom-args/conditionalWarnings.scala", allowDeepSubtypes.and("-deprecation").and("-Xfatal-warnings")),
164165
compileFilesInDir("tests/neg-custom-args/isInstanceOf", allowDeepSubtypes and "-Xfatal-warnings"),

tests/neg-custom-args/i16601a.check

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
-- [E042] Type Error: tests/neg-custom-args/i16601a.scala:1:27 ---------------------------------------------------------
2+
1 |@main def Test: Unit = new concurrent.ExecutionContext // error
3+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
4+
| ExecutionContext is a trait; it cannot be instantiated
5+
|---------------------------------------------------------------------------------------------------------------------
6+
| Explanation (enabled by `-explain`)
7+
|- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
8+
| Abstract classes and traits need to be extended by a concrete class or object
9+
| to make their functionality accessible.
10+
|
11+
| You may want to create an anonymous class extending ExecutionContext with
12+
| class ExecutionContext { }
13+
|
14+
| or add a companion object with
15+
| object ExecutionContext extends ExecutionContext
16+
|
17+
| You need to implement any abstract members in both cases.
18+
---------------------------------------------------------------------------------------------------------------------

tests/neg-custom-args/i16601a.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
@main def Test: Unit = new concurrent.ExecutionContext // error

tests/neg/i16601.check

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
-- [E042] Type Error: tests/neg/i16601.scala:1:27 ----------------------------------------------------------------------
2+
1 |@main def Test: Unit = new concurrent.ExecutionContext // error
3+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
4+
| ExecutionContext is a trait; it cannot be instantiated
5+
|
6+
| longer explanation available when compiling with `-explain`

tests/neg/i16601.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
@main def Test: Unit = new concurrent.ExecutionContext // error

0 commit comments

Comments
 (0)