xso-proc: improve spans for error messages
For codec-based error messages, this reduces the amount of errors per violation to one. For all others, it improves the placement of the error slightly, but we still get duplicates. I couldn't figure out the remaining discrepancies in the spans ...
This commit is contained in:
parent
25d89e02ea
commit
de39f410d5
5 changed files with 75 additions and 49 deletions
|
|
@ -9,8 +9,8 @@
|
|||
//! In particular, it provides the `#[xml(attribute)]` implementation.
|
||||
|
||||
use proc_macro2::Span;
|
||||
use quote::{quote, ToTokens};
|
||||
use syn::*;
|
||||
use quote::{quote, quote_spanned, ToTokens};
|
||||
use syn::{spanned::Spanned, *};
|
||||
|
||||
use std::borrow::Cow;
|
||||
|
||||
|
|
@ -115,8 +115,9 @@ impl Field for AttributeField {
|
|||
|
||||
let finalize = match self.codec {
|
||||
Some(ref codec) => {
|
||||
let decode = text_codec_decode_fn(ty.clone());
|
||||
quote! {
|
||||
let span = codec.span();
|
||||
let decode = text_codec_decode_fn(ty.clone(), span);
|
||||
quote_spanned! { span=>
|
||||
|value| #decode(&#codec, value)
|
||||
}
|
||||
}
|
||||
|
|
@ -170,8 +171,15 @@ impl Field for AttributeField {
|
|||
|
||||
let generator = match self.codec {
|
||||
Some(ref codec) => {
|
||||
let encode = text_codec_encode_fn(ty.clone());
|
||||
quote! { #encode(&#codec, #bound_name)? }
|
||||
let span = codec.span();
|
||||
let encode = text_codec_encode_fn(ty.clone(), span);
|
||||
// NOTE: We need to fudge the span of `bound_name` here,
|
||||
// because its span points outside the macro (the identifier
|
||||
// of the field), which means that quote_spanned will not
|
||||
// override it, which would make the error message ugly.
|
||||
let mut bound_name = bound_name.clone();
|
||||
bound_name.set_span(span);
|
||||
quote_spanned! { span=> #encode(&#codec, #bound_name)? }
|
||||
}
|
||||
None => {
|
||||
let as_optional_xml_text = as_optional_xml_text_fn(ty.clone());
|
||||
|
|
|
|||
Loading…
Reference in a new issue