From d91cfa7ef43c3bb92dd386dc7a234323d5ebfefd Mon Sep 17 00:00:00 2001 From: Michael Sproul Date: Tue, 6 Feb 2024 17:24:54 +1100 Subject: [PATCH] Reject multiple attributes on fields --- src/lib.rs | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/lib.rs b/src/lib.rs index ed65fc4..0243653 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -236,6 +236,9 @@ pub fn superstruct(args: TokenStream, input: TokenStream) -> TokenStream { .next() .unwrap_or_default(); + // Check for conflicting attributes. + check_for_conflicting_superstruct_attrs(&field.attrs); + // Drop the field-level superstruct attributes let mut output_field = field.clone(); output_field.attrs = discard_superstruct_attrs(&output_field.attrs); @@ -1080,6 +1083,21 @@ fn make_as_variant_method( } } +/// Check that there is at most one superstruct attribute, and panic otherwise. +fn check_for_conflicting_superstruct_attrs(attrs: &[Attribute]) { + if attrs + .iter() + .filter(|attr| is_superstruct_attr(attr)) + .count() + > 1 + { + // TODO: this is specific to fields right now, but we could maybe make it work for the + // top-level attributes. I'm just not sure how to get at them under the `AttributeArgs` + // stuff. + panic!("cannot handle more than one superstruct attribute per field"); + } +} + /// Keep all non-superstruct-related attributes from an array. fn discard_superstruct_attrs(attrs: &[Attribute]) -> Vec { attrs