Skip to content

Commit d820ec6

Browse files
AlphaKekstyranron
andauthored
Fix derive(Error) expansion hygiene (#527, #526)
- rename `as_dyn_error()` method of `derive_more::__private::AsDynError` trait to `__derive_more_as_dyn_error()` to omit possible ambiguities - replace `vendor::thiserror::aserror` module with `as_dyn_error` module in `derive_more` crate Co-authored-by: Kai Ren <tyranron@gmail.com>
1 parent 5627282 commit d820ec6

File tree

11 files changed

+91
-260
lines changed

11 files changed

+91
-260
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,13 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](http://keepachangelog.com/)
66
and this project adheres to [Semantic Versioning](http://semver.org/).
77

8+
## master
9+
10+
### Fixed
11+
12+
- `.as_dyn_error()` method hygiene inside `Error` derive expansion.
13+
([#527](https://github.com/JelteF/derive_more/pull/527))
14+
815
## 2.1.0 - 2025-12-02
916

1017
### Added

impl/src/error.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@ fn render_some(mut expr: TokenStream, unpack: bool) -> TokenStream {
287287
if unpack {
288288
expr = quote! { derive_more::core::option::Option::as_ref(#expr)? }
289289
}
290-
quote! { Some(#expr.as_dyn_error()) }
290+
quote! { Some(#expr.__derive_more_as_dyn_error()) }
291291
}
292292

293293
fn parse_fields<'input, 'state>(

src/as_dyn_error.rs

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
//! Coercion into `dyn `[`Error`] used in macro expansions.
2+
//!
3+
//! # Credits
4+
//!
5+
//! The initial idea and implementation was taken from the [`thiserror`] crate and its
6+
//! [`AsDynError`] trait implementation, and slightly modified for usage in derive [`derive_more`].
7+
//!
8+
//! The original code was dual licensed under [Apache License, Version 2.0][APACHE] and [MIT]
9+
//! licenses.
10+
//!
11+
//! [`AsDynError`]: https://github.com/dtolnay/thiserror/blob/2.0.3/src/aserror.rs
12+
//! [`derive_more`]: crate
13+
//! [`thiserror`]: https://github.com/dtolnay/thiserror/blob/2.0.3
14+
//! [APACHE]: https://github.com/dtolnay/thiserror/blob/2.0.3/LICENSE-APACHE
15+
//! [MIT]: https://github.com/dtolnay/thiserror/blob/2.0.3/LICENSE-MIT
16+
17+
use core::{error::Error, panic::UnwindSafe};
18+
19+
#[doc(hidden)]
20+
pub trait AsDynError<'a>: Sealed {
21+
fn __derive_more_as_dyn_error(&self) -> &(dyn Error + 'a);
22+
}
23+
24+
impl<'a, T: Error + 'a> AsDynError<'a> for T {
25+
#[inline]
26+
fn __derive_more_as_dyn_error(&self) -> &(dyn Error + 'a) {
27+
self
28+
}
29+
}
30+
31+
impl<'a> AsDynError<'a> for dyn Error + 'a {
32+
#[inline]
33+
fn __derive_more_as_dyn_error(&self) -> &(dyn Error + 'a) {
34+
self
35+
}
36+
}
37+
38+
impl<'a> AsDynError<'a> for dyn Error + Send + 'a {
39+
#[inline]
40+
fn __derive_more_as_dyn_error(&self) -> &(dyn Error + 'a) {
41+
self
42+
}
43+
}
44+
45+
impl<'a> AsDynError<'a> for dyn Error + Send + Sync + 'a {
46+
#[inline]
47+
fn __derive_more_as_dyn_error(&self) -> &(dyn Error + 'a) {
48+
self
49+
}
50+
}
51+
52+
impl<'a> AsDynError<'a> for dyn Error + Send + Sync + UnwindSafe + 'a {
53+
#[inline]
54+
fn __derive_more_as_dyn_error(&self) -> &(dyn Error + 'a) {
55+
self
56+
}
57+
}
58+
59+
#[doc(hidden)]
60+
pub trait Sealed {}
61+
impl<T: Error> Sealed for T {}
62+
impl Sealed for dyn Error + '_ {}
63+
impl Sealed for dyn Error + Send + '_ {}
64+
impl Sealed for dyn Error + Send + Sync + '_ {}
65+
impl Sealed for dyn Error + Send + Sync + UnwindSafe + '_ {}

src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ pub mod __private {
6868
pub use crate::cmp::AssertParamIsEq;
6969

7070
#[cfg(feature = "error")]
71-
pub use crate::vendor::thiserror::aserror::AsDynError;
71+
pub use crate::as_dyn_error::AsDynError;
7272
}
7373

7474
// The modules containing error types and other helpers.
@@ -93,7 +93,7 @@ mod r#as;
9393
mod fmt;
9494

9595
#[cfg(feature = "error")]
96-
mod vendor;
96+
mod as_dyn_error;
9797

9898
#[cfg(feature = "from_str")]
9999
mod r#str;

src/vendor/mod.rs

Lines changed: 0 additions & 2 deletions
This file was deleted.

src/vendor/thiserror/LICENSE-APACHE

Lines changed: 0 additions & 176 deletions
This file was deleted.

src/vendor/thiserror/LICENSE-MIT

Lines changed: 0 additions & 23 deletions
This file was deleted.

src/vendor/thiserror/README.md

Lines changed: 0 additions & 6 deletions
This file was deleted.

src/vendor/thiserror/aserror.rs

Lines changed: 0 additions & 49 deletions
This file was deleted.

src/vendor/thiserror/mod.rs

Lines changed: 0 additions & 1 deletion
This file was deleted.

0 commit comments

Comments
 (0)