From 501e87c324c8d91be4d3b42d981a275e9532148c Mon Sep 17 00:00:00 2001 From: Gustavo Date: Thu, 7 Mar 2024 15:08:17 -0300 Subject: [PATCH 1/2] Improve location of some errors --- macros/src/attr/field.rs | 13 +++++++------ macros/src/attr/mod.rs | 8 ++++++++ macros/src/types/enum.rs | 8 +++----- macros/src/types/named.rs | 22 +++++++++++----------- macros/src/types/newtype.rs | 4 ++-- macros/src/types/tuple.rs | 14 +++++++------- 6 files changed, 38 insertions(+), 31 deletions(-) diff --git a/macros/src/attr/field.rs b/macros/src/attr/field.rs index 09d1b0d..6f93cd9 100644 --- a/macros/src/attr/field.rs +++ b/macros/src/attr/field.rs @@ -1,11 +1,11 @@ -use syn::{spanned::Spanned, Attribute, Ident, Result}; +use syn::{Attribute, Ident, Result, Path}; -use super::parse_assign_str; +use super::{parse_assign_str, parse_assign_from_str}; use crate::utils::{parse_attrs, parse_docs}; #[derive(Default)] pub struct FieldAttr { - pub type_as: Option, + pub type_as: Option, pub type_override: Option, pub rename: Option, pub inline: bool, @@ -70,18 +70,19 @@ impl FieldAttr { impl_parse! { FieldAttr(input, out) { - "as" => out.type_as = Some(parse_assign_str(input)?), + "as" => out.type_as = Some(parse_assign_from_str(input)?), "type" => out.type_override = Some(parse_assign_str(input)?), "rename" => out.rename = Some(parse_assign_str(input)?), "inline" => out.inline = true, "skip" => out.skip = true, "optional" => { - use syn::{Token, Error}; + use syn::{Token, Error}; let nullable = if input.peek(Token![=]) { input.parse::()?; + let span = input.span(); match Ident::parse(input)?.to_string().as_str() { "nullable" => true, - other => Err(Error::new(other.span(), "expected 'nullable'"))? + _ => Err(Error::new(span, "expected 'nullable'"))? } } else { false diff --git a/macros/src/attr/mod.rs b/macros/src/attr/mod.rs index 6bb7b34..72196dc 100644 --- a/macros/src/attr/mod.rs +++ b/macros/src/attr/mod.rs @@ -89,3 +89,11 @@ fn parse_assign_str(input: ParseStream) -> Result { fn parse_assign_inflection(input: ParseStream) -> Result { parse_assign_str(input).and_then(Inflection::try_from) } + +fn parse_assign_from_str(input: ParseStream) -> Result where T: Parse { + input.parse::()?; + match Lit::parse(input)? { + Lit::Str(string) => string.parse(), + other => Err(Error::new(other.span(), "expected string")), + } +} diff --git a/macros/src/types/enum.rs b/macros/src/types/enum.rs index e9ebfbb..3006366 100644 --- a/macros/src/types/enum.rs +++ b/macros/src/types/enum.rs @@ -1,6 +1,6 @@ use proc_macro2::TokenStream; use quote::{format_ident, quote}; -use syn::{Fields, Generics, ItemEnum, Type, Variant}; +use syn::{Fields, Generics, ItemEnum, Variant}; use crate::{ attr::{EnumAttr, FieldAttr, StructAttr, Tagged, VariantAttr}, @@ -119,8 +119,7 @@ fn format_variant( (Some(_), Some(_)) => syn_err!("`type` is not compatible with `as`"), (Some(type_override), None) => quote! { #type_override }, (None, Some(type_as)) => { - let ty = syn::parse_str::(&type_as)?; - quote!(<#ty as ts_rs::TS>::name()) + quote!(<#type_as as ts_rs::TS>::name()) } (None, None) => { let ty = &unnamed.unnamed[0].ty; @@ -169,8 +168,7 @@ fn format_variant( (Some(_), Some(_)) => syn_err!("`type` is not compatible with `as`"), (Some(type_override), None) => quote! { #type_override }, (None, Some(type_as)) => { - let ty = syn::parse_str::(&type_as)?; - quote!(<#ty as ts_rs::TS>::name()) + quote!(<#type_as as ts_rs::TS>::name()) } (None, None) => { let ty = &unnamed.unnamed[0].ty; diff --git a/macros/src/types/named.rs b/macros/src/types/named.rs index 2df0212..8a02575 100644 --- a/macros/src/types/named.rs +++ b/macros/src/types/named.rs @@ -1,6 +1,6 @@ use proc_macro2::TokenStream; -use quote::quote; -use syn::{Field, FieldsNamed, GenericArgument, Generics, PathArguments, Result, Type}; +use quote::{quote, ToTokens}; +use syn::{spanned::Spanned, Field, FieldsNamed, GenericArgument, Generics, PathArguments, Result, Type}; use crate::{ attr::{FieldAttr, Inflection, Optional, StructAttr}, @@ -93,11 +93,11 @@ fn format_field( } if type_as.is_some() && type_override.is_some() { - syn_err!("`type` is not compatible with `as`") + syn_err!(field.ident.span(); "`type` is not compatible with `as`") } let parsed_ty = if let Some(ref type_as) = type_as { - syn::parse_str::(type_as)? + syn::parse_str::(&type_as.to_token_stream().to_string())? } else { field.ty.clone() }; @@ -120,10 +120,10 @@ fn format_field( if flatten { match (&type_as, &type_override, &rename, inline) { - (Some(_), _, _, _) => syn_err!("`as` is not compatible with `flatten`"), - (_, Some(_), _, _) => syn_err!("`type` is not compatible with `flatten`"), - (_, _, Some(_), _) => syn_err!("`rename` is not compatible with `flatten`"), - (_, _, _, true) => syn_err!("`inline` is not compatible with `flatten`"), + (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`"), _ => {} } @@ -175,12 +175,12 @@ fn extract_option_argument(ty: &Type) -> Result<&Type> { PathArguments::AngleBracketed(args) if args.args.len() == 1 => { match &args.args[0] { GenericArgument::Type(inner_ty) => Ok(inner_ty), - _ => syn_err!("`Option` argument must be a type"), + other => syn_err!(other.span(); "`Option` argument must be a type"), } } - _ => syn_err!("`Option` type must have a single generic argument"), + other => syn_err!(other.span(); "`Option` type must have a single generic argument"), } } - _ => syn_err!("`optional` can only be used on an Option type"), + other => syn_err!(other.span(); "`optional` can only be used on an Option type"), } } diff --git a/macros/src/types/newtype.rs b/macros/src/types/newtype.rs index 1b53e6c..99f6658 100644 --- a/macros/src/types/newtype.rs +++ b/macros/src/types/newtype.rs @@ -1,4 +1,4 @@ -use quote::quote; +use quote::{quote, ToTokens}; use syn::{FieldsUnnamed, Generics, Result, Type}; use crate::{ @@ -44,7 +44,7 @@ pub(crate) fn newtype( } let inner_ty = if let Some(ref type_as) = type_as { - syn::parse_str::(type_as)? + syn::parse_str::(&type_as.to_token_stream().to_string())? } else { inner.ty.clone() }; diff --git a/macros/src/types/tuple.rs b/macros/src/types/tuple.rs index b72cadd..4c0689e 100644 --- a/macros/src/types/tuple.rs +++ b/macros/src/types/tuple.rs @@ -1,6 +1,6 @@ use proc_macro2::TokenStream; -use quote::quote; -use syn::{Field, FieldsUnnamed, Generics, Result, Type}; +use quote::{quote, ToTokens}; +use syn::{Field, FieldsUnnamed, Generics, Result, Type, spanned::Spanned}; use crate::{ attr::{FieldAttr, StructAttr}, @@ -66,25 +66,25 @@ fn format_field( } let ty = if let Some(ref type_as) = type_as { - syn::parse_str::(type_as)? + syn::parse_str::(&type_as.to_token_stream().to_string())? } else { field.ty.clone() }; if type_as.is_some() && type_override.is_some() { - syn_err!("`type` is not compatible with `as`") + syn_err!(field.ty.span(); "`type` is not compatible with `as`") } if rename.is_some() { - syn_err!("`rename` is not applicable to tuple structs") + syn_err!(field.ty.span(); "`rename` is not applicable to tuple structs") } if optional.optional { - syn_err!("`optional` is not applicable to tuple fields") + syn_err!(field.ty.span(); "`optional` is not applicable to tuple fields") } if flatten { - syn_err!("`flatten` is not applicable to tuple fields") + syn_err!(field.ty.span(); "`flatten` is not applicable to tuple fields") } formatted_fields.push(match type_override { From b8a1df92ff64f2f84a3e373df4e183c6c7963e39 Mon Sep 17 00:00:00 2001 From: Gustavo Date: Thu, 7 Mar 2024 16:11:47 -0300 Subject: [PATCH 2/2] 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 {