Skip to content

cgen: fix codegen for return on last statement of return IfExpr - #23551

Merged
spytheman merged 1 commit into
vlang:masterfrom
felipensp:fix_multi_return_on_if_stmt
Jan 23, 2025
Merged

spytheman merged 1 commit into
vlang:masterfrom
felipensp:fix_multi_return_on_if_stmt

Conversation

@felipensp

Copy link
Copy Markdown
Member

Fix #23550

@huly-for-github

Copy link
Copy Markdown

Connected to Huly®: V_0.6-21979

@felipensp felipensp changed the title cgen: fix multi return on if expr cgen: fix codegen for return on last statement of return IfExpr Jan 22, 2025
@JalonSolov

Copy link
Copy Markdown
Collaborator

So you're just making sure the return is output, rather than warning/error?

@felipensp

Copy link
Copy Markdown
Member Author

So you're just making sure the return is output, rather than warning/error?

Yes. Just like we do in return match foo { a { return expr } else { ... } }

@JalonSolov

Copy link
Copy Markdown
Collaborator

I think that's a mistake as well. A return inside a return makes no sense.

@felipensp
felipensp marked this pull request as ready for review January 22, 2025 20:10
@spytheman

Copy link
Copy Markdown
Contributor

I think that's a mistake as well. A return inside a return makes no sense.

It does make sense a bit, since in the if cond { expr1 } else { expr2 } form,
expr1 and expr2 can be compound expression lists, i.e. they may have statements (including logic ones, and early returns).

I do think that in many cases, such long if expressions, can become convoluted, and allow for bugs, but the language does allow them :-| .

@spytheman

spytheman commented Jan 23, 2025 •

Copy link
Copy Markdown
Contributor

In any case, unless they are banned, V should not produce garbage, that fails at runtime (the branch with the return was executed in the issue, but the output was not, what was returned, but empty values).

@spytheman spytheman left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Excellent work.

@spytheman
spytheman merged commit d9a2fb1 into vlang:master Jan 23, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

return if statement broken returning optional multiple values.

3 participants