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
|
|
@ -430,12 +430,14 @@ impl Compound {
|
||||||
} => {
|
} => {
|
||||||
let feed = feed_fn(builder.clone());
|
let feed = feed_fn(builder.clone());
|
||||||
|
|
||||||
|
let mut substate_data_ident = substate_data.clone();
|
||||||
|
substate_data_ident.set_span(ty.span());
|
||||||
states.push(State::new_with_builder(
|
states.push(State::new_with_builder(
|
||||||
state_name.clone(),
|
state_name.clone(),
|
||||||
builder_data_ident,
|
builder_data_ident,
|
||||||
&builder_data_ty,
|
&builder_data_ty,
|
||||||
).with_field(
|
).with_field(
|
||||||
substate_data,
|
&substate_data_ident,
|
||||||
&builder,
|
&builder,
|
||||||
).with_mut(substate_data).with_impl(quote! {
|
).with_mut(substate_data).with_impl(quote! {
|
||||||
match #feed(&mut #substate_data, ev, ctx)? {
|
match #feed(&mut #substate_data, ev, ctx)? {
|
||||||
|
|
|
||||||
|
|
@ -9,8 +9,8 @@
|
||||||
//! In particular, it provides the `#[xml(attribute)]` implementation.
|
//! In particular, it provides the `#[xml(attribute)]` implementation.
|
||||||
|
|
||||||
use proc_macro2::Span;
|
use proc_macro2::Span;
|
||||||
use quote::{quote, ToTokens};
|
use quote::{quote, quote_spanned, ToTokens};
|
||||||
use syn::*;
|
use syn::{spanned::Spanned, *};
|
||||||
|
|
||||||
use std::borrow::Cow;
|
use std::borrow::Cow;
|
||||||
|
|
||||||
|
|
@ -115,8 +115,9 @@ impl Field for AttributeField {
|
||||||
|
|
||||||
let finalize = match self.codec {
|
let finalize = match self.codec {
|
||||||
Some(ref codec) => {
|
Some(ref codec) => {
|
||||||
let decode = text_codec_decode_fn(ty.clone());
|
let span = codec.span();
|
||||||
quote! {
|
let decode = text_codec_decode_fn(ty.clone(), span);
|
||||||
|
quote_spanned! { span=>
|
||||||
|value| #decode(&#codec, value)
|
|value| #decode(&#codec, value)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -170,8 +171,15 @@ impl Field for AttributeField {
|
||||||
|
|
||||||
let generator = match self.codec {
|
let generator = match self.codec {
|
||||||
Some(ref codec) => {
|
Some(ref codec) => {
|
||||||
let encode = text_codec_encode_fn(ty.clone());
|
let span = codec.span();
|
||||||
quote! { #encode(&#codec, #bound_name)? }
|
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 => {
|
None => {
|
||||||
let as_optional_xml_text = as_optional_xml_text_fn(ty.clone());
|
let as_optional_xml_text = as_optional_xml_text_fn(ty.clone());
|
||||||
|
|
|
||||||
|
|
@ -68,7 +68,8 @@ impl Field for ChildField {
|
||||||
|
|
||||||
let from_events = from_events_fn(element_ty.clone());
|
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());
|
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 as_xml_iter = as_xml_iter_fn(item_ty.clone());
|
||||||
let item_iter = item_iter_ty(item_ty.clone(), lifetime.clone());
|
let item_iter = item_iter_ty(item_ty.clone(), lifetime.clone());
|
||||||
|
|
||||||
|
let span = item_ty.span();
|
||||||
(
|
(
|
||||||
TokenStream::default(),
|
TokenStream::default(),
|
||||||
quote! { #as_xml_iter(#bound_name)? },
|
quote_spanned! { span=> #as_xml_iter(#bound_name)? },
|
||||||
item_iter,
|
item_iter,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -9,8 +9,8 @@
|
||||||
//! In particular, it provides the `#[xml(text)]` implementation.
|
//! In particular, it provides the `#[xml(text)]` implementation.
|
||||||
|
|
||||||
use proc_macro2::Span;
|
use proc_macro2::Span;
|
||||||
use quote::quote;
|
use quote::{quote, quote_spanned};
|
||||||
use syn::*;
|
use syn::{spanned::Spanned, *};
|
||||||
|
|
||||||
use crate::error_message::ParentRef;
|
use crate::error_message::ParentRef;
|
||||||
use crate::scope::{AsItemsScope, FromEventsScope};
|
use crate::scope::{AsItemsScope, FromEventsScope};
|
||||||
|
|
@ -38,8 +38,9 @@ impl Field for TextField {
|
||||||
let field_access = scope.access_field(member);
|
let field_access = scope.access_field(member);
|
||||||
let finalize = match self.codec {
|
let finalize = match self.codec {
|
||||||
Some(ref codec) => {
|
Some(ref codec) => {
|
||||||
let decode = text_codec_decode_fn(ty.clone());
|
let span = codec.span();
|
||||||
quote! {
|
let decode = text_codec_decode_fn(ty.clone(), span);
|
||||||
|
quote_spanned! { span=>
|
||||||
#decode(&#codec, #field_access)?
|
#decode(&#codec, #field_access)?
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -71,8 +72,15 @@ impl Field for TextField {
|
||||||
) -> Result<FieldIteratorPart> {
|
) -> Result<FieldIteratorPart> {
|
||||||
let generator = match self.codec {
|
let generator = match self.codec {
|
||||||
Some(ref codec) => {
|
Some(ref codec) => {
|
||||||
let encode = text_codec_encode_fn(ty.clone());
|
let span = codec.span();
|
||||||
quote! { #encode(&#codec, #bound_name)? }
|
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 => {
|
None => {
|
||||||
let as_xml_text = as_xml_text_fn(ty.clone());
|
let as_xml_text = as_xml_text_fn(ty.clone());
|
||||||
|
|
|
||||||
|
|
@ -316,10 +316,11 @@ pub(crate) fn as_xml_text_fn(ty: Type) -> Expr {
|
||||||
|
|
||||||
/// Construct a [`syn::Path`] referring to `::xso::TextCodec::<#for_ty>`,
|
/// Construct a [`syn::Path`] referring to `::xso::TextCodec::<#for_ty>`,
|
||||||
/// returning the span of `for_ty` alongside it.
|
/// returning the span of `for_ty` alongside it.
|
||||||
fn text_codec_of(for_ty: Type) -> (Span, Path) {
|
///
|
||||||
let span = for_ty.span();
|
/// The span used is `codec_span`, in order to ensure that error messages
|
||||||
(
|
/// about a missing implementation point at the codec, not at the type.
|
||||||
span,
|
fn text_codec_of(for_ty: Type, codec_span: Span) -> Path {
|
||||||
|
let span = codec_span;
|
||||||
Path {
|
Path {
|
||||||
leading_colon: Some(syn::token::PathSep {
|
leading_colon: Some(syn::token::PathSep {
|
||||||
spans: [span, span],
|
spans: [span, span],
|
||||||
|
|
@ -343,16 +344,18 @@ fn text_codec_of(for_ty: Type) -> (Span, Path) {
|
||||||
]
|
]
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.collect(),
|
.collect(),
|
||||||
},
|
}
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Construct a [`syn::Expr`] referring to
|
/// Construct a [`syn::Expr`] referring to
|
||||||
/// `::xso::TextCodec::<#for_ty>::encode`.
|
/// `::xso::TextCodec::<#for_ty>::encode`.
|
||||||
pub(crate) fn text_codec_encode_fn(for_ty: Type) -> Expr {
|
///
|
||||||
let (span, mut path) = text_codec_of(for_ty);
|
/// The span used is `codec_span`, in order to ensure that error messages
|
||||||
|
/// about a missing implementation point at the codec, not at the type.
|
||||||
|
pub(crate) fn text_codec_encode_fn(for_ty: Type, codec_span: Span) -> Expr {
|
||||||
|
let mut path = text_codec_of(for_ty, codec_span);
|
||||||
path.segments.push(PathSegment {
|
path.segments.push(PathSegment {
|
||||||
ident: Ident::new("encode", span),
|
ident: Ident::new("encode", codec_span),
|
||||||
arguments: PathArguments::None,
|
arguments: PathArguments::None,
|
||||||
});
|
});
|
||||||
Expr::Path(ExprPath {
|
Expr::Path(ExprPath {
|
||||||
|
|
@ -364,10 +367,13 @@ pub(crate) fn text_codec_encode_fn(for_ty: Type) -> Expr {
|
||||||
|
|
||||||
/// Construct a [`syn::Expr`] referring to
|
/// Construct a [`syn::Expr`] referring to
|
||||||
/// `::xso::TextCodec::<#for_ty>::decode`.
|
/// `::xso::TextCodec::<#for_ty>::decode`.
|
||||||
pub(crate) fn text_codec_decode_fn(for_ty: Type) -> Expr {
|
///
|
||||||
let (span, mut path) = text_codec_of(for_ty);
|
/// The span used is `codec_span`, in order to ensure that error messages
|
||||||
|
/// about a missing implementation point at the codec, not at the type.
|
||||||
|
pub(crate) fn text_codec_decode_fn(for_ty: Type, codec_span: Span) -> Expr {
|
||||||
|
let mut path = text_codec_of(for_ty, codec_span);
|
||||||
path.segments.push(PathSegment {
|
path.segments.push(PathSegment {
|
||||||
ident: Ident::new("decode", span),
|
ident: Ident::new("decode", codec_span),
|
||||||
arguments: PathArguments::None,
|
arguments: PathArguments::None,
|
||||||
});
|
});
|
||||||
Expr::Path(ExprPath {
|
Expr::Path(ExprPath {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue