Skip to content

Commit d11f490

Browse files
authored
Merge pull request #2740 from usethesource/fix/global-init
execute global initialization after all the other declarations have finished
2 parents 30cf1b2 + b27fe91 commit d11f490

2 files changed

Lines changed: 20 additions & 1 deletion

File tree

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
module lang::rascal::tests::basic::FunctionCachesAndGlobals
2+
3+
default int f(int _) = 42;
4+
int x = f(0);
5+
int f(0) = 0;
6+
7+
test bool globalInitLast() = x == 0;
8+
9+
// test bool noCacheDuringInit() = f(0) == 0;

src/org/rascalmpl/semantics/dynamic/Module.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,18 @@ public Result<IValue> interpret(IEvaluator<Result<IValue>> eval) {
5858
List<Toplevel> decls = this.getBody().getToplevels();
5959
eval.__getTypeDeclarator().evaluateDeclarations(decls, eval.getCurrentEnvt(), false);
6060

61+
// first everything that is not a global
6162
for (Toplevel l : decls) {
62-
l.interpret(eval);
63+
if (!l.getDeclaration().isVariable()) {
64+
l.interpret(eval);
65+
}
66+
}
67+
68+
// then the globals which may depend on the previous
69+
for (Toplevel l : decls) {
70+
if (l.getDeclaration().isVariable()) {
71+
l.interpret(eval);
72+
}
6373
}
6474
}
6575
finally {

0 commit comments

Comments
 (0)