Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
89 commits
Select commit Hold shift + click to select a range
3640ff4
Don't recheck inherited trait parameters during capture checking
odersky Oct 30, 2023
b65fd3a
Add SeqView to stdlib
odersky Oct 30, 2023
0ada1d4
Add IndexedSeqView to lib
odersky Oct 30, 2023
dbd0c0e
Add CheckedIndexedView to stdlib
odersky Oct 30, 2023
8a60bc2
Add ArrayBuffer (unchecked) to stdlib
odersky Oct 30, 2023
61bf139
Rename annotation source file
odersky Oct 30, 2023
6b91167
Also count @Sealed annotated abstract types as sealed
odersky Oct 30, 2023
d3876ad
Add ArrayBuffer and GrowableBuilder to stdlib
odersky Oct 30, 2023
75c2058
Coarse restriction to disallow local roots in external types
odersky Oct 30, 2023
bb0b774
Require array element types to be sealed
odersky Oct 31, 2023
6b5e494
Add ArrayBuilder.scala in unchanged form to stdlib
odersky Oct 31, 2023
609549c
Make ArrayBuilder capture checked
odersky Oct 31, 2023
d76f092
Add ArrayDeque.scala in unchanged for to stdlib
odersky Oct 31, 2023
e33fc7d
Make ArrayDeque capture checked
odersky Oct 31, 2023
37b41b6
Add Stepper.scala and ArraySeq.scala in unchanged form to stdlib
odersky Oct 31, 2023
ec70a23
Make Stepper capture checked
odersky Oct 31, 2023
24b8a4c
Don't generate capture set variables for self types of pure classes
odersky Oct 31, 2023
6ba065e
Make ArraySeq capture checked
odersky Oct 31, 2023
bb65046
Add mutable/IndexedSeq.scala to stdlib
odersky Oct 31, 2023
c811e35
Add Queue.scala to stdlib
odersky Oct 31, 2023
f0a1241
Add PriorityQueue.scala to stdlib
odersky Oct 31, 2023
4ac2139
Make PriorityQueue capture checked
odersky Oct 31, 2023
4795ef8
Add Stack.scala and ReusableBuilder.scala to stdlib
odersky Oct 31, 2023
0bf1903
Add Set.scala to stdlib
odersky Oct 31, 2023
35e7166
Make collection/Set capture checked
odersky Oct 31, 2023
14b5093
Add mutable/immutable Set to stdlib
odersky Oct 31, 2023
1f414a3
Make mutable/immutable Set capture checked
odersky Oct 31, 2023
f92c881
Add BitSet classes to stdlib (capture checked)
odersky Oct 31, 2023
6a831aa
Add SortedSet classes to stdlib
odersky Oct 31, 2023
3b6b684
Add Factory.scala to stdlib
odersky Oct 31, 2023
5852a76
Make Factory capture checked
odersky Oct 31, 2023
c9cb044
Avoid reporting post check messages several times
odersky Oct 31, 2023
0a350ea
Add SortedSet and BitSet to stdlib (capture checked)
odersky Oct 31, 2023
06744c2
Add BuildFrom.scala to stdlib
odersky Oct 31, 2023
51c9385
Make BuildFrom capture checked
odersky Oct 31, 2023
238317c
Add WithFilter.scala to stdlib
odersky Nov 1, 2023
1b757b9
Make WithFilter capture checked
odersky Nov 1, 2023
4956e6d
Add ArrayOps to stdlib
odersky Nov 1, 2023
7273c4d
Don't flag wildcard array arguments for not being sealed
odersky Nov 1, 2023
b67422e
Fix isPureClass test
odersky Nov 1, 2023
c64c057
Make sealed an annotation
odersky Nov 1, 2023
2083af3
Make ArrayOps capture checked
odersky Nov 1, 2023
9909d95
Add LazyZipOps and some other files to stdlib
odersky Nov 1, 2023
caeac20
Add mutable/TreeSet.scala to stdlib
odersky Nov 1, 2023
1f4f875
Add mutable and concurrent Map to stdlib
odersky Nov 1, 2023
3dc0eae
Add HashMaps to stdlib
odersky Nov 1, 2023
20f3a97
Add LongMap + friends to stdlib
odersky Nov 1, 2023
ae605d6
Add WeakHashMap and MultiMap to stdlib
odersky Nov 1, 2023
b0d3bbc
Add UnrolledBuffer to stdlib
odersky Nov 1, 2023
09e0868
Add mutable TreeMap and RedBlackTree to stdlib
odersky Nov 1, 2023
0888c40
Add mutable SortedMap and SeqMap to stdlib
odersky Nov 1, 2023
a76129f
Add AnyRefMap to stdlib
odersky Nov 1, 2023
73f9474
Add CollisionProofHashMap to stdlib
odersky Nov 1, 2023
ffbaf1f
Avoid infinite recursions when checking F-bounded types
odersky Nov 1, 2023
7304ba6
Add HashTable.scala to stdlib
odersky Nov 1, 2023
f3ed83b
Add HashSet to stdlib
odersky Nov 1, 2023
7d2f133
Add OpenHashMap to stdlib
odersky Nov 1, 2023
9021cfd
Add MapView to stdlib
odersky Nov 1, 2023
cb66516
Add StrictOptimizedMapOps to stdlib
odersky Nov 1, 2023
be20a7c
Add collection/SortedMap to stdlib
odersky Nov 1, 2023
766b020
Add more Ops classes to stdlib
odersky Nov 1, 2023
36975c1
Add remaining collection classes to stdlib
odersky Nov 1, 2023
b899f8d
Add immutable ArraySeq to stdlib
odersky Nov 2, 2023
1f3fe9e
Add immutable HashSet and HashMap to stdlib
odersky Nov 2, 2023
528c249
Add immutable IntMap and LongMap to stdlib
odersky Nov 2, 2023
02c6281
Add immutable ListSet and ListMap to stdlib
odersky Nov 2, 2023
2cb34e5
Add immutable Map to stdlib
odersky Nov 2, 2023
a1fc706
Add Range classes to stdlib
odersky Nov 2, 2023
5817d40
Add immutable Queue to stdlib
odersky Nov 2, 2023
c16afe5
Add immutable RedBlackTree to stdlib
odersky Nov 2, 2023
d526e10
Add immutable SeqMap to stdlib
odersky Nov 2, 2023
26dde72
Add immutable SortedMap to stdlib
odersky Nov 2, 2023
a08460d
Add immutable StrictOptimizedSeqOps to stdlib
odersky Nov 2, 2023
c66c833
Add Vector and VectorMap to stdlib
odersky Nov 2, 2023
eea6bca
Add immutable TreeSet and TreeMap to stdlib
odersky Nov 2, 2023
4625afc
Add immutable TreeSeqMap to stdlib
odersky Nov 2, 2023
9ba2c39
Add unchecked LazyList to stdlib
odersky Nov 2, 2023
bf58e2b
Survive "cannot establish a reference" errors in TreeTypeMap
odersky Nov 2, 2023
6ae16a4
Add checked LazyList to stdlib
odersky Nov 2, 2023
0acfb8c
Rename LazyList to LazyListIterable
odersky Nov 2, 2023
fd88dc1
Allow deep subtypes when compiling stdlib test
odersky Nov 3, 2023
9ae3ee7
Add generic/IsSeq to stdlib
odersky Nov 3, 2023
f8f2050
Make SubstRecThis typemap idempotent
odersky Nov 3, 2023
b19f981
Capturecheck all files in generic
odersky Nov 3, 2023
868d65b
Drop an unsafeAssumePure in Iterator
odersky Nov 3, 2023
f4066c0
Better error message for capture errors involving self types
odersky Nov 3, 2023
e22744d
Add sealed refs test and fix check files of other tests
odersky Nov 4, 2023
cfec1d0
Simplify CapturedVars phase
odersky Nov 4, 2023
5e49b12
Refine isParametric tests
odersky Nov 4, 2023
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add Stack.scala and ReusableBuilder.scala to stdlib
  • Loading branch information
