Skip to content

Commit 69c18f5

Browse files
authored
Various fixes to stdlib-cc (#19873)
fixes #19819, fixes #19652, fixes #19847, supersedes #19858
2 parents 09f38cf + 76ed7eb commit 69c18f5

File tree

5 files changed

+63
-39
lines changed

5 files changed

+63
-39
lines changed

compiler/src/dotty/tools/dotc/printing/PlainPrinter.scala

+6-4
Original file line numberDiff line numberDiff line change
@@ -242,10 +242,12 @@ class PlainPrinter(_ctx: Context) extends Printer {
242242
val refsText = if showAsCap then rootSetText else toTextCaptureSet(refs)
243243
toTextCapturing(parent, refsText, boxText)
244244
case tp @ RetainingType(parent, refs) =>
245-
val refsText = refs match
246-
case ref :: Nil if ref.symbol == defn.captureRoot => rootSetText
247-
case _ => toTextRetainedElems(refs)
248-
toTextCapturing(parent, refsText, "") ~ Str("R").provided(printDebug)
245+
if Feature.ccEnabledSomewhere then
246+
val refsText = refs match
247+
case ref :: Nil if ref.symbol == defn.captureRoot => rootSetText
248+
case _ => toTextRetainedElems(refs)
249+
toTextCapturing(parent, refsText, "") ~ Str("R").provided(printDebug)
250+
else toText(parent)
249251
case tp: PreviousErrorType if ctx.settings.XprintTypes.value =>
250252
"<error>" // do not print previously reported error message because they may try to print this error type again recuresevely
251253
case tp: ErrorType =>

scala2-library-cc/src/scala/collection/Seq.scala

+9-9
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ trait SeqOps[+A, +CC[_], +C] extends Any with SeqViewOps[A, CC, C] { self =>
206206
*
207207
* @return a new $coll consisting of all the elements of this $coll without duplicates.
208208
*/
209-
def distinct: C = distinctBy(identity)
209+
override def distinct: C = distinctBy(identity)
210210

211211
/** Selects all the elements of this $coll ignoring the duplicates as determined by `==` after applying
212212
* the transforming function `f`.
@@ -215,7 +215,7 @@ trait SeqOps[+A, +CC[_], +C] extends Any with SeqViewOps[A, CC, C] { self =>
215215
* @tparam B the type of the elements after being transformed by `f`
216216
* @return a new $coll consisting of all the elements of this $coll without duplicates.
217217
*/
218-
def distinctBy[B](f: A -> B): C = fromSpecific(new View.DistinctBy(this, f))
218+
override def distinctBy[B](f: A -> B): C = fromSpecific(new View.DistinctBy(this, f))
219219

220220
/** Returns new $coll with elements in reversed order.
221221
*
@@ -293,7 +293,7 @@ trait SeqOps[+A, +CC[_], +C] extends Any with SeqViewOps[A, CC, C] { self =>
293293
* all elements of this $coll followed by the minimal number of occurrences of `elem` so
294294
* that the resulting collection has a length of at least `len`.
295295
*/
296-
def padTo[B >: A](len: Int, elem: B): CC[B] = iterableFactory.from(new View.PadTo(this, len, elem))
296+
override def padTo[B >: A](len: Int, elem: B): CC[B] = iterableFactory.from(new View.PadTo(this, len, elem))
297297

298298
/** Computes the length of the longest segment that starts from the first element
299299
* and whose elements all satisfy some predicate.
@@ -544,7 +544,7 @@ trait SeqOps[+A, +CC[_], +C] extends Any with SeqViewOps[A, CC, C] { self =>
544544
* // List(b, b, a)
545545
* }}}
546546
*/
547-
def permutations: Iterator[C] =
547+
override def permutations: Iterator[C] =
548548
if (isEmpty) Iterator.single(coll)
549549
else new PermutationsItr
550550

@@ -585,7 +585,7 @@ trait SeqOps[+A, +CC[_], +C] extends Any with SeqViewOps[A, CC, C] { self =>
585585
* // List(b, a)
586586
* }}}
587587
*/
588-
def combinations(n: Int): Iterator[C] =
588+
override def combinations(n: Int): Iterator[C] =
589589
if (n < 0 || n > size) Iterator.empty
590590
else new CombinationsItr(n)
591591

@@ -759,7 +759,7 @@ trait SeqOps[+A, +CC[_], +C] extends Any with SeqViewOps[A, CC, C] { self =>
759759
* List("Bobby", "Bob", "John", "Steve", "Tom")
760760
* }}}
761761
*/
762-
def sortWith(lt: (A, A) => Boolean): C = sorted(Ordering.fromLessThan(lt))
762+
override def sortWith(lt: (A, A) => Boolean): C = sorted(Ordering.fromLessThan(lt))
763763

