Skip to content

Commit d3ce210

Browse files
CAIMEOXYoorkin
andauthored
add alert pragma to unsafe function (#186)
Co-authored-by: Yorkin <cyb_yorkin@outlook.com>
1 parent 0ebe01e commit d3ce210

File tree

8 files changed

+30
-22
lines changed

8 files changed

+30
-22
lines changed

deque/deque.mbt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -191,12 +191,12 @@ pub fn push_back[T](self : Deque[T], value : T) -> Unit {
191191
192192
/// Removes a fornt element from a deque.
193193
///
194-
/// Panics if the deque is empty.
195194
/// # Example
196195
/// ```
197196
/// let dq = Deque::[1, 2, 3, 4, 5]
198197
/// dq.pop_front_exn()
199198
/// ```
199+
/// @alert unsafe "Panic if the deque is empty."
200200
pub fn pop_front_exn[T](self : Deque[T]) -> Unit {
201201
if self.len == 0 {
202202
abort("The deque is empty!")
@@ -207,12 +207,12 @@ pub fn pop_front_exn[T](self : Deque[T]) -> Unit {
207207
208208
/// Removes a back element from a deque.
209209
///
210-
/// Panics if the deque is empty.
211210
/// # Example
212211
/// ```
213212
/// let dq = Deque::[1, 2, 3, 4, 5]
214213
/// dq.pop_back_exn()
215214
/// ```
215+
/// @alert unsafe "Panic if the deque is empty."
216216
pub fn pop_back_exn[T](self : Deque[T]) -> Unit {
217217
if self.len == 0 {
218218
abort("The deque is empty!")
@@ -299,6 +299,7 @@ test "push_realloc" {
299299
/// let dq = Deque::[1, 2, 3, 4, 5]
300300
/// println(dq[2]) // 3
301301
/// ```
302+
/// @alert unsafe "Panic if the index is out of bounds."
302303
pub fn op_get[T](self : Deque[T], index : Int) -> T {
303304
if index < 0 || index >= self.len {
304305
let len = self.len
@@ -337,6 +338,7 @@ test "op_get" {
337338
/// dq[2] = 1
338339
/// println(dq[2]) // 1
339340
/// ```
341+
/// @alert unsafe "Panic if the index is out of bounds."
340342
pub fn op_set[T](self : Deque[T], index : Int, value : T) -> Unit {
341343
if index < 0 || index >= self.len {
342344
let len = self.len

hashmap/hashmap.mbt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ pub fn HashMap::from_array[K : Hash + Eq, V](
4040
}
4141

4242
/// Set a key-value pair into hash map.
43+
/// @alert unsafe "Panic if the hash map is full."
4344
pub fn set[K : Hash + Eq, V](self : HashMap[K, V], key : K, value : V) -> Unit {
4445
if self.capacity == 0 || self.size >= self.growAt {
4546
self.grow()

immutable_set/immutable_set.mbt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ pub fn to_vec[T : Compare](self : ImmutableSet[T]) -> @vec.Vec[T] {
9191
/// println(ImmutableSet::[3, 4, 5].remove_min())
9292
/// // output: ImmutableSet::[4, 5]
9393
/// ```
94+
/// @alert unsafe "Panic if the ImmutableSet is empty."
9495
pub fn remove_min[T : Compare](self : ImmutableSet[T]) -> ImmutableSet[T] {
9596
match self {
9697
Empty => abort("remove_min: empty ImmutableSet")
@@ -181,6 +182,7 @@ pub fn remove[T : Compare](
181182
/// println(ImmutableSet::[7, 2, 9, 4, 5, 6, 3, 8, 1].min())
182183
/// // output: 1
183184
/// ```
185+
/// @alert unsafe "Panic if the ImmutableSet is empty."
184186
pub fn min[T : Compare](self : ImmutableSet[T]) -> T {
185187
match self {
186188
Empty => abort("min: there are no values in ImmutableSet.")
@@ -213,6 +215,7 @@ pub fn min_option[T : Compare](self : ImmutableSet[T]) -> Option[T] {
213215
/// println(ImmutableSet::[7, 2, 9, 4, 5, 6, 3, 8, 1].max())
214216
/// // output: 9
215217
/// ```
218+
/// @alert unsafe "Panic if the ImmutableSet is empty."
216219
pub fn max[T : Compare](self : ImmutableSet[T]) -> T {
217220
match self {
218221
Empty => abort("max: there are no values in ImmutableSet.")

list/list.mbt

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,7 @@ test "tail" {
247247
/// debug(from_array([1, 2, 3, 4, 5]).head())
248248
/// // output: 1
249249
/// ```
250+
/// @alert unsafe "Panic if the list is empty"
250251
pub fn head[T](self : List[T]) -> T {
251252
match self {
252253
Nil => abort("head of empty list")
@@ -291,6 +292,7 @@ test "head option" {
291292
/// debug(from_array([1, 2, 3, 4, 5]).last())
292293
/// // output: 5
293294
/// ```
295+
/// @alert unsafe "Panic if the list is empty"
294296
pub fn last[T](self : List[T]) -> T {
295297
match self {
296298
Nil => abort("last of empty list")
@@ -397,10 +399,7 @@ test "fold" {
397399
/// debug(r) // output: from_array([(1, 6), (2, 7), (3, 8), (4, 9), (5, 10)]
398400
/// ```
399401
///
400-
/// # Panics
401-
///
402-
/// If the two lists have different lengths, the function will panic.
403-
///
402+
/// @alert unsafe "Panic if the two lists have different lengths."
404403
pub fn zip[T, U](self : List[T], other : List[U]) -> List[(T, U)] {
405404
match (self, other) {
406405
(Nil, Nil) => Nil
@@ -434,6 +433,7 @@ test "concat_map" {
434433
}
435434
436435
/// Get nth element of the list
436+
/// @alert unsafe "Panic if the index is out of bounds"
437437
pub fn nth[T](self : List[T], n : Int) -> T {
438438
match self {
439439
Nil => abort("nth: index out of bounds")
@@ -582,6 +582,7 @@ test "flatten" {
582582
}
583583
584584
/// Get maximum element of the list.
585+
/// @alert unsafe "Panic if the list is empty"
585586
pub fn maximum[T : Compare](self : List[T]) -> T {
586587
match self {
587588
Nil => abort("maximum: empty list")
@@ -603,6 +604,7 @@ test "maximum" {
603604
}
604605
605606
/// Get minimum element of the list.
607+
/// @alert unsafe "Panic if the list is empty"
606608
pub fn minimum[T : Compare](self : List[T]) -> T {
607609
match self {
608610
Nil => abort("minimum: empty list")
@@ -847,6 +849,7 @@ test "lookup" {
847849
/// println(List::[1, 3, 5, 8].find(fn(element) -> Bool { element % 2 == 0}))
848850
/// // output: 8
849851
/// ```
852+
/// @alert unsafe "Panic if no element satisfies f."
850853
pub fn find[T](self : List[T], f : (T) -> Bool) -> T {
851854
match self {
852855
Nil => abort("find: no matching element")
@@ -900,6 +903,7 @@ test "find_option" {
900903
/// println(List::[1, 3, 5, 8].findi(fn(element, i) -> Bool { (element % 2 == 0) && (i == 3) }))
901904
/// // output: 8
902905
/// ```
906+
/// @alert unsafe "Panic if no element satisfies f."
903907
pub fn findi[T](self : List[T], f : (T, Int) -> Bool) -> T {
904908
fn find(list : List[T], index : Int) -> T {
905909
match list {

priority_queue/priority_queue.mbt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,12 +102,12 @@ test "length" {
102102
103103
/// Pops the first value from the priority queue.
104104
///
105-
/// Panics if the queue is empty.
106105
/// # Example
107106
/// ```
108107
/// let queue = PriorityQueue::[1, 2, 3, 4]
109108
/// let first = queue.pop_exn() // 4
110109
/// ```
110+
/// @alert unsafe "Panic if the queue is empty."
111111
pub fn pop_exn[T : Compare](self : PriorityQueue[T]) -> Unit {
112112
if self.len == 0 {
113113
abort("The PriorityQueue is empty!")

queue/queue.mbt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,12 +163,12 @@ test "push" {
163163
164164
/// Peeks at the first value in the queue.
165165
///
166-
/// Panics if the queue is empty.
167166
/// # Example
168167
/// ```
169168
/// let queue : Queue[Int] = Queue::[1, 2, 3, 4]
170169
/// let first = queue.peek_exn()
171170
/// ```
171+
/// @alert unsafe "Panics if the queue is empty."
172172
pub fn peek_exn[T](self : Queue[T]) -> T {
173173
match self.first {
174174
Nil => abort("Queue is empty")
@@ -208,12 +208,12 @@ test "peek" {
208208
209209
/// Pops the first value from the queue.
210210
///
211-
/// Panics if the queue is empty.
212211
/// # Example
213212
/// ```
214213
/// let queue : Queue[Int] = Queue::[1, 2, 3, 4]
215214
/// let first = queue.pop_exn()
216215
/// ```
216+
/// @alert unsafe "Panics if the queue is empty."
217217
pub fn pop_exn[T](self : Queue[T]) -> T {
218218
match self.first {
219219
Nil => abort("Queue is empty")

stack/stack.mbt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,7 @@ test "pop" {
277277
/// println(s) // output: { elements: Cons(2, Cons(1, Nil)), len: 2 }
278278
/// println(s1.pop_exn()) // abort.
279279
/// ```
280+
/// @alert unsafe "Panic if the stack is empty."
280281
pub fn pop_exn[T](self : Stack[T]) -> T {
281282
match self.elements {
282283
Cons(hd, tl) => {
@@ -388,6 +389,7 @@ test "peek" {
388389
/// println(s.peek_exn()) // output: 3
389390
/// println(s) // output: { elements: Cons(3, Cons(2, Cons(1, Nil))), len: 3 }
390391
/// ```
392+
/// @alert unsafe "Panic if the stack is empty."
391393
pub fn peek_exn[T](self : Stack[T]) -> T {
392394
match self.elements {
393395
Cons(hd, _) => hd

vec/vec.mbt

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -103,14 +103,13 @@ test "realloc" {
103103

104104
/// Retrieves the element at the specified index from the vector.
105105
///
106-
/// If you try to access an index which isn’t in the Vec, it will panic.
107-
///
108106
/// # Example
109107
/// ```
110108
/// let v = Vec::new()
111109
/// v.push(3)
112110
/// println(v[0]) // 3
113111
/// ```
112+
/// @alert unsafe "Panic if index is out of bounds"
114113
pub fn op_get[T](self : Vec[T], index : Int) -> T {
115114
if index < 0 || index >= self.len {
116115
let len = self.len
@@ -123,14 +122,13 @@ pub fn op_get[T](self : Vec[T], index : Int) -> T {
123122
124123
/// Sets the value of the element at the specified index.
125124
///
126-
/// If you try to access an index which isn’t in the Vec, it will panic.
127-
///
128125
/// # Example
129126
/// ```
130127
/// let v = Vec::new()
131128
/// v.push(3)
132129
/// println(v[0]) // 3
133130
/// ```
131+
/// @alert unsafe "Panic if index is out of bounds."
134132
pub fn op_set[T](self : Vec[T], index : Int, value : T) -> Unit {
135133
if index < 0 || index >= self.len {
136134
let len = self.len
@@ -237,13 +235,12 @@ test "pop" {
237235
238236
/// Removes the last element from a vector and returns it.
239237
///
240-
/// Panics if the vector is empty.
241-
///
242238
/// # Example
243239
/// ```
244240
/// let v = Vec::[1, 2, 3]
245241
/// v.pop_exn() // 3
246242
/// ```
243+
/// @alert unsafe "Panic if the vector is empty."
247244
pub fn pop_exn[T](self : Vec[T]) -> T {
248245
if self.len == 0 {
249246
abort("pop from an empty Vec")
@@ -289,13 +286,13 @@ test "push" {
289286
/// Removes the specified range from the vector and returns it.
290287
///
291288
/// This functions returns a vector range from `begin` to `end` `[begin, end)`
292-
/// If you try to access an index which isn’t in the Vec, it will panic.
293289
///
294290
/// # Example
295291
/// ```
296292
/// let v = Vec::[3, 4, 5]
297293
/// let vv = v.drain(1, 2) // vv = Vec::[4], v = Vec::[3, 5]
298294
/// ```
295+
/// @alert unsafe "Panic if index is out of bounds."
299296
pub fn drain[T](self : Vec[T], begin : Int, end : Int) -> Vec[T] {
300297
if begin < 0 || begin >= self.len || end < 0 || end > self.len || begin > end {
301298
let len = self.len
@@ -564,14 +561,13 @@ test "reverse" {
564561
565562
/// Split the vector into two at the given index.
566563
///
567-
/// If you try to access an index which isn’t in the Vec, it will panic.
568-
///
569564
/// # Example
570565
/// ```
571566
/// let v = Vec::[3, 4, 5]
572567
/// let (v1, v2) = v.split_at(1)
573568
/// ```
574569
/// TODO: perf could be optimized
570+
/// @alert unsafe "Panic if index is out of bounds."
575571
pub fn split_at[T](self : Vec[T], index : Int) -> (Vec[T], Vec[T]) {
576572
if index < 0 || index >= self.len {
577573
let len = self.len
@@ -761,13 +757,12 @@ test "search" {
761757
762758
/// Swap two elements in the vector.
763759
///
764-
/// If you try to access an index which isn’t in the Vec, it will panic.
765-
///
766760
/// # Example
767761
/// ```
768762
/// let v = Vec::[3, 4, 5]
769763
/// v.swap(1, 2)
770764
/// ```
765+
/// @alert unsafe "Panic if index is out of bounds."
771766
pub fn swap[T](self : Vec[T], i : Int, j : Int) -> Unit {
772767
if i >= self.len || j >= self.len || i < 0 || j < 0 {
773768
let len = self.len
@@ -797,6 +792,7 @@ test "swap" {
797792
/// let v = Vec::[3, 4, 5]
798793
/// v.remove(1)
799794
/// ```
795+
/// @alert unsafe "Panic if index is out of bounds."
800796
pub fn remove[T](self : Vec[T], index : Int) -> T {
801797
if index < 0 || index >= self.len {
802798
let len = self.len
@@ -849,6 +845,7 @@ test "retain" {
849845
/// ```
850846
/// Vec::[3, 4, 5].resize(1)
851847
/// ```
848+
/// @alert unsafe "Panic if new length is negative."
852849
pub fn resize[T](self : Vec[T], new_len : Int, f : T) -> Unit {
853850
if new_len < 0 {
854851
abort("negative new length")
@@ -875,12 +872,11 @@ test "resize" {
875872
876873
/// Inserts an element at a given index within the vector.
877874
///
878-
/// If you try to access an index which isn’t in the Vec, it will panic.
879-
///
880875
/// # Example
881876
/// ```
882877
/// Vec::[3, 4, 5].insert(1, 6)
883878
/// ```
879+
/// @alert unsafe "Panic if index is out of bounds."
884880
pub fn insert[T](self : Vec[T], index : Int, value : T) -> Unit {
885881
if index < 0 || index > self.len {
886882
let len = self.len

0 commit comments

Comments
 (0)