gccgo: does not detect missing return
dvyukov opened this issue · comments
gccgo successfully compiles the following program:
package a
func f() int { for{}; _:}
while both gc and go/types reject it with missing return
.
gcc version 6.0.0 2015070 (experimental) (GCC)
This is happening because Gogo::add_label_definition just returns if the label name is "_".
Hmm, I'm missing some subtlety here. The reason why this program isn't rejected is because f is treated as if it were defined as func f() int { for{}; }
. I agree that this does not have a return statement and an error should be returned. However, gc accepts the above simplification. Is the above program valid? If it is, why does it matter if we completely ignore sink labels?
The rule in the spec is that it's OK to omit a return statement if the last statement in the function is a terminating statement. "for{}" is a terminating statement--execution will never proceed past that point. "_:" is not a terminating statement--it's a label, and in principle somebody might goto that label, even though obviously that can't happen in this case.
Does that answer the question?
@paranoiacblack It's a "Labeled Statement" where the statement is the empty statement. So there's actually a non-terminating (empty statement) at the end.
CL https://golang.org/cl/12043 mentions this issue.