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/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/CollectOperators.rsc b/src/org/rascalmpl/compiler/lang/rascalcore/check/CollectOperators.rsc index db9d716024c..e9f3eb15a91 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 = introducedVars(pat, c)<1>; collect(pat, c); if(operator == "!:=") c.leaveScope(pat); c.pop(patternContainer); + + for (p <- patternVars) { + storeDisallowUseOfDef(p, expression, c); + } collect(expression, c); } @@ -757,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){ @@ -769,25 +774,25 @@ private set[str] introducedVars(Expression exp, Collector c){ } } -private set[str] introducedVars(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 += nm; + if(nm[0] != "_" && !c.isAlreadyDefined(nm, qualifiedName)) vars += ; } case (Pattern) ` ` : { nm = prettyPrintName(name); - if(nm[0] != "_") vars += nm; + if(nm[0] != "_") vars += ; } case (Pattern) ` : `: { nm = prettyPrintName(name); - if(nm[0] != "_" && !c.isAlreadyDefined(nm, name)) vars += nm; + if(nm[0] != "_" && !c.isAlreadyDefined(nm, name)) vars += ; vars += introducedVars(pattern, c); } case (Pattern) ` : `: { nm = prettyPrintName(name); - if(nm[0] != "_") vars += nm; + if(nm[0] != "_") vars += ; vars += introducedVars(pattern, c); } case (Pattern) ` ( <{Pattern ","}* arguments> )`: { @@ -812,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); 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 d8f4e7a2688..b53b2acf9b2 100644 --- a/src/org/rascalmpl/compiler/lang/rascalcore/check/RascalConfig.rsc +++ b/src/org/rascalmpl/compiler/lang/rascalcore/check/RascalConfig.rsc @@ -168,6 +168,14 @@ Accept rascalIsAcceptableSimple(loc def, Use use, Solver s){ if(d.idRole == keywordFormalId() && s.isContainedIn(use.occ, d.defined)){ 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(); } 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..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,6 +37,30 @@ test bool AssignmentError2() = unexpectedType("{int i = true;};"); test bool AssignmentError3() = unexpectedType("{int n = 3; n = true;}"); +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 () { + 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/PatternTCTests.rsc b/src/org/rascalmpl/compiler/lang/rascalcore/check/tests/PatternTCTests.rsc index c1050669fcd..a7beb1dab2c 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,47 @@ 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 f(int i) { + 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; + } + } +"); + +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; };"); 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}); + } +"); 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 7218658d39e..d2b48932338 100644 --- a/src/org/rascalmpl/compiler/lang/rascalcore/check/tests/StaticTestingUtils.rsc +++ b/src/org/rascalmpl/compiler/lang/rascalcore/check/tests/StaticTestingUtils.rsc @@ -202,6 +202,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 = (); @@ -224,13 +225,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){ @@ -241,19 +242,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 = "); + // 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()) { 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} 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..ebf210f1775 100644 --- a/src/org/rascalmpl/compiler/lang/rascalcore/check/tests/UseDefTests.rsc +++ b/src/org/rascalmpl/compiler/lang/rascalcore/check/tests/UseDefTests.rsc @@ -105,6 +105,147 @@ 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 ; -} \ 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, {3}>)); +} + +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}>)) + && 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, 3}>)) + && 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}>)); +} + +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}>)); +}