Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
6085f29
support no_std with error-in-core
jordens May 16, 2024
c560bcc
exlude compiletest from no_std, fix no_std doctest
jordens May 16, 2024
ec51e63
undo Error->MyError renames
jordens May 16, 2024
f7f6c77
use __private namespace
jordens May 16, 2024
f1d9df3
add comment
jordens May 16, 2024
33b7a36
Merge branch 'master' into no-std
jordens May 17, 2024
3663fd1
use explicit doc(test(attr))
jordens May 22, 2024
61b1c17
error_in_core is stable
jordens Jun 14, 2024
3e792e0
{std -> core}:error: update docs/comment
jordens Jun 14, 2024
b278258
docs.rs: std is default
jordens Jun 14, 2024
8cbdc4e
bump MSRV to 1.81
jordens Jun 14, 2024
cc00ef8
Merge remote-tracking branch 'upstream/master' into no-std
jordens Sep 3, 2024
7fa183b
Revert "bump MSRV to 1.81"
jordens Sep 6, 2024
28ce40b
Revert "{std -> core}:error: update docs/comment"
jordens Sep 6, 2024
0b058bc
Revert "error_in_core is stable"
jordens Sep 6, 2024
e7e9341
minimalize changes, maintain MSRV for `std`
jordens Sep 6, 2024
bfd7357
test 1.81 and no_std
jordens Sep 6, 2024
706fb6a
ci no_std: not check but test, 1.81 not necessary
jordens Sep 6, 2024
a3d073b
compiletest: allow unused_attributes
jordens Sep 6, 2024
89008c0
prefer required-features
jordens Sep 9, 2024
c9cd3b9
compiletest doesn't need std
jordens Sep 9, 2024
0ef9d1b
impl hygiene: fully qualify all paths
jordens Sep 11, 2024
7182e3f
test_no_std: add strictly no_std test
jordens Sep 16, 2024
515bd36
test_{transparent,_lints}: also work with no-std lib
jordens Sep 18, 2024
2614b53
test_no_std: reduce
jordens Sep 18, 2024
44737a5
test_no_std: can't compile on old rust
jordens Sep 18, 2024
e779e1b
Merge remote-tracking branch 'upstream/master' into no-std
jordens Sep 25, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Revert "error_in_core is stable"
This reverts commit 61b1c17.
  • Loading branch information
jordens committed Sep 6, 2024
commit 0b058bcb5e54646b38ce1db7d8c1f0c09eec2118
5 changes: 5 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,8 @@ rustdoc-args = ["--generate-link-to-definition"]
[features]
std = []
default = ["std"]

