Skip to content

Commit 342724a

Browse files
authored
fix(interpreter): initialize assoc/indexed arrays for local -A/-a (#886)
## Summary - `local -A m` followed by `m["a"]="1"` produced an indexed array instead of associative, because `execute_local_builtin` only inserted into `call_stack.locals` without creating the `assoc_arrays` entry - Initialize the appropriate array type when `local -A` or `-a` is used without a compound assignment value - Fix applies to both function-scope and global-scope paths - Add spec tests `assoc_local_declare_then_assign` and `assoc_local_keys_in_cmdsub` ## Test plan - [x] New spec tests pass - [x] All 1812 bash spec tests pass (100%) - [x] Full `cargo test --all-features` passes - [x] `cargo fmt --check` and `cargo clippy` clean Closes #875
1 parent a0588db commit 342724a

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

crates/bashkit/src/interpreter/mod.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4353,6 +4353,11 @@ impl Interpreter {
43534353
.unwrap()
43544354
.locals
43554355
.insert(arg.to_string(), String::new());
4356+
if flags.assoc {
4357+
self.assoc_arrays.entry(arg.to_string()).or_default();
4358+
} else if flags.array {
4359+
self.arrays.entry(arg.to_string()).or_default();
4360+
}
43564361
if flags.integer {
43574362
self.variables
43584363
.insert(format!("_INTEGER_{}", arg), "1".to_string());
@@ -4437,7 +4442,13 @@ impl Interpreter {
44374442
.insert(var_name.to_string(), value.to_string());
44384443
}
44394444
} else if !is_internal_variable(arg) {
4440-
self.insert_variable_checked(arg.to_string(), String::new());
4445+
if flags.assoc {
4446+
self.assoc_arrays.entry(arg.to_string()).or_default();
4447+
} else if flags.array {
4448+
self.arrays.entry(arg.to_string()).or_default();
4449+
} else {
4450+
self.insert_variable_checked(arg.to_string(), String::new());
4451+
}
44414452
}
44424453
}
44434454
}

crates/bashkit/tests/spec_cases/bash/assoc-arrays.test.sh

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,35 @@ b=2
173173
c=3
174174
### end
175175

176+
### assoc_local_declare_then_assign
177+
myfunc() {
178+
local -A m
179+
m["a"]="1"
180+
m["b"]="2"
181+
echo "count: ${#m[@]}"
182+
for k in "${!m[@]}"; do echo "key=$k"; done | sort
183+
}
184+
myfunc
185+
### expect
186+
count: 2
187+
key=a
188+
key=b
189+
### end
190+
191+
### assoc_local_keys_in_cmdsub
192+
myfunc() {
193+
local -A m
194+
m["a"]="1"
195+
m["b"]="2"
196+
result="$(printf '%s\n' "${!m[@]}" | sort)"
197+
echo "result: [$result]"
198+
}
199+
myfunc
200+
### expect
201+
result: [a
202+
b]
203+
### end
204+
176205
### assoc_iteration
177206
declare -A m
178207
m[a]="1"

0 commit comments

Comments
 (0)