syn
syn copied to clipboard
Using new Span diagnostics
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?
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;
)
-
pm1::SourceFile
cannot be manually constructed based on(PathBuf, bool)
(ref https://github.com/rust-lang/rust/issues/54725#issuecomment-1345366759) -
pm1::Span
cannot be manually constructed based on(pm1::SourceFile, pm1::LineColumn, pm2::LineColumn)
-
From<pm2::LineColumn> for pm1::LineColumn
must be added -
From<pm2::SourceFile> for pm1::SourceFile
must be added -
From<pm2::Span> for pm1::Span
must be added (ref dtolnay/proc-macro2#181) -
syn::Error
needs to haveLevel
s added. This part is easy and should be backwards compatible with existing code once the above is completed.
For now, compile_error!
is the only error reporting I am interested in supporting in this crate.