764764
/** Sorts this $coll according to the Ordering which results from transforming
765765
* an implicitly given Ordering with a transformation function.
@@ -786,7 +786,7 @@ trait SeqOps[+A, +CC[_], +C] extends Any with SeqViewOps[A, CC, C] { self =>
786786
* res0: Array[String] = Array(The, dog, fox, the, lazy, over, brown, quick, jumped)
787787
* }}}
788788
*/
789-
def sortBy[B](f: A => B)(implicit ord: Ordering[B]): C = sorted(ord on f)
789+
override def sortBy[B](f: A => B)(implicit ord: Ordering[B]): C = sorted(ord on f)
790790

791791
/** Produces the range of all indices of this sequence.
792792
* $willForceEvaluation
@@ -944,7 +944,7 @@ trait SeqOps[+A, +CC[_], +C] extends Any with SeqViewOps[A, CC, C] { self =>
944944
* except that `replaced` elements starting from `from` are replaced
945945
* by all the elements of `other`.
946946
*/
947-
def patch[B >: A](from: Int, other: IterableOnce[B]^, replaced: Int): CC[B] =
947+
override def patch[B >: A](from: Int, other: IterableOnce[B]^, replaced: Int): CC[B] =
948948
iterableFactory.from(new View.Patched(this, from, other, replaced))
949949
.unsafeAssumePure // assume pure OK since iterableFactory.from is eager for Seq
950950

