Skip to content

Commit 6004cab

Browse files
committed
Lock in significance of syntactic vs. semantic cycle thru test
1 parent 85f7871 commit 6004cab

2 files changed

Lines changed: 34 additions & 0 deletions

File tree

build/build_tests.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ pub fn generate(tests_file: &mut File) {
4646
("cyclic/", "globals_and_class_thru_difficult"),
4747

4848
("cyclic/", "err_cyclic"),
49+
("cyclic/", "err_granularity_fun_in_init"),
4950
("cyclic/", "err_cyclic_fun"),
5051
("cyclic/", "err_cyclic_multi_fun"),
5152
("cyclic/", "err_cyclic_fun_five"),
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// This is explicitly called out as not supported in the source code right now,
2+
// so let's be sure to solidify that semantic with a test case.
3+
//
4+
// Basically, the cyclic checks are & will remain, by and large, *syntactic*,
5+
// rather than truly deep analysis of the semantics. The initializer of a
6+
// *syntactically* refers to a_fun which refers to a, even though the initializer
7+
// of a does not ever *semantically* actually refer to itself.
8+
//
9+
// This is still considered an error, because otherwise you have to do things
10+
// like escape analysis, and the definition of valid code becomes very complicated
11+
// and prone to change.
12+
//
13+
// This simpler, but more restrictive semantics, is at least easy to understand,
14+
// which is good for the compiler and the programmer. In particular, if the main
15+
// relationship creating cycles is *syntactic reference* to a variable, then it
16+
// becomes very clear that to break the cycle simply means to remove one of the
17+
// syntactic references in the chain, rather than to carefully micromanage the
18+
// semantics of every function in the chain.
19+
//
20+
// And, of course, there's really no reason a variable initializer should depend
21+
// on a function that depends on that variable. If you really must, you can just
22+
// initialize the variable to a dummy value then call the "real" initializer in
23+
// init(). (Could be inconvenient for option types, but I really am willing to
24+
// make that sacrifice).
25+
26+
//? Cycle in variable initialization order
27+
28+
var a = {
29+
var b = a_fun;
30+
10;
31+
};
32+
33+
fun a_fun() { print(a); }

0 commit comments

Comments
 (0)