Skip to content

Commit 1a172fd

Browse files
authored
Merge pull request scala#6809 from lrytz/ordMsg
Use @implicitNotFound on SortedSet and SortedMap
2 parents 5a0a36a + ff425b5 commit 1a172fd

File tree

10 files changed

+252
-26
lines changed

10 files changed

+252
-26
lines changed

src/compiler/scala/tools/nsc/typechecker/Implicits.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1665,7 +1665,7 @@ trait Implicits {
16651665

16661666
val argTypes1 = if (prefix == NoType) paramTypeRefs else paramTypeRefs.map(t => t.asSeenFrom(prefix, fun.symbol.owner))
16671667
val argTypes2 = fun match {
1668-
case TypeApply(_, targs) => argTypes1.map(_.instantiateTypeParams(fun.symbol.info.typeParams, targs.map(_.tpe)))
1668+
case treeInfo.Applied(_, targs, _) => argTypes1.map(_.instantiateTypeParams(fun.symbol.info.typeParams, targs.map(_.tpe)))
16691669
case _ => argTypes1
16701670
}
16711671

src/library/scala/Enumeration.scala

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@
88

99
package scala
1010

11-
import scala.collection.{ mutable, immutable, StrictOptimizedIterableOps, SpecificIterableFactory, View }
12-
import java.lang.reflect.{ Method => JMethod, Field => JField }
11+
import scala.collection.{SpecificIterableFactory, StrictOptimizedIterableOps, View, immutable, mutable}
12+
import java.lang.reflect.{Field => JField, Method => JMethod}
13+
14+
import scala.annotation.implicitNotFound
1315
import scala.reflect.NameTransformer._
1416
import scala.util.matching.Regex
1517

@@ -294,15 +296,24 @@ abstract class Enumeration (initial: Int) extends Serializable {
294296
def flatMap(f: Value => IterableOnce[Value]): ValueSet = fromSpecificIterable(new View.FlatMap(toIterable, f))
295297

296298
// necessary for disambiguation:
297-
override def map[B : Ordering](f: Value => B): SortedIterableCC[B] = super[SortedSet].map[B](f)
298-
override def flatMap[B : Ordering](f: Value => IterableOnce[B]): SortedIterableCC[B] = super[SortedSet].flatMap[B](f)
299+
override def map[B](f: Value => B)(implicit @implicitNotFound(ValueSet.ordMsg) ev: Ordering[B]): SortedIterableCC[B] =
300+
super[SortedSet].map[B](f)
301+
override def flatMap[B](f: Value => IterableOnce[B])(implicit @implicitNotFound(ValueSet.ordMsg) ev: Ordering[B]): SortedIterableCC[B] =
302+
super[SortedSet].flatMap[B](f)
303+
override def zip[B](that: Iterable[B])(implicit @implicitNotFound(ValueSet.zipOrdMsg) ev: Ordering[(Value, B)]): SortedIterableCC[(Value, B)] =
304+
super[SortedSet].zip[B](that)
305+
override def collect[B](pf: PartialFunction[Value, B])(implicit @implicitNotFound(ValueSet.ordMsg) ev: Ordering[B]): SortedIterableCC[B] =
306+
super[SortedSet].collect[B](pf)
299307

300308
override protected[this] def writeReplace(): AnyRef = this
301309
}
302310

303311
/** A factory object for value sets */
304312
@SerialVersionUID(3L)
305313
object ValueSet extends SpecificIterableFactory[Value, ValueSet] {
314+
private final val ordMsg = "No implicit Ordering[${B}] found to build a SortedSet[${B}]. You may want to upcast to a Set[Value] first by calling `unsorted`."
315+
private final val zipOrdMsg = "No implicit Ordering[${B}] found to build a SortedSet[(Value, ${B})]. You may want to upcast to a Set[Value] first by calling `unsorted`."
316+
306317
/** The empty value set */
307318
val empty = new ValueSet(immutable.BitSet.empty)
308319
/** A value set containing all the values for the zero-adjusted ids

src/library/scala/collection/BitSet.scala

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ trait BitSet extends SortedSet[Int] with BitSetOps[BitSet] {
2828

2929
@SerialVersionUID(3L)
3030
object BitSet extends SpecificIterableFactory[Int, BitSet] {
31+
private[collection] final val ordMsg = "No implicit Ordering[${B}] found to build a SortedSet[${B}]. You may want to upcast to a Set[Int] first by calling `unsorted`."
32+
private[collection] final val zipOrdMsg = "No implicit Ordering[${B}] found to build a SortedSet[(Int, ${B})]. You may want to upcast to a Set[Int] first by calling `unsorted`."
33+
3134
def empty: BitSet = immutable.BitSet.empty
3235
def newBuilder: Builder[Int, BitSet] = immutable.BitSet.newBuilder
3336
def fromSpecific(it: IterableOnce[Int]): BitSet = immutable.BitSet.fromSpecific(it)

src/library/scala/collection/SortedMap.scala

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package scala
22
package collection
33

4+
import scala.annotation.implicitNotFound
45
import scala.collection.immutable.TreeMap
5-
import scala.collection.mutable.Builder
66
import scala.language.higherKinds
77
import scala.annotation.unchecked.uncheckedVariance
88
import scala.collection.generic.DefaultSerializationProxy
@@ -144,7 +144,7 @@ trait SortedMapOps[K, +V, +CC[X, Y] <: Map[X, Y] with SortedMapOps[X, Y, CC, _],
144144
* @return a new $coll resulting from applying the given function
145145
* `f` to each element of this $coll and collecting the results.
146146
*/
147-
def map[K2, V2](f: ((K, V)) => (K2, V2))(implicit ordering: Ordering[K2]): CC[K2, V2] =
147+
def map[K2, V2](f: ((K, V)) => (K2, V2))(implicit @implicitNotFound(SortedMapOps.ordMsg) ordering: Ordering[K2]): CC[K2, V2] =
148148
sortedMapFactory.from(new View.Map[(K, V), (K2, V2)](toIterable, f))
149149

150150
/** Builds a new sorted map by applying a function to all elements of this $coll
@@ -154,7 +154,7 @@ trait SortedMapOps[K, +V, +CC[X, Y] <: Map[X, Y] with SortedMapOps[X, Y, CC, _],
154154
* @return a new $coll resulting from applying the given collection-valued function
155155
* `f` to each element of this $coll and concatenating the results.
156156
*/
157-
def flatMap[K2, V2](f: ((K, V)) => IterableOnce[(K2, V2)])(implicit ordering: Ordering[K2]): CC[K2, V2] =
157+
def flatMap[K2, V2](f: ((K, V)) => IterableOnce[(K2, V2)])(implicit @implicitNotFound(SortedMapOps.ordMsg) ordering: Ordering[K2]): CC[K2, V2] =
158158
sortedMapFactory.from(new View.FlatMap(toIterable, f))
159159

160160
/** Builds a new sorted map by applying a partial function to all elements of this $coll
@@ -165,7 +165,7 @@ trait SortedMapOps[K, +V, +CC[X, Y] <: Map[X, Y] with SortedMapOps[X, Y, CC, _],
165165
* `pf` to each element on which it is defined and collecting the results.
166166
* The order of the elements is preserved.
167167
*/
168-
def collect[K2, V2](pf: PartialFunction[(K, V), (K2, V2)])(implicit ordering: Ordering[K2]): CC[K2, V2] =
168+
def collect[K2, V2](pf: PartialFunction[(K, V), (K2, V2)])(implicit @implicitNotFound(SortedMapOps.ordMsg) ordering: Ordering[K2]): CC[K2, V2] =
169169
flatMap { kv =>
170170
if (pf.isDefinedAt(kv)) new View.Single(pf(kv))
171171
else View.Empty
@@ -186,6 +186,8 @@ trait SortedMapOps[K, +V, +CC[X, Y] <: Map[X, Y] with SortedMapOps[X, Y, CC, _],
186186
}
187187

188188
object SortedMapOps {
189+
private final val ordMsg = "No implicit Ordering[${K2}] found to build a SortedMap[${K2}, ${V2}]. You may want to upcast to a Map[${K}, ${V}] first by calling `unsorted`."
190+
189191
/** Specializes `MapWithFilter` for sorted Map collections
190192
*
191193
* @define coll sorted map collection

src/library/scala/collection/SortedSet.scala

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package scala.collection
22

3+
import scala.annotation.implicitNotFound
34
import scala.annotation.unchecked.uncheckedVariance
45
import scala.collection.generic.DefaultSerializationProxy
56
import scala.language.higherKinds
@@ -95,7 +96,8 @@ trait SortedSetOps[A, +CC[X] <: SortedSet[X], +C <: SortedSetOps[A, CC, C]]
9596
* @return a new $coll resulting from applying the given function
9697
* `f` to each element of this $coll and collecting the results.
9798
*/
98-
def map[B : Ordering](f: A => B): CC[B] = sortedFromIterable(new View.Map(toIterable, f))
99+
def map[B](f: A => B)(implicit @implicitNotFound(SortedSetOps.ordMsg) ev: Ordering[B]): CC[B] =
100+
sortedFromIterable(new View.Map(toIterable, f))
99101

100102
/** Builds a new sorted collection by applying a function to all elements of this $coll
101103
* and using the elements of the resulting collections.
@@ -105,7 +107,8 @@ trait SortedSetOps[A, +CC[X] <: SortedSet[X], +C <: SortedSetOps[A, CC, C]]
105107
* @return a new $coll resulting from applying the given collection-valued function
106108
* `f` to each element of this $coll and concatenating the results.
107109
*/
108-
def flatMap[B : Ordering](f: A => IterableOnce[B]): CC[B] = sortedFromIterable(new View.FlatMap(toIterable, f))
110+
def flatMap[B](f: A => IterableOnce[B])(implicit @implicitNotFound(SortedSetOps.ordMsg) ev: Ordering[B]): CC[B] =
111+
sortedFromIterable(new View.FlatMap(toIterable, f))
109112

110113
/** Returns a $coll formed from this $coll and another iterable collection
111114
* by combining corresponding elements in pairs.
@@ -116,7 +119,7 @@ trait SortedSetOps[A, +CC[X] <: SortedSet[X], +C <: SortedSetOps[A, CC, C]]
116119
* @return a new $coll containing pairs consisting of corresponding elements of this $coll and `that`.
117120
* The length of the returned collection is the minimum of the lengths of this $coll and `that`.
118121
*/
119-
def zip[B](that: Iterable[B])(implicit ev: Ordering[(A @uncheckedVariance, B)]): CC[(A @uncheckedVariance, B)] = // sound bcs of VarianceNote
122+
def zip[B](that: Iterable[B])(implicit @implicitNotFound(SortedSetOps.zipOrdMsg) ev: Ordering[(A @uncheckedVariance, B)]): CC[(A @uncheckedVariance, B)] = // sound bcs of VarianceNote
120123
sortedFromIterable(new View.Zip(toIterable, that))
121124

122125
/** Builds a new sorted collection by applying a partial function to all elements of this $coll
@@ -128,13 +131,16 @@ trait SortedSetOps[A, +CC[X] <: SortedSet[X], +C <: SortedSetOps[A, CC, C]]
128131
* `pf` to each element on which it is defined and collecting the results.
129132
* The order of the elements is preserved.
130133
*/
131-
def collect[B: Ordering](pf: scala.PartialFunction[A, B]): CC[B] = flatMap(a =>
132-
if (pf.isDefinedAt(a)) new View.Single(pf(a))
133-
else View.Empty
134-
)
134+
def collect[B](pf: scala.PartialFunction[A, B])(implicit @implicitNotFound(SortedSetOps.ordMsg) ev: Ordering[B]): CC[B] =
135+
flatMap(a =>
136+
if (pf.isDefinedAt(a)) new View.Single(pf(a))
137+
else View.Empty)
138+
135139
}
136140

137141
object SortedSetOps {
142+
private[collection] final val ordMsg = "No implicit Ordering[${B}] found to build a SortedSet[${B}]. You may want to upcast to a Set[${A}] first by calling `unsorted`."
143+
private[collection] final val zipOrdMsg = "No implicit Ordering[${B}] found to build a SortedSet[(${A}, ${B})]. You may want to upcast to a Set[${A}] first by calling `unsorted`."
138144

139145
/** Specialize `WithFilter` for sorted collections
140146
*

src/library/scala/collection/StrictOptimizedSortedSetOps.scala

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@ package scala
22
package collection
33

44

5+
import scala.annotation.implicitNotFound
56
import scala.annotation.unchecked.uncheckedVariance
67
import scala.language.higherKinds
78

89
trait StrictOptimizedSortedSetOps[A, +CC[X] <: SortedSet[X], +C <: SortedSetOps[A, CC, C]]
910
extends SortedSetOps[A, CC, C]
1011
with StrictOptimizedIterableOps[A, Set, C] {
1112

12-
override def map[B : Ordering](f: A => B): CC[B] = {
13+
override def map[B](f: A => B)(implicit @implicitNotFound(SortedSetOps.ordMsg) ev: Ordering[B]): CC[B] = {
1314
val b = sortedIterableFactory.newBuilder[B]
1415
val it = iterator
1516
while (it.hasNext) {
@@ -18,7 +19,7 @@ trait StrictOptimizedSortedSetOps[A, +CC[X] <: SortedSet[X], +C <: SortedSetOps[
1819
b.result()
1920
}
2021

21-
override def flatMap[B : Ordering](f: A => IterableOnce[B]): CC[B] = {
22+
override def flatMap[B](f: A => IterableOnce[B])(implicit @implicitNotFound(SortedSetOps.ordMsg) ev: Ordering[B]): CC[B] = {
2223
val b = sortedIterableFactory.newBuilder[B]
2324
val it = iterator
2425
while (it.hasNext) {
@@ -27,7 +28,7 @@ trait StrictOptimizedSortedSetOps[A, +CC[X] <: SortedSet[X], +C <: SortedSetOps[
2728
b.result()
2829
}
2930

30-
override def zip[B](that: Iterable[B])(implicit ev: Ordering[(A @uncheckedVariance, B)]): CC[(A @uncheckedVariance, B)] = { // sound bcs of VarianceNot
31+
override def zip[B](that: Iterable[B])(implicit @implicitNotFound(SortedSetOps.zipOrdMsg) ev: Ordering[(A @uncheckedVariance, B)]): CC[(A @uncheckedVariance, B)] = { // sound bcs of VarianceNot
3132
val b = sortedIterableFactory.newBuilder[(A, B)]
3233
val it1 = iterator
3334
val it2 = that.iterator
@@ -37,7 +38,7 @@ trait StrictOptimizedSortedSetOps[A, +CC[X] <: SortedSet[X], +C <: SortedSetOps[
3738
b.result()
3839
}
3940

40-
override def collect[B : Ordering](pf: PartialFunction[A, B]): CC[B] = {
41+
override def collect[B](pf: PartialFunction[A, B])(implicit @implicitNotFound(SortedSetOps.ordMsg) ev: Ordering[B]): CC[B] = {
4142
val b = sortedIterableFactory.newBuilder[B]
4243
val it = iterator
4344
while (it.hasNext) {

src/library/scala/collection/immutable/BitSet.scala

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import java.io.{ObjectInputStream, ObjectOutputStream}
66

77
import BitSetOps.{LogWL, updateArray}
88
import mutable.{Builder, GrowableBuilder}
9+
import scala.annotation.implicitNotFound
910

1011

1112
/** A class for immutable bitsets.
@@ -50,13 +51,20 @@ sealed abstract class BitSet
5051
protected def updateWord(idx: Int, w: Long): BitSet
5152

5253
override def map(f: Int => Int): BitSet = super[BitSet].map(f)
53-
override def map[B : Ordering](f: Int => B): SortedSet[B] = super[SortedSetOps].map(f)
54+
override def map[B](f: Int => B)(implicit @implicitNotFound(collection.BitSet.ordMsg) ev: Ordering[B]): SortedSet[B] =
55+
super[SortedSetOps].map(f)
5456

5557
override def flatMap(f: Int => IterableOnce[Int]): BitSet = super[BitSet].flatMap(f)
56-
override def flatMap[B : Ordering](f: Int => IterableOnce[B]): SortedSet[B] = super[SortedSetOps].flatMap(f)
58+
override def flatMap[B](f: Int => IterableOnce[B])(implicit @implicitNotFound(collection.BitSet.ordMsg) ev: Ordering[B]): SortedSet[B] =
59+
super[SortedSetOps].flatMap(f)
5760

5861
override def collect(pf: PartialFunction[Int, Int]): BitSet = super[BitSet].collect(pf)
59-
override def collect[B: Ordering](pf: scala.PartialFunction[Int, B]): SortedSet[B] = super[SortedSetOps].collect(pf)
62+
override def collect[B](pf: scala.PartialFunction[Int, B])(implicit @implicitNotFound(collection.BitSet.ordMsg) ev: Ordering[B]): SortedSet[B] =
63+
super[SortedSetOps].collect(pf)
64+
65+
// necessary for disambiguation
66+
override def zip[B](that: scala.Iterable[B])(implicit @implicitNotFound(collection.BitSet.zipOrdMsg) ev: Ordering[(Int, B)]): SortedSet[(Int, B)] =
67+
super.zip(that)
6068

6169
override protected[this] def writeReplace(): AnyRef = new BitSet.SerializationProxy(this)
6270
}

src/library/scala/collection/mutable/BitSet.scala

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import java.io.{ObjectInputStream, ObjectOutputStream}
66

77
import scala.collection.immutable.Range
88
import BitSetOps.{LogWL, MaxSize}
9+
import scala.annotation.implicitNotFound
910

1011

1112
/**
@@ -141,13 +142,20 @@ class BitSet(protected[collection] final var elems: Array[Long])
141142
def toImmutable: immutable.BitSet = immutable.BitSet.fromBitMask(elems)
142143

143144
override def map(f: Int => Int): BitSet = super[BitSet].map(f)
144-
override def map[B : Ordering](f: Int => B): SortedSet[B] = super[SortedSetOps].map(f)
145+
override def map[B](f: Int => B)(implicit @implicitNotFound(collection.BitSet.ordMsg) ev: Ordering[B]): SortedSet[B] =
146+
super[SortedSetOps].map(f)
145147

146148
override def flatMap(f: Int => IterableOnce[Int]): BitSet = super[BitSet].flatMap(f)
147-
override def flatMap[B : Ordering](f: Int => IterableOnce[B]): SortedSet[B] = super[SortedSetOps].flatMap(f)
149+
override def flatMap[B](f: Int => IterableOnce[B])(implicit @implicitNotFound(collection.BitSet.ordMsg) ev: Ordering[B]): SortedSet[B] =
150+
super[SortedSetOps].flatMap(f)
148151

149152
override def collect(pf: PartialFunction[Int, Int]): BitSet = super[BitSet].collect(pf)
150-
override def collect[B: Ordering](pf: scala.PartialFunction[Int, B]): SortedSet[B] = super[SortedSetOps].collect(pf)
153+
override def collect[B](pf: scala.PartialFunction[Int, B])(implicit @implicitNotFound(collection.BitSet.ordMsg) ev: Ordering[B]): SortedSet[B] =
154+
super[SortedSetOps].collect(pf)
155+
156+
// necessary for disambiguation
157+
override def zip[B](that: scala.Iterable[B])(implicit @implicitNotFound(collection.BitSet.zipOrdMsg) ev: Ordering[(Int, B)]): SortedSet[(Int, B)] =
158+
super.zip(that)
151159

152160
override protected[this] def writeReplace(): AnyRef = new BitSet.SerializationProxy(this)
153161
}

0 commit comments

Comments
 (0)