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

Getting the correct span for multi-segment Path #72

Open
shouya opened this issue Apr 28, 2024 · 1 comment
Open

Getting the correct span for multi-segment Path #72

shouya opened this issue Apr 28, 2024 · 1 comment

Comments

@shouya
Copy link

shouya commented Apr 28, 2024

Hi,

I spent a long time tracing down a bug in my implementation of the sorted macro against the 06-pattern-path test. Namely, my output always looked like this:

error: Error::Fmt should sort before Error::Io
  --> main.rs:23:7
   |
23 |       Error::Fmt(e) => write!(f, "{}", e),
   |       ^^^^^

where the error span is just Error instead of the expected Error::Fmt. I checked my code to made sure in my code that it's indeed the path's span being stored and emitted instead of the ident's. It's even more confusing because cargo expand gives the expected output whereas the cargo test and cargo run gives different answer.

After more debugging and searching I finally stumbled upon this issue. So after switching my toolchain from stable to nightly, the test passes as expected. Now I see it's because the stable proc-macro doesn't allow joining of spans, thus only the first token of the syntax tree is returned upon calling the Spanned::span function.

I think it may be good to indicate this trick somewhere in note since it's probably not a trivial bug. Anyway, to the future learners who had their head scratched hard for this error, I hope this issue can be helpful :)

@shouya
Copy link
Author

shouya commented Apr 28, 2024

Alternatively, instead of storing the Span, it's also possible to store the Path in its original form. Then one can use Error::new_spanned on the Path to create an error message spanning multiple tokens.

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

No branches or pull requests

1 participant