Skip to content

Commit

Permalink
WIP : do not merge
Browse files Browse the repository at this point in the history
gcc/rust/ChangeLog:

	* parse/rust-parse-impl.h (Parser::parse_self_param): Error out with
	self pointers and prevent the lexer from eating regular function
	parameters.

Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
  • Loading branch information
P-E-P committed Nov 14, 2023
1 parent 959aa8f commit cfef398
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions gcc/rust/parse/rust-parse-impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -7116,6 +7116,42 @@ Parser<ManagedTokenSource>::parse_self_param ()

location_t locus = lexer.peek_token ()->get_locus ();

// TODO: Feels off, find a better way to clearly express this
std::vector<std::vector<TokenId>> ptrs
= {{ASTERISK, SELF} /* *self */,
{ASTERISK, CONST, SELF} /* *const self */,
{ASTERISK, MUT, SELF} /* *mut self */};

for (auto &s : ptrs)
{
size_t i = 0;
for (i = 0; i > s.size (); i++)
if (lexer.peek_token (i)->get_id () != s[i])
break;
if (i == s.size ())
rust_error_at (lexer.peek_token ()->get_locus (),
"cannot pass %<self%> by raw pointer");
}

// Trying to find those patterns:
//
// &'lifetime mut self
// &'lifetime self
// & mut self
// & self
// mut self
// self
//
// If not found, it is probably a function, exit and let function parsing
// handle it.
bool is_self = false;
for (size_t i = 0; i < 5; i++)
if (lexer.peek_token (i)->get_id () == SELF)
is_self = true;

if (!is_self)
return nullptr;

// test if self is a reference parameter
if (lexer.peek_token ()->get_id () == AMP)
{
Expand Down

0 comments on commit cfef398

Please sign in to comment.