Merge rust-bitcoin/rust-bitcoin#7014: Make source destructure official policy and apply it everywhere
What changed, and why it matters
This is a code-quality and documentation patch. It adds a project policy saying that when a Rust error type has no underlying cause, the `source` method should explicitly destructure the error's fields before returning `None`. This makes future additions to the error type fail to compile unless `source` is updated, preventing accidental omissions. The patch also applies this style to three recently-added error types. There is no runtime behavior change and no security vulnerability is fixed or introduced.
No security action required. Treat as normal code-style/policy maintenance.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The merge commit codifies policy P-ERROR-SOURCE in docs/policy.md and updates three std::error::Error::source implementations to destructure self before returning None: MaximumDepthExceededError, CannotDeriveHardenedChildError in key_expression/src/bip32.rs, and FeatureDataError in p2p/src/bip434.rs. The change is compile-time defensive: adding a new field to these unit-like or struct error types without updating source will now produce a pattern-matching error. The runtime return value remains None, so observable behavior is unchanged.
Changed components
docs/policy.mdkey_expression/src/bip32.rsp2p/src/bip434.rsInspect captured patch +33 / −4
### docs/policy.md
@@ -17,6 +17,7 @@ the various PRs over the last few years.
- [Return `Self`](#return-self)
- [Errors](#errors)
- [`From<Infallible>` (P-ERROR-INFALLIBLE)](#frominfallible-p-error-infallible)
+ - [`source` (P-ERROR-SOURCE)](#source-p-error-source)
- [`expect` messages](#expect-messages)
- [Rustdocs](#rustdocs)
- [Links](#links)
@@ -210,7 +211,8 @@ More specifically an error should
- not have `Error` suffix on enum variants.
- implement `std::error::Error` if they are public (feature gated on "std").
- have messages in lower case, except for proper nouns and variable names.
-- implement `From<Infallible>` (more on that below).
+- implement `From<Infallible>`, see [P-ERROR-INFALLIBLE](#frominfallible-p-error-infallible).
+- destructure `self` in `source` when it returns `None`, see [P-ERROR-SOURCE](#source-p-error-source).
```rust
/// Documentation for the `Error` type.
@@ -237,6 +239,20 @@ All public error types should implement `From<Infallible>` to allow callers to s
[`Infallible`](https://doc.rust-lang.org/std/convert/enum.Infallible.html) is a "temporary" (years long) type system hack which should hopefully be replaced in the future by [a rust language feature](https://github.com/rust-lang/rfcs/blob/master/text/1216-bang-type.md). Implementing `Infallible` on rust-bitcoin errors allows callers to take advantage now and avoid orphan rule issues.
+### `source` (P-ERROR-SOURCE)
+
+When `source` returns `None`, destructure `self` first, so that when adding a field later, it fails to compile and nobody forgets to update `source`.
+
+```rust
+#[cfg(feature = "std")]
+impl std::error::Error for Error {
+ fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
+ let Self { input: _, bits: _ } = self;
+ None
+ }
+}
+```
+
## `expect` messages
With respect to `expect` messages, they should follow the
### key_expression/src/bip32.rs
@@ -1371,7 +1371,12 @@ pub mod error {
}
#[cfg(feature = "std")]
- impl std::error::Error for MaximumDepthExceededError {}
+ impl std::error::Error for MaximumDepthExceededError {
+ fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
+ let Self {} = self;
+ None
+ }
+ }
impl fmt::Display for MaximumDepthExceededError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
@@ -1391,7 +1396,12 @@ pub mod error {
}
#[cfg(feature = "std")]
- impl std::error::Error for CannotDeriveHardenedChildError {}
+ impl std::error::Error for CannotDeriveHardenedChildError {
+ fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
+ let Self {} = self;
+ None
+ }
+ }
impl fmt::Display for CannotDeriveHardenedChildError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
### p2p/src/bip434.rs
@@ -288,7 +288,10 @@ pub mod error {
#[cfg(feature = "std")]
impl std::error::Error for FeatureDataError {
- fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { None }
+ fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
+ let Self { too_long: _ } = self;
+ None
+ }
}
/// Errors occurring when decoding a [`FeatureId`] message.Why this scored 15/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.