From 2922dd5c8758ae4d351a09118d84b65d06f42734 Mon Sep 17 00:00:00 2001 From: Ted Driggs Date: Wed, 10 Feb 2021 10:46:49 -0800 Subject: [PATCH] Remove use of unreachable for unions Fixes #85 --- core/src/ast/data.rs | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/core/src/ast/data.rs b/core/src/ast/data.rs index 69a075e..5d9fb36 100644 --- a/core/src/ast/data.rs +++ b/core/src/ast/data.rs @@ -20,11 +20,14 @@ pub enum Data { impl Data { /// Creates an empty body of the same shape as the passed-in body. + /// + /// # Panics + /// This function will panic if passed `syn::Data::Union`. pub fn empty_from(src: &syn::Data) -> Self { match *src { syn::Data::Enum(_) => Data::Enum(vec![]), syn::Data::Struct(ref vd) => Data::Struct(Fields::empty_from(&vd.fields)), - syn::Data::Union(_) => unreachable!(), + syn::Data::Union(_) => panic!("Unions are not supported"), } } @@ -120,7 +123,10 @@ impl Data { } } syn::Data::Struct(ref data) => Ok(Data::Struct(Fields::try_from(&data.fields)?)), - syn::Data::Union(_) => unreachable!(), + // This deliberately doesn't set a span on the error message, as the error is most useful if + // applied to the call site of the offending macro. Given that the message is very generic, + // putting it on the union keyword ends up being confusing. + syn::Data::Union(_) => Err(Error::custom("Unions are not supported")), } } }