Skip to content

Change 'is abstract; cannot be instantiated' to Message #2012

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 2 commits into from
Feb 22, 2017
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@ public enum ErrorMessageID {
OverridesNothingButNameExistsID,
ForwardReferenceExtendsOverDefinitionID,
ExpectedTokenButFoundID,
MixedLeftAndRightAssociativeOpsID;
MixedLeftAndRightAssociativeOpsID,
CantInstantiateAbstractClassOrTraitID,
;

public int errorNumber() {
return ordinal() - 2;
Expand Down
19 changes: 19 additions & 0 deletions compiler/src/dotty/tools/dotc/reporting/diagnostic/messages.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1127,4 +1127,23 @@ object messages {
|""".stripMargin
}

case class CantInstantiateAbstractClassOrTrait(cls: Symbol, isTrait: Boolean)(implicit ctx: Context)
extends Message(CantInstantiateAbstractClassOrTraitID) {
val kind = "Usage"
private val traitOrAbstract = if (isTrait) hl"a trait" else hl"abstract"
val msg = hl"""${cls.name} is ${traitOrAbstract}; it cannot be instantiated"""
val explanation =
hl"""|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 ${cls.name} with
| ${s"class ${cls.name} { }"}
|
|or add a companion object with
| ${s"object ${cls.name} extends ${cls.name}"}
|
|You need to implement any abstract members in both cases.
|""".stripMargin
}

}
3 changes: 2 additions & 1 deletion compiler/src/dotty/tools/dotc/typer/Checking.scala
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import ErrorReporting.{err, errorType}
import config.Printers.typr
import collection.mutable
import SymDenotations.NoCompleter
import dotty.tools.dotc.reporting.diagnostic.messages.CantInstantiateAbstractClassOrTrait
import dotty.tools.dotc.transform.ValueClasses._

object Checking {
Expand Down Expand Up @@ -103,7 +104,7 @@ object Checking {
case tref: TypeRef =>
val cls = tref.symbol
if (cls.is(AbstractOrTrait))
ctx.error(em"$cls is abstract; cannot be instantiated", pos)
ctx.error(CantInstantiateAbstractClassOrTrait(cls, isTrait = cls.is(Trait)), pos)
if (!cls.is(Module)) {
// 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
39 changes: 39 additions & 0 deletions compiler/test/dotty/tools/dotc/reporting/ErrorMessagesTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -159,4 +159,43 @@ class ErrorMessagesTests extends ErrorMessagesTest {
assertEquals("+:", op2.show)
assertFalse(op2LeftAssoc)
}

@Test def cantInstantiateAbstract =
checkMessagesAfter("refchecks") {
"""
|object Scope {
| abstract class Concept
| new Concept()
|}
""".stripMargin
}
.expect { (ictx, messages) =>
implicit val ctx: Context = ictx
val defn = ictx.definitions

assertMessageCount(1, messages)
val CantInstantiateAbstractClassOrTrait(cls, isTrait) :: Nil = messages
assertEquals("Concept", cls.name.show)
assertFalse("expected class", isTrait)
}

@Test def cantInstantiateTrait =
checkMessagesAfter("refchecks") {
"""
|object Scope {
| trait Concept
| new Concept()
|}
""".stripMargin
}
.expect { (ictx, messages) =>
implicit val ctx: Context = ictx
val defn = ictx.definitions

assertMessageCount(1, messages)
val CantInstantiateAbstractClassOrTrait(cls, isTrait) :: Nil = messages
assertEquals("Concept", cls.name.show)
assertTrue("expected trait", isTrait)
}

}