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

ast: builder: Add Return expression builder #3386

Merged
merged 1 commit into from
Feb 2, 2025

Conversation

CohenArthur
Copy link
Member

Add new methods for building return <x>; expressions/statements in our AST.

gcc/rust/ChangeLog:

	* ast/rust-ast-builder.h: Declare it.
	* ast/rust-ast-builder.cc (Builder::return_expr): Define it.
Comment on lines +220 to +221
return std::unique_ptr<Expr> (
new ReturnExpr (std::move (to_return), {}, loc));
Copy link
Member

Choose a reason for hiding this comment

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

Couldn't we use make_unique here ? I believe we can even get rid of our implementation since baseline cpp version has been bumped to cpp14.

Copy link
Member Author

Choose a reason for hiding this comment

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

tried using it and it complains about not finding a matching constructor:

../../gcc/rust/ast/rust-ast-builder.cc:220:39: error: no matching function for call to ‘make_unique<Rust::AST::ReturnExpr>(std::remove_reference<std::unique_ptr<Rust::AST::Expr>&>::type, <brace-enclosed initializer list>, location_t&)’
  220 |   return std::make_unique<ReturnExpr> (std::move (to_return), {}, loc);
      |          ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

but maybe I'm missing something

Comment on lines +89 to +90
std::unique_ptr<Expr> return_expr (std::unique_ptr<Expr> &&to_return
= nullptr);
Copy link
Member

Choose a reason for hiding this comment

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

std::unique_ptr<Expr> && I wish it was an optional instead, this would draw a clear line and we would be able to easily catch rogue null pointers.

Copy link
Member Author

Choose a reason for hiding this comment

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

I think we'd need to change the AST for this then, because the ReturnExpr class only keeps a unique_ptr. So the builder's code would look like this:

std::unique_ptr<Expr>
Builder::return_expr (tl::optional<std::unique_ptr<Expr>> &&to_return)
{
  if (to_return)
    return std::unique_ptr<Expr> (
      new ReturnExpr (std::move (*to_return), {}, loc));

  return std::unique_ptr<Expr> (new ReturnExpr (nullptr, {}, loc));
}

which is quite heavy

@CohenArthur CohenArthur added this pull request to the merge queue Feb 2, 2025
Merged via the queue into Rust-GCC:master with commit 4a05373 Feb 2, 2025
12 checks passed
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.

2 participants