Skip to content

Commit b90d924

Browse files
authored
fix(stdlib): Properly resize empty Stack on push (#1867)
1 parent f86fd7a commit b90d924

File tree

2 files changed

+9
-1
lines changed

2 files changed

+9
-1
lines changed

compiler/test/stdlib/stack.test.gr

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,12 @@ assert Stack.pop(stack) == Some(2)
5050
assert Stack.pop(stack) == Some(1)
5151
assert Stack.pop(stack) == None
5252

53+
let stack = Stack.makeSized(0)
54+
Stack.push(0, stack)
55+
let stack2 = Stack.makeSized(1)
56+
Stack.push(0, stack2)
57+
assert stack == stack2
58+
5359
module Immutable {
5460
from Stack use { Immutable as Stack }
5561

stdlib/stack.gr

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,9 @@ provide let peek = stack => {
8989
*/
9090
provide let push = (value, stack) => {
9191
let arrLen = Array.length(stack.array)
92-
if (stack.size == arrLen) {
92+
if (arrLen == 0) {
93+
stack.array = Array.make(1, None)
94+
} else if (stack.size == arrLen) {
9395
let newArray = Array.make(stack.size * 2, None)
9496
for (let mut i = 0; i < arrLen; i += 1) {
9597
newArray[i] = stack.array[i]

0 commit comments

Comments
 (0)