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:
Jonas Schäfer 2025-05-02 16:56:22 +02:00
commit de39f410d5
5 changed files with 75 additions and 49 deletions

View file

@ -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());

View file

@ -68,7 +68,8 @@ impl Field for ChildField {
let from_events = from_events_fn(element_ty.clone());
let matcher = quote! { #from_events(name, attrs, ctx) };
let span = element_ty.span();
let matcher = quote_spanned! { span=> #from_events(name, attrs, ctx) };
let builder = from_xml_builder_ty(element_ty.clone());
(
@ -178,9 +179,10 @@ impl Field for ChildField {
let as_xml_iter = as_xml_iter_fn(item_ty.clone());
let item_iter = item_iter_ty(item_ty.clone(), lifetime.clone());
let span = item_ty.span();
(
TokenStream::default(),
quote! { #as_xml_iter(#bound_name)? },
quote_spanned! { span=> #as_xml_iter(#bound_name)? },
item_iter,
)
}

View file

@ -9,8 +9,8 @@
//! In particular, it provides the `#[xml(text)]` implementation.
use proc_macro2::Span;
use quote::quote;
use syn::*;
use quote::{quote, quote_spanned};
use syn::{spanned::Spanned, *};
use crate::error_message::ParentRef;
use crate::scope::{AsItemsScope, FromEventsScope};
@ -38,8 +38,9 @@ impl Field for TextField {
let field_access = scope.access_field(member);
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=>
#decode(&#codec, #field_access)?
}
}
@ -71,8 +72,15 @@ impl Field for TextField {
) -> Result<FieldIteratorPart> {
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_xml_text = as_xml_text_fn(ty.clone());