feat(parser): Phase 285A1.4 & A1.5 - Weak field sugar + Parser hang fix

A1.4: Add sugar syntax `public weak parent` ≡ `public { weak parent }`
A1.5: Fix parser hang on unsupported `param: Type` syntax

Key changes:
- A1.4: Extend visibility parser to handle weak modifier (fields.rs)
- A1.5: Shared helper `parse_param_name_list()` with progress-zero detection
- A1.5: Fix 6 vulnerable parameter parsing loops (methods, constructors, functions)
- Tests: Sugar syntax (OK/NG), parser hang (timeout-based)
- Docs: lifecycle.md, EBNF.md, phase-285a1-boxification.md

Additional changes:
- weak() builtin implementation (handlers/weak.rs)
- Leak tracking improvements (leak_tracker.rs)
- Documentation updates (lifecycle, types, memory-finalization, etc.)

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
2025-12-24 07:44:50 +09:00
parent a47f850d02
commit ab76e39036
60 changed files with 2099 additions and 454 deletions

View File

@ -2,6 +2,8 @@
**最終更新: 2025年8月19日 - 統合仕様書**
注: 言語レベルの SSOT は `docs/reference/language/lifecycle.md`。本書は設計ートであり、SSOT と矛盾する記述があれば SSOT を優先する。
## 📋 概要
Nyashは「Everything is Box」哲学のもと、統一的なメモリ管理と予測可能なリソース解放を実現しています。
@ -72,20 +74,23 @@ box MyResource {
**重要**: `fini()`は「このオブジェクトをもう使わない」という宣言であり、物理的な即時破棄ではありません。
### 実行順序(確定仕様
### 実行順序(設計SSOTへの案内
最終的な順序・禁止事項の SSOT は `docs/reference/language/lifecycle.md` に集約する。
本セクションの箇条書きは “目標像/設計メモ” として読む。
#### 自動カスケード解放
```nyash
box Pipeline {
init { r1, r2, r3, weak monitor }
init { r1, r2, r3, monitor_weak }
fini() {
// 1) ユーザー定義処理(柔軟な順序制御可能)
me.r3.fini() // 依存関係でr3→r2の順
me.r2.fini()
// 2) 自動カスケード: 残りのr1がinit宣言順で自動解放
// 3) weakフィールドは対象外lazy nil化
// 2) 自動カスケード: 残りのr1が自動解放weak参照は対象外
// 3) weak参照は weak_to_strong() で観測し、失効時は null=void/noneを返す
}
}
```
@ -94,7 +99,7 @@ box Pipeline {
1. **finalized チェック** - 既に解放済みなら何もしないidempotent
2. **再入防止** - `in_finalization`フラグで再帰呼び出し防止
3. **ユーザー定義fini()実行** - カスタムクリーンアップ処理
4. **自動カスケード** - `init`宣言順で未処理フィールドを解放
4. **自動カスケード** - strong-owned フィールドを決定的順序で解放weakはスキップ
5. **フィールドクリア** - 全フィールドを無効化
6. **finalized設定** - 以後の使用を禁止
@ -102,25 +107,26 @@ box Pipeline {
```nyash
box Node {
init { id, weak next } // 'next'は弱参照
init { id, next_weak } // 弱参照は値として保持する(`weak(x)`
}
local node1 = new Node("A", null)
local node2 = new Node("B", node1) // node2はnode1への弱参照を持つ
node1.next = node2 // node1はnode2への強参照を持つ
local node2 = new Node("B", null)
node2.next_weak = weak(node1)
node1.next_weak = weak(node2)
// 循環参照を回避し、安全に解放される
```
#### weak参照の特性
- **所有権なし**: オブジェクトの生存期間に影響しない
- **自動nil化**: 参照先が解放されると自動的に`null`になる
- **観測はweak_to_strong**: 参照先が Dead/Freed の場合、`weak_to_strong()``null` を返す
- **fini()対象外**: 弱参照フィールドはfini()カスケードでスキップ
### 不変条件(重要)
- **weak参照**: `weak`フィールドに対して`fini()`を直接呼ぶことはできません
- **finalized後禁止**: `fini()`呼び出し後は、そのオブジェクトの使用はすべて禁止
- **カスケード順序**: `init`宣言順の**逆順**で実行、`weak`フィールドはスキップ
- **カスケード順序**: strong-owned フィールドに対して決定的に実行`weak`フィールドはスキップ順序のSSOTは `docs/reference/language/lifecycle.md`)。
## 🌟 実用例
@ -130,7 +136,7 @@ box FileHandler {
init { file, buffer }
fini() {
// オブジェクト削除時に自動呼び出し
// 終了時に資源を解放(必要なら明示的に呼ぶ)
if me.file != null {
me.file.close()
console.log("File closed automatically")
@ -177,4 +183,4 @@ box PluginResource {
**関連ドキュメント**:
- [Box型リファレンス](box-reference.md)
- [プラグインシステム](../plugin-system/)
- [BID-FFI仕様](../plugin-system/ffi-abi-specification.md)
- [BID-FFI仕様](../plugin-system/ffi-abi-specification.md)