Skip to content

Commit

Permalink
Port "implicit definition needs explicit type" error to new scheme
Browse files Browse the repository at this point in the history
Related to scala#1589
  • Loading branch information
x3ro committed Oct 28, 2019
1 parent d06a5ff commit 06cdfe8
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,8 @@ enum ErrorMessageID extends java.lang.Enum[ErrorMessageID] {
StaticFieldsShouldPrecedeNonStaticID,
IllegalSuperAccessorID,
TraitParameterUsedAsParentPrefixID,
UnknownNamedEnclosingClassOrObjectID
UnknownNamedEnclosingClassOrObjectID,
ImplicitDefinitionNeedsExplicitTypeID

def errorNumber = ordinal - 2
}
Original file line number Diff line number Diff line change
Expand Up @@ -2392,4 +2392,12 @@ object messages {
|current scope.
""".stripMargin
}

case class ImplicitDefinitionNeedsExplicitType(sym: Symbol, kindOfType: Option[String])(implicit val ctx: Context)
extends Message(ImplicitDefinitionNeedsExplicitTypeID) {
val kind: String = "Type"
val msg: String =
em"implicit definition of ${sym.show} needs explicit ${kindOfType.map(_ + " ").getOrElse("")}type"
val explanation: String = ""
}
}
10 changes: 5 additions & 5 deletions compiler/src/dotty/tools/dotc/typer/Namer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -762,8 +762,8 @@ class Namer { typer: Typer =>
else bound
}

def missingType(sym: Symbol, modifier: String)(implicit ctx: Context): Unit = {
ctx.error(s"${modifier}type of implicit definition needs to be given explicitly", sym.sourcePos)
def missingType(sym: Symbol, kindOfType: Option[String])(implicit ctx: Context): Unit = {
ctx.error(ImplicitDefinitionNeedsExplicitType(sym, kindOfType), sym.sourcePos)
sym.resetFlag(GivenOrImplicit)
}

Expand Down Expand Up @@ -1131,7 +1131,7 @@ class Namer { typer: Typer =>
if (ptype.typeParams.isEmpty) ptype
else {
if (denot.is(ModuleClass) && denot.sourceModule.isOneOf(GivenOrImplicit))
missingType(denot.symbol, "parent ")(creationContext)
missingType(denot.symbol, Some("parent"))(creationContext)
fullyDefinedType(typedAheadExpr(parent).tpe, "class parent", parent.span)
}
case _ =>
Expand Down Expand Up @@ -1381,8 +1381,8 @@ class Namer { typer: Typer =>
else {
if (sym.is(Implicit))
mdef match {
case _: DefDef => missingType(sym, "result ")
case _: ValDef if sym.owner.isType => missingType(sym, "")
case _: DefDef => missingType(sym, Some("result"))
case _: ValDef if sym.owner.isType => missingType(sym, None)
case _ =>
}
lhsType orElse WildcardType
Expand Down
32 changes: 32 additions & 0 deletions compiler/test/dotty/tools/dotc/reporting/ErrorMessagesTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1672,4 +1672,36 @@ class ErrorMessagesTests extends ErrorMessagesTest {
val UnknownNamedEnclosingClassOrObject(name) :: Nil = messages
assertEquals("doesNotExist", name.show)
}

@Test def implicitDefinitionNeedsExplicitTypeValue() =
checkMessagesAfter(RefChecks.name) {
"""
|class TestObject {
| implicit val foo = 5
|}
""".stripMargin
}
.expect { (ictx, messages) =>
implicit val ctx: Context = ictx
assertMessageCount(1, messages)
val ImplicitDefinitionNeedsExplicitType(sym, kindOfType) :: Nil = messages
assertEquals("value foo", sym.show)
assertEquals(None, kindOfType)
}

@Test def implicitDefinitionNeedsExplicitTypeDef() =
checkMessagesAfter(RefChecks.name) {
"""
|class TestObject {
| implicit def bar = 6
|}
""".stripMargin
}
.expect { (ictx, messages) =>
implicit val ctx: Context = ictx
assertMessageCount(1, messages)
val ImplicitDefinitionNeedsExplicitType(sym, kindOfType) :: Nil = messages
assertEquals("method bar", sym.show)
assertEquals(Some("result"), kindOfType)
}
}

0 comments on commit 06cdfe8

Please sign in to comment.