Skip to content

Commit ab22def

Browse files
authored
Merge pull request #6500 from eed3si9n/wip/deprecate-view-bounds
scala/bug#10719. Deprecate view bounds without -Xfuture
2 parents 48c7ef4 + 14f1dd8 commit ab22def

14 files changed

+43
-22
lines changed

src/compiler/scala/tools/nsc/ast/parser/Parsers.scala

+5-4
Original file line numberDiff line numberDiff line change
@@ -2425,10 +2425,11 @@ self =>
24252425
TypeDef(mods, pname, tparams, typeBounds())
24262426
}
24272427
if (contextBoundBuf ne null) {
2428+
def msg(what: String) = s"""view bounds are $what; use an implicit parameter instead.
2429+
| example: instead of `def f[A <% Int](a: A)` use `def f[A](a: A)(implicit ev: A => Int)`""".stripMargin
24282430
while (in.token == VIEWBOUND) {
2429-
val msg = "Use an implicit parameter instead.\nExample: Instead of `def f[A <% Int](a: A)` use `def f[A](a: A)(implicit ev: A => Int)`."
2430-
if (settings.future)
2431-
deprecationWarning(in.offset, s"View bounds are deprecated. $msg", "2.12.0")
2431+
if (settings.isScala214) syntaxError(in.offset, msg("unsupported"))
2432+
else deprecationWarning(in.offset, msg("deprecated"), "2.12.0")
24322433
contextBoundBuf += atPos(in.skipToken())(makeFunctionTypeTree(List(Ident(pname)), typ()))
24332434
}
24342435
while (in.token == COLON) {
@@ -2878,7 +2879,7 @@ self =>
28782879
classContextBounds = contextBoundBuf.toList
28792880
val tstart = (in.offset :: classContextBounds.map(_.pos.start)).min
28802881
if (!classContextBounds.isEmpty && mods.isTrait) {
2881-
val viewBoundsExist = if (settings.future) "" else " nor view bounds `<% ...'"
2882+
val viewBoundsExist = if (settings.isScala214) "" else " nor view bounds `<% ...'"
28822883
syntaxError(s"traits cannot have type parameters with context bounds `: ...'$viewBoundsExist", skipIt = false)
28832884
classContextBounds = List()
28842885
}

test/files/neg/overloaded-implicit.scala

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@ object Test {
22
implicit def imp1[T](x: List[T]): Map[T, T] = Map()
33
implicit def imp1[T](x: Set[T]): Map[T, T] = Map()
44

5-
def f[T <% Map[Int, Int]](x: T): Double = 1.0d
5+
def f[T](x: T)(implicit ev: T => Map[Int, Int]): Double = 1.0d
66

77
// not parameterized, no warning
88
implicit def imp2(x: List[Int]): String = "a"
99
implicit def imp2(x: Set[Int]): String = "b"
1010

11-
def g[T <% String](x: T): Double = 2.0d
11+
def g[T](x: T)(implicit ev: T => String): Double = 2.0d
1212

1313
def main(args: Array[String]): Unit = {
1414
// println(f(List(1)))

test/files/neg/t7629-view-bounds-deprecation.check

-11
This file was deleted.

test/files/neg/t7629-view-bounds-deprecation.flags

-1
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
view-bounds-deprecation.scala:2: warning: view bounds are deprecated; use an implicit parameter instead.
2+
example: instead of `def f[A <% Int](a: A)` use `def f[A](a: A)(implicit ev: A => Int)`
3+
def f[A <% Int](a: A) = null
4+
^
5+
view-bounds-deprecation.scala:3: warning: view bounds are deprecated; use an implicit parameter instead.
6+
example: instead of `def f[A <% Int](a: A)` use `def f[A](a: A)(implicit ev: A => Int)`
7+
def g[C, B <: C, A <% B : Numeric](a: A) = null
8+
^
9+
error: No warnings can be incurred under -Xfatal-warnings.
10+
two warnings found
11+
one error found
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
-deprecation -Xfatal-warnings
+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
view-bounds-removal.scala:2: error: view bounds are unsupported; use an implicit parameter instead.
2+
example: instead of `def f[A <% Int](a: A)` use `def f[A](a: A)(implicit ev: A => Int)`
3+
def f[A <% Int](a: A) = null
4+
^
5+
view-bounds-removal.scala:3: error: view bounds are unsupported; use an implicit parameter instead.
6+
example: instead of `def f[A <% Int](a: A)` use `def f[A](a: A)(implicit ev: A => Int)`
7+
def g[C, B <: C, A <% B : Numeric](a: A) = null
8+
^
9+
two errors found
+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
-Xsource:2.14
+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
object Test {
2+
def f[A <% Int](a: A) = null
3+
def g[C, B <: C, A <% B : Numeric](a: A) = null
4+
}

test/files/run/iterator-from.scala

+4-4
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ object Test extends App {
1111
val maxKey = 50
1212
val maxValue = 50
1313

14-
def testSet[A <% Ordered[A]](set: SortedSet[A], list: List[A]) {
14+
def testSet[A](set: SortedSet[A], list: List[A])(implicit ev: A => Ordered[A]): Unit = {
1515
val distinctSorted = list.distinct.sorted
1616
assertEquals("Set size wasn't the same as list sze", set.size, distinctSorted.size)
1717

@@ -24,7 +24,7 @@ object Test extends App {
2424
}
2525
}
2626

27-
def testMap[A <% Ordered[A], B](map: SortedMap[A, B], list: List[(A, B)]) {
27+
def testMap[A, B](map: SortedMap[A, B], list: List[(A, B)])(implicit ev: A => Ordered[A]): Unit = {
2828
val distinctSorted = distinctByKey(list).sortBy(_._1)
2929
assertEquals("Map size wasn't the same as list sze", map.size, distinctSorted.size)
3030

@@ -39,11 +39,11 @@ object Test extends App {
3939
}
4040
}
4141

42-
def check[A](clazz: Class[_], list: List[_], m1: String, m2: String, l1: List[A], l2: List[A]) {
42+
def check[A](clazz: Class[_], list: List[_], m1: String, m2: String, l1: List[A], l2: List[A]): Unit = {
4343
assertEquals(s"$clazz: `$m1` didn't match `$m2` on list $list", l1, l2)
4444
}
4545

46-
def assertEquals[A](msg: String, x: A, y: A) {
46+
def assertEquals[A](msg: String, x: A, y: A): Unit = {
4747
assert(x == y, s"$msg\n1: $x\n2: $y")
4848
}
4949

Original file line numberDiff line numberDiff line change
@@ -1 +1,5 @@
1+
Macros_Test_2.scala:10: warning: view bounds are deprecated; use an implicit parameter instead.
2+
example: instead of `def f[A <% Int](a: A)` use `def f[A](a: A)(implicit ev: A => Int)`
3+
def bar[T <% Option[Int]](x: T) = println(x)
4+
^
15
2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
-deprecation

test/files/run/t3346e.check

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
warning: there were two deprecation warnings (since 2.12.0); re-run with -deprecation for details
12
eqw
23
List(0, 2)
34
List(0, 2)

0 commit comments

Comments
 (0)