Skip to content

Commit fe51fbe

Browse files
on-keydayclaude
andcommitted
docs: resolve the last open item on scope rules
Where a type parameter lands was listed as unconfirmed when reading parse.cpp answers it. Foo[T]'s T is built with parse_ident_no_scope and enters no scope at that point; it is collected and handed to parse_indent_block, which pushes it into the body scope as the first entry before any of the body is parsed. So T is visible throughout the body without tripping the backward-only rule, is invisible outside the format, and passes the only_type_allowed filter on the way out of a nested scope because define_type_parameter is in is_type_ident. Nothing is left unconfirmed. All three items the investigation opened with were readable from the implementation and are now in the body. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
1 parent 17a9919 commit fe51fbe

1 file changed

Lines changed: 29 additions & 4 deletions

File tree

rebrgen/docs/draft/scope_rules.md

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,33 @@ typing_assign(bin);
173173
`if` / `match` / `for` は**条件部にもスコープを持つ** (`if_->cond_scope` など)。
174174
`for x in ...` の `x` がループ全体に見えるのはこのため。本体のブロックはさらに内側になる。
175175
176+
### 型パラメータはどこに入るか
177+
178+
`format Foo[T]:` の `T` は `parse_ident_no_scope` で作られ (parse.cpp:1534)、
179+
その場ではどのスコープにも入らない。`type_param_idents` に集められて
180+
`parse_indent_block` へ渡り、**本体のスコープに、本体を解析する前に、最初の要素として**
181+
push される (parse.cpp:210-216)。
182+
183+
```cpp
184+
auto c = state.new_indent(s, current_indent, block->scope, scope_owner); // 本体スコープ
185+
if (ident) {
186+
for (auto& i : *ident) {
187+
i->scope = block->scope;
188+
block->scope->push(i);
189+
check_duplicated_def(i.get());
190+
}
191+
}
192+
```
193+
194+
したがって:
195+
196+
- `T` は本体の全体で見える。宣言順の先頭にあるので、後方参照の制約に引っかからない
197+
- `T` は format の外からは見えない。本体スコープの中に居るため
198+
- 本体で `T` という名前を再宣言すると `check_duplicated_def` が拾う。
199+
仮に通っても `objects` は逆順に見られるので後の宣言が勝つ
200+
- `is_type_ident``define_type_parameter` が入っているので、入れ子の format から
201+
外へ上がるときの `only_type_allowed` を通り抜ける
202+
176203
## 6. 気づいた点
177204

178205
- **`prev` が二役**。親スコープでもあり、同じ段の前の区間でもある。`branch_root`
@@ -188,7 +215,5 @@ typing_assign(bin);
188215

189216
## 7. 未確認
190217

191-
- 型パラメータ (`define_type_parameter`) が `is_type_ident` に入っているが、
192-
スコープのどの段に push されるか
193-
194-
§4 の非対称と `recurse_detect` は確認済みなのでここから外した。
218+
無し。調査開始時に挙げた 3 件 (§4 の非対称 / `recurse_detect` / 型パラメータの push 先) は
219+
いずれも実装を読めば分かるもので、すべて本文に取り込んだ。

0 commit comments

Comments
 (0)