Skip to content
This repository was archived by the owner on Mar 5, 2025. It is now read-only.

Commit 67cf77c

Browse files
committed
Improved handling of || expressions
This solves false "Unused variable" warnings of variables introduced in right-hand side
1 parent 59967d4 commit 67cf77c

File tree

5 files changed

+36
-26
lines changed

5 files changed

+36
-26
lines changed
Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
eclipse.preferences.version=1
22
encoding//src/org/rascalmpl/core/library=UTF-8
3-
encoding//target/generated-test-resources=UTF-8
4-
encoding//target/generated-test-sources=UTF-8
53
encoding/<project>=UTF-8
64
encoding/src=UTF-8
75
encoding/test=UTF-8

src/org/rascalmpl/core/library/lang/rascalcore/check/BasicRascalConfig.rsc

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,14 @@ data PathRole
5151
;
5252

5353
data ScopeRole
54-
= moduleScope()
55-
| functionScope()
56-
| conditionalScope()
54+
= //moduleScope()
55+
functionScope()
56+
//| conditionalScope()
5757
| replacementScope()
5858
| visitOrSwitchScope()
59-
| boolScope()
59+
//| boolScope()
6060
| loopScope()
61+
| orScope()
6162
;
6263

6364
data Vis

src/org/rascalmpl/core/library/lang/rascalcore/check/CollectOperators.rsc

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -756,7 +756,8 @@ void collect(current: (Expression) `<Expression lhs> && <Expression rhs>`, Colle
756756
}
757757

758758
private set[str] introducedVars(Expression exp, Collector c){
759-
return exp is match ? introducedVars(exp.pattern, c) : {};
759+
return exp is \bracket ? introducedVars(exp.expression, c)
760+
: (exp is match ? introducedVars(exp.pattern, c) : {});
760761
}
761762

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

794795
// ---- or
795796

797+
data OrInfo = orInfo(set[str] vars);
798+
796799
void collect(current: (Expression) `<Expression lhs> || <Expression rhs>`, Collector c){
797800
c.fact(current, abool());
798801

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

805-
// Check that the names introduced in lhs and rhs are the same
808+
collect(lhs, c);
806809

807810
introLhs = introducedVars(lhs, c);
808-
introRhs = introducedVars(rhs, c);
811+
introRhs = introducedVars(rhs, c);
809812

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

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

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

src/org/rascalmpl/core/library/lang/rascalcore/check/CollectPattern.rsc

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,15 @@ void collect(current: (Pattern) `<Type tp> <Name name>`, Collector c){
7878
&& c.isAlreadyDefined("<name>", name)){
7979
c.use(name, {variableId(), moduleVariableId(), formalId(), nestedFormalId(), patternVariableId()});
8080
} else {
81+
orScopes = c.getScopeInfo(orScope());
82+
for(<_, orInfo(vars)> <- orScopes){
83+
for(str id <- vars, id == uname){
84+
if(c.isAlreadyDefined("<name>", name)){
85+
c.use(name, {variableId(), formalId(), nestedFormalId(), patternVariableId()});
86+
return;
87+
}
88+
}
89+
}
8190
c.define(uname, formalOrPatternFormal(c), name, defType(tpResolved));
8291
}
8392
} else {

src/org/rascalmpl/core/library/lang/rascalcore/compile/Examples/Tst5.rsc

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,5 @@
11
module lang::rascalcore::compile::Examples::Tst5
22

3-
void main(){
4-
previous = "non empty";
5-
msg = "<for (x <- ["a", "b", "c"]) { continue;><x>
6-
'<}>";
7-
}
8-
93
//MH
104
//public void showUsageCounts(Corpus corpus, lrel[str p, str v, QueryResult qr] res) {
115
// mr = ( p : size([ e | <p,_,e> <- res ]) | p <- corpus );
@@ -19,6 +13,13 @@ void main(){
1913
// return -1;
2014
//}
2115

16+
value main(){
17+
if ((j := 1) || (j := 2)) {
18+
return j;
19+
}
20+
return -1;
21+
}
22+
2223

2324
//data Wrapper[&SAME] = something(&SAME wrapped);
2425
//

0 commit comments

Comments
 (0)