From f924031fb559b892fe58f82ca735a376e6093646 Mon Sep 17 00:00:00 2001 From: Toine Hartman Date: Wed, 25 Nov 2026 14:03:39 +0100 Subject: [PATCH 01/17] Add various tests for pattern match resolution/shadowing. --- .../rascalcore/check/tests/PatternTCTests.rsc | 12 +++++ .../rascalcore/check/tests/UseDefTests.rsc | 49 ++++++++++++++++++- 2 files changed, 60 insertions(+), 1 deletion(-) diff --git a/src/org/rascalmpl/compiler/lang/rascalcore/check/tests/PatternTCTests.rsc b/src/org/rascalmpl/compiler/lang/rascalcore/check/tests/PatternTCTests.rsc index c1050669fcd..077455808b2 100644 --- a/src/org/rascalmpl/compiler/lang/rascalcore/check/tests/PatternTCTests.rsc +++ b/src/org/rascalmpl/compiler/lang/rascalcore/check/tests/PatternTCTests.rsc @@ -322,6 +322,18 @@ test bool If4() = unexpectedDeclaration("{ if(int x := 1 && int x := 2 && x == 1 test bool If5() = checkOK("{ if(int x := 1 && int y := 2 && x == 1 ) x + y; };"); test bool If6() = unexpectedType("{ if(int x := 1 && int y := 2 && x == 1 ) x + y; else y;};"); +test bool If7() = undeclaredVariable("{ if (int i := i) { ; } }"); +test bool If8() = checkOK("{ if (int i := 0, int j := i) { x = i + j; } }"); +test bool If9() = checkOK("{ + int i = 0; + if (int j := i) { x = i + j; } +}"); +test bool If10() = checkOK("{ + void (int i) { + if (int j := i) { x = i + j; } + } +}"); + test bool IfU1() = checkOK("{ if(x := 1) x + 1; };"); test bool IfU2() = unexpectedType("{ if(x := 1) x + 1; else x + 2;};"); test bool IfU3() = checkOK("{ if(x := 1 && x == 1 ) x + 1; };"); diff --git a/src/org/rascalmpl/compiler/lang/rascalcore/check/tests/UseDefTests.rsc b/src/org/rascalmpl/compiler/lang/rascalcore/check/tests/UseDefTests.rsc index 8a812d28544..a133a6ae189 100644 --- a/src/org/rascalmpl/compiler/lang/rascalcore/check/tests/UseDefTests.rsc +++ b/src/org/rascalmpl/compiler/lang/rascalcore/check/tests/UseDefTests.rsc @@ -107,4 +107,51 @@ test bool overloadedSyntaxField(){ return useDefOK(mtext, ("c": <0, {2}>)) // check uses of first declaration of c && useDefOK(mtext, ("c": <1, {3}>)) // check uses of first declaration of c ; -} \ No newline at end of file +} + +test bool patternMatchShadowing1() { + mtext = " + module SelfReferential + void f(int i) { + if (int i := i) { + int x = i; + } + } + "; + + return useDefOK(mtext, ("i": <0, {2}>)) + && useDefOK(mtext, ("i": <1, {}>)) // this is a def, not a use + && useDefOK(mtext, ("i": <3, {1}>)); +} + +test bool patternMatchShadowing2() { + mtext = " + module SelfReferential + void f(int i) { + int j = 0; + if (int i := i + j) { + int x = i; + } + } + "; + + return useDefOK(mtext, ("i": <0, {2}>)) + && useDefOK(mtext, ("i": <1, {3}>)) // this is a def, not a use + && useDefOK(mtext, ("j": <0, {1}>)); +} + +test bool patternMatchShadowing3() { + mtext = " + module SelfReferential + void f(int i) { + if (int j := i + , int i := i + j) { + int x = i; + } + } + "; + + return useDefOK(mtext, ("i": <0, {1}>)) + && useDefOK(mtext, ("i": <2, {3, 4}>)) + && useDefOK(mtext, ("j": <0, {1}>)); +} From 19c57e9a5ac1c755110ce3e0007553e70d901500 Mon Sep 17 00:00:00 2001 From: Toine Hartman Date: Wed, 25 Nov 2026 14:04:30 +0100 Subject: [PATCH 02/17] Show all failing expectations in use/def tests. --- .../rascalcore/check/tests/StaticTestingUtils.rsc | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/org/rascalmpl/compiler/lang/rascalcore/check/tests/StaticTestingUtils.rsc b/src/org/rascalmpl/compiler/lang/rascalcore/check/tests/StaticTestingUtils.rsc index c56ef6374d6..5eb495e1881 100644 --- a/src/org/rascalmpl/compiler/lang/rascalcore/check/tests/StaticTestingUtils.rsc +++ b/src/org/rascalmpl/compiler/lang/rascalcore/check/tests/StaticTestingUtils.rsc @@ -201,6 +201,7 @@ bool checkModuleOK(str moduleText, PathConfig pathConfig = pathConfigForTesting( } bool validateUseDefs(str moduleName, map[str, tuple[int, set[int]]] defuses, ModuleStatus ms){ + result = true; = getModuleParseTree(moduleName2moduleId(moduleName), ms); map[str,list[loc]] names = (); map[loc, str] loc2name = (); @@ -240,19 +241,24 @@ bool validateUseDefs(str moduleName, map[str, tuple[int, set[int]]] defuses, Mod = defuses[v]; list[loc] occ = []; if(names[v]?) occ = names[v]; else throw "Name not found in tree"; - if(!occ[def]?) throw "Missing define for name "; + if(!occ[def]?) { + println("Missing define for name "); + result = false; + } for(int u <- uses){ println("u = "); if(!occ[u]?){ - throw "Missing use for "; + println("Missing use for "); + result = false; } potentialDefs = foundUseDefs[occ[u]]; if(isEmpty(potentialDefs) || !any(d <- potentialDefs, occ[def] == (tm.logical2physical[d] ? d))){ - throw "Missing def for use of "; + println("Missing def for use of "); + result = false; } } } - return true; + return result; } bool useDefOK(str moduleText, map[str, tuple[int, set[int]]] defuses, PathConfig pathConfig = pathConfigForTesting()) { From 50be0acc48e1d4175c131a4c785c503d07a04939 Mon Sep 17 00:00:00 2001 From: Toine Hartman Date: Thu, 26 Nov 2026 10:18:00 +0100 Subject: [PATCH 03/17] Fix parse error in test. --- .../compiler/lang/rascalcore/check/tests/PatternTCTests.rsc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/org/rascalmpl/compiler/lang/rascalcore/check/tests/PatternTCTests.rsc b/src/org/rascalmpl/compiler/lang/rascalcore/check/tests/PatternTCTests.rsc index 077455808b2..be03862b559 100644 --- a/src/org/rascalmpl/compiler/lang/rascalcore/check/tests/PatternTCTests.rsc +++ b/src/org/rascalmpl/compiler/lang/rascalcore/check/tests/PatternTCTests.rsc @@ -331,7 +331,7 @@ test bool If9() = checkOK("{ test bool If10() = checkOK("{ void (int i) { if (int j := i) { x = i + j; } - } + }; }"); test bool IfU1() = checkOK("{ if(x := 1) x + 1; };"); From 2a0580827ae6491d91233fd9b0bf1c71f4927532 Mon Sep 17 00:00:00 2001 From: Toine Hartman Date: Thu, 26 Nov 2026 10:34:34 +0100 Subject: [PATCH 04/17] Use module check instead of statements. --- .../lang/rascalcore/check/tests/PatternTCTests.rsc | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/org/rascalmpl/compiler/lang/rascalcore/check/tests/PatternTCTests.rsc b/src/org/rascalmpl/compiler/lang/rascalcore/check/tests/PatternTCTests.rsc index be03862b559..cb38b0f8a87 100644 --- a/src/org/rascalmpl/compiler/lang/rascalcore/check/tests/PatternTCTests.rsc +++ b/src/org/rascalmpl/compiler/lang/rascalcore/check/tests/PatternTCTests.rsc @@ -328,11 +328,12 @@ test bool If9() = checkOK("{ int i = 0; if (int j := i) { x = i + j; } }"); -test bool If10() = checkOK("{ - void (int i) { +test bool If10() = checkModuleOK(" + module PatternIf10 + void f(int i) { if (int j := i) { x = i + j; } - }; -}"); + } +"); test bool IfU1() = checkOK("{ if(x := 1) x + 1; };"); test bool IfU2() = unexpectedType("{ if(x := 1) x + 1; else x + 2;};"); From 13c38e657feee3b86b7eb095fbcedd308d3a1bb2 Mon Sep 17 00:00:00 2001 From: Toine Hartman Date: Thu, 26 Nov 2026 12:03:16 +0100 Subject: [PATCH 05/17] Fix use/def test expectations. --- .../lang/rascalcore/check/tests/UseDefTests.rsc | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/src/org/rascalmpl/compiler/lang/rascalcore/check/tests/UseDefTests.rsc b/src/org/rascalmpl/compiler/lang/rascalcore/check/tests/UseDefTests.rsc index a133a6ae189..3adb03554ce 100644 --- a/src/org/rascalmpl/compiler/lang/rascalcore/check/tests/UseDefTests.rsc +++ b/src/org/rascalmpl/compiler/lang/rascalcore/check/tests/UseDefTests.rsc @@ -105,7 +105,7 @@ test bool overloadedSyntaxField(){ return [x.c, y.c]; }"; return useDefOK(mtext, ("c": <0, {2}>)) // check uses of first declaration of c - && useDefOK(mtext, ("c": <1, {3}>)) // check uses of first declaration of c + && useDefOK(mtext, ("c": <1, {3}>)) // check uses of second declaration of c ; } @@ -120,8 +120,7 @@ test bool patternMatchShadowing1() { "; return useDefOK(mtext, ("i": <0, {2}>)) - && useDefOK(mtext, ("i": <1, {}>)) // this is a def, not a use - && useDefOK(mtext, ("i": <3, {1}>)); + && useDefOK(mtext, ("i": <1, {3}>)); } test bool patternMatchShadowing2() { @@ -136,7 +135,7 @@ test bool patternMatchShadowing2() { "; return useDefOK(mtext, ("i": <0, {2}>)) - && useDefOK(mtext, ("i": <1, {3}>)) // this is a def, not a use + && useDefOK(mtext, ("i": <1, {3}>)) && useDefOK(mtext, ("j": <0, {1}>)); } @@ -151,7 +150,7 @@ test bool patternMatchShadowing3() { } "; - return useDefOK(mtext, ("i": <0, {1}>)) - && useDefOK(mtext, ("i": <2, {3, 4}>)) + return useDefOK(mtext, ("i": <0, {1, 3}>)) + && useDefOK(mtext, ("i": <2, {4}>)) && useDefOK(mtext, ("j": <0, {1}>)); } From 0c1c0a265d498aa356516aa7e60fd7579f073abe Mon Sep 17 00:00:00 2001 From: Toine Hartman Date: Mon, 30 Nov 2026 16:49:25 +0100 Subject: [PATCH 06/17] Test if with conditions from stdlib." --- .../check/tests/StatementTCTests.rsc | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/src/org/rascalmpl/compiler/lang/rascalcore/check/tests/StatementTCTests.rsc b/src/org/rascalmpl/compiler/lang/rascalcore/check/tests/StatementTCTests.rsc index 5ae44f94a11..92a6d443491 100644 --- a/src/org/rascalmpl/compiler/lang/rascalcore/check/tests/StatementTCTests.rsc +++ b/src/org/rascalmpl/compiler/lang/rascalcore/check/tests/StatementTCTests.rsc @@ -107,4 +107,21 @@ test bool Issue416() = checkModuleOK(" // https://github.com/cwi-swat/rascal/issues/432 -test bool Issue432() = unexpectedType("set[value] s := {} && s\<0\> == {};"); \ No newline at end of file +test bool Issue432() = unexpectedType("set[value] s := {} && s\<0\> == {};"); + +test bool IfConditions() = checkModuleOK(" + module IfConditions + + data RuntimeException + = PathNotFound(loc location) + | PathNotFound(set[loc] locs); + + bool exists(loc f); + + public loc find(str name, list[loc] path) throws PathNotFound { + if (dir \<- path, f := dir + \"/\\", exists(f)) { + return f; + } + throw PathNotFound({dir + \"/\\" | dir \<- path}); + } +"); From a09b328985f8bcb5b7ce8ea16ecddfd870614951 Mon Sep 17 00:00:00 2001 From: Toine Hartman Date: Tue, 1 Dec 2026 16:48:46 +0100 Subject: [PATCH 07/17] Add use-before-def tests. --- .../compiler/lang/rascalcore/check/tests/PatternTCTests.rsc | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/org/rascalmpl/compiler/lang/rascalcore/check/tests/PatternTCTests.rsc b/src/org/rascalmpl/compiler/lang/rascalcore/check/tests/PatternTCTests.rsc index cb38b0f8a87..e401c3cba8f 100644 --- a/src/org/rascalmpl/compiler/lang/rascalcore/check/tests/PatternTCTests.rsc +++ b/src/org/rascalmpl/compiler/lang/rascalcore/check/tests/PatternTCTests.rsc @@ -335,6 +335,12 @@ test bool If10() = checkModuleOK(" } "); +test bool Comprehension1() = checkOK("{ i + j | n \<- {0, 1, 2, 3}, int i := n % 2, int j := n % 3 }"); +test bool Comprehension2() = undeclaredVariable("{ i + j | n \<- {0, 1, 2, 3}, int i := i % 2, int j := n % 3 }"); + +test bool Reduction1() = checkOK("( 0 | it + i + j | n \<- {0, 1, 2, 3}, int i := n % 2, int j := n % 3 )"); +test bool Reduction2() = undeclaredVariable("(0 | it + i + j | n \<- {0, 1, 2, 3}, int i := i % 2, int j := n % 3 )"); + test bool IfU1() = checkOK("{ if(x := 1) x + 1; };"); test bool IfU2() = unexpectedType("{ if(x := 1) x + 1; else x + 2;};"); test bool IfU3() = checkOK("{ if(x := 1 && x == 1 ) x + 1; };"); From 15304d23beaf656a6c73a2f9fdfb1d429e7392ad Mon Sep 17 00:00:00 2001 From: Toine Hartman Date: Wed, 2 Dec 2026 10:54:27 +0100 Subject: [PATCH 08/17] Fix parse errors in tests. --- .../lang/rascalcore/check/tests/PatternTCTests.rsc | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/org/rascalmpl/compiler/lang/rascalcore/check/tests/PatternTCTests.rsc b/src/org/rascalmpl/compiler/lang/rascalcore/check/tests/PatternTCTests.rsc index e401c3cba8f..e8000b68538 100644 --- a/src/org/rascalmpl/compiler/lang/rascalcore/check/tests/PatternTCTests.rsc +++ b/src/org/rascalmpl/compiler/lang/rascalcore/check/tests/PatternTCTests.rsc @@ -335,11 +335,11 @@ test bool If10() = checkModuleOK(" } "); -test bool Comprehension1() = checkOK("{ i + j | n \<- {0, 1, 2, 3}, int i := n % 2, int j := n % 3 }"); -test bool Comprehension2() = undeclaredVariable("{ i + j | n \<- {0, 1, 2, 3}, int i := i % 2, int j := n % 3 }"); +test bool Comprehension1() = checkOK("{ i + j | n \<- {0, 1, 2, 3}, int i := n % 2, int j := n % 3 };"); +test bool Comprehension2() = undeclaredVariable("{ i + j | n \<- {0, 1, 2, 3}, int i := i % 2, int j := n % 3 };"); -test bool Reduction1() = checkOK("( 0 | it + i + j | n \<- {0, 1, 2, 3}, int i := n % 2, int j := n % 3 )"); -test bool Reduction2() = undeclaredVariable("(0 | it + i + j | n \<- {0, 1, 2, 3}, int i := i % 2, int j := n % 3 )"); +test bool Reduction1() = checkOK("( 0 | it + i + j | n \<- {0, 1, 2, 3}, int i := n % 2, int j := n % 3 );"); +test bool Reduction2() = undeclaredVariable("(0 | it + i + j | n \<- {0, 1, 2, 3}, int i := i % 2, int j := n % 3 );"); test bool IfU1() = checkOK("{ if(x := 1) x + 1; };"); test bool IfU2() = unexpectedType("{ if(x := 1) x + 1; else x + 2;};"); From 1cb4adc27a3e096bec7b4f211e608c30bcdbde08 Mon Sep 17 00:00:00 2001 From: Toine Hartman Date: Wed, 2 Dec 2026 11:57:18 +0100 Subject: [PATCH 09/17] Add tests with multiple vars in match. --- .../rascalcore/check/tests/PatternTCTests.rsc | 28 +++++++++++++++++-- .../check/tests/StaticTestingUtils.rsc | 12 ++++---- .../rascalcore/check/tests/UseDefTests.rsc | 14 ++++++++++ 3 files changed, 45 insertions(+), 9 deletions(-) diff --git a/src/org/rascalmpl/compiler/lang/rascalcore/check/tests/PatternTCTests.rsc b/src/org/rascalmpl/compiler/lang/rascalcore/check/tests/PatternTCTests.rsc index e8000b68538..a7beb1dab2c 100644 --- a/src/org/rascalmpl/compiler/lang/rascalcore/check/tests/PatternTCTests.rsc +++ b/src/org/rascalmpl/compiler/lang/rascalcore/check/tests/PatternTCTests.rsc @@ -328,10 +328,32 @@ test bool If9() = checkOK("{ int i = 0; if (int j := i) { x = i + j; } }"); -test bool If10() = checkModuleOK(" - module PatternIf10 +test bool If10() = checkOK(" void f(int i) { - if (int j := i) { x = i + j; } + if (int j := i) { + x = i + j; + } + } +"); +test bool If11() = checkOK(" + void f(int i, int j) { + if (\ := \) { + x = i + j; + } + } +"); +test bool If12() = undeclaredVariable(" + void f(int i) { + if (\ := \) { + x = i + j; + } + } +"); +test bool If13() = undeclaredVariable(" + void f(int j) { + if (\ := \) { + x = i + j; + } } "); diff --git a/src/org/rascalmpl/compiler/lang/rascalcore/check/tests/StaticTestingUtils.rsc b/src/org/rascalmpl/compiler/lang/rascalcore/check/tests/StaticTestingUtils.rsc index 5eb495e1881..b51c75c5d2b 100644 --- a/src/org/rascalmpl/compiler/lang/rascalcore/check/tests/StaticTestingUtils.rsc +++ b/src/org/rascalmpl/compiler/lang/rascalcore/check/tests/StaticTestingUtils.rsc @@ -224,13 +224,13 @@ bool validateUseDefs(str moduleName, map[str, tuple[int, set[int]]] defuses, Mod loc2name[nm.src] = ""; } } - println("names:"); iprintln(names); + // println("names:"); iprintln(names); = getTModelForModule(moduleName, ms); foundUseDefs = tm.useDef; - println("foundUseDefs:"); - for( <- foundUseDefs){ - println(": ==\> "); - } + // println("foundUseDefs:"); + // for( <- foundUseDefs){ + // println(": ==\> "); + // } for(u <- domain(foundUseDefs)){ defs = foundUseDefs[u]; if(size(defs) != 1){ @@ -246,7 +246,7 @@ bool validateUseDefs(str moduleName, map[str, tuple[int, set[int]]] defuses, Mod result = false; } for(int u <- uses){ - println("u = "); + // println("u = "); if(!occ[u]?){ println("Missing use for "); result = false; diff --git a/src/org/rascalmpl/compiler/lang/rascalcore/check/tests/UseDefTests.rsc b/src/org/rascalmpl/compiler/lang/rascalcore/check/tests/UseDefTests.rsc index 3adb03554ce..3b54710b38d 100644 --- a/src/org/rascalmpl/compiler/lang/rascalcore/check/tests/UseDefTests.rsc +++ b/src/org/rascalmpl/compiler/lang/rascalcore/check/tests/UseDefTests.rsc @@ -154,3 +154,17 @@ test bool patternMatchShadowing3() { && useDefOK(mtext, ("i": <2, {4}>)) && useDefOK(mtext, ("j": <0, {1}>)); } + +test bool patternMatchShadowing4() { + mtext = " + module SelfReferential + void f(int i, int j) { + if (\ := \) { x = i + j; } + } + "; + + return useDefOK(mtext, ("i": <0, {2}>)) + && useDefOK(mtext, ("i": <1, {3}>)) + && useDefOK(mtext, ("j": <0, {2}>)) + && useDefOK(mtext, ("j": <1, {3}>)); +} From e9f6a2baf4fa33f7473030e73eecbfd62ca957e0 Mon Sep 17 00:00:00 2001 From: Toine Hartman Date: Wed, 2 Dec 2026 11:16:02 +0100 Subject: [PATCH 10/17] Disallow use of match lhs in rhs. Closes #2264. --- .../rascalcore/check/BasicRascalConfig.rsc | 9 ++++++ .../rascalcore/check/CollectOperators.rsc | 28 ++++++++++++------- .../lang/rascalcore/check/RascalConfig.rsc | 5 ++++ 3 files changed, 32 insertions(+), 10 deletions(-) diff --git a/src/org/rascalmpl/compiler/lang/rascalcore/check/BasicRascalConfig.rsc b/src/org/rascalmpl/compiler/lang/rascalcore/check/BasicRascalConfig.rsc index 814048e778c..d26701bee59 100644 --- a/src/org/rascalmpl/compiler/lang/rascalcore/check/BasicRascalConfig.rsc +++ b/src/org/rascalmpl/compiler/lang/rascalcore/check/BasicRascalConfig.rsc @@ -165,6 +165,15 @@ void storeAllowUseBeforeDef(Tree container, list[Tree] allowedParts, Collector c c.push(key_allow_use_before_def, ); } +public str key_disallow_use_of_def = "disallow_use_of_def"; + +void storeDisallowUseOfDef(Tree def, Tree disallowedUseTree, Collector c) + = storeDisallowUseOfDef(def, getLoc(disallowedUseTree), c); + +void storeDisallowUseOfDef(Tree def, loc disallowedUseRange, Collector c) { + c.push(key_disallow_use_of_def, ); +} + public str key_bom = "bill_of_materials"; public str key_current_module = "current_module"; public str key_pathconfig = "pathconfig"; diff --git a/src/org/rascalmpl/compiler/lang/rascalcore/check/CollectOperators.rsc b/src/org/rascalmpl/compiler/lang/rascalcore/check/CollectOperators.rsc index db9d716024c..ba8f4f2c301 100644 --- a/src/org/rascalmpl/compiler/lang/rascalcore/check/CollectOperators.rsc +++ b/src/org/rascalmpl/compiler/lang/rascalcore/check/CollectOperators.rsc @@ -591,9 +591,14 @@ void computeMatchPattern(Expression current, Pattern pat, str operator, Expressi }); c.push(patternContainer, "match"); if(operator == "!:=") c.enterScope(pat); // wrap in extra scope to avoid that variables in pattern leak to surroundings + patternVars = introducedVarTrees(pat, c); collect(pat, c); if(operator == "!:=") c.leaveScope(pat); c.pop(patternContainer); + + for (p <- patternVars) { + storeDisallowUseOfDef(p, expression, c); + } collect(expression, c); } @@ -769,32 +774,35 @@ private set[str] introducedVars(Expression exp, Collector c){ } } -private set[str] introducedVars(Pattern e, Collector c){ +private set[str] introducedVars(Pattern e, Collector c) + = {prettyPrintName(n) | n <- introducedVarTrees(e, c)}; + +private set[Tree] introducedVarTrees(Pattern e, Collector c) { vars = {}; top-down-break visit(e){ case (Pattern) ``: { nm = prettyPrintName(qualifiedName); - if(nm[0] != "_" && !c.isAlreadyDefined(nm, qualifiedName)) vars += nm; + if(nm[0] != "_" && !c.isAlreadyDefined(nm, qualifiedName)) vars += qualifiedName; } case (Pattern) ` ` : { nm = prettyPrintName(name); - if(nm[0] != "_") vars += nm; + if(nm[0] != "_") vars += name; } case (Pattern) ` : `: { nm = prettyPrintName(name); - if(nm[0] != "_" && !c.isAlreadyDefined(nm, name)) vars += nm; - vars += introducedVars(pattern, c); + if(nm[0] != "_" && !c.isAlreadyDefined(nm, name)) vars += name; + vars += introducedVarTrees(pattern, c); } case (Pattern) ` : `: { nm = prettyPrintName(name); - if(nm[0] != "_") vars += nm; - vars += introducedVars(pattern, c); + if(nm[0] != "_") vars += name; + vars += introducedVarTrees(pattern, c); } case (Pattern) ` ( <{Pattern ","}* arguments> )`: { - if(!(expression is qualifiedName)) vars += introducedVars(expression, c); - vars += {*introducedVars(argument, c) | argument <- arguments}; + if(!(expression is qualifiedName)) vars += introducedVarTrees(expression, c); + vars += {*introducedVarTrees(argument, c) | argument <- arguments}; if(keywordArguments is \default){ - vars += { *introducedVars(kwa.expression, c) | kwa <- keywordArguments.keywordArgumentList }; + vars += { *introducedVarTrees(kwa.expression, c) | kwa <- keywordArguments.keywordArgumentList }; } } } diff --git a/src/org/rascalmpl/compiler/lang/rascalcore/check/RascalConfig.rsc b/src/org/rascalmpl/compiler/lang/rascalcore/check/RascalConfig.rsc index d8f4e7a2688..2815d6322aa 100644 --- a/src/org/rascalmpl/compiler/lang/rascalcore/check/RascalConfig.rsc +++ b/src/org/rascalmpl/compiler/lang/rascalcore/check/RascalConfig.rsc @@ -167,6 +167,11 @@ Accept rascalIsAcceptableSimple(loc def, Use use, Solver s){ // Uses of a keyword formal inside its initializing expression are rejected if(d.idRole == keywordFormalId() && s.isContainedIn(use.occ, d.defined)){ return ignoreContinue(); + } else if (d.idRole == patternVariableId() + && lrel[loc, loc] disallowedUseRange := s.getStack(key_disallow_use_of_def) + , useRange <- disallowedUseRange[d.defined] + , isContainedIn(use.occ, useRange)) { + return ignoreContinue(); } return acceptBinding(); } From 540dc5391f0f6d232896def0ac43ba455c5c9a66 Mon Sep 17 00:00:00 2001 From: Toine Hartman Date: Wed, 2 Dec 2026 13:18:09 +0100 Subject: [PATCH 11/17] Improve disallowed-use-range check. --- .../compiler/lang/rascalcore/check/RascalConfig.rsc | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/org/rascalmpl/compiler/lang/rascalcore/check/RascalConfig.rsc b/src/org/rascalmpl/compiler/lang/rascalcore/check/RascalConfig.rsc index 2815d6322aa..93157a681f9 100644 --- a/src/org/rascalmpl/compiler/lang/rascalcore/check/RascalConfig.rsc +++ b/src/org/rascalmpl/compiler/lang/rascalcore/check/RascalConfig.rsc @@ -167,11 +167,10 @@ Accept rascalIsAcceptableSimple(loc def, Use use, Solver s){ // Uses of a keyword formal inside its initializing expression are rejected if(d.idRole == keywordFormalId() && s.isContainedIn(use.occ, d.defined)){ return ignoreContinue(); - } else if (d.idRole == patternVariableId() - && lrel[loc, loc] disallowedUseRange := s.getStack(key_disallow_use_of_def) - , useRange <- disallowedUseRange[d.defined] - , isContainedIn(use.occ, useRange)) { - return ignoreContinue(); + } else if (d.idRole == patternVariableId() && lrel[loc, loc] disallowedUseRange := s.getStack(key_disallow_use_of_def)) { + for (useRange <- disallowedUseRange[d.defined], isContainedIn(use.occ, useRange)) { + return ignoreContinue(); + } } return acceptBinding(); } From 17bbc6f7f2216e6d5af1d8b8545cf4c08e67dd10 Mon Sep 17 00:00:00 2001 From: Toine Hartman Date: Wed, 2 Dec 2026 16:39:57 +0100 Subject: [PATCH 12/17] Add self-referential assignment tests from #2783. Co-authored-by: Sung-Shik Jongmans --- .../check/tests/AssignmentTCTests.rsc | 20 +++++ .../rascalcore/check/tests/UseDefTests.rsc | 81 +++++++++++++++++++ 2 files changed, 101 insertions(+) diff --git a/src/org/rascalmpl/compiler/lang/rascalcore/check/tests/AssignmentTCTests.rsc b/src/org/rascalmpl/compiler/lang/rascalcore/check/tests/AssignmentTCTests.rsc index 70a9a3575d3..96e564bef3b 100644 --- a/src/org/rascalmpl/compiler/lang/rascalcore/check/tests/AssignmentTCTests.rsc +++ b/src/org/rascalmpl/compiler/lang/rascalcore/check/tests/AssignmentTCTests.rsc @@ -37,6 +37,26 @@ test bool AssignmentError2() = unexpectedType("{int i = true;};"); test bool AssignmentError3() = unexpectedType("{int n = 3; n = true;}"); +test bool SelfReferentialAssignment() = undeclaredVariable("{ int i = i; }"); + +test bool Issue2783a() = undeclaredVariable("int () { int z = z; return z; };"); +test bool Issue2783b() = unexpectedDeclaration("int () { + str z = \"foo\"; + f = int() { + int z = z; + return z; + }; + return f(); +};"); + +test bool Issue2783c() = unexpectedDeclaration("int () { + f = int(str z = \"foo\") { + int z = z; + return z; + }; + return f(); +};"); + test bool IntAddAssign() = checkOK("{int n = 3; n += 3;}"); test bool IntAddAssignError() = unexpectedType("{int n = 3; n += true;}"); diff --git a/src/org/rascalmpl/compiler/lang/rascalcore/check/tests/UseDefTests.rsc b/src/org/rascalmpl/compiler/lang/rascalcore/check/tests/UseDefTests.rsc index 3b54710b38d..ebf210f1775 100644 --- a/src/org/rascalmpl/compiler/lang/rascalcore/check/tests/UseDefTests.rsc +++ b/src/org/rascalmpl/compiler/lang/rascalcore/check/tests/UseDefTests.rsc @@ -168,3 +168,84 @@ test bool patternMatchShadowing4() { && useDefOK(mtext, ("j": <0, {2}>)) && useDefOK(mtext, ("j": <1, {3}>)); } + +test bool initShadowing1() { + mtext = " + module SelfReferential + int i = 1; + void f() { + int i = i; + int x = i; + } + "; + + return useDefOK(mtext, ("i": <0, {2}>)) + && useDefOK(mtext, ("i": <1, {3}>)); +} + +test bool initShadowing2() { + mtext = " + module SelfReferential + int i = 1; + void f() { + i = i; + int x = i; + } + "; + + return useDefOK(mtext, ("i": <0, {1, 2, 3}>)); +} + +test bool initShadowing3() { + mtext = " + module SelfReferential + int i = 1; + void f() { + int i = i; + int x = i; + } + "; + + return useDefOK(mtext, ("i": <0, {2}>)) + && useDefOK(mtext, ("i": <1, {3}>)); +} + +test bool assignShadowing1() { + mtext = " + module SelfReferential + int i = 1; + void f() { + i = i; + int x = i; + } + "; + + return useDefOK(mtext, ("i": <0, {1, 2, 3}>)); +} + +test bool assignShadowing2() { + mtext = " + module SelfReferential + int i = 1; + void f() { + if (true) { + i += i; + int x = i; + } + } + "; + + return useDefOK(mtext, ("i": <0, {1, 2, 3}>)); +} + +test bool assignShadowing3() { + mtext = " + module SelfReferential + void f(int i) { + i += i; + int x = i; + } + "; + + return useDefOK(mtext, ("i": <0, {1, 2, 3}>)); +} From df9ced3c0a8dee6b22a766f4dabab7fb2ead8827 Mon Sep 17 00:00:00 2001 From: Toine Hartman Date: Wed, 2 Dec 2026 16:45:37 +0100 Subject: [PATCH 13/17] Disallow use of variable in its own intializer. Closes #2783. --- .../lang/rascalcore/check/CollectStatement.rsc | 1 + .../compiler/lang/rascalcore/check/RascalConfig.rsc | 10 ++++++---- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/src/org/rascalmpl/compiler/lang/rascalcore/check/CollectStatement.rsc b/src/org/rascalmpl/compiler/lang/rascalcore/check/CollectStatement.rsc index 5dc5bf2dd57..a1c51d2c3d1 100644 --- a/src/org/rascalmpl/compiler/lang/rascalcore/check/CollectStatement.rsc +++ b/src/org/rascalmpl/compiler/lang/rascalcore/check/CollectStatement.rsc @@ -1232,6 +1232,7 @@ void collect(current: (Statement) ` <{Variable ","}+ variables>;`, c.report(error(initial, "Right-hand side of assignment does not always have a value")); } } + storeDisallowUseOfDef(var.name, initial, c); c.enterLubScope(var); c.require("initialization of ``", initial, [initial, varType], makeVarInitRequirement(var)); collect(initial, c); diff --git a/src/org/rascalmpl/compiler/lang/rascalcore/check/RascalConfig.rsc b/src/org/rascalmpl/compiler/lang/rascalcore/check/RascalConfig.rsc index 93157a681f9..177f908b5c7 100644 --- a/src/org/rascalmpl/compiler/lang/rascalcore/check/RascalConfig.rsc +++ b/src/org/rascalmpl/compiler/lang/rascalcore/check/RascalConfig.rsc @@ -167,10 +167,12 @@ Accept rascalIsAcceptableSimple(loc def, Use use, Solver s){ // Uses of a keyword formal inside its initializing expression are rejected if(d.idRole == keywordFormalId() && s.isContainedIn(use.occ, d.defined)){ return ignoreContinue(); - } else if (d.idRole == patternVariableId() && lrel[loc, loc] disallowedUseRange := s.getStack(key_disallow_use_of_def)) { - for (useRange <- disallowedUseRange[d.defined], isContainedIn(use.occ, useRange)) { - return ignoreContinue(); - } + } + + for (lrel[loc, loc] disallowedUseRange := s.getStack(key_disallow_use_of_def) + , ranges:[_, *_] := disallowedUseRange[d.defined] + , r <- ranges, isContainedIn(use.occ, r)) { + return ignoreContinue(); } return acceptBinding(); } From 36580297a47ae3bc2829b6aaa282e5eafb51cc52 Mon Sep 17 00:00:00 2001 From: Toine Hartman Date: Wed, 2 Dec 2026 17:04:37 +0100 Subject: [PATCH 14/17] Disallow use of module variable in its own intializer. --- .../compiler/lang/rascalcore/check/CollectDeclaration.rsc | 1 + .../lang/rascalcore/check/tests/AssignmentTCTests.rsc | 6 +++++- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/src/org/rascalmpl/compiler/lang/rascalcore/check/CollectDeclaration.rsc b/src/org/rascalmpl/compiler/lang/rascalcore/check/CollectDeclaration.rsc index e54377ff379..0540cd6b2e3 100644 --- a/src/org/rascalmpl/compiler/lang/rascalcore/check/CollectDeclaration.rsc +++ b/src/org/rascalmpl/compiler/lang/rascalcore/check/CollectDeclaration.rsc @@ -203,6 +203,7 @@ void collect(current: (Declaration) ` `", initial, [initial, varType], makeVarInitRequirement(var)); collect(initial, c); } diff --git a/src/org/rascalmpl/compiler/lang/rascalcore/check/tests/AssignmentTCTests.rsc b/src/org/rascalmpl/compiler/lang/rascalcore/check/tests/AssignmentTCTests.rsc index 96e564bef3b..6a4cd4f5126 100644 --- a/src/org/rascalmpl/compiler/lang/rascalcore/check/tests/AssignmentTCTests.rsc +++ b/src/org/rascalmpl/compiler/lang/rascalcore/check/tests/AssignmentTCTests.rsc @@ -37,7 +37,11 @@ test bool AssignmentError2() = unexpectedType("{int i = true;};"); test bool AssignmentError3() = unexpectedType("{int n = 3; n = true;}"); -test bool SelfReferentialAssignment() = undeclaredVariable("{ int i = i; }"); +test bool SelfReferentialInit() = undeclaredVariable("{ int i = i; }"); +test bool SelfReferentialModuleInit() = undeclaredVariableInModule(" + module SelfReference + int i = i; +"); test bool Issue2783a() = undeclaredVariable("int () { int z = z; return z; };"); test bool Issue2783b() = unexpectedDeclaration("int () { From 34061bbbb9fcf7882474ee8918fadab37c84d52f Mon Sep 17 00:00:00 2001 From: Toine Hartman Date: Wed, 2 Dec 2026 17:10:17 +0100 Subject: [PATCH 15/17] Only perform disallowed-use check for selected ID roles. --- .../compiler/lang/rascalcore/check/RascalConfig.rsc | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/org/rascalmpl/compiler/lang/rascalcore/check/RascalConfig.rsc b/src/org/rascalmpl/compiler/lang/rascalcore/check/RascalConfig.rsc index 177f908b5c7..b53b2acf9b2 100644 --- a/src/org/rascalmpl/compiler/lang/rascalcore/check/RascalConfig.rsc +++ b/src/org/rascalmpl/compiler/lang/rascalcore/check/RascalConfig.rsc @@ -169,10 +169,12 @@ Accept rascalIsAcceptableSimple(loc def, Use use, Solver s){ return ignoreContinue(); } - for (lrel[loc, loc] disallowedUseRange := s.getStack(key_disallow_use_of_def) - , ranges:[_, *_] := disallowedUseRange[d.defined] - , r <- ranges, isContainedIn(use.occ, r)) { - return ignoreContinue(); + if (d.idRole in {moduleVariableId(), variableId(), patternVariableId()}) { + for (lrel[loc, loc] disallowedUseRange := s.getStack(key_disallow_use_of_def) + , ranges:[_, *_] := disallowedUseRange[d.defined] + , r <- ranges, isContainedIn(use.occ, r)) { + return ignoreContinue(); + } } return acceptBinding(); } From 99ca99752451425e3137c38afbba56581994a8de Mon Sep 17 00:00:00 2001 From: Toine Hartman Date: Wed, 2 Dec 2026 20:00:51 +0100 Subject: [PATCH 16/17] Fix static error in positive test. --- .../lang/rascalcore/check/tests/SyntaxDeclarationTCTests.rsc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/org/rascalmpl/compiler/lang/rascalcore/check/tests/SyntaxDeclarationTCTests.rsc b/src/org/rascalmpl/compiler/lang/rascalcore/check/tests/SyntaxDeclarationTCTests.rsc index 2397c13323d..caee9f0b128 100644 --- a/src/org/rascalmpl/compiler/lang/rascalcore/check/tests/SyntaxDeclarationTCTests.rsc +++ b/src/org/rascalmpl/compiler/lang/rascalcore/check/tests/SyntaxDeclarationTCTests.rsc @@ -504,7 +504,7 @@ test bool Param3() = checkModuleOK(" \> add: EXP[&T] lhs \"+\" EXP[&T] rhs !\>\> [0-9] ); EXP[INT] exp1; - EXP[INT] exp2 = add(exp1, exp2); + EXP[INT] exp2 = add(exp1, exp1); "); @ignore{Does not generate error} From 7b751801584f6b3775eec8a42ea4ecf6ed09428d Mon Sep 17 00:00:00 2001 From: Toine Hartman Date: Thu, 3 Dec 2026 10:08:48 +0100 Subject: [PATCH 17/17] Pretty-print introduced variables only once. --- .../rascalcore/check/CollectOperators.rsc | 31 +++++++++---------- 1 file changed, 14 insertions(+), 17 deletions(-) diff --git a/src/org/rascalmpl/compiler/lang/rascalcore/check/CollectOperators.rsc b/src/org/rascalmpl/compiler/lang/rascalcore/check/CollectOperators.rsc index ba8f4f2c301..e9f3eb15a91 100644 --- a/src/org/rascalmpl/compiler/lang/rascalcore/check/CollectOperators.rsc +++ b/src/org/rascalmpl/compiler/lang/rascalcore/check/CollectOperators.rsc @@ -591,7 +591,7 @@ void computeMatchPattern(Expression current, Pattern pat, str operator, Expressi }); c.push(patternContainer, "match"); if(operator == "!:=") c.enterScope(pat); // wrap in extra scope to avoid that variables in pattern leak to surroundings - patternVars = introducedVarTrees(pat, c); + patternVars = introducedVars(pat, c)<1>; collect(pat, c); if(operator == "!:=") c.leaveScope(pat); c.pop(patternContainer); @@ -762,7 +762,7 @@ void collect(current: (Expression) ` && `, Colle collect(lhs, rhs, c); } -private set[str] introducedVars(Expression exp, Collector c){ +private rel[str, Tree] introducedVars(Expression exp, Collector c){ if(exp is \bracket){ return introducedVars(exp.expression, c); } else if (exp is match){ @@ -774,35 +774,32 @@ private set[str] introducedVars(Expression exp, Collector c){ } } -private set[str] introducedVars(Pattern e, Collector c) - = {prettyPrintName(n) | n <- introducedVarTrees(e, c)}; - -private set[Tree] introducedVarTrees(Pattern e, Collector c) { +private rel[str, Tree] introducedVars(Pattern e, Collector c) { vars = {}; top-down-break visit(e){ case (Pattern) ``: { nm = prettyPrintName(qualifiedName); - if(nm[0] != "_" && !c.isAlreadyDefined(nm, qualifiedName)) vars += qualifiedName; + if(nm[0] != "_" && !c.isAlreadyDefined(nm, qualifiedName)) vars += ; } case (Pattern) ` ` : { nm = prettyPrintName(name); - if(nm[0] != "_") vars += name; + if(nm[0] != "_") vars += ; } case (Pattern) ` : `: { nm = prettyPrintName(name); - if(nm[0] != "_" && !c.isAlreadyDefined(nm, name)) vars += name; - vars += introducedVarTrees(pattern, c); + if(nm[0] != "_" && !c.isAlreadyDefined(nm, name)) vars += ; + vars += introducedVars(pattern, c); } case (Pattern) ` : `: { nm = prettyPrintName(name); - if(nm[0] != "_") vars += name; - vars += introducedVarTrees(pattern, c); + if(nm[0] != "_") vars += ; + vars += introducedVars(pattern, c); } case (Pattern) ` ( <{Pattern ","}* arguments> )`: { - if(!(expression is qualifiedName)) vars += introducedVarTrees(expression, c); - vars += {*introducedVarTrees(argument, c) | argument <- arguments}; + if(!(expression is qualifiedName)) vars += introducedVars(expression, c); + vars += {*introducedVars(argument, c) | argument <- arguments}; if(keywordArguments is \default){ - vars += { *introducedVarTrees(kwa.expression, c) | kwa <- keywordArguments.keywordArgumentList }; + vars += { *introducedVars(kwa.expression, c) | kwa <- keywordArguments.keywordArgumentList }; } } } @@ -820,8 +817,8 @@ void collect(current: (Expression) ` || `, Colle s.requireUnify(abool(), rhs, error(rhs, "Argument of || should be `bool`, found %t", rhs)); }); - introLhs = introducedVars(lhs, c); - introRhs = introducedVars(rhs, c); + introLhs = introducedVars(lhs, c)<0>; + introRhs = introducedVars(rhs, c)<0>; collect(lhs, c);