From cfb5df7050afe08ba5c50805cc9d8532a3935988 Mon Sep 17 00:00:00 2001 From: David Pedersen Date: Wed, 22 Mar 2023 14:48:27 +0100 Subject: [PATCH] Give better error if generics are used with #[derive(FromRef)] (#1874) --- axum-macros/CHANGELOG.md | 2 ++ axum-macros/src/from_ref.rs | 16 +++++++++++++--- axum-macros/src/lib.rs | 2 +- axum-macros/tests/from_ref/fail/generics.rs | 8 ++++++++ axum-macros/tests/from_ref/fail/generics.stderr | 5 +++++ 5 files changed, 29 insertions(+), 4 deletions(-) create mode 100644 axum-macros/tests/from_ref/fail/generics.rs create mode 100644 axum-macros/tests/from_ref/fail/generics.stderr diff --git a/axum-macros/CHANGELOG.md b/axum-macros/CHANGELOG.md index 4bb7e6e7..abc8be40 100644 --- a/axum-macros/CHANGELOG.md +++ b/axum-macros/CHANGELOG.md @@ -8,8 +8,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 # Unreleased - **change:** Update to syn 2.0 ([#1862]) +- **fixed:** Give better error if generics are used with `#[derive(FromRef)]` ([#1874]) [#1862]: https://github.com/tokio-rs/axum/pull/1862 +[#1874]: https://github.com/tokio-rs/axum/pull/1874 # 0.3.6 (13. March, 2023) diff --git a/axum-macros/src/from_ref.rs b/axum-macros/src/from_ref.rs index 56628b9c..2ab69eb5 100644 --- a/axum-macros/src/from_ref.rs +++ b/axum-macros/src/from_ref.rs @@ -8,12 +8,22 @@ use syn::{ use crate::attr_parsing::{combine_unary_attribute, parse_attrs, Combine}; -pub(crate) fn expand(item: ItemStruct) -> TokenStream { - item.fields +pub(crate) fn expand(item: ItemStruct) -> syn::Result { + if !item.generics.params.is_empty() { + return Err(syn::Error::new_spanned( + item.generics, + "`#[derive(FromRef)]` doesn't support generics", + )); + } + + let tokens = item + .fields .iter() .enumerate() .map(|(idx, field)| expand_field(&item.ident, idx, field)) - .collect() + .collect(); + + Ok(tokens) } fn expand_field(state: &Ident, idx: usize, field: &Field) -> TokenStream { diff --git a/axum-macros/src/lib.rs b/axum-macros/src/lib.rs index 2fe5de10..584d8315 100644 --- a/axum-macros/src/lib.rs +++ b/axum-macros/src/lib.rs @@ -655,7 +655,7 @@ pub fn derive_typed_path(input: TokenStream) -> TokenStream { /// [`FromRef`]: https://docs.rs/axum/latest/axum/extract/trait.FromRef.html #[proc_macro_derive(FromRef, attributes(from_ref))] pub fn derive_from_ref(item: TokenStream) -> TokenStream { - expand_with(item, |item| Ok(from_ref::expand(item))) + expand_with(item, from_ref::expand) } fn expand_with(input: TokenStream, f: F) -> TokenStream diff --git a/axum-macros/tests/from_ref/fail/generics.rs b/axum-macros/tests/from_ref/fail/generics.rs new file mode 100644 index 00000000..2f9f2bb8 --- /dev/null +++ b/axum-macros/tests/from_ref/fail/generics.rs @@ -0,0 +1,8 @@ +use axum::extract::FromRef; + +#[derive(Clone, FromRef)] +struct AppState { + foo: T, +} + +fn main() {} diff --git a/axum-macros/tests/from_ref/fail/generics.stderr b/axum-macros/tests/from_ref/fail/generics.stderr new file mode 100644 index 00000000..bfe26643 --- /dev/null +++ b/axum-macros/tests/from_ref/fail/generics.stderr @@ -0,0 +1,5 @@ +error: `#[derive(FromRef)]` doesn't support generics + --> tests/from_ref/fail/generics.rs:4:16 + | +4 | struct AppState { + | ^^^