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

return; in functions with explicit Void return type #518

Closed
zygoloid opened this issue May 4, 2021 · 4 comments
Closed

return; in functions with explicit Void return type #518

zygoloid opened this issue May 4, 2021 · 4 comments
Labels
leads question A question for the leads team

Comments

@zygoloid
Copy link
Contributor

zygoloid commented May 4, 2021

We should consider one change relative to the C++ rules for return; / return expression;.

In C++ and in #415, either form can be used in a function with a Void return type. The proposed alternative rule is:

  • If the function specifies a return type (eg, fn F() -> Int { or fn F() -> Void {), then every return statement must have an expression, and control reaching the end of the function is a bug (without meaning to specify here what the response to the bug is -- UB or a diagnostic or a trap or something).
  • If the function does not specify a return type (eg, fn F() {), then every return statement (if any) must be of the form return;, and control reaching the end of the function implicitly returns.

This makes the handling of Void uniform, removing special cases, while still supporting both return; in the "functions that don't return a value" case and also return expression; in a generic context even when the return type ends up being Void.

@zygoloid zygoloid mentioned this issue May 4, 2021
@fowles
Copy link

fowles commented May 4, 2021 via email

@zygoloid
Copy link
Contributor Author

zygoloid commented May 4, 2021

What about the current structure for must_tail which requires return foo() for void tail calls?

If we were to exactly follow the design of the Clang attribute, this would mean that function that contain a guaranteed tail call would need to be written with an explicit return type -- fn FThatMakesATailCall() -> Void { -- instead of with an omitted return type. But I think there are lots of assumptions here, and we might end up with a completely different syntax for guaranteed tail calls in Carbon -- assuming we have them at all.

@chandlerc
Copy link
Contributor

Agreed with the suggested direction. We might want to restrict further or tweak, but this is a good start.

and we might end up with a completely different syntax for guaranteed tail calls in Carbon -- assuming we have them at all

FWIW, I would like tail calls, but would specifically prefer a different syntax. Anyways, not something we need to stress about today.

@zygoloid
Copy link
Contributor Author

Decision: Make the proposed change.

@jonmeow jonmeow added the leads question A question for the leads team label Aug 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
leads question A question for the leads team
Projects
None yet
Development

No branches or pull requests

4 participants