Skip to content

[BUG] Bad code is generated for return inside the one-line style function definition #267

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Closed
msadeqhe opened this issue Mar 7, 2023 · 0 comments
Assignees
Labels
bug Something isn't working

Comments

@msadeqhe
Copy link

msadeqhe commented Mar 7, 2023

Describe the bug
A return statement inside the one-line style function definition, generates bad code.

To Reproduce
This is the sample CPP2 code:

#include <iostream>

func1 :() = std::cout << "HI" << "\n";
func2 :() = return;

I've used Compiler Explorer.

It should generate the following C++ code:

auto func1() -> void { std::cout << "HI" << "\n"; }
auto func2() -> void { return; }

But it generates the following C++ code:

auto func1() -> void { std::cout << "HI" << "\n"; }
auto func2() -> voidreturn ;

Additional context
The transpiler is combined the return type void and the statement return of func2, becuase it missed block symbols {} in the generated code.

@msadeqhe msadeqhe added the bug Something isn't working label Mar 7, 2023
@hsutter hsutter self-assigned this Mar 9, 2023
@hsutter hsutter closed this as completed in fb4a9fc Mar 9, 2023
zaucy pushed a commit to zaucy/cppfront that referenced this issue Dec 5, 2023
This is an omnibus commit of the last few evenings' changes. Primarily it was to start laying the groundwork for constructors, but it includes other fixes and closes a few issues.

Details:

- Begin infrastructure preparation for constructors

- Started creating navigation APIs to replace the low-level graph node chasing; this makes cppfront's own code cleaner and the tree easier to change if needed, but it's also a step toward a reflection API

- Extended `main:(args)` to require the name "args" for the single-parameter `main`, and to support `args.argc` and `args.argv`, further on hsutter#262 (see comment thread)

- Changed default return type for unnamed functions to be `-> void`, same as named functions, closes hsutter#257

- Disallow single-expression function body to be just `return`, closes hsutter#267

- Make `make_args` inline, closes hsutter#268

- Bug fix: emit prolog also for single-expression function body. Specifically, this enables main:(args)=expression; to work correctly. Generally, this also corrects the code gen for examples that were trying (but failing) to inject prologs in single-expression functions... in the regression tests this corrected the code gen for `pure2-forward-return.cpp2` which was missing the contract check before.
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants