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

go/types: incorrect error about missing return #11698

Closed
dvyukov opened this issue Jul 13, 2015 · 7 comments
Closed

go/types: incorrect error about missing return #11698

dvyukov opened this issue Jul 13, 2015 · 7 comments
Assignees
Milestone

Comments

@dvyukov
Copy link
Member

@dvyukov dvyukov commented Jul 13, 2015

gc successfully compiles the following program:

package a
func f() int {
    for {};
    ;
}

while gotype says:

go.go:5: missing return at end of function

go version devel +9b04852 Sat Jul 11 00:08:50 2015 +0000 linux/amd64

@dvyukov
Copy link
Member Author

@dvyukov dvyukov commented Jul 13, 2015

Loading

@mdempsky
Copy link
Member

@mdempsky mdempsky commented Jul 13, 2015

I think the Go spec unambiguously supports the gotype error. Functions with result parameters need to end in a terminating statement, and empty statements are not terminating statements.

Loading

@ianlancetaylor ianlancetaylor added this to the Go1.6 milestone Jul 13, 2015
@rsc rsc added this to the Unplanned milestone Dec 5, 2015
@rsc rsc removed this from the Go1.6 milestone Dec 5, 2015
@odeke-em
Copy link
Member

@odeke-em odeke-em commented Oct 3, 2016

Interestingly if we change the body of that function from for {} to add in conditions ie for true {} or for i := 0; i < 10; i++ {} the compiler catches the missing return https://play.golang.org/p/tMrrNlXJI6 or inlined here

package main

import "fmt"

func f() int {
    for {
    }
}

func f1() int {
    for true {
    }
}

func main() {
    fmt.Printf("hello world")
}
$ tmp/sandbox109964380/main.go:13: missing return at end of function

Loading

@extemporalgenome
Copy link
Contributor

@extemporalgenome extemporalgenome commented Oct 3, 2016

Per https://golang.org/ref/spec#Terminating_statements these programs contain a for loop without a loop condition or break statement, therefore the for loop is a terminating statement. These programs are thus valid and gotype is failing to detect the terminating statement?

Loading

@minux
Copy link
Member

@minux minux commented Oct 3, 2016

Isn't that this is working as intended for cmd/gc, and a bug
for gotype?

The for loop doesn't terminate, so the function won't ever
return, thus there is no need for a return.

Loading

@griesemer
Copy link
Contributor

@griesemer griesemer commented Oct 3, 2016

This is now a go/types error. The relevant lines from the spec are:

"If the function's signature declares result parameters, the function body's statement list must end in a terminating statement." (https://tip.golang.org/ref/spec#Function_declarations)

"A statement list ends in a terminating statement if the list is not empty and its final non-empty statement is terminating." (https://golang.org/ref/spec#Terminating_statements, end of paragraph).

Thus:

  1. The function must end in a terminating statement since it declares a result; and
  2. The final non-empty statement is the for loop which neither has a loop condition nor a break statement and thus is terminating.

@mdempsky was correct, too, because in 2015 this was still a gc bug :-) The relevant sentence in the spec changed in 2016 (b5ddbb9).

Loading

@griesemer griesemer added this to the Go1.8 milestone Oct 3, 2016
@griesemer griesemer removed this from the Unplanned milestone Oct 3, 2016
@griesemer griesemer assigned griesemer and unassigned rsc Oct 3, 2016
@griesemer griesemer changed the title cmd/compile: does not detect a missing return go/types: incorrect error about missing return Oct 3, 2016
@griesemer
Copy link
Contributor

@griesemer griesemer commented Oct 5, 2016

With the spec change, this became a duplicate of #14537 which has been fixed. Not-reproducible anymore. Closing.

Loading

@griesemer griesemer closed this Oct 5, 2016
@golang golang locked and limited conversation to collaborators Oct 5, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
9 participants