Skip to content

Commit

Permalink
ast: Full lifetime elision handling
Browse files Browse the repository at this point in the history
gcc/rust/ChangeLog:

	* parse/rust-parse-impl.h (Parser::parse_generic_param): Lifetime elision control.
	(Parser::parse_lifetime_where_clause_item): Lifetime elision control.
	(Parser::parse_type_param_bound): Lifetime elision control.
	(Parser::parse_lifetime_bounds): Lifetime elision control.
	(Parser::parse_lifetime): Lifetime elision control.
	(Parser::parse_path_generic_args): Lifetime elision control.
	(Parser::parse_self_param): Lifetime elision control.
	(Parser::parse_break_expr): Lifetime elision control.
	(Parser::parse_continue_expr): Lifetime elision control.
	(Parser::parse_reference_type_inner): Lifetime elision control.
	* parse/rust-parse.h: Lifetime elision control.

Signed-off-by: Jakub Dupak <[email protected]>
  • Loading branch information
jdupak committed Dec 11, 2023
1 parent 98f525f commit 02cdcc7
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 13 deletions.
25 changes: 13 additions & 12 deletions gcc/rust/parse/rust-parse-impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -3099,7 +3099,7 @@ Parser<ManagedTokenSource>::parse_generic_param (EndTokenPred is_end_token)
switch (token->get_id ())
{
case LIFETIME: {
auto lifetime = parse_lifetime ();
auto lifetime = parse_lifetime (false);
if (lifetime.is_error ())
{
rust_error_at (
Expand Down Expand Up @@ -3808,7 +3808,7 @@ template <typename ManagedTokenSource>
std::unique_ptr<AST::LifetimeWhereClauseItem>
Parser<ManagedTokenSource>::parse_lifetime_where_clause_item ()
{
AST::Lifetime lifetime = parse_lifetime ();
AST::Lifetime lifetime = parse_lifetime (false);
if (lifetime.is_error ())
{
// TODO: error here?
Expand Down Expand Up @@ -4001,7 +4001,7 @@ Parser<ManagedTokenSource>::parse_type_param_bound ()
{
case LIFETIME:
return std::unique_ptr<AST::Lifetime> (
new AST::Lifetime (parse_lifetime ()));
new AST::Lifetime (parse_lifetime (false)));
case LEFT_PAREN:
case QUESTION_MARK:
case FOR:
Expand Down Expand Up @@ -4075,7 +4075,7 @@ Parser<ManagedTokenSource>::parse_lifetime_bounds ()

while (true)
{
AST::Lifetime lifetime = parse_lifetime ();
AST::Lifetime lifetime = parse_lifetime (false);

// quick exit for parsing failure
if (lifetime.is_error ())
Expand Down Expand Up @@ -4105,7 +4105,7 @@ Parser<ManagedTokenSource>::parse_lifetime_bounds (EndTokenPred is_end_token)

while (!is_end_token (lexer.peek_token ()->get_id ()))
{
AST::Lifetime lifetime = parse_lifetime ();
AST::Lifetime lifetime = parse_lifetime (false);

if (lifetime.is_error ())
{
Expand Down Expand Up @@ -4136,12 +4136,13 @@ Parser<ManagedTokenSource>::parse_lifetime_bounds (EndTokenPred is_end_token)
* existing. */
template <typename ManagedTokenSource>
AST::Lifetime
Parser<ManagedTokenSource>::parse_lifetime ()
Parser<ManagedTokenSource>::parse_lifetime (bool allow_elided)
{
const_TokenPtr lifetime_tok = lexer.peek_token ();
if (lifetime_tok->get_id () != LIFETIME)
{
return AST::Lifetime::elided ();
return (allow_elided) ? AST::Lifetime::elided ()
: AST::Lifetime::error ();
}
lexer.skip_token ();

Expand Down Expand Up @@ -6604,7 +6605,7 @@ Parser<ManagedTokenSource>::parse_path_generic_args ()
location_t locus = t->get_locus ();
while (!is_right_angle_tok (t->get_id ()))
{
AST::Lifetime lifetime = parse_lifetime ();
AST::Lifetime lifetime = parse_lifetime (false);
if (lifetime.is_error ())
{
// not necessarily an error
Expand Down Expand Up @@ -7225,7 +7226,7 @@ Parser<ManagedTokenSource>::parse_self_param ()
// now test whether it has a lifetime
if (lexer.peek_token ()->get_id () == LIFETIME)
{
lifetime = parse_lifetime ();
lifetime = parse_lifetime (true);

// something went wrong somehow
if (lifetime.is_error ())
Expand Down Expand Up @@ -7761,7 +7762,7 @@ Parser<ManagedTokenSource>::parse_break_expr (AST::AttrVec outer_attrs,
AST::Lifetime label = AST::Lifetime::error ();
if (lexer.peek_token ()->get_id () == LIFETIME)
{
label = parse_lifetime ();
label = parse_lifetime (false);
}

// parse break return expression if it exists
Expand Down Expand Up @@ -7792,7 +7793,7 @@ Parser<ManagedTokenSource>::parse_continue_expr (AST::AttrVec outer_attrs,
AST::Lifetime label = AST::Lifetime::error ();
if (lexer.peek_token ()->get_id () == LIFETIME)
{
label = parse_lifetime ();
label = parse_lifetime (false);
}

return std::unique_ptr<AST::ContinueExpr> (
Expand Down Expand Up @@ -9836,7 +9837,7 @@ Parser<ManagedTokenSource>::parse_reference_type_inner (location_t locus)
AST::Lifetime lifetime = AST::Lifetime::elided ();
if (lexer.peek_token ()->get_id () == LIFETIME)
{
lifetime = parse_lifetime ();
lifetime = parse_lifetime (true);
if (lifetime.is_error ())
{
Error error (lexer.peek_token ()->get_locus (),
Expand Down
2 changes: 1 addition & 1 deletion gcc/rust/parse/rust-parse.h
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ template <typename ManagedTokenSource> class Parser
std::vector<AST::Lifetime> parse_lifetime_bounds ();
template <typename EndTokenPred>
std::vector<AST::Lifetime> parse_lifetime_bounds (EndTokenPred is_end_token);
AST::Lifetime parse_lifetime ();
AST::Lifetime parse_lifetime (bool allow_elided);
AST::Lifetime lifetime_from_token (const_TokenPtr tok);
std::unique_ptr<AST::ExternalTypeItem>
parse_external_type_item (AST::Visibility vis, AST::AttrVec outer_attrs);
Expand Down

0 comments on commit 02cdcc7

Please sign in to comment.