summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMatthew Maurer <mmaurer@google.com>2020-06-02 11:15:31 -0700
committerMatthew Maurer <mmaurer@google.com>2020-06-02 11:15:31 -0700
commit3bb580618503f71ead9cda932ff5a5c0b8bb190e (patch)
treea98dc7b32d0027a8efb90e780f8ed1912c52c212
parent679f44bdd835c3f946ae2677e1f567910103b738 (diff)
downloadthiserror-impl-3bb580618503f71ead9cda932ff5a5c0b8bb190e.tar.gz
Import thiserror-impl-1.0.16
Change-Id: If439e1c68f7afb772354f3c07ebfec7a94b01300
-rw-r--r--.cargo_vcs_info.json5
-rw-r--r--Cargo.toml35
-rw-r--r--Cargo.toml.orig22
-rw-r--r--LICENSE-APACHE201
-rw-r--r--LICENSE-MIT23
-rw-r--r--src/ast.rs133
-rw-r--r--src/attr.rs181
-rw-r--r--src/expand.rs407
-rw-r--r--src/fmt.rs138
-rw-r--r--src/lib.rs21
-rw-r--r--src/prop.rs111
-rw-r--r--src/valid.rs226
12 files changed, 1503 insertions, 0 deletions
diff --git a/.cargo_vcs_info.json b/.cargo_vcs_info.json
new file mode 100644
index 0000000..30d59f6
--- /dev/null
+++ b/.cargo_vcs_info.json
@@ -0,0 +1,5 @@
+{
+ "git": {
+ "sha1": "94e62a81bc6da67efb06a45a8f45caf49768d5e4"
+ }
+}
diff --git a/Cargo.toml b/Cargo.toml
new file mode 100644
index 0000000..9197bc0
--- /dev/null
+++ b/Cargo.toml
@@ -0,0 +1,35 @@
+# THIS FILE IS AUTOMATICALLY GENERATED BY CARGO
+#
+# When uploading crates to the registry Cargo will automatically
+# "normalize" Cargo.toml files for maximal compatibility
+# with all versions of Cargo and also rewrite `path` dependencies
+# to registry (e.g., crates.io) dependencies
+#
+# If you believe there's an error in this file please file an
+# issue against the rust-lang/cargo repository. If you're
+# editing this file be aware that the upstream Cargo.toml
+# will likely look very different (and much more reasonable)
+
+[package]
+edition = "2018"
+name = "thiserror-impl"
+version = "1.0.16"
+authors = ["David Tolnay <dtolnay@gmail.com>"]
+description = "Implementation detail of the `thiserror` crate"
+license = "MIT OR Apache-2.0"
+repository = "https://github.com/dtolnay/thiserror"
+[package.metadata.docs.rs]
+targets = ["x86_64-unknown-linux-gnu"]
+
+[lib]
+proc-macro = true
+[dependencies.proc-macro2]
+version = "1.0"
+
+[dependencies.quote]
+version = "1.0"
+
+[dependencies.syn]
+version = "1.0.11"
+[badges.travis-ci]
+repository = "dtolnay/thiserror"
diff --git a/Cargo.toml.orig b/Cargo.toml.orig
new file mode 100644
index 0000000..46363da
--- /dev/null
+++ b/Cargo.toml.orig
@@ -0,0 +1,22 @@
+[package]
+name = "thiserror-impl"
+version = "1.0.16"
+authors = ["David Tolnay <dtolnay@gmail.com>"]
+edition = "2018"
+license = "MIT OR Apache-2.0"
+description = "Implementation detail of the `thiserror` crate"
+repository = "https://github.com/dtolnay/thiserror"
+
+[lib]
+proc-macro = true
+
+[badges]
+travis-ci = { repository = "dtolnay/thiserror" }
+
+[dependencies]
+proc-macro2 = "1.0"
+quote = "1.0"
+syn = "1.0.11"
+
+[package.metadata.docs.rs]
+targets = ["x86_64-unknown-linux-gnu"]
diff --git a/LICENSE-APACHE b/LICENSE-APACHE
new file mode 100644
index 0000000..16fe87b
--- /dev/null
+++ b/LICENSE-APACHE
@@ -0,0 +1,201 @@
+ Apache License
+ Version 2.0, January 2004
+ http://www.apache.org/licenses/
+
+TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
+
+1. Definitions.
+
+ "License" shall mean the terms and conditions for use, reproduction,
+ and distribution as defined by Sections 1 through 9 of this document.
+
+ "Licensor" shall mean the copyright owner or entity authorized by
+ the copyright owner that is granting the License.
+
+ "Legal Entity" shall mean the union of the acting entity and all
+ other entities that control, are controlled by, or are under common
+ control with that entity. For the purposes of this definition,
+ "control" means (i) the power, direct or indirect, to cause the
+ direction or management of such entity, whether by contract or
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
+ outstanding shares, or (iii) beneficial ownership of such entity.
+
+ "You" (or "Your") shall mean an individual or Legal Entity
+ exercising permissions granted by this License.
+
+ "Source" form shall mean the preferred form for making modifications,
+ including but not limited to software source code, documentation
+ source, and configuration files.
+
+ "Object" form shall mean any form resulting from mechanical
+ transformation or translation of a Source form, including but
+ not limited to compiled object code, generated documentation,
+ and conversions to other media types.
+
+ "Work" shall mean the work of authorship, whether in Source or
+ Object form, made available under the License, as indicated by a
+ copyright notice that is included in or attached to the work
+ (an example is provided in the Appendix below).
+
+ "Derivative Works" shall mean any work, whether in Source or Object
+ form, that is based on (or derived from) the Work and for which the
+ editorial revisions, annotations, elaborations, or other modifications
+ represent, as a whole, an original work of authorship. For the purposes
+ of this License, Derivative Works shall not include works that remain
+ separable from, or merely link (or bind by name) to the interfaces of,
+ the Work and Derivative Works thereof.
+
+ "Contribution" shall mean any work of authorship, including
+ the original version of the Work and any modifications or additions
+ to that Work or Derivative Works thereof, that is intentionally
+ submitted to Licensor for inclusion in the Work by the copyright owner
+ or by an individual or Legal Entity authorized to submit on behalf of
+ the copyright owner. For the purposes of this definition, "submitted"
+ means any form of electronic, verbal, or written communication sent
+ to the Licensor or its representatives, including but not limited to
+ communication on electronic mailing lists, source code control systems,
+ and issue tracking systems that are managed by, or on behalf of, the
+ Licensor for the purpose of discussing and improving the Work, but
+ excluding communication that is conspicuously marked or otherwise
+ designated in writing by the copyright owner as "Not a Contribution."
+
+ "Contributor" shall mean Licensor and any individual or Legal Entity
+ on behalf of whom a Contribution has been received by Licensor and
+ subsequently incorporated within the Work.
+
+2. Grant of Copyright License. Subject to the terms and conditions of
+ this License, each Contributor hereby grants to You a perpetual,
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
+ copyright license to reproduce, prepare Derivative Works of,
+ publicly display, publicly perform, sublicense, and distribute the
+ Work and such Derivative Works in Source or Object form.
+
+3. Grant of Patent License. Subject to the terms and conditions of
+ this License, each Contributor hereby grants to You a perpetual,
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
+ (except as stated in this section) patent license to make, have made,
+ use, offer to sell, sell, import, and otherwise transfer the Work,
+ where such license applies only to those patent claims licensable
+ by such Contributor that are necessarily infringed by their
+ Contribution(s) alone or by combination of their Contribution(s)
+ with the Work to which such Contribution(s) was submitted. If You
+ institute patent litigation against any entity (including a
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
+ or a Contribution incorporated within the Work constitutes direct
+ or contributory patent infringement, then any patent licenses
+ granted to You under this License for that Work shall terminate
+ as of the date such litigation is filed.
+
+4. Redistribution. You may reproduce and distribute copies of the
+ Work or Derivative Works thereof in any medium, with or without
+ modifications, and in Source or Object form, provided that You
+ meet the following conditions:
+
+ (a) You must give any other recipients of the Work or
+ Derivative Works a copy of this License; and
+
+ (b) You must cause any modified files to carry prominent notices
+ stating that You changed the files; and
+
+ (c) You must retain, in the Source form of any Derivative Works
+ that You distribute, all copyright, patent, trademark, and
+ attribution notices from the Source form of the Work,
+ excluding those notices that do not pertain to any part of
+ the Derivative Works; and
+
+ (d) If the Work includes a "NOTICE" text file as part of its
+ distribution, then any Derivative Works that You distribute must
+ include a readable copy of the attribution notices contained
+ within such NOTICE file, excluding those notices that do not
+ pertain to any part of the Derivative Works, in at least one
+ of the following places: within a NOTICE text file distributed
+ as part of the Derivative Works; within the Source form or
+ documentation, if provided along with the Derivative Works; or,
+ within a display generated by the Derivative Works, if and
+ wherever such third-party notices normally appear. The contents
+ of the NOTICE file are for informational purposes only and
+ do not modify the License. You may add Your own attribution
+ notices within Derivative Works that You distribute, alongside
+ or as an addendum to the NOTICE text from the Work, provided
+ that such additional attribution notices cannot be construed
+ as modifying the License.
+
+ You may add Your own copyright statement to Your modifications and
+ may provide additional or different license terms and conditions
+ for use, reproduction, or distribution of Your modifications, or
+ for any such Derivative Works as a whole, provided Your use,
+ reproduction, and distribution of the Work otherwise complies with
+ the conditions stated in this License.
+
+5. Submission of Contributions. Unless You explicitly state otherwise,
+ any Contribution intentionally submitted for inclusion in the Work
+ by You to the Licensor shall be under the terms and conditions of
+ this License, without any additional terms or conditions.
+ Notwithstanding the above, nothing herein shall supersede or modify
+ the terms of any separate license agreement you may have executed
+ with Licensor regarding such Contributions.
+
+6. Trademarks. This License does not grant permission to use the trade
+ names, trademarks, service marks, or product names of the Licensor,
+ except as required for reasonable and customary use in describing the
+ origin of the Work and reproducing the content of the NOTICE file.
+
+7. Disclaimer of Warranty. Unless required by applicable law or
+ agreed to in writing, Licensor provides the Work (and each
+ Contributor provides its Contributions) on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
+ implied, including, without limitation, any warranties or conditions
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
+ PARTICULAR PURPOSE. You are solely responsible for determining the
+ appropriateness of using or redistributing the Work and assume any
+ risks associated with Your exercise of permissions under this License.
+
+8. Limitation of Liability. In no event and under no legal theory,
+ whether in tort (including negligence), contract, or otherwise,
+ unless required by applicable law (such as deliberate and grossly
+ negligent acts) or agreed to in writing, shall any Contributor be
+ liable to You for damages, including any direct, indirect, special,
+ incidental, or consequential damages of any character arising as a
+ result of this License or out of the use or inability to use the
+ Work (including but not limited to damages for loss of goodwill,
+ work stoppage, computer failure or malfunction, or any and all
+ other commercial damages or losses), even if such Contributor
+ has been advised of the possibility of such damages.
+
+9. Accepting Warranty or Additional Liability. While redistributing
+ the Work or Derivative Works thereof, You may choose to offer,
+ and charge a fee for, acceptance of support, warranty, indemnity,
+ or other liability obligations and/or rights consistent with this
+ License. However, in accepting such obligations, You may act only
+ on Your own behalf and on Your sole responsibility, not on behalf
+ of any other Contributor, and only if You agree to indemnify,
+ defend, and hold each Contributor harmless for any liability
+ incurred by, or claims asserted against, such Contributor by reason
+ of your accepting any such warranty or additional liability.
+
+END OF TERMS AND CONDITIONS
+
+APPENDIX: How to apply the Apache License to your work.
+
+ To apply the Apache License to your work, attach the following
+ boilerplate notice, with the fields enclosed by brackets "[]"
+ replaced with your own identifying information. (Don't include
+ the brackets!) The text should be enclosed in the appropriate
+ comment syntax for the file format. We also recommend that a
+ file or class name and description of purpose be included on the
+ same "printed page" as the copyright notice for easier
+ identification within third-party archives.
+
+Copyright [yyyy] [name of copyright owner]
+
+Licensed under the Apache License, Version 2.0 (the "License");
+you may not use this file except in compliance with the License.
+You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
diff --git a/LICENSE-MIT b/LICENSE-MIT
new file mode 100644
index 0000000..31aa793
--- /dev/null
+++ b/LICENSE-MIT
@@ -0,0 +1,23 @@
+Permission is hereby granted, free of charge, to any
+person obtaining a copy of this software and associated
+documentation files (the "Software"), to deal in the
+Software without restriction, including without
+limitation the rights to use, copy, modify, merge,
+publish, distribute, sublicense, and/or sell copies of
+the Software, and to permit persons to whom the Software
+is furnished to do so, subject to the following
+conditions:
+
+The above copyright notice and this permission notice
+shall be included in all copies or substantial portions
+of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
+ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
+TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
+PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
+SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
+CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
+IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+DEALINGS IN THE SOFTWARE.
diff --git a/src/ast.rs b/src/ast.rs
new file mode 100644
index 0000000..e16401f
--- /dev/null
+++ b/src/ast.rs
@@ -0,0 +1,133 @@
+use crate::attr::{self, Attrs};
+use syn::{
+ Data, DataEnum, DataStruct, DeriveInput, Error, Fields, Generics, Ident, Index, Member, Result,
+ Type,
+};
+
+pub enum Input<'a> {
+ Struct(Struct<'a>),
+ Enum(Enum<'a>),
+}
+
+pub struct Struct<'a> {
+ pub original: &'a DeriveInput,
+ pub attrs: Attrs<'a>,
+ pub ident: Ident,
+ pub generics: &'a Generics,
+ pub fields: Vec<Field<'a>>,
+}
+
+pub struct Enum<'a> {
+ pub original: &'a DeriveInput,
+ pub attrs: Attrs<'a>,
+ pub ident: Ident,
+ pub generics: &'a Generics,
+ pub variants: Vec<Variant<'a>>,
+}
+
+pub struct Variant<'a> {
+ pub original: &'a syn::Variant,
+ pub attrs: Attrs<'a>,
+ pub ident: Ident,
+ pub fields: Vec<Field<'a>>,
+}
+
+pub struct Field<'a> {
+ pub original: &'a syn::Field,
+ pub attrs: Attrs<'a>,
+ pub member: Member,
+ pub ty: &'a Type,
+}
+
+impl<'a> Input<'a> {
+ pub fn from_syn(node: &'a DeriveInput) -> Result<Self> {
+ match &node.data {
+ Data::Struct(data) => Struct::from_syn(node, data).map(Input::Struct),
+ Data::Enum(data) => Enum::from_syn(node, data).map(Input::Enum),
+ Data::Union(_) => Err(Error::new_spanned(
+ node,
+ "union as errors are not supported",
+ )),
+ }
+ }
+}
+
+impl<'a> Struct<'a> {
+ fn from_syn(node: &'a DeriveInput, data: &'a DataStruct) -> Result<Self> {
+ let mut attrs = attr::get(&node.attrs)?;
+ let fields = Field::multiple_from_syn(&data.fields)?;
+ if let Some(display) = &mut attrs.display {
+ display.expand_shorthand(&fields);
+ }
+ Ok(Struct {
+ original: node,
+ attrs,
+ ident: node.ident.clone(),
+ generics: &node.generics,
+ fields,
+ })
+ }
+}
+
+impl<'a> Enum<'a> {
+ fn from_syn(node: &'a DeriveInput, data: &'a DataEnum) -> Result<Self> {
+ let attrs = attr::get(&node.attrs)?;
+ let variants = data
+ .variants
+ .iter()
+ .map(|node| {
+ let mut variant = Variant::from_syn(node)?;
+ if let display @ None = &mut variant.attrs.display {
+ *display = attrs.display.clone();
+ }
+ if let Some(display) = &mut variant.attrs.display {
+ display.expand_shorthand(&variant.fields);
+ } else if variant.attrs.transparent.is_none() {
+ variant.attrs.transparent = attrs.transparent;
+ }
+ Ok(variant)
+ })
+ .collect::<Result<_>>()?;
+ Ok(Enum {
+ original: node,
+ attrs,
+ ident: node.ident.clone(),
+ generics: &node.generics,
+ variants,
+ })
+ }
+}
+
+impl<'a> Variant<'a> {
+ fn from_syn(node: &'a syn::Variant) -> Result<Self> {
+ Ok(Variant {
+ original: node,
+ attrs: attr::get(&node.attrs)?,
+ ident: node.ident.clone(),
+ fields: Field::multiple_from_syn(&node.fields)?,
+ })
+ }
+}
+
+impl<'a> Field<'a> {
+ fn multiple_from_syn(fields: &'a Fields) -> Result<Vec<Self>> {
+ fields
+ .iter()
+ .enumerate()
+ .map(|(i, field)| Field::from_syn(i, field))
+ .collect()
+ }
+
+ fn from_syn(i: usize, node: &'a syn::Field) -> Result<Self> {
+ Ok(Field {
+ original: node,
+ attrs: attr::get(&node.attrs)?,
+ member: node
+ .ident
+ .clone()
+ .map(Member::Named)
+ .unwrap_or_else(|| Member::Unnamed(Index::from(i))),
+ ty: &node.ty,
+ })
+ }
+}
diff --git a/src/attr.rs b/src/attr.rs
new file mode 100644
index 0000000..30ecfc8
--- /dev/null
+++ b/src/attr.rs
@@ -0,0 +1,181 @@
+use proc_macro2::{Delimiter, Group, TokenStream, TokenTree};
+use quote::{format_ident, quote, ToTokens};
+use std::iter::FromIterator;
+use syn::parse::{Nothing, ParseStream};
+use syn::{
+ braced, bracketed, parenthesized, token, Attribute, Error, Ident, Index, LitInt, LitStr,
+ Result, Token,
+};
+
+pub struct Attrs<'a> {
+ pub display: Option<Display<'a>>,
+ pub source: Option<&'a Attribute>,
+ pub backtrace: Option<&'a Attribute>,
+ pub from: Option<&'a Attribute>,
+ pub transparent: Option<&'a Attribute>,
+}
+
+#[derive(Clone)]
+pub struct Display<'a> {
+ pub original: &'a Attribute,
+ pub fmt: LitStr,
+ pub args: TokenStream,
+ pub has_bonus_display: bool,
+}
+
+pub fn get(input: &[Attribute]) -> Result<Attrs> {
+ let mut attrs = Attrs {
+ display: None,
+ source: None,
+ backtrace: None,
+ from: None,
+ transparent: None,
+ };
+
+ for attr in input {
+ if attr.path.is_ident("error") {
+ parse_error_attribute(&mut attrs, attr)?;
+ } else if attr.path.is_ident("source") {
+ require_empty_attribute(attr)?;
+ if attrs.source.is_some() {
+ return Err(Error::new_spanned(attr, "duplicate #[source] attribute"));
+ }
+ attrs.source = Some(attr);
+ } else if attr.path.is_ident("backtrace") {
+ require_empty_attribute(attr)?;
+ if attrs.backtrace.is_some() {
+ return Err(Error::new_spanned(attr, "duplicate #[backtrace] attribute"));
+ }
+ attrs.backtrace = Some(attr);
+ } else if attr.path.is_ident("from") {
+ if !attr.tokens.is_empty() {
+ // Assume this is meant for derive_more crate or something.
+ continue;
+ }
+ if attrs.from.is_some() {
+ return Err(Error::new_spanned(attr, "duplicate #[from] attribute"));
+ }
+ attrs.from = Some(attr);
+ }
+ }
+
+ Ok(attrs)
+}
+
+fn parse_error_attribute<'a>(attrs: &mut Attrs<'a>, attr: &'a Attribute) -> Result<()> {
+ syn::custom_keyword!(transparent);
+
+ attr.parse_args_with(|input: ParseStream| {
+ if input.parse::<Option<transparent>>()?.is_some() {
+ if attrs.transparent.is_some() {
+ return Err(Error::new_spanned(
+ attr,
+ "duplicate #[error(transparent)] attribute",
+ ));
+ }
+ attrs.transparent = Some(attr);
+ return Ok(());
+ }
+
+ let display = Display {
+ original: attr,
+ fmt: input.parse()?,
+ args: parse_token_expr(input, false)?,
+ has_bonus_display: false,
+ };
+ if attrs.display.is_some() {
+ return Err(Error::new_spanned(
+ attr,
+ "only one #[error(...)] attribute is allowed",
+ ));
+ }
+ attrs.display = Some(display);
+ Ok(())
+ })
+}
+
+fn parse_token_expr(input: ParseStream, mut begin_expr: bool) -> Result<TokenStream> {
+ let mut tokens = Vec::new();
+ while !input.is_empty() {
+ if begin_expr && input.peek(Token![.]) {
+ if input.peek2(Ident) {
+ input.parse::<Token![.]>()?;
+ begin_expr = false;
+ continue;
+ }
+ if input.peek2(LitInt) {
+ input.parse::<Token![.]>()?;
+ let int: Index = input.parse()?;
+ let ident = format_ident!("_{}", int.index, span = int.span);
+ tokens.push(TokenTree::Ident(ident));
+ begin_expr = false;
+ continue;
+ }
+ }
+
+ begin_expr = input.peek(Token![break])
+ || input.peek(Token![continue])
+ || input.peek(Token![if])
+ || input.peek(Token![in])
+ || input.peek(Token![match])
+ || input.peek(Token![mut])
+ || input.peek(Token![return])
+ || input.peek(Token![while])
+ || input.peek(Token![+])
+ || input.peek(Token![&])
+ || input.peek(Token![!])
+ || input.peek(Token![^])
+ || input.peek(Token![,])
+ || input.peek(Token![/])
+ || input.peek(Token![=])
+ || input.peek(Token![>])
+ || input.peek(Token![<])
+ || input.peek(Token![|])
+ || input.peek(Token![%])
+ || input.peek(Token![;])
+ || input.peek(Token![*])
+ || input.peek(Token![-]);
+
+ let token: TokenTree = if input.peek(token::Paren) {
+ let content;
+ let delimiter = parenthesized!(content in input);
+ let nested = parse_token_expr(&content, true)?;
+ let mut group = Group::new(Delimiter::Parenthesis, nested);
+ group.set_span(delimiter.span);
+ TokenTree::Group(group)
+ } else if input.peek(token::Brace) {
+ let content;
+ let delimiter = braced!(content in input);
+ let nested = parse_token_expr(&content, true)?;
+ let mut group = Group::new(Delimiter::Brace, nested);
+ group.set_span(delimiter.span);
+ TokenTree::Group(group)
+ } else if input.peek(token::Bracket) {
+ let content;
+ let delimiter = bracketed!(content in input);
+ let nested = parse_token_expr(&content, true)?;
+ let mut group = Group::new(Delimiter::Bracket, nested);
+ group.set_span(delimiter.span);
+ TokenTree::Group(group)
+ } else {
+ input.parse()?
+ };
+ tokens.push(token);
+ }
+ Ok(TokenStream::from_iter(tokens))
+}
+
+fn require_empty_attribute(attr: &Attribute) -> Result<()> {
+ syn::parse2::<Nothing>(attr.tokens.clone())?;
+ Ok(())
+}
+
+impl ToTokens for Display<'_> {
+ fn to_tokens(&self, tokens: &mut TokenStream) {
+ let fmt = &self.fmt;
+ let args = &self.args;
+ tokens.extend(quote! {
+ write!(__formatter, #fmt #args)
+ });
+ }
+}
diff --git a/src/expand.rs b/src/expand.rs
new file mode 100644
index 0000000..3da8f41
--- /dev/null
+++ b/src/expand.rs
@@ -0,0 +1,407 @@
+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! {
+ impl #impl_generics std::fmt::Display for #ty #ty_generics #where_clause {
+ 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! {
+ 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! {
+ 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 dyn_error = quote_spanned!(source.span()=> source #asref.as_dyn_error());
+ quote! {
+ #ty::#ident {#source: source, ..} => 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 source_backtrace = if type_is_option(source_field.ty) {
+ quote_spanned! {source.span()=>
+ source.as_ref().and_then(|source| source.as_dyn_error().backtrace())
+ }
+ } else {
+ quote_spanned! {source.span()=>
+ source.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: source,
+ ..
+ } => {
+ 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! {
+ 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)]
+ 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! {
+ 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! {
+ 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::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)
+}
diff --git a/src/fmt.rs b/src/fmt.rs
new file mode 100644
index 0000000..ea1b518
--- /dev/null
+++ b/src/fmt.rs
@@ -0,0 +1,138 @@
+use crate::ast::Field;
+use crate::attr::Display;
+use proc_macro2::TokenTree;
+use quote::{format_ident, quote_spanned};
+use std::collections::HashSet as Set;
+use syn::ext::IdentExt;
+use syn::parse::{ParseStream, Parser};
+use syn::{Ident, Index, LitStr, Member, Result, Token};
+
+impl Display<'_> {
+ // Transform `"error {var}"` to `"error {}", var`.
+ pub fn expand_shorthand(&mut self, fields: &[Field]) {
+ let raw_args = self.args.clone();
+ let mut named_args = explicit_named_args.parse2(raw_args).unwrap();
+ let fields: Set<Member> = fields.iter().map(|f| f.member.clone()).collect();
+
+ let span = self.fmt.span();
+ let fmt = self.fmt.value();
+ let mut read = fmt.as_str();
+ let mut out = String::new();
+ let mut args = self.args.clone();
+ let mut has_bonus_display = false;
+
+ let mut has_trailing_comma = false;
+ if let Some(TokenTree::Punct(punct)) = args.clone().into_iter().last() {
+ if punct.as_char() == ',' {
+ has_trailing_comma = true;
+ }
+ }
+
+ while let Some(brace) = read.find('{') {
+ out += &read[..brace + 1];
+ read = &read[brace + 1..];
+ if read.starts_with('{') {
+ out.push('{');
+ read = &read[1..];
+ continue;
+ }
+ let next = match read.chars().next() {
+ Some(next) => next,
+ None => return,
+ };
+ let member = match next {
+ '0'..='9' => {
+ let int = take_int(&mut read);
+ let member = match int.parse::<u32>() {
+ Ok(index) => Member::Unnamed(Index { index, span }),
+ Err(_) => return,
+ };
+ if !fields.contains(&member) {
+ out += &int;
+ continue;
+ }
+ member
+ }
+ 'a'..='z' | 'A'..='Z' | '_' => {
+ let ident = take_ident(&mut read);
+ Member::Named(Ident::new(&ident, span))
+ }
+ _ => continue,
+ };
+ let local = match &member {
+ Member::Unnamed(index) => format_ident!("_{}", index),
+ Member::Named(ident) => ident.clone(),
+ };
+ let mut formatvar = local.clone();
+ if formatvar.to_string().starts_with('_') {
+ // Work around leading underscore being rejected by 1.40 and
+ // older compilers. https://github.com/rust-lang/rust/pull/66847
+ formatvar = format_ident!("field_{}", formatvar);
+ }
+ out += &formatvar.to_string();
+ if !named_args.insert(formatvar.clone()) {
+ // Already specified in the format argument list.
+ continue;
+ }
+ if !has_trailing_comma {
+ args.extend(quote_spanned!(span=> ,));
+ }
+ args.extend(quote_spanned!(span=> #formatvar = #local));
+ if read.starts_with('}') && fields.contains(&member) {
+ has_bonus_display = true;
+ args.extend(quote_spanned!(span=> .as_display()));
+ }
+ has_trailing_comma = false;
+ }
+
+ out += read;
+ self.fmt = LitStr::new(&out, self.fmt.span());
+ self.args = args;
+ self.has_bonus_display = has_bonus_display;
+ }
+}
+
+fn explicit_named_args(input: ParseStream) -> Result<Set<Ident>> {
+ let mut named_args = Set::new();
+
+ while !input.is_empty() {
+ if input.peek(Token![,]) && input.peek2(Ident::peek_any) && input.peek3(Token![=]) {
+ input.parse::<Token![,]>()?;
+ let ident: Ident = input.parse()?;
+ input.parse::<Token![=]>()?;
+ named_args.insert(ident);
+ } else {
+ input.parse::<TokenTree>()?;
+ }
+ }
+
+ Ok(named_args)
+}
+
+fn take_int(read: &mut &str) -> String {
+ let mut int = String::new();
+ for (i, ch) in read.char_indices() {
+ match ch {
+ '0'..='9' => int.push(ch),
+ _ => {
+ *read = &read[i..];
+ break;
+ }
+ }
+ }
+ int
+}
+
+fn take_ident(read: &mut &str) -> String {
+ let mut ident = String::new();
+ for (i, ch) in read.char_indices() {
+ match ch {
+ 'a'..='z' | 'A'..='Z' | '0'..='9' | '_' => ident.push(ch),
+ _ => {
+ *read = &read[i..];
+ break;
+ }
+ }
+ }
+ ident
+}
diff --git a/src/lib.rs b/src/lib.rs
new file mode 100644
index 0000000..95b9e39
--- /dev/null
+++ b/src/lib.rs
@@ -0,0 +1,21 @@
+#![allow(clippy::block_in_if_condition_stmt, clippy::range_plus_one)]
+
+extern crate proc_macro;
+
+mod ast;
+mod attr;
+mod expand;
+mod fmt;
+mod prop;
+mod valid;
+
+use proc_macro::TokenStream;
+use syn::{parse_macro_input, DeriveInput};
+
+#[proc_macro_derive(Error, attributes(backtrace, error, from, source))]
+pub fn derive_error(input: TokenStream) -> TokenStream {
+ let input = parse_macro_input!(input as DeriveInput);
+ expand::derive(&input)
+ .unwrap_or_else(|err| err.to_compile_error())
+ .into()
+}
diff --git a/src/prop.rs b/src/prop.rs
new file mode 100644
index 0000000..e011848
--- /dev/null
+++ b/src/prop.rs
@@ -0,0 +1,111 @@
+use crate::ast::{Enum, Field, Struct, Variant};
+use syn::{Member, Type};
+
+impl Struct<'_> {
+ pub(crate) fn from_field(&self) -> Option<&Field> {
+ from_field(&self.fields)
+ }
+
+ pub(crate) fn source_field(&self) -> Option<&Field> {
+ source_field(&self.fields)
+ }
+
+ pub(crate) fn backtrace_field(&self) -> Option<&Field> {
+ backtrace_field(&self.fields)
+ }
+}
+
+impl Enum<'_> {
+ pub(crate) fn has_source(&self) -> bool {
+ self.variants
+ .iter()
+ .any(|variant| variant.source_field().is_some() || variant.attrs.transparent.is_some())
+ }
+
+ pub(crate) fn has_backtrace(&self) -> bool {
+ self.variants
+ .iter()
+ .any(|variant| variant.backtrace_field().is_some())
+ }
+
+ pub(crate) fn has_display(&self) -> bool {
+ self.attrs.display.is_some()
+ || self.attrs.transparent.is_some()
+ || self
+ .variants
+ .iter()
+ .any(|variant| variant.attrs.display.is_some())
+ || self
+ .variants
+ .iter()
+ .all(|variant| variant.attrs.transparent.is_some())
+ }
+}
+
+impl Variant<'_> {
+ pub(crate) fn from_field(&self) -> Option<&Field> {
+ from_field(&self.fields)
+ }
+
+ pub(crate) fn source_field(&self) -> Option<&Field> {
+ source_field(&self.fields)
+ }
+
+ pub(crate) fn backtrace_field(&self) -> Option<&Field> {
+ backtrace_field(&self.fields)
+ }
+}
+
+impl Field<'_> {
+ pub(crate) fn is_backtrace(&self) -> bool {
+ type_is_backtrace(self.ty)
+ }
+}
+
+fn from_field<'a, 'b>(fields: &'a [Field<'b>]) -> Option<&'a Field<'b>> {
+ for field in fields {
+ if field.attrs.from.is_some() {
+ return Some(&field);
+ }
+ }
+ None
+}
+
+fn source_field<'a, 'b>(fields: &'a [Field<'b>]) -> Option<&'a Field<'b>> {
+ for field in fields {
+ if field.attrs.from.is_some() || field.attrs.source.is_some() {
+ return Some(&field);
+ }
+ }
+ for field in fields {
+ match &field.member {
+ Member::Named(ident) if ident == "source" => return Some(&field),
+ _ => {}
+ }
+ }
+ None
+}
+
+fn backtrace_field<'a, 'b>(fields: &'a [Field<'b>]) -> Option<&'a Field<'b>> {
+ for field in fields {
+ if field.attrs.backtrace.is_some() {
+ return Some(&field);
+ }
+ }
+ for field in fields {
+ if field.is_backtrace() {
+ return Some(&field);
+ }
+ }
+ None
+}
+
+fn type_is_backtrace(ty: &Type) -> bool {
+ let path = match ty {
+ Type::Path(ty) => &ty.path,
+ _ => return false,
+ };
+
+ let last = path.segments.last().unwrap();
+ last.ident == "Backtrace" && last.arguments.is_empty()
+}
diff --git a/src/valid.rs b/src/valid.rs
new file mode 100644
index 0000000..0d5ba36
--- /dev/null
+++ b/src/valid.rs
@@ -0,0 +1,226 @@
+use crate::ast::{Enum, Field, Input, Struct, Variant};
+use crate::attr::Attrs;
+use quote::ToTokens;
+use std::collections::BTreeSet as Set;
+use syn::{Error, GenericArgument, Member, PathArguments, Result, Type};
+
+impl Input<'_> {
+ pub(crate) fn validate(&self) -> Result<()> {
+ match self {
+ Input::Struct(input) => input.validate(),
+ Input::Enum(input) => input.validate(),
+ }
+ }
+}
+
+impl Struct<'_> {
+ fn validate(&self) -> Result<()> {
+ check_non_field_attrs(&self.attrs)?;
+ if let Some(transparent) = self.attrs.transparent {
+ if self.fields.len() != 1 {
+ return Err(Error::new_spanned(
+ transparent,
+ "#[error(transparent)] requires exactly one field",
+ ));
+ }
+ if let Some(source) = self.fields.iter().filter_map(|f| f.attrs.source).next() {
+ return Err(Error::new_spanned(
+ source,
+ "transparent error struct can't contain #[source]",
+ ));
+ }
+ }
+ check_field_attrs(&self.fields)?;
+ for field in &self.fields {
+ field.validate()?;
+ }
+ Ok(())
+ }
+}
+
+impl Enum<'_> {
+ fn validate(&self) -> Result<()> {
+ check_non_field_attrs(&self.attrs)?;
+ let has_display = self.has_display();
+ for variant in &self.variants {
+ variant.validate()?;
+ if has_display && variant.attrs.display.is_none() && variant.attrs.transparent.is_none()
+ {
+ return Err(Error::new_spanned(
+ variant.original,
+ "missing #[error(\"...\")] display attribute",
+ ));
+ }
+ }
+ let mut from_types = Set::new();
+ for variant in &self.variants {
+ if let Some(from_field) = variant.from_field() {
+ let repr = from_field.ty.to_token_stream().to_string();
+ if !from_types.insert(repr) {
+ return Err(Error::new_spanned(
+ from_field.original,
+ "cannot derive From because another variant has the same source type",
+ ));
+ }
+ }
+ }
+ Ok(())
+ }
+}
+
+impl Variant<'_> {
+ fn validate(&self) -> Result<()> {
+ check_non_field_attrs(&self.attrs)?;
+ if self.attrs.transparent.is_some() {
+ if self.fields.len() != 1 {
+ return Err(Error::new_spanned(
+ self.original,
+ "#[error(transparent)] requires exactly one field",
+ ));
+ }
+ if let Some(source) = self.fields.iter().filter_map(|f| f.attrs.source).next() {
+ return Err(Error::new_spanned(
+ source,
+ "transparent variant can't contain #[source]",
+ ));
+ }
+ }
+ check_field_attrs(&self.fields)?;
+ for field in &self.fields {
+ field.validate()?;
+ }
+ Ok(())
+ }
+}
+
+impl Field<'_> {
+ fn validate(&self) -> Result<()> {
+ if let Some(display) = &self.attrs.display {
+ return Err(Error::new_spanned(
+ display.original,
+ "not expected here; the #[error(...)] attribute belongs on top of a struct or an enum variant",
+ ));
+ }
+ Ok(())
+ }
+}
+
+fn check_non_field_attrs(attrs: &Attrs) -> Result<()> {
+ if let Some(from) = &attrs.from {
+ return Err(Error::new_spanned(
+ from,
+ "not expected here; the #[from] attribute belongs on a specific field",
+ ));
+ }
+ if let Some(source) = &attrs.source {
+ return Err(Error::new_spanned(
+ source,
+ "not expected here; the #[source] attribute belongs on a specific field",
+ ));
+ }
+ if let Some(backtrace) = &attrs.backtrace {
+ return Err(Error::new_spanned(
+ backtrace,
+ "not expected here; the #[backtrace] attribute belongs on a specific field",
+ ));
+ }
+ if let Some(display) = &attrs.display {
+ if attrs.transparent.is_some() {
+ return Err(Error::new_spanned(
+ display.original,
+ "cannot have both #[error(transparent)] and a display attribute",
+ ));
+ }
+ }
+ Ok(())
+}
+
+fn check_field_attrs(fields: &[Field]) -> Result<()> {
+ let mut from_field = None;
+ let mut source_field = None;
+ let mut backtrace_field = None;
+ let mut has_backtrace = false;
+ for field in fields {
+ if let Some(from) = field.attrs.from {
+ if from_field.is_some() {
+ return Err(Error::new_spanned(from, "duplicate #[from] attribute"));
+ }
+ from_field = Some(field);
+ }
+ if let Some(source) = field.attrs.source {
+ if source_field.is_some() {
+ return Err(Error::new_spanned(source, "duplicate #[source] attribute"));
+ }
+ source_field = Some(field);
+ }
+ if let Some(backtrace) = field.attrs.backtrace {
+ if backtrace_field.is_some() {
+ return Err(Error::new_spanned(
+ backtrace,
+ "duplicate #[backtrace] attribute",
+ ));
+ }
+ backtrace_field = Some(field);
+ has_backtrace = true;
+ }
+ if let Some(transparent) = field.attrs.transparent {
+ return Err(Error::new_spanned(
+ transparent,
+ "#[error(transparent)] needs to go outside the enum or struct, not on an individual field",
+ ));
+ }
+ has_backtrace |= field.is_backtrace();
+ }
+ if let (Some(from_field), Some(source_field)) = (from_field, source_field) {
+ if !same_member(from_field, source_field) {
+ return Err(Error::new_spanned(
+ from_field.attrs.from,
+ "#[from] is only supported on the source field, not any other field",
+ ));
+ }
+ }
+ if let Some(from_field) = from_field {
+ if fields.len() > 1 + has_backtrace as usize {
+ return Err(Error::new_spanned(
+ from_field.attrs.from,
+ "deriving From requires no fields other than source and backtrace",
+ ));
+ }
+ }
+ if let Some(source_field) = source_field.or(from_field) {
+ if contains_non_static_lifetime(source_field) {
+ return Err(Error::new_spanned(
+ source_field.original,
+ "non-static lifetimes are not allowed in the source of an error",
+ ));
+ }
+ }
+ Ok(())
+}
+
+fn same_member(one: &Field, two: &Field) -> bool {
+ match (&one.member, &two.member) {
+ (Member::Named(one), Member::Named(two)) => one == two,
+ (Member::Unnamed(one), Member::Unnamed(two)) => one.index == two.index,
+ _ => unreachable!(),
+ }
+}
+
+fn contains_non_static_lifetime(field: &Field) -> bool {
+ let ty = match field.ty {
+ Type::Path(ty) => ty,
+ _ => return false, // maybe implement later if there are common other cases
+ };
+ let bracketed = match &ty.path.segments.last().unwrap().arguments {
+ PathArguments::AngleBracketed(bracketed) => bracketed,
+ _ => return false,
+ };
+ for arg in &bracketed.args {
+ if let GenericArgument::Lifetime(lifetime) = arg {
+ if lifetime.ident != "static" {
+ return true;
+ }
+ }
+ }
+ false
+}