Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,15 @@ void storeAllowUseBeforeDef(Tree container, list[Tree] allowedParts, Collector c
c.push(key_allow_use_before_def, <getLoc(container), cover([getLoc(allowed) | allowed <- allowedParts])>);
}

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, <getLoc(def), disallowedUseRange>);
}
Comment thread
toinehartman marked this conversation as resolved.
Resolved

public str key_bom = "bill_of_materials";
public str key_current_module = "current_module";
public str key_pathconfig = "pathconfig";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ void collect(current: (Declaration) `<Tags tags> <Visibility visibility> <Type v
c.report(error(initial, "Right-hand side of assignment does not always have a value"));
}
}
storeDisallowUseOfDef(var.name, initial, c);

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Prohibit int i = i at module level

c.require("initialization of `<vname>`", initial, [initial, varType], makeVarInitRequirement(var));
collect(initial, c);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Prohibit i := i

}
collect(expression, c);
}

Expand Down Expand Up @@ -757,7 +762,7 @@ void collect(current: (Expression) `<Expression lhs> && <Expression rhs>`, 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){
Expand All @@ -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) `<QualifiedName qualifiedName>`: {
nm = prettyPrintName(qualifiedName);
if(nm[0] != "_" && !c.isAlreadyDefined(nm, qualifiedName)) vars += nm;
if(nm[0] != "_" && !c.isAlreadyDefined(nm, qualifiedName)) vars += <nm, qualifiedName>;
}
case (Pattern) `<Type _> <Name name>` : {
nm = prettyPrintName(name);
if(nm[0] != "_") vars += nm;
if(nm[0] != "_") vars += <nm, name>;
}
case (Pattern) `<Name name> : <Pattern pattern>`: {
nm = prettyPrintName(name);
if(nm[0] != "_" && !c.isAlreadyDefined(nm, name)) vars += nm;
if(nm[0] != "_" && !c.isAlreadyDefined(nm, name)) vars += <nm, name>;
vars += introducedVars(pattern, c);
}
case (Pattern) `<Type _> <Name name> : <Pattern pattern>`: {
nm = prettyPrintName(name);
if(nm[0] != "_") vars += nm;
if(nm[0] != "_") vars += <nm, name>;
vars += introducedVars(pattern, c);
}
case (Pattern) `<Pattern expression> ( <{Pattern ","}* arguments> <KeywordArguments[Pattern] keywordArguments> )`: {
Expand All @@ -812,8 +817,8 @@ void collect(current: (Expression) `<Expression lhs> || <Expression rhs>`, 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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1232,6 +1232,7 @@ void collect(current: (Statement) `<Type varType> <{Variable ","}+ variables>;`,
c.report(error(initial, "Right-hand side of assignment does not always have a value"));
}
}
storeDisallowUseOfDef(var.name, initial, c);

@toinehartman toinehartman Dec 2, 2026 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Prohibit int i = i; in functions

c.enterLubScope(var);
c.require("initialization of `<var.name>`", initial, [initial, varType], makeVarInitRequirement(var));
collect(initial, c);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;}");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 (\<int i, int j\> := \<i, j\>) {
x = i + j;
}
}
");
test bool If12() = undeclaredVariable("
void f(int i) {
if (\<int i, int j\> := \<i, j\>) {
x = i + j;
}
}
");
test bool If13() = undeclaredVariable("
void f(int j) {
if (\<int i, int j\> := \<i, j\>) {
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; };");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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\> == {};");
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 + \"/\<name\>\", exists(f)) {
return f;
}
throw PathNotFound({dir + \"/\<name\>\" | dir \<- path});
}
");
Original file line number Diff line number Diff line change
Expand Up @@ -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;
<found, pt, ms> = getModuleParseTree(moduleName2moduleId(moduleName), ms);
map[str,list[loc]] names = ();
map[loc, str] loc2name = ();
Expand All @@ -224,13 +225,13 @@ bool validateUseDefs(str moduleName, map[str, tuple[int, set[int]]] defuses, Mod
loc2name[nm.src] = "<nm>";
}
}
println("names:"); iprintln(names);
// println("names:"); iprintln(names);
<found, tm, ms> = getTModelForModule(moduleName, ms);
foundUseDefs = tm.useDef;
println("foundUseDefs:");
for(<u, d> <- foundUseDefs){
println("<readFile(u)>:<u> ==\> <d>");
}
// println("foundUseDefs:");
// for(<u, d> <- foundUseDefs){
// println("<readFile(u)>:<u> ==\> <d>");
// }

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Disabled debug printing

for(u <- domain(foundUseDefs)){
defs = foundUseDefs[u];
if(size(defs) != 1){
Expand All @@ -241,19 +242,24 @@ bool validateUseDefs(str moduleName, map[str, tuple[int, set[int]]] defuses, Mod
<def, uses> = defuses[v];
list[loc] occ = [];
if(names[v]?) occ = names[v]; else throw "Name <v> not found in tree";
if(!occ[def]?) throw "Missing define <def> for name <v>";
if(!occ[def]?) {
println("Missing define <def> for name <v>");

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Print all failure reasons and return false, instead of throwing on the first assertion

result = false;
}
for(int u <- uses){
println("u = <u>");
// println("u = <u>");
if(!occ[u]?){
throw "Missing use <u> for <v>";
println("Missing use <u> for <v>");
result = false;
}
potentialDefs = foundUseDefs[occ[u]];
if(isEmpty(potentialDefs) || !any(d <- potentialDefs, occ[def] == (tm.logical2physical[d] ? d))){
throw "Missing def for use <u> of <v>";
println("Missing def for use <u> of <v>");
result = false;
}
}
}
return true;
return result;
}

bool useDefOK(str moduleText, map[str, tuple[int, set[int]]] defuses, PathConfig pathConfig = pathConfigForTesting()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down
Loading
Loading