1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416
use crate::ast::{Enum, Field, Input, Struct}; use proc_macro2::TokenStream; use quote::{format_ident, quote, quote_spanned, ToTokens}; use syn::spanned::Spanned; use syn::{Data, DeriveInput, Member, PathArguments, Result, Type, Visibility}; pub fn derive(node: &DeriveInput) -> Result<TokenStream> { let input = Input::from_syn(node)?; input.validate()?; Ok(match input { Input::Struct(input) => impl_struct(input), Input::Enum(input) => impl_enum(input), }) } fn impl_struct(input: Struct) -> TokenStream { let ty = &input.ident; let (impl_generics, ty_generics, where_clause) = input.generics.split_for_impl(); let source_body = if input.attrs.transparent.is_some() { let only_field = &input.fields[0].member; Some(quote! { std::error::Error::source(self.#only_field.as_dyn_error()) }) } else if let Some(source_field) = input.source_field() { let source = &source_field.member; let asref = if type_is_option(source_field.ty) { Some(quote_spanned!(source.span()=> .as_ref()?)) } else { None }; let dyn_error = quote_spanned!(source.span()=> self.#source #asref.as_dyn_error()); Some(quote! { std::option::Option::Some(#dyn_error) }) } else { None }; let source_method = source_body.map(|body| { quote! { fn source(&self) -> std::option::Option<&(dyn std::error::Error + 'static)> { use thiserror::private::AsDynError; #body } } }); let backtrace_method = input.backtrace_field().map(|backtrace_field| { let backtrace = &backtrace_field.member; let body = if let Some(source_field) = input.source_field() { let source = &source_field.member; let source_backtrace = if type_is_option(source_field.ty) { quote_spanned! {source.span()=> self.#source.as_ref().and_then(|source| source.as_dyn_error().backtrace()) } } else { quote_spanned! {source.span()=> self.#source.as_dyn_error().backtrace() } }; let combinator = if type_is_option(backtrace_field.ty) { quote! { #source_backtrace.or(self.#backtrace.as_ref()) } } else { quote! { std::option::Option::Some(#source_backtrace.unwrap_or(&self.#backtrace)) } }; quote! { use thiserror::private::AsDynError; #combinator } } else if type_is_option(backtrace_field.ty) { quote! { self.#backtrace.as_ref() } } else { quote! { std::option::Option::Some(&self.#backtrace) } }; quote! { fn backtrace(&self) -> std::option::Option<&std::backtrace::Backtrace> { #body } } }); let display_body = if input.attrs.transparent.is_some() { let only_field = &input.fields[0].member; Some(quote! { std::fmt::Display::fmt(&self.#only_field, __formatter) }) } else if let Some(display) = &input.attrs.display { let use_as_display = if display.has_bonus_display { Some(quote! { #[allow(unused_imports)] use thiserror::private::{DisplayAsDisplay, PathAsDisplay}; }) } else { None }; let pat = fields_pat(&input.fields); Some(quote! { #use_as_display #[allow(unused_variables, deprecated)] let Self #pat = self; #display }) } else { None }; let display_impl = display_body.map(|body| { quote! { #[allow(unused_qualifications)] impl #impl_generics std::fmt::Display for #ty #ty_generics #where_clause { #[allow(clippy::used_underscore_binding)] fn fmt(&self, __formatter: &mut std::fmt::Formatter) -> std::fmt::Result { #body } } } }); let from_impl = input.from_field().map(|from_field| { let backtrace_field = input.backtrace_field(); let from = from_field.ty; let body = from_initializer(from_field, backtrace_field); quote! { #[allow(unused_qualifications)] impl #impl_generics std::convert::From<#from> for #ty #ty_generics #where_clause { #[allow(deprecated)] fn from(source: #from) -> Self { #ty #body } } } }); let error_trait = spanned_error_trait(input.original); quote! { #[allow(unused_qualifications)] impl #impl_generics #error_trait for #ty #ty_generics #where_clause { #source_method #backtrace_method } #display_impl #from_impl } } fn impl_enum(input: Enum) -> TokenStream { let ty = &input.ident; let (impl_generics, ty_generics, where_clause) = input.generics.split_for_impl(); let source_method = if input.has_source() { let arms = input.variants.iter().map(|variant| { let ident = &variant.ident; if variant.attrs.transparent.is_some() { let only_field = &variant.fields[0].member; let source = quote!(std::error::Error::source(transparent.as_dyn_error())); quote! { #ty::#ident {#only_field: transparent} => #source, } } else if let Some(source_field) = variant.source_field() { let source = &source_field.member; let asref = if type_is_option(source_field.ty) { Some(quote_spanned!(source.span()=> .as_ref()?)) } else { None }; let varsource = quote!(source); let dyn_error = quote_spanned!(source.span()=> #varsource #asref.as_dyn_error()); quote! { #ty::#ident {#source: #varsource, ..} => std::option::Option::Some(#dyn_error), } } else { quote! { #ty::#ident {..} => std::option::Option::None, } } }); Some(quote! { fn source(&self) -> std::option::Option<&(dyn std::error::Error + 'static)> { use thiserror::private::AsDynError; #[allow(deprecated)] match self { #(#arms)* } } }) } else { None }; let backtrace_method = if input.has_backtrace() { let arms = input.variants.iter().map(|variant| { let ident = &variant.ident; match (variant.backtrace_field(), variant.source_field()) { (Some(backtrace_field), Some(source_field)) if backtrace_field.attrs.backtrace.is_none() => { let backtrace = &backtrace_field.member; let source = &source_field.member; let varsource = quote!(source); let source_backtrace = if type_is_option(source_field.ty) { quote_spanned! {source.span()=> #varsource.as_ref().and_then(|source| source.as_dyn_error().backtrace()) } } else { quote_spanned! {source.span()=> #varsource.as_dyn_error().backtrace() } }; let combinator = if type_is_option(backtrace_field.ty) { quote! { #source_backtrace.or(backtrace.as_ref()) } } else { quote! { std::option::Option::Some(#source_backtrace.unwrap_or(backtrace)) } }; quote! { #ty::#ident { #backtrace: backtrace, #source: #varsource, .. } => { use thiserror::private::AsDynError; #combinator } } } (Some(backtrace_field), _) => { let backtrace = &backtrace_field.member; let body = if type_is_option(backtrace_field.ty) { quote!(backtrace.as_ref()) } else { quote!(std::option::Option::Some(backtrace)) }; quote! { #ty::#ident {#backtrace: backtrace, ..} => #body, } } (None, _) => quote! { #ty::#ident {..} => std::option::Option::None, }, } }); Some(quote! { fn backtrace(&self) -> std::option::Option<&std::backtrace::Backtrace> { #[allow(deprecated)] match self { #(#arms)* } } }) } else { None }; let display_impl = if input.has_display() { let use_as_display = if input.variants.iter().any(|v| { v.attrs .display .as_ref() .map_or(false, |display| display.has_bonus_display) }) { Some(quote! { #[allow(unused_imports)] use thiserror::private::{DisplayAsDisplay, PathAsDisplay}; }) } else { None }; let void_deref = if input.variants.is_empty() { Some(quote!(*)) } else { None }; let arms = input.variants.iter().map(|variant| { let display = match &variant.attrs.display { Some(display) => display.to_token_stream(), None => { let only_field = match &variant.fields[0].member { Member::Named(ident) => ident.clone(), Member::Unnamed(index) => format_ident!("_{}", index), }; quote!(std::fmt::Display::fmt(#only_field, __formatter)) } }; let ident = &variant.ident; let pat = fields_pat(&variant.fields); quote! { #ty::#ident #pat => #display } }); Some(quote! { #[allow(unused_qualifications)] impl #impl_generics std::fmt::Display for #ty #ty_generics #where_clause { fn fmt(&self, __formatter: &mut std::fmt::Formatter) -> std::fmt::Result { #use_as_display #[allow(unused_variables, deprecated, clippy::used_underscore_binding)] match #void_deref self { #(#arms,)* } } } }) } else { None }; let from_impls = input.variants.iter().filter_map(|variant| { let from_field = variant.from_field()?; let backtrace_field = variant.backtrace_field(); let variant = &variant.ident; let from = from_field.ty; let body = from_initializer(from_field, backtrace_field); Some(quote! { #[allow(unused_qualifications)] impl #impl_generics std::convert::From<#from> for #ty #ty_generics #where_clause { #[allow(deprecated)] fn from(source: #from) -> Self { #ty::#variant #body } } }) }); let error_trait = spanned_error_trait(input.original); quote! { #[allow(unused_qualifications)] impl #impl_generics #error_trait for #ty #ty_generics #where_clause { #source_method #backtrace_method } #display_impl #(#from_impls)* } } fn fields_pat(fields: &[Field]) -> TokenStream { let mut members = fields.iter().map(|field| &field.member).peekable(); match members.peek() { Some(Member::Named(_)) => quote!({ #(#members),* }), Some(Member::Unnamed(_)) => { let vars = members.map(|member| match member { Member::Unnamed(member) => format_ident!("_{}", member), Member::Named(_) => unreachable!(), }); quote!((#(#vars),*)) } None => quote!({}), } } fn from_initializer(from_field: &Field, backtrace_field: Option<&Field>) -> TokenStream { let from_member = &from_field.member; let backtrace = backtrace_field.map(|backtrace_field| { let backtrace_member = &backtrace_field.member; if type_is_option(backtrace_field.ty) { quote! { #backtrace_member: std::option::Option::Some(std::backtrace::Backtrace::capture()), } } else { quote! { #backtrace_member: std::convert::From::from(std::backtrace::Backtrace::capture()), } } }); quote!({ #from_member: source, #backtrace }) } fn type_is_option(ty: &Type) -> bool { let path = match ty { Type::Path(ty) => &ty.path, _ => return false, }; let last = path.segments.last().unwrap(); if last.ident != "Option" { return false; } match &last.arguments { PathArguments::AngleBracketed(bracketed) => bracketed.args.len() == 1, _ => false, } } fn spanned_error_trait(input: &DeriveInput) -> TokenStream { let vis_span = match &input.vis { Visibility::Public(vis) => Some(vis.pub_token.span()), Visibility::Crate(vis) => Some(vis.crate_token.span()), Visibility::Restricted(vis) => Some(vis.pub_token.span()), Visibility::Inherited => None, }; let data_span = match &input.data { Data::Struct(data) => data.struct_token.span(), Data::Enum(data) => data.enum_token.span(), Data::Union(data) => data.union_token.span(), }; let first_span = vis_span.unwrap_or(data_span); let last_span = input.ident.span(); let path = quote_spanned!(first_span=> std::error::); let error = quote_spanned!(last_span=> Error); quote!(#path #error) }