Change span of as_dyn_error() to point compile error at attribute.

This commit is contained in:
Maarten de Vries 2023-10-19 15:04:40 +02:00
parent f4eac7ef7b
commit a49f7c603d
14 changed files with 209 additions and 8 deletions

View file

@ -23,14 +23,14 @@ fn impl_struct(input: Struct) -> TokenStream {
let (impl_generics, ty_generics, where_clause) = input.generics.split_for_impl();
let mut error_inferred_bounds = InferredBounds::new();
let source_body = if input.attrs.transparent.is_some() {
let source_body = if let Some(transparent_attr) = &input.attrs.transparent {
let only_field = &input.fields[0];
if only_field.contains_generic {
error_inferred_bounds.insert(only_field.ty, quote!(std::error::Error));
}
let member = &only_field.member;
Some(quote! {
std::error::Error::source(self.#member.as_dyn_error())
Some(quote_spanned! {
transparent_attr.span => std::error::Error::source(self.#member.as_dyn_error())
})
} else if let Some(source_field) = input.source_field() {
let source = &source_field.member;
@ -43,7 +43,8 @@ fn impl_struct(input: Struct) -> TokenStream {
} else {
None
};
let dyn_error = quote_spanned!(source.span()=> self.#source #asref.as_dyn_error());
let dyn_error =
quote_spanned!(source_field.source_span() => self.#source #asref.as_dyn_error());
Some(quote! {
::core::option::Option::Some(#dyn_error)
})
@ -193,13 +194,13 @@ fn impl_enum(input: Enum) -> TokenStream {
let source_method = if input.has_source() {
let arms = input.variants.iter().map(|variant| {
let ident = &variant.ident;
if variant.attrs.transparent.is_some() {
if let Some(transparent_attr) = &variant.attrs.transparent {
let only_field = &variant.fields[0];
if only_field.contains_generic {
error_inferred_bounds.insert(only_field.ty, quote!(std::error::Error));
}
let member = &only_field.member;
let source = quote!(std::error::Error::source(transparent.as_dyn_error()));
let source = quote_spanned!(transparent_attr.span => std::error::Error::source(transparent.as_dyn_error()));
quote! {
#ty::#ident {#member: transparent} => #source,
}
@ -215,7 +216,7 @@ fn impl_enum(input: Enum) -> TokenStream {
None
};
let varsource = quote!(source);
let dyn_error = quote_spanned!(source.span()=> #varsource #asref.as_dyn_error());
let dyn_error = quote_spanned!(source_field.source_span()=> #varsource #asref.as_dyn_error());
quote! {
#ty::#ident {#source: #varsource, ..} => ::core::option::Option::Some(#dyn_error),
}