From d3ed325f983140fca63adef0ab52b1100f0362c2 Mon Sep 17 00:00:00 2001 From: Joel Galenson Date: Wed, 22 Sep 2021 11:24:03 -0700 Subject: Upgrade rust/crates/thiserror to 1.0.29 Test: make Change-Id: I6abd1fcf8199ea5b50c14f2138d25555374c5375 --- .cargo_vcs_info.json | 2 +- Android.bp | 9 +-- Cargo.toml | 13 ++-- Cargo.toml.orig | 4 +- METADATA | 8 +-- README.md | 14 ++++ src/lib.rs | 16 +++++ tests/test_backtrace.rs | 43 +++++++++++-- tests/test_display.rs | 4 -- tests/test_expr.rs | 6 +- tests/test_from.rs | 27 ++++++-- tests/test_generics.rs | 161 ++++++++++++++++++++++++++++++++++++++++++++++ tests/test_lints.rs | 5 -- tests/test_path.rs | 4 -- tests/test_source.rs | 4 -- tests/test_transparent.rs | 4 -- 16 files changed, 267 insertions(+), 57 deletions(-) create mode 100644 tests/test_generics.rs diff --git a/.cargo_vcs_info.json b/.cargo_vcs_info.json index 16f81ed..d146052 100644 --- a/.cargo_vcs_info.json +++ b/.cargo_vcs_info.json @@ -1,5 +1,5 @@ { "git": { - "sha1": "031fea6f3b82c72be11477e7550c6ae3579e6139" + "sha1": "c7dd271dcd92af17168746a809503ee392d6f6ad" } } diff --git a/Android.bp b/Android.bp index e369dc6..0c3bbe8 100644 --- a/Android.bp +++ b/Android.bp @@ -41,6 +41,8 @@ rust_library { name: "libthiserror", host_supported: true, crate_name: "thiserror", + cargo_env_compat: true, + cargo_pkg_version: "1.0.29", srcs: ["src/lib.rs"], edition: "2018", proc_macros: ["libthiserror_impl"], @@ -49,10 +51,3 @@ rust_library { "com.android.virt", ], } - -// dependent_library ["feature_list"] -// proc-macro2-1.0.28 "default,proc-macro" -// quote-1.0.9 "default,proc-macro" -// syn-1.0.74 "clone-impls,default,derive,parsing,printing,proc-macro,quote" -// thiserror-impl-1.0.26 -// unicode-xid-0.2.2 "default" diff --git a/Cargo.toml b/Cargo.toml index c0da22c..ea80376 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -3,17 +3,16 @@ # 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 +# 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) +# If you are reading this file be aware that the original Cargo.toml +# will likely look very different (and much more reasonable). +# See Cargo.toml.orig for the original contents. [package] edition = "2018" name = "thiserror" -version = "1.0.26" +version = "1.0.29" authors = ["David Tolnay "] description = "derive(Error)" documentation = "https://docs.rs/thiserror" @@ -24,7 +23,7 @@ repository = "https://github.com/dtolnay/thiserror" [package.metadata.docs.rs] targets = ["x86_64-unknown-linux-gnu"] [dependencies.thiserror-impl] -version = "=1.0.26" +version = "=1.0.29" [dev-dependencies.anyhow] version = "1.0" diff --git a/Cargo.toml.orig b/Cargo.toml.orig index dd0b38e..f619245 100644 --- a/Cargo.toml.orig +++ b/Cargo.toml.orig @@ -1,6 +1,6 @@ [package] name = "thiserror" -version = "1.0.26" +version = "1.0.29" authors = ["David Tolnay "] edition = "2018" license = "MIT OR Apache-2.0" @@ -11,7 +11,7 @@ categories = ["rust-patterns"] readme = "README.md" [dependencies] -thiserror-impl = { version = "=1.0.26", path = "impl" } +thiserror-impl = { version = "=1.0.29", path = "impl" } [dev-dependencies] anyhow = "1.0" diff --git a/METADATA b/METADATA index 3b3c034..6f9cb40 100644 --- a/METADATA +++ b/METADATA @@ -7,13 +7,13 @@ third_party { } url { type: ARCHIVE - value: "https://static.crates.io/crates/thiserror/thiserror-1.0.26.crate" + value: "https://static.crates.io/crates/thiserror/thiserror-1.0.29.crate" } - version: "1.0.26" + version: "1.0.29" license_type: NOTICE last_upgrade_date { year: 2021 - month: 8 - day: 9 + month: 9 + day: 22 } } diff --git a/README.md b/README.md index 76c436a..e12b693 100644 --- a/README.md +++ b/README.md @@ -137,6 +137,20 @@ pub enum DataStoreError { } ``` +- If a field is both a source (named `source`, or has `#[source]` or `#[from]` + attribute) *and* is marked `#[backtrace]`, then the Error trait's + `backtrace()` method is forwarded to the source's backtrace. + + ```rust + #[derive(Error, Debug)] + pub enum MyError { + Io { + #[backtrace] + source: io::Error, + }, + } + ``` + - Errors may use `error(transparent)` to forward the source and Display methods straight through to an underlying error without adding an additional message. This would be appropriate for enums that need an "anything else" variant. diff --git a/src/lib.rs b/src/lib.rs index 11df494..2fae25c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -161,6 +161,22 @@ //! # }; //! ``` //! +//! - If a field is both a source (named `source`, or has `#[source]` or +//! `#[from]` attribute) *and* is marked `#[backtrace]`, then the Error +//! trait's `backtrace()` method is forwarded to the source's backtrace. +//! +//! ```rust +//! # const IGNORE: &str = stringify! { +//! #[derive(Error, Debug)] +//! pub enum MyError { +//! Io { +//! #[backtrace] +//! source: io::Error, +//! }, +//! } +//! # }; +//! ``` +//! //! - Errors may use `error(transparent)` to forward the source and Display //! methods straight through to an underlying error without adding an //! additional message. This would be appropriate for enums that need an diff --git a/tests/test_backtrace.rs b/tests/test_backtrace.rs index c7ba3c4..42e37ca 100644 --- a/tests/test_backtrace.rs +++ b/tests/test_backtrace.rs @@ -1,8 +1,4 @@ #![cfg_attr(thiserror_nightly_testing, feature(backtrace))] -#![allow( - // Clippy bug: https://github.com/rust-lang/rust-clippy/issues/7422 - clippy::nonstandard_macro_braces, -)] use thiserror::Error; @@ -10,9 +6,16 @@ use thiserror::Error; #[error("...")] pub struct Inner; +#[cfg(thiserror_nightly_testing)] +#[derive(Error, Debug)] +#[error("...")] +pub struct InnerBacktrace { + backtrace: std::backtrace::Backtrace, +} + #[cfg(thiserror_nightly_testing)] pub mod structs { - use super::Inner; + use super::{Inner, InnerBacktrace}; use std::backtrace::Backtrace; use std::error::Error; use std::sync::Arc; @@ -54,6 +57,14 @@ pub mod structs { backtrace: Backtrace, } + #[derive(Error, Debug)] + #[error("...")] + pub struct CombinedBacktraceFrom { + #[from] + #[backtrace] + source: InnerBacktrace, + } + #[derive(Error, Debug)] #[error("...")] pub struct OptBacktraceFrom { @@ -97,6 +108,11 @@ pub mod structs { let error = BacktraceFrom::from(Inner); assert!(error.backtrace().is_some()); + let error = CombinedBacktraceFrom::from(InnerBacktrace { + backtrace: Backtrace::capture(), + }); + assert!(error.backtrace().is_some()); + let error = OptBacktraceFrom::from(Inner); assert!(error.backtrace().is_some()); @@ -107,7 +123,7 @@ pub mod structs { #[cfg(thiserror_nightly_testing)] pub mod enums { - use super::Inner; + use super::{Inner, InnerBacktrace}; use std::backtrace::Backtrace; use std::error::Error; use std::sync::Arc; @@ -157,6 +173,16 @@ pub mod enums { }, } + #[derive(Error, Debug)] + pub enum CombinedBacktraceFrom { + #[error("...")] + Test { + #[from] + #[backtrace] + source: InnerBacktrace, + }, + } + #[derive(Error, Debug)] pub enum OptBacktraceFrom { #[error("...")] @@ -204,6 +230,11 @@ pub mod enums { let error = BacktraceFrom::from(Inner); assert!(error.backtrace().is_some()); + let error = CombinedBacktraceFrom::from(InnerBacktrace { + backtrace: Backtrace::capture(), + }); + assert!(error.backtrace().is_some()); + let error = OptBacktraceFrom::from(Inner); assert!(error.backtrace().is_some()); diff --git a/tests/test_display.rs b/tests/test_display.rs index 8a9ebf7..949d9ed 100644 --- a/tests/test_display.rs +++ b/tests/test_display.rs @@ -1,8 +1,4 @@ #![deny(clippy::all, clippy::pedantic)] -#![allow( - // Clippy bug: https://github.com/rust-lang/rust-clippy/issues/7422 - clippy::nonstandard_macro_braces, -)] use std::fmt::Display; use thiserror::Error; diff --git a/tests/test_expr.rs b/tests/test_expr.rs index 50835a5..87a56cf 100644 --- a/tests/test_expr.rs +++ b/tests/test_expr.rs @@ -1,9 +1,5 @@ #![deny(clippy::all, clippy::pedantic)] -#![allow( - // Clippy bug: https://github.com/rust-lang/rust-clippy/issues/7422 - clippy::nonstandard_macro_braces, - clippy::option_if_let_else, -)] +#![allow(clippy::option_if_let_else)] use std::fmt::Display; use thiserror::Error; diff --git a/tests/test_from.rs b/tests/test_from.rs index b6a3c0c..a25ce35 100644 --- a/tests/test_from.rs +++ b/tests/test_from.rs @@ -1,8 +1,4 @@ #![deny(clippy::all, clippy::pedantic)] -#![allow( - // Clippy bug: https://github.com/rust-lang/rust-clippy/issues/7422 - clippy::nonstandard_macro_braces, -)] use std::io; use thiserror::Error; @@ -14,10 +10,21 @@ pub struct ErrorStruct { source: io::Error, } +#[derive(Error, Debug)] +#[error("...")] +pub struct ErrorStructOptional { + #[from] + source: Option, +} + #[derive(Error, Debug)] #[error("...")] pub struct ErrorTuple(#[from] io::Error); +#[derive(Error, Debug)] +#[error("...")] +pub struct ErrorTupleOptional(#[from] Option); + #[derive(Error, Debug)] #[error("...")] pub enum ErrorEnum { @@ -27,6 +34,15 @@ pub enum ErrorEnum { }, } +#[derive(Error, Debug)] +#[error("...")] +pub enum ErrorEnumOptional { + Test { + #[from] + source: Option, + }, +} + #[derive(Error, Debug)] #[error("...")] pub enum Many { @@ -39,7 +55,10 @@ fn assert_impl>() {} #[test] fn test_from() { assert_impl::(); + assert_impl::(); assert_impl::(); + assert_impl::(); assert_impl::(); + assert_impl::(); assert_impl::(); } diff --git a/tests/test_generics.rs b/tests/test_generics.rs new file mode 100644 index 0000000..f5e1de2 --- /dev/null +++ b/tests/test_generics.rs @@ -0,0 +1,161 @@ +#![deny(clippy::all, clippy::pedantic)] + +use std::fmt::{self, Debug, Display}; +use thiserror::Error; + +pub struct NoFormat; + +#[derive(Debug)] +pub struct DebugOnly; + +pub struct DisplayOnly; + +impl Display for DisplayOnly { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + f.write_str("display only") + } +} + +#[derive(Debug)] +pub struct DebugAndDisplay; + +impl Display for DebugAndDisplay { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + f.write_str("debug and display") + } +} + +// Should expand to: +// +// impl Display for EnumDebugField +// where +// E: Debug; +// +// impl Error for EnumDebugField +// where +// Self: Debug + Display; +// +#[derive(Error, Debug)] +pub enum EnumDebugGeneric { + #[error("{0:?}")] + FatalError(E), +} + +// Should expand to: +// +// impl Display for EnumFromGeneric; +// +// impl Error for EnumFromGeneric +// where +// EnumDebugGeneric: Error + 'static, +// Self: Debug + Display; +// +#[derive(Error, Debug)] +pub enum EnumFromGeneric { + #[error("enum from generic")] + Source(#[from] EnumDebugGeneric), +} + +// Should expand to: +// +// impl Display +// for EnumCompound +// where +// HasDisplay: Display, +// HasDebug: Debug; +// +// impl Error +// for EnumCompound +// where +// Self: Debug + Display; +// +#[derive(Error)] +pub enum EnumCompound { + #[error("{0} {1:?}")] + DisplayDebug(HasDisplay, HasDebug), + #[error("{0}")] + Display(HasDisplay, HasNeither), + #[error("{1:?}")] + Debug(HasNeither, HasDebug), +} + +impl Debug for EnumCompound { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + f.write_str("EnumCompound") + } +} + +#[test] +fn test_display_enum_compound() { + let mut instance: EnumCompound; + + instance = EnumCompound::DisplayDebug(DisplayOnly, DebugOnly); + assert_eq!(format!("{}", instance), "display only DebugOnly"); + + instance = EnumCompound::Display(DisplayOnly, NoFormat); + assert_eq!(format!("{}", instance), "display only"); + + instance = EnumCompound::Debug(NoFormat, DebugOnly); + assert_eq!(format!("{}", instance), "DebugOnly"); +} + +// Should expand to: +// +// impl Display for EnumTransparentGeneric +// where +// E: Display; +// +// impl Error for EnumTransparentGeneric +// where +// E: Error, +// Self: Debug + Display; +// +#[derive(Error, Debug)] +pub enum EnumTransparentGeneric { + #[error(transparent)] + Other(E), +} + +// Should expand to: +// +// impl Display for StructDebugGeneric +// where +// E: Debug; +// +// impl Error for StructDebugGeneric +// where +// Self: Debug + Display; +// +#[derive(Error, Debug)] +#[error("{underlying:?}")] +pub struct StructDebugGeneric { + pub underlying: E, +} + +// Should expand to: +// +// impl Error for StructFromGeneric +// where +// StructDebugGeneric: Error + 'static, +// Self: Debug + Display; +// +#[derive(Error, Debug)] +pub struct StructFromGeneric { + #[from] + pub source: StructDebugGeneric, +} + +// Should expand to: +// +// impl Display for StructTransparentGeneric +// where +// E: Display; +// +// impl Error for StructTransparentGeneric +// where +// E: Error, +// Self: Debug + Display; +// +#[derive(Error, Debug)] +#[error(transparent)] +pub struct StructTransparentGeneric(E); diff --git a/tests/test_lints.rs b/tests/test_lints.rs index 457b799..59699a4 100644 --- a/tests/test_lints.rs +++ b/tests/test_lints.rs @@ -1,8 +1,3 @@ -#![allow( - // Clippy bug: https://github.com/rust-lang/rust-clippy/issues/7422 - clippy::nonstandard_macro_braces, -)] - use thiserror::Error; pub use std::error::Error; diff --git a/tests/test_path.rs b/tests/test_path.rs index 777b5de..a10b1f3 100644 --- a/tests/test_path.rs +++ b/tests/test_path.rs @@ -1,8 +1,4 @@ #![deny(clippy::all, clippy::pedantic)] -#![allow( - // Clippy bug: https://github.com/rust-lang/rust-clippy/issues/7422 - clippy::nonstandard_macro_braces, -)] use ref_cast::RefCast; use std::fmt::Display; diff --git a/tests/test_source.rs b/tests/test_source.rs index c7ddda8..ab16097 100644 --- a/tests/test_source.rs +++ b/tests/test_source.rs @@ -1,8 +1,4 @@ #![deny(clippy::all, clippy::pedantic)] -#![allow( - // Clippy bug: https://github.com/rust-lang/rust-clippy/issues/7422 - clippy::nonstandard_macro_braces, -)] use std::error::Error as StdError; use std::io; diff --git a/tests/test_transparent.rs b/tests/test_transparent.rs index 7d02826..84d7c91 100644 --- a/tests/test_transparent.rs +++ b/tests/test_transparent.rs @@ -1,8 +1,4 @@ #![deny(clippy::all, clippy::pedantic)] -#![allow( - // Clippy bug: https://github.com/rust-lang/rust-clippy/issues/7422 - clippy::nonstandard_macro_braces, -)] use anyhow::anyhow; use std::error::Error as _; -- cgit v1.2.3