syn icon indicating copy to clipboard operation
syn copied to clipboard

Using new Span diagnostics

Open Qix- opened this issue 2 years ago • 1 comments

I'm trying to figure out how to effectively use the new Diagnostic API in lieu of the Parse trait's Result<T, syn::Error> type, which only emits the old spanned compiler_error!() trick. In particular, when I want to replace a failed parse with a better error message complete with help text et al (there's currently no way to return an Error that has no span or associated error messages).

Would it be possible to either construct an empty Error, or otherwise enable the Diagnostics functionality? Perhaps behind an unstable feature flag?

Qix- avatar Dec 09 '22 00:12 Qix-

Did a bit of digging and an initial go at both a syn and proc_macro2 implementation for this and turns out there's a bit of a rabbit hole of changes that need to be made to make this happen.

(assume use proc_macro as pm1; use proc_macro2 as pm2;)

  1. pm1::SourceFile cannot be manually constructed based on (PathBuf, bool) (ref https://github.com/rust-lang/rust/issues/54725#issuecomment-1345366759)
  2. pm1::Span cannot be manually constructed based on (pm1::SourceFile, pm1::LineColumn, pm2::LineColumn)
  3. From<pm2::LineColumn> for pm1::LineColumn must be added
  4. From<pm2::SourceFile> for pm1::SourceFile must be added
  5. From<pm2::Span> for pm1::Span must be added (ref dtolnay/proc-macro2#181)
  6. syn::Error needs to have Levels added. This part is easy and should be backwards compatible with existing code once the above is completed.

Qix- avatar Dec 10 '22 19:12 Qix-

For now, compile_error! is the only error reporting I am interested in supporting in this crate.

dtolnay avatar Apr 17 '24 21:04 dtolnay