Skip to content

Commit

Permalink
Various fixes to stdlib-cc (#19873)
Browse files Browse the repository at this point in the history
fixes #19819, fixes #19652, fixes #19847, supersedes #19858
  • Loading branch information
Linyxus authored Mar 6, 2024
2 parents 09f38cf + 76ed7eb commit 69c18f5
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 39 deletions.
10 changes: 6 additions & 4 deletions compiler/src/dotty/tools/dotc/printing/PlainPrinter.scala
Original file line number Diff line number Diff line change
Expand Up @@ -242,10 +242,12 @@ class PlainPrinter(_ctx: Context) extends Printer {
val refsText = if showAsCap then rootSetText else toTextCaptureSet(refs)
toTextCapturing(parent, refsText, boxText)
case tp @ RetainingType(parent, refs) =>
val refsText = refs match
case ref :: Nil if ref.symbol == defn.captureRoot => rootSetText
case _ => toTextRetainedElems(refs)
toTextCapturing(parent, refsText, "") ~ Str("R").provided(printDebug)
if Feature.ccEnabledSomewhere then
val refsText = refs match
case ref :: Nil if ref.symbol == defn.captureRoot => rootSetText
case _ => toTextRetainedElems(refs)
toTextCapturing(parent, refsText, "") ~ Str("R").provided(printDebug)
else toText(parent)
case tp: PreviousErrorType if ctx.settings.XprintTypes.value =>
"<error>" // do not print previously reported error message because they may try to print this error type again recuresevely
case tp: ErrorType =>
Expand Down
18 changes: 9 additions & 9 deletions scala2-library-cc/src/scala/collection/Seq.scala
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ trait SeqOps[+A, +CC[_], +C] extends Any with SeqViewOps[A, CC, C] { self =>
*
* @return a new $coll consisting of all the elements of this $coll without duplicates.
*/
def distinct: C = distinctBy(identity)
override def distinct: C = distinctBy(identity)

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

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

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

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

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

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

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

Expand All @@ -957,7 +957,7 @@ trait SeqOps[+A, +CC[_], +C] extends Any with SeqViewOps[A, CC, C] { self =>
* lazy collection this exception may be thrown at a later time or not at
* all (if the end of the collection is never evaluated).
*/
def updated[B >: A](index: Int, elem: B): CC[B] = {
override def updated[B >: A](index: Int, elem: B): CC[B] = {
if(index < 0) throw new IndexOutOfBoundsException(index.toString)
val k = knownSize
if(k >= 0 && index >= k) throw new IndexOutOfBoundsException(index.toString)
Expand Down
43 changes: 33 additions & 10 deletions scala2-library-cc/src/scala/collection/SeqView.scala
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,43 @@ trait SeqViewOps[+A, +CC[_], +C] extends Any with IterableOps[A, CC, C] {
def length: Int
def apply(x: Int): A
def appended[B >: A](elem: B): CC[B]^{this}
def updated[B >: A](index: Int, elem: B): CC[B]^{this}
def prepended[B >: A](elem: B): CC[B]^{this}
def reverse: C^{this}
def sorted[B >: A](implicit ord: Ordering[B]): C^{this}

// Placeholder implementations for the corresponding methods in SeqOps.
// This is needed due to the change in the class hierarchy in cc stdlib.
// See #19660 and #19819.
// -------------------
def updated[B >: A](index: Int, elem: B): CC[B]^{this} =
assert(false, "This is a placeholder implementation in the capture checked Scala 2 library.")
???
def padTo[B >: A](len: Int, elem: B): CC[B]^{this} =
assert(false, "This is a placeholder implementation in the capture checked Scala 2 library.")
???
def patch[B >: A](from: Int, other: IterableOnce[B]^, replaced: Int): CC[B]^{this, other} =
assert(false, "This is a placeholder implementation in the capture checked Scala 2 library.")
???
def combinations(n: Int): Iterator[C^{this}]^{this} =
assert(false, "This is a placeholder implementation in the capture checked Scala 2 library.")
???
def sortBy[B](f: A => B)(implicit ord: Ordering[B]): C^{this, f} =
assert(false, "This is a placeholder implementation in the capture checked Scala 2 library.")
???
def sortWith(lt: (A, A) => Boolean): C^{this, lt} =
assert(false, "This is a placeholder implementation in the capture checked Scala 2 library.")
???
def permutations: Iterator[C^{this}]^{this} =
assert(false, "This is a placeholder implementation in the capture checked Scala 2 library.")
???
def distinct: C^{this} =
assert(false, "This is a placeholder implementation in the capture checked Scala 2 library.")
???
def distinctBy[B](f: A -> B): C^{this} =
assert(false, "This is a placeholder implementation in the capture checked Scala 2 library.")
???
// -------------------

def reverseIterator: Iterator[A]^{this} = reversed.iterator
}

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

// Copied from SeqOps. This is needed due to the change of class hierarchy in stdlib.
// See #19660.
override def updated[B >: A](index: Int, elem: B): View[B]^{this} = {
if(index < 0) throw new IndexOutOfBoundsException(index.toString)
val k = knownSize
if(k >= 0 && index >= k) throw new IndexOutOfBoundsException(index.toString)
iterableFactory.from(new View.Updated(this, index, elem))
}

override def prepended[B >: A](elem: B): SeqView[B]^{this} = new SeqView.Prepended(elem, this)
override def reverse: SeqView[A]^{this} = new SeqView.Reverse(this)
override def take(n: Int): SeqView[A]^{this} = new SeqView.Take(this, n)
Expand Down
30 changes: 14 additions & 16 deletions scala2-library-cc/src/scala/collection/generic/IsSeq.scala
Original file line number Diff line number Diff line change
Expand Up @@ -54,24 +54,22 @@ object IsSeq {
seqOpsIsSeqVal.asInstanceOf[IsSeq[CC0[A0]] { type A = A0; type C = CC0[A0] }]

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

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

implicit val stringIsSeq: IsSeq[String] { type A = Char; type C = String } =
new IsSeq[String] {
Expand Down
1 change: 1 addition & 0 deletions tests/run/enrich-gentraversable.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import scala.language.postfixOps

object Test extends App {
import scala.collection.generic.IsIterable
import scala.collection.generic.IsSeq.seqViewIsSeq
import scala.collection.{BuildFrom, Iterable, IterableOps, View}
import scala.collection.immutable.TreeMap

Expand Down

0 comments on commit 69c18f5

Please sign in to comment.