Locating a Span for a specific character in source

Note: This is my first real effort at working on the compiler, so I might be going at this all wrong.

I am trying to work on #70926, and I am having a hard time figuring out how to approach it. The parser is trying to parse an Arm of the match statement. As part of this process, it calls parse_expr_res to parse the actual content of that Arm. If the user accidentally typed a dot instead of a comma, the parser tries to parse the next pattern as part of the expression. This obviously fails, so we get a diagnostic returned back to the prase_arm function. I want to attach a suggestion pointing at the dot telling the user that they might want it to be a comma.

I just don't know how to actually implement this. What I have tried so far is creating a Span that runs from the => at the start of the arm to the point at which parse_expr_res fails. If this span covers multiple lines, and if the failure happened on another => token, then there is a good chance that we have a missing comma somewhere.

My only issue is that I do not know how to pinpoint the location of the missing comma. I tried using some string processing to pinpoint a location in the source where a dot was used at the end of a line. This is where I want to attach my suggestion, but I do not know how to get a Span from a position in a string.

Hi there @tsandstr -- I've left you some mentoring instructions on https://github.com/rust-lang/rust/issues/70926#issuecomment-611672692. Let's continue the discussion over there. :slight_smile:

Appreciate it! Thanks

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.