Repository navigation
Fix self-referential variable initialization and pattern match #3028
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
f924031
19c57e9
50be0ac
2a05808
13c38e6
0c1c0a2
a09b328
15304d2
1cb4adc
e9f6a2b
540dc53
97a9a7f
17bbc6f
df9ced3
3658029
34061bb
99ca997
7b75180
fd67dd7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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); | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Prohibit |
||
| c.require("initialization of `<vname>`", initial, [initial, varType], makeVarInitRequirement(var)); | ||
| collect(initial, c); | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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); | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Prohibit |
||
| } | ||
| collect(expression, c); | ||
| } | ||
|
|
||
|
|
@@ -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){ | ||
|
|
@@ -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> )`: { | ||
|
|
@@ -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); | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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); | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Prohibit |
||
| c.enterLubScope(var); | ||
| c.require("initialization of `<var.name>`", initial, [initial, varType], makeVarInitRequirement(var)); | ||
| collect(initial, c); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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 = (); | ||
|
|
@@ -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>"); | ||
| // } | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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){ | ||
|
|
@@ -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>"); | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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()) { | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.