You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Refactor: Split self-param logic from parse_param_general #144206
This makes the function harder to follow and less composable.
Observation
This function is only used in one place: Parser::parse_fn_params.
Because of this, it is safe and straightforward to remove the first_param flag and instead handle the optional self parameter directly in parse_fn_params.
Suggested Refactor
Move the self parsing logic out of parse_param_general and into parse_fn_params.
This means:
Add a try_parse_self_param() function to explicitly attempt parsing self as the first parameter.
If successful, push the resulting param into the ThinVec before looping over regular parameters.
Remove the first_param boolean from parse_param_general.
Keep req_name logic intact (used to require named params).
Benefits
Simpler and more maintainable code.
Clearer separation of responsibilities.
Easier to test general parameter logic.
More idiomatic parser design (handle special cases separately).
Challenge
A key complication is that self parsing must occur after the ( token has been consumed — it is not syntactically valid before that point.
As a result, while extracting self-param parsing into a separate function (like try_parse_self_param) seems clean in theory, it doesn't buy us much in practice, because:
It cannot be called before parse_fn_params sees the opening (.
parse_param_general is only called after the paren is parsed anyway.
Therefore, the decision logic for "is this the first parameter?" and "can it be self?" may remain right there.
Notes
This was previously attempted in PR #144198 by myself, but I closed due to mentioned challange.
This issue tracks reintroducing that refactor cleanly.
parse_param_general has a call to self.collect_tokens() that is important. That call would need to be duplicated for the self param parsing if the self param parsing is moved. I don't think this would be an improvement over the current code structure. (See also #145088.) So I'm going to close this issue: on the surface it's a reasonable idea, but it doesn't work out in practice.
Activity
nnethercote commentedon Aug 22, 2025
parse_param_generalhas a call toself.collect_tokens()that is important. That call would need to be duplicated for theselfparam parsing if the self param parsing is moved. I don't think this would be an improvement over the current code structure. (See also #145088.) So I'm going to close this issue: on the surface it's a reasonable idea, but it doesn't work out in practice.