Thread overview | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
|
August 26, 2005 compiler does not complain no required return value | ||||
---|---|---|---|---|
| ||||
Instead, the generated binary will abort at run-time and give misleading message: int f() { return 0; // gdc Error: AssertError Failure rtn.d(3); dmd Aborted } int main(char[][] args) { f(); return 0; } |
August 26, 2005 Re: compiler does not complain no required return value | ||||
---|---|---|---|---|
| ||||
Posted in reply to z | >int f() { > // return 0; // gdc Error: AssertError Failure rtn.d(3); dmd Aborted ---^^ need to comment out line 3, to see the error. >} > >int main(char[][] args) { >f(); >return 0; >} |
August 26, 2005 Re: compiler does not complain no required return value | ||||
---|---|---|---|---|
| ||||
Posted in reply to z | On Fri, 26 Aug 2005 07:18:51 +0000 (UTC), z@gg.com wrote: > Instead, the generated binary will abort at run-time and give misleading message: > > int f() { > return 0; // gdc Error: AssertError Failure rtn.d(3); dmd Aborted > } > > int main(char[][] args) { > f(); > return 0; > } This is not a bug. If you define a routine to return a value, but you don't actually return anything, the function will crash at run time. The compiler does not attempt to detect this situation, instead it generates a hidden assert at the end of the function. -- Derek (skype: derek.j.parnell) Melbourne, Australia 26/08/2005 6:04:14 PM |
August 26, 2005 Re: compiler does not complain no required return value | ||||
---|---|---|---|---|
| ||||
Posted in reply to Derek Parnell | Derek Parnell wrote: > On Fri, 26 Aug 2005 07:18:51 +0000 (UTC), z@gg.com wrote: > >> Instead, the generated binary will abort at run-time and give misleading >> message: >> >> int f() { [implemented correction] >> // return 0; // gdc Error: AssertError Failure rtn.d(3); dmd Aborted >> } >> >> int main(char[][] args) { >> f(); >> return 0; >> } > > This is not a bug. Nonsense. http://www.digitalmars.com/d/statement.html#return "At least one return statement is required if the function specifies a return type that is not void." Stewart. -- -----BEGIN GEEK CODE BLOCK----- Version: 3.1 GCS/M d- s:- a->--- UB@ P+ L E@ W++@ N+++ o K- w++@ O? M V? PS- PE- Y? PGP- t- 5? X? R b DI? D G e++>++++ h-- r-- !y ------END GEEK CODE BLOCK------ My e-mail is valid but not my primary mailbox. Please keep replies on the 'group where everyone may benefit. |
August 26, 2005 Re: compiler does not complain no required return value | ||||
---|---|---|---|---|
| ||||
Posted in reply to Stewart Gordon | On Fri, 26 Aug 2005 12:09:41 +0100, Stewart Gordon wrote: > Derek Parnell wrote: >> On Fri, 26 Aug 2005 07:18:51 +0000 (UTC), z@gg.com wrote: >> >>> Instead, the generated binary will abort at run-time and give misleading message: >>> >>> int f() { > [implemented correction] >>> // return 0; // gdc Error: AssertError Failure rtn.d(3); dmd Aborted >>> } >>> >>> int main(char[][] args) { >>> f(); >>> return 0; >>> } >> >> This is not a bug. > > Nonsense. > > http://www.digitalmars.com/d/statement.html#return > > "At least one return statement is required if the function specifies a return type that is not void." But 'required' by what? I suspect it is the run-time application that requires it rather than the compiler itself. -- Derek Parnell Melbourne, Australia 27/08/2005 12:27:36 AM |
August 26, 2005 Re: compiler does not complain no required return value | ||||
---|---|---|---|---|
| ||||
Posted in reply to Derek Parnell | Derek Parnell wrote: > On Fri, 26 Aug 2005 12:09:41 +0100, Stewart Gordon wrote: <snip> >> http://www.digitalmars.com/d/statement.html#return >> >> "At least one return statement is required if the function specifies a >> return type that is not void." > > But 'required' by what? The D language. And hence any correct D compiler. > I suspect it is the run-time application that > requires it rather than the compiler itself. If that's so, then you could equally state that "The body of all asserts is required to evaluate to non-zero." By which the average person is likely to interpret what? Stewart. -- -----BEGIN GEEK CODE BLOCK----- Version: 3.1 GCS/M d- s:- a->--- UB@ P+ L E@ W++@ N+++ o K- w++@ O? M V? PS- PE- Y? PGP- t- 5? X? R b DI? D G e++>++++ h-- r-- !y ------END GEEK CODE BLOCK------ My e-mail is valid but not my primary mailbox. Please keep replies on the 'group where everyone may benefit. |
August 26, 2005 Re: compiler does not complain no required return value | ||||
---|---|---|---|---|
| ||||
Posted in reply to Stewart Gordon | In article <dengj7$1is0$1@digitaldaemon.com>, Stewart Gordon says... > >Derek Parnell wrote: >> On Fri, 26 Aug 2005 12:09:41 +0100, Stewart Gordon wrote: ><snip> >>> http://www.digitalmars.com/d/statement.html#return >>> >>> "At least one return statement is required if the function specifies a return type that is not void." >> >> But 'required' by what? > >The D language. And hence any correct D compiler. > >> I suspect it is the run-time application that >> requires it rather than the compiler itself. > >If that's so, then you could equally state that > >"The body of all asserts is required to evaluate to non-zero." > As long as your not talking about the compiler checking every possible path for a return statement (something that probably no compiler has ever gotten 100% right anyhow), then I would have to agree that the compiler should at least check for a return statement inside the body of a function. Take a look in the archives - IMO, Walter has good reasoning for the compiler not forcing a return for all paths. But checking that a function actually has a valid return statement if it's expected to return something should be part of the reference compiler because 1) it will save programmers a lot of "oopses", 2) it's gotta be readily do-able to verify 100% of the time and 3) the spec. above really is unequivocal on the matter. - Dave >By which the average person is likely to interpret what? > >Stewart. > >-- >-----BEGIN GEEK CODE BLOCK----- >Version: 3.1 >GCS/M d- s:- a->--- UB@ P+ L E@ W++@ N+++ o K- w++@ O? M V? PS- PE- Y? >PGP- t- 5? X? R b DI? D G e++>++++ h-- r-- !y >------END GEEK CODE BLOCK------ > >My e-mail is valid but not my primary mailbox. Please keep replies on the 'group where everyone may benefit. |
August 26, 2005 Re: compiler does not complain no required return value | ||||
---|---|---|---|---|
| ||||
Posted in reply to Derek Parnell | >> return 0; // gdc Error: AssertError Failure rtn.d(3); dmd Aborted >The compiler does not >attempt to detect this situation, instead it generates a hidden assert at >the end of the function. *EVEN* in that case, a more meaningfull message should be generated, e.g. "NoReturnValueAssertError" Which is much more human readable. |
August 26, 2005 Re: compiler does not complain no required return value | ||||
---|---|---|---|---|
| ||||
Posted in reply to z | z@gg.com wrote: >>>return 0; // gdc Error: AssertError Failure rtn.d(3); dmd Aborted > *EVEN* in that case, a more meaningfull message should be generated, e.g. > > "NoReturnValueAssertError" > > Which is much more human readable. > > > When you compile with -w the compiler will let you know of missing return statements: warning - <filename>(<line #>): function <function name> no return at end of function. Walter added this as a concession after much debate over the default behaviour. So if you want to catch these cases at compile time, use -w and be happy. |
Copyright © 1999-2021 by the D Language Foundation