[[test]]
name = "compiletest"
# Merely to avoid spraying cfg(error_in_core)
required-features = ["std"]
28 changes: 15 additions & 13 deletions impl/src/expand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ fn fallback(input: &DeriveInput, error: syn::Error) -> TokenStream {
#error

#[allow(unused_qualifications)]
impl #impl_generics ::core::error::Error for #ty #ty_generics #where_clause
impl #impl_generics ::thiserror::__private::error::Error for #ty #ty_generics #where_clause
Copy link

@dhedey dhedey Oct 11, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Out of interest, why did we choose this rather than hardcoding ::std or ::core, in the compiled output (via e.g. an #error_crate variable)?

My slight concern is that use of ::thiserror::__private may leak this __private namespace out and could confuse people debugging this. But using ::std / ::core directly it's more obvious what's failing.

where
// Work around trivial bounds being unstable.
// https://github.com/rust-lang/rust/issues/48214
Expand All @@ -60,17 +60,19 @@ fn impl_struct(input: Struct) -> TokenStream {
let source_body = if let Some(transparent_attr) = &input.attrs.transparent {
let only_field = &input.fields[0];
if only_field.contains_generic {
error_inferred_bounds.insert(only_field.ty, quote!(::core::error::Error));
error_inferred_bounds
.insert(only_field.ty, quote!(::thiserror::__private::error::Error));
}
let member = &only_field.member;
Some(quote_spanned! {transparent_attr.span=>
::core::error::Error::source(self.#member.as_dyn_error())
::thiserror::__private::error::Error::source(self.#member.as_dyn_error())
})
} else if let Some(source_field) = input.source_field() {
let source = &source_field.member;
if source_field.contains_generic {
let ty = unoptional_type(source_field.ty);
error_inferred_bounds.insert(ty, quote!(::core::error::Error + 'static));
error_inferred_bounds
.insert(ty, quote!(::thiserror::__private::error::Error + 'static));
}
let asref = if type_is_option(source_field.ty) {
Some(quote_spanned!(source.member_span()=> .as_ref()?))
Expand All @@ -88,7 +90,7 @@ fn impl_struct(input: Struct) -> TokenStream {
};
let source_method = source_body.map(|body| {
quote! {
fn source(&self) -> ::core::option::Option<&(dyn ::core::error::Error + 'static)> {
fn source(&self) -> ::core::option::Option<&(dyn ::thiserror::__private::error::Error + 'static)> {
use thiserror::__private::AsDynError as _;
#body
}
Expand Down Expand Up @@ -141,7 +143,7 @@ fn impl_struct(input: Struct) -> TokenStream {
}
};
quote! {
fn provide<'_request>(&'_request self, #request: &mut ::core::error::Request<'_request>) {
fn provide<'_request>(&'_request self, #request: &mut std::error::Request<'_request>) {
#body
}
}
Expand Down Expand Up @@ -211,7 +213,7 @@ fn impl_struct(input: Struct) -> TokenStream {

quote! {
#[allow(unused_qualifications)]
impl #impl_generics ::core::error::Error for #ty #ty_generics #error_where_clause {
impl #impl_generics ::thiserror::__private::error::Error for #ty #ty_generics #error_where_clause {
#source_method
#provide_method
}
Expand All @@ -231,11 +233,11 @@ fn impl_enum(input: Enum) -> TokenStream {
if let Some(transparent_attr) = &variant.attrs.transparent {
let only_field = &variant.fields[0];
if only_field.contains_generic {
error_inferred_bounds.insert(only_field.ty, quote!(::core::error::Error));
error_inferred_bounds.insert(only_field.ty, quote!(::thiserror::__private::error::Error));
}
let member = &only_field.member;
let source = quote_spanned! {transparent_attr.span=>
::core::error::Error::source(transparent.as_dyn_error())
::thiserror::__private::error::Error::source(transparent.as_dyn_error())
};
quote! {
#ty::#ident {#member: transparent} => #source,
Expand All @@ -244,7 +246,7 @@ fn impl_enum(input: Enum) -> TokenStream {
let source = &source_field.member;
if source_field.contains_generic {
let ty = unoptional_type(source_field.ty);
error_inferred_bounds.insert(ty, quote!(::core::error::Error + 'static));
error_inferred_bounds.insert(ty, quote!(::thiserror::__private::error::Error + 'static));
}
let asref = if type_is_option(source_field.ty) {
Some(quote_spanned!(source.member_span()=> .as_ref()?))
Expand All @@ -265,7 +267,7 @@ fn impl_enum(input: Enum) -> TokenStream {
}
});
Some(quote! {
fn source(&self) -> ::core::option::Option<&(dyn ::core::error::Error + 'static)> {
fn source(&self) -> ::core::option::Option<&(dyn std::error::Error + 'static)> {
use thiserror::__private::AsDynError as _;
#[allow(deprecated)]
match self {
Expand Down Expand Up @@ -370,7 +372,7 @@ fn impl_enum(input: Enum) -> TokenStream {
}
});
Some(quote! {
fn provide<'_request>(&'_request self, #request: &mut ::core::error::Request<'_request>) {
fn provide<'_request>(&'_request self, #request: &mut std::error::Request<'_request>) {
#[allow(deprecated)]
match self {
#(#arms)*
Expand Down Expand Up @@ -467,7 +469,7 @@ fn impl_enum(input: Enum) -> TokenStream {

quote! {
#[allow(unused_qualifications)]
impl #impl_generics ::core::error::Error for #ty #ty_generics #error_where_clause {
impl #impl_generics ::thiserror::__private::error::Error for #ty #ty_generics #error_where_clause {
#source_method
#provide_method
}
Expand Down
2 changes: 1 addition & 1 deletion src/aserror.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use core::error::Error;
use crate::__private::error::Error;
use core::panic::UnwindSafe;

#[doc(hidden)]
Expand Down
13 changes: 12 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,12 @@
clippy::wildcard_imports
)]
#![cfg_attr(error_generic_member_access, feature(error_generic_member_access))]
#![cfg_attr(not(feature = "std"), no_std)]
#![cfg_attr(
not(feature = "std"),
no_std,
feature(error_in_core),
doc(test(attr(feature(error_in_core))))
)]

#[cfg(all(thiserror_nightly_testing, not(error_generic_member_access)))]
compile_error!("Build script probe failed to compile.");
Expand All @@ -288,4 +293,10 @@ pub mod __private {
#[cfg(error_generic_member_access)]
#[doc(hidden)]
pub use crate::provide::ThiserrorProvide;
#[cfg(not(feature = "std"))]
#[doc(hidden)]
pub use core::error;
#[cfg(feature = "std")]
#[doc(hidden)]
pub use std::error;
}
2 changes: 1 addition & 1 deletion src/provide.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use core::error::{Error, Request};
use crate::__private::error::{Error, Request};

#[doc(hidden)]
pub trait ThiserrorProvide: Sealed {
Expand Down
1 change: 1 addition & 0 deletions tests/test_backtrace.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#![cfg_attr(not(feature = "std"), feature(error_in_core))]
#![cfg_attr(thiserror_nightly_testing, feature(error_generic_member_access))]

use thiserror::Error;
Expand Down
1 change: 1 addition & 0 deletions tests/test_deprecated.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#![cfg_attr(not(feature = "std"), feature(error_in_core))]
#![deny(deprecated, clippy::all, clippy::pedantic)]

use thiserror::Error;
Expand Down
1 change: 1 addition & 0 deletions tests/test_display.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#![cfg_attr(not(feature = "std"), feature(error_in_core))]
#![allow(clippy::needless_raw_string_hashes, clippy::uninlined_format_args)]

use core::fmt::{self, Display};
Expand Down
1 change: 1 addition & 0 deletions tests/test_error.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#![cfg_attr(not(feature = "std"), feature(error_in_core))]
#![allow(dead_code)]

use core::fmt::{self, Display};
Expand Down
1 change: 1 addition & 0 deletions tests/test_expr.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#![cfg_attr(not(feature = "std"), feature(error_in_core))]
#![allow(clippy::iter_cloned_collect, clippy::uninlined_format_args)]

use core::fmt::Display;
Expand Down
1 change: 1 addition & 0 deletions tests/test_from.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#![cfg_attr(not(feature = "std"), feature(error_in_core))]
#![allow(clippy::extra_unused_type_parameters)]

use std::io;
Expand Down
1 change: 1 addition & 0 deletions tests/test_generics.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#![cfg_attr(not(feature = "std"), feature(error_in_core))]
#![allow(clippy::needless_late_init, clippy::uninlined_format_args)]

use core::fmt::{self, Debug, Display};
Expand Down
4 changes: 3 additions & 1 deletion tests/test_lints.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
#![cfg_attr(not(feature = "std"), feature(error_in_core))]
#![allow(clippy::mixed_attributes_style)]

use thiserror::Error;

pub use core::error::Error;
// std or core
pub use thiserror::__private::error::Error;

#[test]
fn test_unused_qualifications() {
Expand Down
2 changes: 2 additions & 0 deletions tests/test_source.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![cfg_attr(not(feature = "std"), feature(error_in_core))]

use std::error::Error as StdError;
use std::io;
use thiserror::Error;
Expand Down
5 changes: 4 additions & 1 deletion tests/test_transparent.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
#![cfg_attr(not(feature = "std"), feature(error_in_core))]

use anyhow::anyhow;
use core::error::Error;
use thiserror::Error;
// std or core
use thiserror::__private::error::Error;

#[cfg(feature = "std")]
#[test]
Expand Down