How can this "function ends without a return statement" be right?

1,528 views
Skip to first unread message

josvazg

unread,
Nov 15, 2011, 1:39:17 AM11/15/11
to golang-nuts
I have a couple of functions that look like this:

func somefunc(saddr string) (FinalResultType, os.Error) {
if result, err := doSomeCalculation(); e != nil {
return nil, e
} else {
return wrapIt(result), nil
}
// return nil,nil
}

Even though the third and last return (I believe) IS DEAD CODE, If I
don't include it (I left it commented as in the sample) I get the
following error:

"function ends without a return statement"

Which I think it is a plain lie! (maybe a bug) cause it either returns
on the IF clause or the ELSE clause, there IS NO WAY it can go through
a path with NO return statement... or is there?

What I am missing here?

Miek Gieben

unread,
Nov 15, 2011, 1:46:31 AM11/15/11
to josvazg, Go list
[ Quoting <jos...@gmail.com> at 22:39 on Nov 14 in "[go-nuts] How can th..." ]

> I have a couple of functions that look like this:
>
> func somefunc(saddr string) (FinalResultType, os.Error) {
> if result, err := doSomeCalculation(); e != nil {
> return nil, e
> } else {
> return wrapIt(result), nil
> }
> // return nil,nil
> }
>
> Even though the third and last return (I believe) IS DEAD CODE, If I
> don't include it (I left it commented as in the sample) I get the
> following error:
>
> "function ends without a return statement"

Its a compiler bug, make the function look like:

func somefunc(saddr string) (FinalResultType, os.Error) {
if result, err := doSomeCalculation(); e != nil {
return nil, e
}

return wrapIt(result), nil
}


grtz,

--
Miek

signature.asc

Rob 'Commander' Pike

unread,
Nov 15, 2011, 1:49:52 AM11/15/11
to josvazg, golang-nuts

The compilers require either a return or a panic to be lexically last in a function with a result. This rule is easier than requiring full flow control analysis to determine whether a function reaches the end without returning (which is very hard in general), and simpler than rules to enumerate easy cases such as this one. Also, being purely lexical, the error cannot arise spontaneously due to changes in values such as constants used in control structures inside the function.

-rob

Rob 'Commander' Pike

unread,
Nov 15, 2011, 1:50:18 AM11/15/11
to Miek Gieben, josvazg, Go list

On Nov 14, 2011, at 10:46 PM, Miek Gieben wrote:

> [ Quoting <jos...@gmail.com> at 22:39 on Nov 14 in "[go-nuts] How can th..." ]
>> I have a couple of functions that look like this:
>>
>> func somefunc(saddr string) (FinalResultType, os.Error) {
>> if result, err := doSomeCalculation(); e != nil {
>> return nil, e
>> } else {
>> return wrapIt(result), nil
>> }
>> // return nil,nil
>> }
>>
>> Even though the third and last return (I believe) IS DEAD CODE, If I
>> don't include it (I left it commented as in the sample) I get the
>> following error:
>>
>> "function ends without a return statement"
>
> Its a compiler bug, make the function look like:

It's not a bug, it's a deliberate design decision.

-rob


josvazg

unread,
Nov 15, 2011, 2:26:44 AM11/15/11
to golang-nuts
Ok, but then I need to define the result outside the if clause like
this:

func somefunc(saddr string) (FinalResultType, os.Error) {
result, err := doSomeCalculation();
if e != nil {
return nil, e
}
return wrapIt(result), nil
}

Otherwise the result scope is lost befor ethe last return statement

On Nov 15, 7:46 am, Miek Gieben <m...@miek.nl> wrote:
> [ Quoting <josv...@gmail.com> at 22:39 on Nov 14 in "[go-nuts] How can th..." ]
>  signature.asc
> < 1KViewDownload

josvazg

unread,
Nov 15, 2011, 2:33:08 AM11/15/11
to golang-nuts
Ok, Thanks!

I will take this into account from now on.
I guess it's on the docs (probably the Specs) but I failed to remember
it...

Dave Cheney

unread,
Nov 15, 2011, 2:35:07 AM11/15/11
to josvazg, golang-nuts
Hi

This is a long standing request,

http://code.google.com/p/go/issues/detail?id=65

It's generally possible to work around the issue by removing the else clause and let the logic fall through to a return at the bottom of the function.

Sent from my iPad

Miek Gieben

unread,
Nov 15, 2011, 3:25:03 AM11/15/11
to Go list
[ Quoting <r...@google.com> at 22:50 on Nov 14 in "Re: [go-nuts] How ca..." ]

> > Its a compiler bug, make the function look like:
>
> It's not a bug, it's a deliberate design decision.

Ah, I still thought it was considered a bug. So
issue 65 can be closed? (just asking)

grtz Miek

signature.asc

Josef Johansson

unread,
Nov 15, 2011, 6:59:03 PM11/15/11
to Go list
Hi,

If you define the variables in the in the first line, you'll keep the
nice formatting along with the scope.

func somefunc(saddr string) (result FinalResultType, err os.Error) {
   if result, err = doSomeCalculation(); err != nil {
return nil, err       }         return wrapIt(result), nil}

Regards
Josef

Reply all
Reply to author
Forward
0 new messages