Skip to content
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

implement naked returns #189

Merged
merged 2 commits into from
Jan 7, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
44 changes: 44 additions & 0 deletions eval/testdata/func10.ng
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
func f1() (i, j int) { return }

if v1, v2 := f1(); v1 != 0 || v2 != 0 {
panic("ERROR 1")
}

func f2() (i, j int) {
i = 1
j = 2
return
}

if v1, v2 := f2(); v1 != 1 || v2 != 2 {
panic("ERROR 2")
}

func f3(i int) (v int) {
if i == 0 {
return
}
if i == 1 {
v = 1
return
}
if i == 2 {
v = 2
return v
}
return
}

if v := f3(0); v != 0 {
panic("ERROR 3.1")
}

if v := f3(1); v != 1 {
panic("ERROR 3.2")
}

if v := f3(2); v != 2 {
panic("ERROR 3.3")
}

print("OK")
1 change: 1 addition & 0 deletions eval/testdata/func11_error.ng
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
func f() (i, j, k int) { return 1, 2 } // ERROR: not enough arguments to return
64 changes: 38 additions & 26 deletions typecheck/typecheck.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ func IsError(t tipe.Type) bool {
return Universe.Objs["error"].Type == t
}

func (c *Checker) stmt(s stmt.Stmt, retType *tipe.Tuple) tipe.Type {
func (c *Checker) stmt(s stmt.Stmt, retType *tipe.Tuple, retNames []string) tipe.Type {
switch s := s.(type) {
case *stmt.ConstSet:
for _, v := range s.Consts {
Expand Down Expand Up @@ -277,7 +277,7 @@ func (c *Checker) stmt(s stmt.Stmt, retType *tipe.Tuple) tipe.Type {
c.pushScope()
defer c.popScope()
for _, s := range s.Stmts {
c.stmt(s, retType)
c.stmt(s, retType, retNames)
}
return nil

Expand All @@ -289,28 +289,28 @@ func (c *Checker) stmt(s stmt.Stmt, retType *tipe.Tuple) tipe.Type {
if s.Init != nil {
c.pushScope()
defer c.popScope()
c.stmt(s.Init, retType)
c.stmt(s.Init, retType, retNames)
}
c.expr(s.Cond)
c.stmt(s.Body, retType)
c.stmt(s.Body, retType, retNames)
if s.Else != nil {
c.stmt(s.Else, retType)
c.stmt(s.Else, retType, retNames)
}
return nil

case *stmt.For:
if s.Init != nil {
c.pushScope()
defer c.popScope()
c.stmt(s.Init, retType)
c.stmt(s.Init, retType, retNames)
}
if s.Cond != nil {
c.expr(s.Cond)
}
if s.Post != nil {
c.stmt(s.Post, retType)
c.stmt(s.Post, retType, retNames)
}
c.stmt(s.Body, retType)
c.stmt(s.Body, retType, retNames)
return nil

case *stmt.Range:
Expand Down Expand Up @@ -365,7 +365,7 @@ func (c *Checker) stmt(s stmt.Stmt, retType *tipe.Tuple) tipe.Type {
c.types[s.Val] = vt
}
}
c.stmt(s.Body, retType)
c.stmt(s.Body, retType, retNames)
return nil

case *stmt.TypeDecl:
Expand All @@ -383,7 +383,7 @@ func (c *Checker) stmt(s stmt.Stmt, retType *tipe.Tuple) tipe.Type {

case *stmt.TypeDeclSet:
for _, s := range s.TypeDecls {
c.stmt(s, retType)
c.stmt(s, retType, retNames)
}
return nil

Expand Down Expand Up @@ -453,13 +453,23 @@ func (c *Checker) stmt(s stmt.Stmt, retType *tipe.Tuple) tipe.Type {
c.errorfmt("too many arguments to return")
return nil
case len(s.Exprs) < len(retType.Elems):
c.errorfmt("not enough arguments to return")
return nil
if !(len(s.Exprs) == 0 && len(retNames) == len(retType.Elems)) {
c.errorfmt("not enough arguments to return")
return nil
}
}
var partials []partial
for i, e := range s.Exprs {
partials = append(partials, c.expr(e))
c.constrainUntyped(&partials[i], retType.Elems[i])
switch len(s.Exprs) {
case 0:
partials = make([]partial, len(retType.Elems))
for i, t := range retType.Elems {
c.constrainUntyped(&partials[i], t)
}
default:
for i, e := range s.Exprs {
partials = append(partials, c.expr(e))
c.constrainUntyped(&partials[i], retType.Elems[i])
}
}
for _, p := range partials {
if p.mode == modeInvalid {
Expand Down Expand Up @@ -543,14 +553,14 @@ func (c *Checker) stmt(s stmt.Stmt, retType *tipe.Tuple) tipe.Type {
return nil

case *stmt.Labeled:
c.stmt(s.Stmt, retType)
c.stmt(s.Stmt, retType, retNames)
return nil

case *stmt.Switch:
if s.Init != nil {
c.pushScope()
defer c.popScope()
c.stmt(s.Init, retType)
c.stmt(s.Init, retType, retNames)
}
var typ tipe.Type = tipe.Bool
if s.Cond != nil {
Expand Down Expand Up @@ -595,23 +605,23 @@ func (c *Checker) stmt(s stmt.Stmt, retType *tipe.Tuple) tipe.Type {
c.constrainUntyped(&p, typ)
}
}
c.stmt(cse.Body, retType)
c.stmt(cse.Body, retType, retNames)
}
return nil

case *stmt.TypeSwitch:
if s.Init != nil {
c.pushScope()
defer c.popScope()
c.stmt(s.Init, retType)
c.stmt(s.Init, retType, retNames)
}
c.pushScope()
defer c.popScope()
if s.Assign == nil {
c.errorfmt("type switch needs a type switch guard")
return nil
}
c.stmt(s.Assign, retType)
c.stmt(s.Assign, retType, retNames)
var (
e *expr.TypeAssert
id expr.Expr
Expand Down Expand Up @@ -667,7 +677,7 @@ func (c *Checker) stmt(s stmt.Stmt, retType *tipe.Tuple) tipe.Type {
)
}
}
c.stmt(cse.Body, retType)
c.stmt(cse.Body, retType, retNames)
}
return nil

Expand Down Expand Up @@ -716,9 +726,9 @@ func (c *Checker) stmt(s stmt.Stmt, retType *tipe.Tuple) tipe.Type {
if cse.Stmt != nil {
c.pushScope()
defer c.popScope()
c.stmt(cse.Stmt, retType)
c.stmt(cse.Stmt, retType, retNames)
}
c.stmt(cse.Body, retType)
c.stmt(cse.Body, retType, retNames)
}(&cse)
}
return nil
Expand Down Expand Up @@ -1292,7 +1302,7 @@ func (c *Checker) parseFile(filename string, source []byte) error {
c.curPkg.Syntax = f

for _, s := range f.Stmts {
c.stmt(s, nil)
c.stmt(s, nil, nil)
if len(c.errs) > 0 {
return c.errs[0]
}
Expand Down Expand Up @@ -2086,12 +2096,14 @@ func (c *Checker) exprPartial(e expr.Expr, hint typeHint) (p partial) {
}
}
}
var retNames []string
if e.Type.Results != nil {
for i, t := range e.Type.Results.Elems {
e.Type.Results.Elems[i], _ = c.resolve(t)
}
for i, rname := range e.ResultNames {
if rname != "" {
retNames = append(retNames, rname)
t := e.Type.Results.Elems[i]
c.addObj(&Obj{
Name: rname,
Expand All @@ -2102,7 +2114,7 @@ func (c *Checker) exprPartial(e expr.Expr, hint typeHint) (p partial) {
}
}
}
c.stmt(e.Body.(*stmt.Block), e.Type.Results)
c.stmt(e.Body.(*stmt.Block), e.Type.Results, retNames)
for _, pname := range e.ParamNames {
delete(c.cur.foundInParent, pname)
}
Expand Down Expand Up @@ -3468,7 +3480,7 @@ func round(v constant.Value, t tipe.Basic) constant.Value {
func (c *Checker) Add(s stmt.Stmt) tipe.Type {
c.mu.Lock()
defer c.mu.Unlock()
return c.stmt(s, nil)
return c.stmt(s, nil, nil)
}

func (c *Checker) Lookup(name string) *Obj {
Expand Down