Skip to content

Commit

Permalink
spec: load statements may not be nested in any other statement (#276)
Browse files Browse the repository at this point in the history
This is a potentially breaking language change, but I suspect
few users care. If you are one of them, please report the issue.

Fixes #275
  • Loading branch information
adonovan authored Jun 9, 2020
1 parent 0aa9569 commit cd131d1
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 2 deletions.
2 changes: 1 addition & 1 deletion doc/spec.md
Original file line number Diff line number Diff line change
Expand Up @@ -2945,7 +2945,7 @@ load("module.star", "x", "y", "z") # assigns x, y, and z
load("module.star", "x", y2="y", "z") # assigns x, y2, and z
```
A load statement within a function is a static error.
A load statement may not be nested inside any other statement.
## Module execution
Expand Down
10 changes: 9 additions & 1 deletion resolve/resolve.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,8 @@ type resolver struct {
// isGlobal may be nil.
isGlobal, isPredeclared, isUniversal func(name string) bool

loops int // number of enclosing for loops
loops int // number of enclosing for/while loops
ifstmts int // number of enclosing if statements loops

errors ErrorList
}
Expand Down Expand Up @@ -497,8 +498,10 @@ func (r *resolver) stmt(stmt syntax.Stmt) {
r.errorf(stmt.If, "if statement not within a function")
}
r.expr(stmt.Cond)
r.ifstmts++
r.stmts(stmt.True)
r.stmts(stmt.False)
r.ifstmts--

case *syntax.AssignStmt:
r.expr(stmt.RHS)
Expand Down Expand Up @@ -551,8 +554,13 @@ func (r *resolver) stmt(stmt syntax.Stmt) {
}

case *syntax.LoadStmt:
// A load statement may not be nested in any other statement.
if r.container().function != nil {
r.errorf(stmt.Load, "load statement within a function")
} else if r.loops > 0 {
r.errorf(stmt.Load, "load statement within a loop")
} else if r.ifstmts > 0 {
r.errorf(stmt.Load, "load statement within a conditional")
}

for i, from := range stmt.From {
Expand Down
15 changes: 15 additions & 0 deletions resolve/testdata/resolve.star
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,21 @@ load("foo",
c="_d", ### "load: names with leading underscores are not exported: _d"
_e="f") # ok

---
# option:globalreassign
if M:
load("foo", "bar") ### "load statement within a conditional"

---
# option:globalreassign
for x in M:
load("foo", "bar") ### "load statement within a loop"

---
# option:recursion option:globalreassign
while M:
load("foo", "bar") ### "load statement within a loop"

---
# return statements must be within a function

Expand Down

0 comments on commit cd131d1

Please sign in to comment.