Skip to content

Commit 02b98a6

Browse files
committed
Add error message for unbound wildcard type.
1 parent 39c27b6 commit 02b98a6

File tree

2 files changed

+58
-1
lines changed

2 files changed

+58
-1
lines changed

compiler/src/dotty/tools/dotc/parsing/Parsers.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -676,7 +676,7 @@ object Parsers {
676676
val t = typ()
677677
findWildcardType(t) match {
678678
case Some(wildcardPos) =>
679-
syntaxError("unbound wildcard type", wildcardPos)
679+
syntaxError(UnboundWildcardType(), wildcardPos)
680680
scalaAny
681681
case None => t
682682
}

compiler/src/dotty/tools/dotc/reporting/diagnostic/messages.scala

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -923,4 +923,61 @@ object messages {
923923
|"""
924924
}
925925

926+
case class UnboundWildcardType()(implicit ctx: Context) extends Message(35) {
927+
val kind = "Syntax"
928+
val msg = "Unbound wildcard type"
929+
val explanation = {
930+
931+
val paramListWildcard = """def foo(x: _) = ..."""
932+
val paramList = """def foo(x: Any) = ..."""
933+
934+
val typeArgsWildcard = """val foo = List[_](1, 2)"""
935+
val typeArgs = """val foo = List[Int](1, 2)"""
936+
937+
val typeBoundsWildcard = """def foo[T <: _](x: T) = ..."""
938+
val typeBounds = """def foo[T](x: T) = ..."""
939+
940+
val valTypeWildcard = """val foo: _ = 3"""
941+
val valType = """val foo: Int = 3"""
942+
943+
hl"""|The wildcard type syntax (`_`) was used where it could not be bound.
944+
|Replace `_` with a non-wildcard type. If the type doesn't matter,
945+
|try replacing `_` with ${"Any"}.
946+
|
947+
|Examples:
948+
|
949+
|- Parameter lists
950+
|
951+
| Instead of:
952+
| $paramListWildcard
953+
|
954+
| Use ${"Any"} if the type doesn't matter:
955+
| $paramList
956+
|
957+
|- Type arguments
958+
|
959+
| Instead of:
960+
| $typeArgsWildcard
961+
|
962+
| Use:
963+
| $typeArgs
964+
|
965+
|- Type bounds
966+
|
967+
| Instead of:
968+
| $typeBoundsWildcard
969+
|
970+
| Remove the bounds if the type doesn't matter:
971+
| $typeBounds
972+
|
973+
|- ${"val"} and ${"def"} types
974+
|
975+
| Instead of:
976+
| $valTypeWildcard
977+
|
978+
| Use:
979+
| $valType
980+
|"""
981+
}
982+
}
926983
}

0 commit comments

Comments
 (0)