@@ -46,8 +46,10 @@ use crate::utils::generate_unique_field_name;
4646pub fn error ( _args : TokenStream , input : TokenStream ) -> TokenStream {
4747 let mut input = parse_macro_input ! ( input as DeriveInput ) ;
4848
49+ if let Err ( err) = add_ohno_core_field ( & mut input) {
50+ return TokenStream :: from ( err. to_compile_error ( ) ) ;
51+ }
4952 add_fiasko_error_derive ( & mut input) ;
50- add_ohno_core_field ( & mut input) ;
5153
5254 TokenStream :: from ( quote ! { #input } )
5355}
@@ -61,7 +63,7 @@ fn add_fiasko_error_derive(input: &mut DeriveInput) {
6163 ) ;
6264}
6365
64- fn add_ohno_core_field ( input : & mut DeriveInput ) {
66+ fn add_ohno_core_field ( input : & mut DeriveInput ) -> syn :: Result < ( ) > {
6567 if let Data :: Struct ( data_struct) = & mut input. data {
6668 match & mut data_struct. fields {
6769 Fields :: Unit => {
@@ -95,8 +97,12 @@ fn add_ohno_core_field(input: &mut DeriveInput) {
9597 } ) ;
9698 }
9799 }
100+ Ok ( ( ) )
98101 } else {
99- // Not a struct, can't transform
102+ Err ( syn:: Error :: new_spanned (
103+ & input. ident ,
104+ "#[ohno::error] can only be applied to structs" ,
105+ ) )
100106 }
101107}
102108
@@ -134,7 +140,7 @@ mod tests {
134140 }
135141 } ;
136142
137- crate :: error_type_attr:: add_ohno_core_field ( & mut input) ;
143+ crate :: error_type_attr:: add_ohno_core_field ( & mut input) . unwrap ( ) ;
138144
139145 let expected: proc_macro2:: TokenStream = parse_quote ! {
140146 struct TestError {
@@ -149,19 +155,36 @@ mod tests {
149155
150156 #[ test]
151157 fn test_add_ohno_core_field_with_enum ( ) {
152- // Test that the function doesn't crash when given an enum
153- // This covers line 99 (the else branch for non-struct types)
158+ // Test that the function returns an error when given an enum
154159 let mut input: DeriveInput = parse_quote ! {
155160 enum TestError {
156161 Variant1 ,
157162 Variant2 ,
158163 }
159164 } ;
160165
161- let original = input. to_token_stream ( ) . to_string ( ) ;
162- crate :: error_type_attr:: add_ohno_core_field ( & mut input) ;
166+ let result = crate :: error_type_attr:: add_ohno_core_field ( & mut input) ;
167+ assert ! ( result. is_err( ) ) ;
168+ let err = result. unwrap_err ( ) ;
169+ assert ! ( err. to_string( ) . contains( "#[ohno::error] can only be applied to structs" ) ) ;
170+ }
171+
172+ #[ test]
173+ fn test_add_ohno_core_field_enum_produces_compile_error ( ) {
174+ // Verifies the to_compile_error() path used by the error proc macro (line 50)
175+ let mut input: DeriveInput = parse_quote ! {
176+ enum NotAStruct {
177+ A ,
178+ }
179+ } ;
180+
181+ let err = crate :: error_type_attr:: add_ohno_core_field ( & mut input) . unwrap_err ( ) ;
182+ let compile_error = err. to_compile_error ( ) . to_string ( ) ;
163183
164- // The enum should remain unchanged since we can't transform it
165- assert_eq ! ( input. to_token_stream( ) . to_string( ) , original) ;
184+ assert ! ( compile_error. contains( "compile_error" ) ) ;
185+ assert ! (
186+ compile_error. contains( "#[ohno::error] can only be applied to structs" ) ,
187+ "compile error should contain the expected message, got: {compile_error}"
188+ ) ;
166189 }
167190}
0 commit comments