Skip to content

Comments

feat(interpreter): implement declare -n nameref variables#255

Merged
chaliy merged 1 commit intomainfrom
claude/bashkit-bash-compatibility-BsDKD
Feb 24, 2026
Merged

feat(interpreter): implement declare -n nameref variables#255
chaliy merged 1 commit intomainfrom
claude/bashkit-bash-compatibility-BsDKD

Conversation

@chaliy
Copy link
Contributor

@chaliy chaliy commented Feb 24, 2026

Summary

  • Implement declare -n nameref variables that act as aliases for other variable names
  • Add resolve_nameref() with chain following (max 10 levels) to prevent infinite loops
  • Integrate nameref resolution into variable expansion and assignment paths

Changes

  • crates/bashkit/src/interpreter/mod.rs: Parse -n flag, store _NAMEREF_<name> markers, add resolve_nameref(), integrate into expand_variable() and set_variable()
  • crates/bashkit/tests/spec_cases/bash/declare.test.sh: 6 new spec tests (basic, assign-through, chain, function param, read-unset, reassign-target)
  • specs/009-implementation-status.md: Update test counts (declare 10→16, Bash 887→893, Total 1305→1311)

Test plan

  • cargo test --all-features passes
  • cargo fmt --check clean
  • cargo clippy --all-targets --all-features -- -D warnings clean
  • All 6 nameref spec tests pass
  • Existing declare tests still pass (no regressions)

Add support for name reference variables (namerefs) via `declare -n`.
Namerefs allow a variable to act as an alias for another variable name,
enabling pass-by-reference semantics in functions.

Implementation:
- Parse `-n` flag in `execute_declare_builtin`
- Store nameref targets as `_NAMEREF_<name>` marker variables
- Add `resolve_nameref()` method with chain following (max 10 levels)
- Integrate nameref resolution into `expand_variable()` and `set_variable()`

Features:
- Basic nameref: `declare -n ref=x; echo $ref` reads through ref
- Assignment through nameref: `ref=value` sets the target variable
- Nameref chains: `declare -n b=a; declare -n c=b; echo $c`
- Function parameter passing: `declare -n ref=$1` in functions
- Reading through nameref to unset variable returns empty
- Target variable changes reflect through nameref

Tests: 6 new spec tests (declare.test.sh: 10 → 16)
@chaliy chaliy merged commit 4d514d2 into main Feb 24, 2026
16 checks passed
@chaliy chaliy deleted the claude/bashkit-bash-compatibility-BsDKD branch February 24, 2026 22:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants