From b8a1df92ff64f2f84a3e373df4e183c6c7963e39 Mon Sep 17 00:00:00 2001 From: Gustavo Date: Thu, 7 Mar 2024 16:11:47 -0300 Subject: [PATCH] Make inacurate error spans capture the entire item --- macros/src/types/enum.rs | 4 ++-- macros/src/types/named.rs | 10 +++++----- macros/src/types/newtype.rs | 8 ++++---- macros/src/types/tuple.rs | 10 +++++----- macros/src/utils.rs | 6 ++++++ 5 files changed, 22 insertions(+), 16 deletions(-) diff --git a/macros/src/types/enum.rs b/macros/src/types/enum.rs index 3006366..41556b4 100644 --- a/macros/src/types/enum.rs +++ b/macros/src/types/enum.rs @@ -116,7 +116,7 @@ fn format_variant( quote!(format!("{{ \"{}\": \"{}\" }}", #tag, #name)) } else { let ty = match (type_override, type_as) { - (Some(_), Some(_)) => syn_err!("`type` is not compatible with `as`"), + (Some(_), Some(_)) => syn_err_spanned!(variant; "`type` is not compatible with `as`"), (Some(type_override), None) => quote! { #type_override }, (None, Some(type_as)) => { quote!(<#type_as as ts_rs::TS>::name()) @@ -165,7 +165,7 @@ fn format_variant( quote!(format!("{{ \"{}\": \"{}\" }}", #tag, #name)) } else { let ty = match (type_override, type_as) { - (Some(_), Some(_)) => syn_err!("`type` is not compatible with `as`"), + (Some(_), Some(_)) => syn_err_spanned!(variant; "`type` is not compatible with `as`"), (Some(type_override), None) => quote! { #type_override }, (None, Some(type_as)) => { quote!(<#type_as as ts_rs::TS>::name()) diff --git a/macros/src/types/named.rs b/macros/src/types/named.rs index 8a02575..8ba236b 100644 --- a/macros/src/types/named.rs +++ b/macros/src/types/named.rs @@ -93,7 +93,7 @@ fn format_field( } if type_as.is_some() && type_override.is_some() { - syn_err!(field.ident.span(); "`type` is not compatible with `as`") + syn_err_spanned!(field; "`type` is not compatible with `as`") } let parsed_ty = if let Some(ref type_as) = type_as { @@ -120,10 +120,10 @@ fn format_field( if flatten { match (&type_as, &type_override, &rename, inline) { - (Some(_), _, _, _) => syn_err!(field.ident.span(); "`as` is not compatible with `flatten`"), - (_, Some(_), _, _) => syn_err!(field.ident.span(); "`type` is not compatible with `flatten`"), - (_, _, Some(_), _) => syn_err!(field.ident.span(); "`rename` is not compatible with `flatten`"), - (_, _, _, true) => syn_err!(field.ident.span(); "`inline` is not compatible with `flatten`"), + (Some(_), _, _, _) => syn_err_spanned!(field; "`as` is not compatible with `flatten`"), + (_, Some(_), _, _) => syn_err_spanned!(field; "`type` is not compatible with `flatten`"), + (_, _, Some(_), _) => syn_err_spanned!(field; "`rename` is not compatible with `flatten`"), + (_, _, _, true) => syn_err_spanned!(field; "`inline` is not compatible with `flatten`"), _ => {} } diff --git a/macros/src/types/newtype.rs b/macros/src/types/newtype.rs index 99f6658..24ce4d2 100644 --- a/macros/src/types/newtype.rs +++ b/macros/src/types/newtype.rs @@ -32,15 +32,15 @@ pub(crate) fn newtype( } = FieldAttr::from_attrs(&inner.attrs)?; match (&rename_inner, skip, optional.optional, flatten) { - (Some(_), ..) => syn_err!("`rename` is not applicable to newtype fields"), + (Some(_), ..) => syn_err_spanned!(fields; "`rename` is not applicable to newtype fields"), (_, true, ..) => return super::unit::null(attr, name, generics.clone()), - (_, _, true, ..) => syn_err!("`optional` is not applicable to newtype fields"), - (_, _, _, true) => syn_err!("`flatten` is not applicable to newtype fields"), + (_, _, true, ..) => syn_err_spanned!(fields; "`optional` is not applicable to newtype fields"), + (_, _, _, true) => syn_err_spanned!(fields; "`flatten` is not applicable to newtype fields"), _ => {} }; if type_as.is_some() && type_override.is_some() { - syn_err!("`type` is not compatible with `as`") + syn_err_spanned!(fields; "`type` is not compatible with `as`") } let inner_ty = if let Some(ref type_as) = type_as { diff --git a/macros/src/types/tuple.rs b/macros/src/types/tuple.rs index 4c0689e..cfe536d 100644 --- a/macros/src/types/tuple.rs +++ b/macros/src/types/tuple.rs @@ -1,6 +1,6 @@ use proc_macro2::TokenStream; use quote::{quote, ToTokens}; -use syn::{Field, FieldsUnnamed, Generics, Result, Type, spanned::Spanned}; +use syn::{Field, FieldsUnnamed, Generics, Result, Type}; use crate::{ attr::{FieldAttr, StructAttr}, @@ -72,19 +72,19 @@ fn format_field( }; if type_as.is_some() && type_override.is_some() { - syn_err!(field.ty.span(); "`type` is not compatible with `as`") + syn_err_spanned!(field; "`type` is not compatible with `as`") } if rename.is_some() { - syn_err!(field.ty.span(); "`rename` is not applicable to tuple structs") + syn_err_spanned!(field; "`rename` is not applicable to tuple structs") } if optional.optional { - syn_err!(field.ty.span(); "`optional` is not applicable to tuple fields") + syn_err_spanned!(field; "`optional` is not applicable to tuple fields") } if flatten { - syn_err!(field.ty.span(); "`flatten` is not applicable to tuple fields") + syn_err_spanned!(field; "`flatten` is not applicable to tuple fields") } formatted_fields.push(match type_override { diff --git a/macros/src/utils.rs b/macros/src/utils.rs index 0b7205a..fb60061 100644 --- a/macros/src/utils.rs +++ b/macros/src/utils.rs @@ -17,6 +17,12 @@ macro_rules! syn_err { }; } +macro_rules! syn_err_spanned { + ($s:expr; $l:literal $(, $a:expr)*) => { + return Err(syn::Error::new_spanned($s, format!($l $(, $a)*))) + }; +} + macro_rules! impl_parse { ($i:ident ($input:ident, $out:ident) { $($k:pat => $e:expr),* $(,)? }) => { impl std::convert::TryFrom<&syn::Attribute> for $i {