Skip to content

Commit 17a9919

Browse files
on-keydayclaude
andcommitted
docs: the global forward reference is intentional
I had left it open on the grounds that nothing comments on it. Absence of a comment is not evidence, and two things point the other way. recurse_defs.bgn defines x ::= y, y ::= z, z ::= y at global scope - a cycle. It only exists if line 1's reference reaches the y declared on line 8, so the file cannot be testing what its name says unless forward resolution works. typing.cpp:1344 types a definition on demand when resolution lands on one that has no expr_type yet, and recurse_detect guards the re-entry. That path is only reachable when a reference resolves to a definition that has not been typed - which is what a forward reference is. Backward references land on definitions already typed. Both were listed as unconfirmed; they are not. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
1 parent cbad57e commit 17a9919

1 file changed

Lines changed: 19 additions & 6 deletions

File tree

rebrgen/docs/draft/scope_rules.md

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -141,9 +141,23 @@ line 6 col 11 y usage=reference base=12 5 行目の local の y
141141
```
142142

143143
4 行目が global に落ちるのは、その時点で local の `y` がまだ宣言されていないため。
144-
ファイル自身が `# global y` と書いているので、**外へ抜けて global に落ちる部分は意図されている**
145-
1 行目のように **global の定数を前方参照できる**点についてはコメントが無く、
146-
fallback が型限定でないことの副作用として出ているのか、そう決めたのかは分からない。
144+
ファイル自身が `# global y` / `# local y` と書いている。
145+
146+
**この非対称は意図されている。** 根拠は 2 つ。
147+
148+
1. このファイルの global の定義は `x ::= y` / `y ::= z` / `z ::= y`**循環している**
149+
1 行目の前方参照が解決しなければ循環が構成されず、`recurse_defs` というテストが成立しない
150+
2. `typing.cpp:1344-1353` に、解決先の定義にまだ型が付いていない場合
151+
(`def && !def->expr_type`) その定義を先に型付けしに行く機構がある。
152+
`recurse_detect` はその再入を止めるためのもので、**前方参照が解決するからこそ要る**
153+
後方参照だけなら解決先は既に型付け済みである
154+
155+
```cpp
156+
recurse_detect.insert(ident.get());
157+
auto guard = futils::helper::defer([&] { recurse_detect.erase(ident.get()); });
158+
typing_expr(bin->right);
159+
typing_assign(bin);
160+
```
147161
148162
## 5. スコープを作る構文
149163
@@ -174,8 +188,7 @@ fallback が型限定でないことの副作用として出ているのか、
174188
175189
## 7. 未確認
176190
177-
- global の定数を前方参照できること (§4) が、そう決めたものか、
178-
fallback を型限定にしなかった副作用か
179-
- `recurse_detect` (`typing.cpp:856`) がどの循環を止めているか
180191
- 型パラメータ (`define_type_parameter`) が `is_type_ident` に入っているが、
181192
スコープのどの段に push されるか
193+
194+
§4 の非対称と `recurse_detect` は確認済みなのでここから外した。

0 commit comments

Comments
 (0)