@@ -957,7 +957,7 @@ trait SeqOps[+A, +CC[_], +C] extends Any with SeqViewOps[A, CC, C] { self =>
957957
* lazy collection this exception may be thrown at a later time or not at
958958
* all (if the end of the collection is never evaluated).
959959
*/
960-
def updated[B >: A](index: Int, elem: B): CC[B] = {
960+
override def updated[B >: A](index: Int, elem: B): CC[B] = {
961961
if(index < 0) throw new IndexOutOfBoundsException(index.toString)
962962
val k = knownSize
963963
if(k >= 0 && index >= k) throw new IndexOutOfBoundsException(index.toString)

scala2-library-cc/src/scala/collection/SeqView.scala

+33-10
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,43 @@ trait SeqViewOps[+A, +CC[_], +C] extends Any with IterableOps[A, CC, C] {
3030
def length: Int
3131
def apply(x: Int): A
3232
def appended[B >: A](elem: B): CC[B]^{this}
33-
def updated[B >: A](index: Int, elem: B): CC[B]^{this}
3433
def prepended[B >: A](elem: B): CC[B]^{this}
3534
def reverse: C^{this}
3635
def sorted[B >: A](implicit ord: Ordering[B]): C^{this}
3736

37+
// Placeholder implementations for the corresponding methods in SeqOps.
38+
// This is needed due to the change in the class hierarchy in cc stdlib.
39+
// See #19660 and #19819.
40+
// -------------------
41+
def updated[B >: A](index: Int, elem: B): CC[B]^{this} =
42+
assert(false, "This is a placeholder implementation in the capture checked Scala 2 library.")
43+
???
44+
def padTo[B >: A](len: Int, elem: B): CC[B]^{this} =
45+
assert(false, "This is a placeholder implementation in the capture checked Scala 2 library.")
46+
???
47+
def patch[B >: A](from: Int, other: IterableOnce[B]^, replaced: Int): CC[B]^{this, other} =
48+
assert(false, "This is a placeholder implementation in the capture checked Scala 2 library.")
49+
???
50+
def combinations(n: Int): Iterator[C^{this}]^{this} =
51+
assert(false, "This is a placeholder implementation in the capture checked Scala 2 library.")
52+
???
53+
def sortBy[B](f: A => B)(implicit ord: Ordering[B]): C^{this, f} =
54+
assert(false, "This is a placeholder implementation in the capture checked Scala 2 library.")
55+
???
56+
def sortWith(lt: (A, A) => Boolean): C^{this, lt} =
57+
assert(false, "This is a placeholder implementation in the capture checked Scala 2 library.")
58+
???
59+
def permutations: Iterator[C^{this}]^{this} =
60+
assert(false, "This is a placeholder implementation in the capture checked Scala 2 library.")
61+
???
62+
def distinct: C^{this} =
63+
assert(false, "This is a placeholder implementation in the capture checked Scala 2 library.")
64+
???
65+
def distinctBy[B](f: A -> B): C^{this} =
66+
assert(false, "This is a placeholder implementation in the capture checked Scala 2 library.")
67+
???
68+
// -------------------
69+
3870
def reverseIterator: Iterator[A]^{this} = reversed.iterator
3971
}
4072

@@ -46,15 +78,6 @@ trait SeqView[+A] extends SeqViewOps[A, View, View[A]] with View[A] {
4678
override def map[B](f: A => B): SeqView[B]^{this, f} = new SeqView.Map(this, f)
4779
override def appended[B >: A](elem: B): SeqView[B]^{this} = new SeqView.Appended(this, elem)
4880

49-
// Copied from SeqOps. This is needed due to the change of class hierarchy in stdlib.
50-
// See #19660.
51-
override def updated[B >: A](index: Int, elem: B): View[B]^{this} = {
52-
if(index < 0) throw new IndexOutOfBoundsException(index.toString)
53-
val k = knownSize
54-
if(k >= 0 && index >= k) throw new IndexOutOfBoundsException(index.toString)
55-
iterableFactory.from(new View.Updated(this, index, elem))
56-
}
57-
5881
override def prepended[B >: A](elem: B): SeqView[B]^{this} = new SeqView.Prepended(elem, this)
5982
override def reverse: SeqView[A]^{this} = new SeqView.Reverse(this)
6083
override def take(n: Int): SeqView[A]^{this} = new SeqView.Take(this, n)

scala2-library-cc/src/scala/collection/generic/IsSeq.scala

+14-16
Original file line numberDiff line numberDiff line change
@@ -54,24 +54,22 @@ object IsSeq {
5454
seqOpsIsSeqVal.asInstanceOf[IsSeq[CC0[A0]] { type A = A0; type C = CC0[A0] }]
5555

5656
/** !!! Under cc, views are not Seqs and can't use SeqOps.
57-
* So this should be renamed to seqViewIsIterable
58-
*/
59-
implicit def seqViewIsSeq[CC0[X] <: SeqView[X], A0]: IsIterable[CC0[A0]] { type A = A0; type C = View[A0] } =
60-
new IsIterable[CC0[A0]] {
61-
type A = A0
62-
type C = View[A]
63-
def apply(coll: CC0[A0]): IterableOps[A0, View, View[A0]] = coll
64-
}
57+
* Therefore, [[seqViewIsSeq]] now returns an [[IsIterable]].
58+
* The helper method [[seqViewIsSeq_]] is added to make the binary compatible.
59+
*/
60+
@annotation.targetName("seqViewIsSeq")
61+
@annotation.publicInBinary
62+
private[IsSeq] def seqViewIsSeq_[CC0[X] <: SeqView[X], A0]: IsSeq[CC0[A0]] { type A = A0; type C = View[A0] } = ???
63+
implicit inline def seqViewIsSeq[CC0[X] <: SeqView[X], A0]: IsIterable[CC0[A0]] { type A = A0; type C = View[A0] } = seqViewIsSeq_[CC0, A0].asInstanceOf
6564

6665
/** !!! Under cc, views are not Seqs and can't use SeqOps.
67-
* So this should be renamed to stringViewIsIterable
68-
*/
69-
implicit val stringViewIsSeq: IsIterable[StringView] { type A = Char; type C = View[Char] } =
70-
new IsIterable[StringView] {
71-
type A = Char
72-
type C = View[Char]
73-
def apply(coll: StringView): IterableOps[Char, View, View[Char]] = coll
74-
}
66+
* Therefore, [[stringViewIsSeq]] now returns an [[IsIterable]].
67+
* The helper method [[stringViewIsSeq__]] is added to make the binary compatible.
68+
*/
69+
@annotation.targetName("stringViewIsSeq")
70+
@annotation.publicInBinary
71+
private[IsSeq] val stringViewIsSeq_ : IsSeq[StringView] { type A = Char; type C = View[Char] } = ???
72+
inline implicit def stringViewIsSeq: IsIterable[StringView] { type A = Char; type C = View[Char] } = stringViewIsSeq_.asInstanceOf
7573

7674
implicit val stringIsSeq: IsSeq[String] { type A = Char; type C = String } =
7775
new IsSeq[String] {

tests/run/enrich-gentraversable.scala

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import scala.language.postfixOps
33

44
object Test extends App {
55
import scala.collection.generic.IsIterable
6+
import scala.collection.generic.IsSeq.seqViewIsSeq
67
import scala.collection.{BuildFrom, Iterable, IterableOps, View}
78
import scala.collection.immutable.TreeMap
89

0 commit comments

Comments
 (0)