odersky committed Nov 4, 2023
commit 4795ef84780e4b331ce6fbde87aba383ec70de48
56 changes: 56 additions & 0 deletions tests/pos-special/stdlib/collection/mutable/ReusableBuilder.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Scala (https://www.scala-lang.org)
*
* Copyright EPFL and Lightbend, Inc.
*
* Licensed under Apache License 2.0
* (http://www.apache.org/licenses/LICENSE-2.0).
*
* See the NOTICE file distributed with this work for
* additional information regarding copyright ownership.
*/

package scala
package collection
package mutable

import language.experimental.captureChecking

/** `ReusableBuilder` is a marker trait that indicates that a `Builder`
* can be reused to build more than one instance of a collection. In
* particular, calling `result()` followed by `clear()` will produce a
* collection and reset the builder to begin building a new collection
* of the same type.
*
* In general no method other than `clear()` may be called after `result()`.
* It is up to subclasses to implement and to document other allowed sequences
* of operations (e.g. calling other methods after `result()` in order to obtain
* different snapshots of a collection under construction).
*
* @tparam Elem the type of elements that get added to the builder.
* @tparam To the type of collection that it produced.
*
* @define multipleResults
*
* This Builder can be reused after calling `result()` without an
* intermediate call to `clear()` in order to build multiple related results.
*/
trait ReusableBuilder[-Elem, +To] extends Builder[Elem, To] {
/** Clears the contents of this builder.
* After execution of this method, the builder will contain no elements.
*
* If executed immediately after a call to `result()`, this allows a new
* instance of the same type of collection to be built.
*/
override def clear(): Unit // Note: overriding for Scaladoc only!

/** Produces a collection from the added elements.
*
* After a call to `result`, the behavior of all other methods is undefined
* save for `clear()`. If `clear()` is called, then the builder is reset and
* may be used to build another instance.
*
* @return a collection containing the elements added to this builder.
*/
override def result(): To // Note: overriding for Scaladoc only!
}
144 changes: 144 additions & 0 deletions tests/pos-special/stdlib/collection/mutable/Stack.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
/*
* Scala (https://www.scala-lang.org)
*
* Copyright EPFL and Lightbend, Inc.
*
* Licensed under Apache License 2.0
* (http://www.apache.org/licenses/LICENSE-2.0).
*
* See the NOTICE file distributed with this work for
* additional information regarding copyright ownership.
*/

package scala.collection.mutable

import scala.annotation.{migration, nowarn}
import scala.collection.generic.DefaultSerializable
import scala.collection.{IterableFactoryDefaults, IterableOnce, SeqFactory, StrictOptimizedSeqFactory, StrictOptimizedSeqOps}

import language.experimental.captureChecking

/** A stack implements a data structure which allows to store and retrieve
* objects in a last-in-first-out (LIFO) fashion.
*
* Note that operations which consume and produce iterables preserve order,
* rather than reversing it (as would be expected from building a new stack
* by pushing an element at a time).
*
* @tparam A type of the elements contained in this stack.
*
* @define Coll `Stack`
* @define coll stack
* @define orderDependent
* @define orderDependentFold
* @define mayNotTerminateInf
* @define willNotTerminateInf
*/
@migration("Stack is now based on an ArrayDeque instead of a linked list", "2.13.0")
class Stack[sealed A] protected (array: Array[AnyRef], start: Int, end: Int)
extends ArrayDeque[A](array, start, end)
with IndexedSeqOps[A, Stack, Stack[A]]
with StrictOptimizedSeqOps[A, Stack, Stack[A]]
with IterableFactoryDefaults[A, Stack]
with ArrayDequeOps[A, Stack, Stack[A]]
with Cloneable[Stack[A]]
with DefaultSerializable {

def this(initialSize: Int = ArrayDeque.DefaultInitialSize) =
this(ArrayDeque.alloc(initialSize), start = 0, end = 0)

override def iterableFactory: SeqFactory[Stack] = Stack

@nowarn("""cat=deprecation&origin=scala\.collection\.Iterable\.stringPrefix""")
override protected[this] def stringPrefix = "Stack"

/**
* Add elements to the top of this stack
*
* @param elem
* @return
*/
def push(elem: A): this.type = prepend(elem)

/** Push two or more elements onto the stack. The last element
* of the sequence will be on top of the new stack.
*
* @param elems the element sequence.
* @return the stack with the new elements on top.
*/
def push(elem1: A, elem2: A, elems: A*): this.type = {
val k = elems.knownSize
ensureSize(length + (if(k >= 0) k + 2 else 3))
prepend(elem1).prepend(elem2).pushAll(elems)
}

/** Push all elements in the given iterable object onto the stack. The
* last element in the iterable object will be on top of the new stack.
*
* @param elems the iterable object.
* @return the stack with the new elements on top.
*/
def pushAll(elems: scala.collection.IterableOnce[A]): this.type =
prependAll(elems match {
case it: scala.collection.Seq[A] => it.view.reverse
case it => IndexedSeq.from(it).view.reverse
})

/**
* Removes the top element from this stack and return it
*
* @return
* @throws NoSuchElementException when stack is empty
*/
def pop(): A = removeHead()

/**
* Pop all elements from this stack and return it
*
* @return The removed elements
*/
def popAll(): scala.collection.Seq[A] = removeAll()

/**
* Returns and removes all elements from the top of this stack which satisfy the given predicate
*
* @param f the predicate used for choosing elements
* @return The removed elements
*/
def popWhile(f: A => Boolean): scala.collection.Seq[A] = removeHeadWhile(f)

/** Returns the top element of the stack. This method will not remove
* the element from the stack. An error is signaled if there is no
* element on the stack.
*
* @throws NoSuchElementException
* @return the top element
*/
@`inline` final def top: A = head

override protected def klone(): Stack[A] = {
val bf = newSpecificBuilder
bf ++= this
bf.result()
}

override protected def ofArray(array: Array[AnyRef], end: Int): Stack[A] =
new Stack(array, start = 0, end)

}

/**
* $factoryInfo
* @define coll stack
* @define Coll `Stack`
*/
@SerialVersionUID(3L)
object Stack extends StrictOptimizedSeqFactory[Stack] {

def from[sealed A](source: IterableOnce[A]): Stack[A] = empty ++= source

def empty[sealed A]: Stack[A] = new Stack

def newBuilder[sealed A]: Builder[A, Stack[A]] = new GrowableBuilder[A, Stack[A]](empty)

}