dtolnay / dtolnay/proc-macro-workshop
Getting the correct span for multi-segment Path
- Dominant language
- Rust
- Stars
- 4.9k
- Forks
- 1.2k
- PR merge metrics
- No merged PRs in 30d
Description
Hi,
I spent a long time tracing down a bug in my implementation of the `sorted` macro against the [06-pattern-path](https://github.com/dtolnay/proc-macro-workshop/blob/master/sorted/tests/06-pattern-path.stderr#L4) 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](https://github.com/dtolnay/syn/issues/531). 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 :)
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.