Repository navigation
Avoid method-call shadowing of the hidden AsDynError helper - #463
Open
draarivpatel-ui wants to merge 2 commits into
Open
draarivpatel-ui wants to merge 2 commits into
draarivpatel-ui wants to merge 2 commits into
Conversation
The derive generates calls to `AsDynError::as_dyn_error` through method-call resolution, so an inherent method with the same name on the source field type silently shadows it. That makes `#[error(transparent)]` and `#[source]` fields forward to the wrong error, which rootcause hit when it added an inherent `as_dyn_error`. Fully qualifying the call would break auto-deref for fields such as `Box<dyn Error>`, because UFCS does not deref through the box. Instead, rename the hidden method to `__thiserror_as_dyn_error`. Method-call resolution keeps the auto-deref behavior, and the generated code no longer collides with a name that downstream crates use. Adds a regression test covering transparent structs and enums as well as source struct and enum variants with an inherent `as_dyn_error`. Fixes dtolnay#452
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
thiserrorexpands#[error(transparent)]and#[source]fields to a call of the hiddenAsDynError::as_dyn_errormethod through method-call resolution. An inherent method with the same name on the field type therefore shadows the trait method, and the generatedsource()forwards to whatever the inherent method returns. Rootcause hit this after adding an inherentas_dyn_error(#452).Change
Fully qualifying the call as suggested in the issue is not possible without breaking
Box<dyn Error>fields: UFCS does not deref through the box, andBox<dyn Error>does not implementErrorbecause of itsSizedbound. Instead this renames the hidden method to__thiserror_as_dyn_error, which keeps method-call auto-deref while making accidental shadowing essentially impossible.Tests
cargo test(full suite) andcargo fmt --checkpass. The new regression test defines a source type with an inherentas_dyn_errorand checks transparent struct/enum and source struct/enum variants still forward to the real error. It fails on the previous code.Fixes #452