Skip to content

Commit

Permalink
Improved handling of || expressions
Browse files Browse the repository at this point in the history
This solves false "Unused variable" warnings of variables introduced in
right-hand side
  • Loading branch information
PaulKlint committed Mar 19, 2024
1 parent 59967d4 commit 67cf77c
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 26 deletions.
2 changes: 0 additions & 2 deletions .settings/org.eclipse.core.resources.prefs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
eclipse.preferences.version=1
encoding//src/org/rascalmpl/core/library=UTF-8
encoding//target/generated-test-resources=UTF-8
encoding//target/generated-test-sources=UTF-8
encoding/<project>=UTF-8
encoding/src=UTF-8
encoding/test=UTF-8
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,14 @@ data PathRole
;

data ScopeRole
= moduleScope()
| functionScope()
| conditionalScope()
= //moduleScope()
functionScope()
//| conditionalScope()
| replacementScope()
| visitOrSwitchScope()
| boolScope()
//| boolScope()
| loopScope()
| orScope()
;

data Vis
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -756,7 +756,8 @@ void collect(current: (Expression) `<Expression lhs> && <Expression rhs>`, Colle
}

private set[str] introducedVars(Expression exp, Collector c){
return exp is match ? introducedVars(exp.pattern, c) : {};
return exp is \bracket ? introducedVars(exp.expression, c)
: (exp is match ? introducedVars(exp.pattern, c) : {});
}

private set[str] introducedVars(Pattern e, Collector c){
Expand Down Expand Up @@ -793,6 +794,8 @@ private set[str] introducedVars(Pattern e, Collector c){

// ---- or

data OrInfo = orInfo(set[str] vars);

void collect(current: (Expression) `<Expression lhs> || <Expression rhs>`, Collector c){
c.fact(current, abool());

Expand All @@ -802,24 +805,22 @@ void collect(current: (Expression) `<Expression lhs> || <Expression rhs>`, Colle
s.requireUnify(abool(), rhs, error(rhs, "Argument of || should be `bool`, found %t", rhs));
});

// Check that the names introduced in lhs and rhs are the same
collect(lhs, c);

introLhs = introducedVars(lhs, c);
introRhs = introducedVars(rhs, c);
introRhs = introducedVars(rhs, c);

collect(lhs, c);
// make common variables available when collecting rhs;
// variables in rhs will use definition from lhs (see CollectPattern: typed variable pattern, qualifiedName pattern)
c.setScopeInfo(c.getScope(), orScope(), orInfo(introLhs));
collect(rhs, c);

// Trick 1: wrap rhs in a separate scope to avoid double declarations with names introduced in lhs
// Trick 2: use "current" as scope (to avoid clash with scope created by rhs)
c.enterScope(lhs);
collect(rhs, c);
c.leaveScope(lhs);
// Check that the names introduced in lhs and rhs are the same
common = introLhs & introRhs;
missing = (introLhs - common) + (introRhs - common);

for(nm <- introLhs){
if(nm notin introRhs) c.report(error(rhs, "Arguments of `||` should introduce same variables, right argument does not introduce `%v`", nm));
}
for(nm <- introRhs){
if(nm notin introLhs) c.report(error(lhs, "Arguments of `||` should introduce same variables, left argument does not introduce `%v`", nm));
if(!isEmpty(missing)){
c.report(error(current, "Variable(s) %v should be introduced on both sides of `||` operator", missing));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,15 @@ void collect(current: (Pattern) `<Type tp> <Name name>`, Collector c){
&& c.isAlreadyDefined("<name>", name)){
c.use(name, {variableId(), moduleVariableId(), formalId(), nestedFormalId(), patternVariableId()});
} else {
orScopes = c.getScopeInfo(orScope());
for(<_, orInfo(vars)> <- orScopes){
for(str id <- vars, id == uname){
if(c.isAlreadyDefined("<name>", name)){
c.use(name, {variableId(), formalId(), nestedFormalId(), patternVariableId()});
return;
}
}
}
c.define(uname, formalOrPatternFormal(c), name, defType(tpResolved));
}
} else {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
module lang::rascalcore::compile::Examples::Tst5

void main(){
previous = "non empty";
msg = "<for (x <- ["a", "b", "c"]) { continue;><x>
'<}>";
}

//MH
//public void showUsageCounts(Corpus corpus, lrel[str p, str v, QueryResult qr] res) {
// mr = ( p : size([ e | <p,_,e> <- res ]) | p <- corpus );
Expand All @@ -19,6 +13,13 @@ void main(){
// return -1;
//}

value main(){
if ((j := 1) || (j := 2)) {
return j;
}
return -1;
}


//data Wrapper[&SAME] = something(&SAME wrapped);
//
Expand Down

0 comments on commit 67cf77c

Please sign in to comment.