Repository navigation
feat: allow deprecated variants in the impls that stack_error generates - #52
Conversation
|
Documentation for this PR has been generated and is available at: https://n0-computer.github.io/n0-error/pr/52/docs/n0_error/ Last updated: 2026-07-14T10:38:44Z |
flub
left a comment
There was a problem hiding this comment.
When I've done this by hand I only apply the #[allow(deprecated)] on branch that is deprecated instead of the entire enum block. Is that worth doing here as well or is there no downside to allowing it on all branches?
Suppresses the deprecated lint in the generated impls, so enums can have
30af282 to
98afee4
Compare
Drop the allow_deprecated option and emit #[allow(deprecated)] on the generated enum impls unconditionally, like thiserror does. The impls only touch the user's own variants, so there is nothing to opt into.
|
Rebased onto main. I dropped the anyhow lock bump because main already has 1.0.104. The conflicts with #61 and #68 were mechanical, and tests pass. A suggestion: we may not need the option. The generated impls only touch the enum's own variants, so an unconditional On @flub's question: allowing per match arm would work, but it would mean touching every arm generator and the |
Alternative to #52, based on its branch: instead of an `allow_deprecated` option, always put `#[allow(deprecated)]` on the generated enum impls. The generated impls only match on and construct the user's own variants, so the allow never hides a real warning. Using a deprecated variant in user code still warns. thiserror does the same thing unconditionally, and serde does it automatically when it sees `#[deprecated]` on the item or a variant. Neither has an opt-in. The test from #52 now runs without the option and also covers a deprecated `from_sources` variant. Without the change it fails with 5 `deprecated` errors. ## Change checklist - [x] Self-review. - [x] Tests if relevant.
1bc3284 to
98afee4
Compare
|
I think it is preferably to solve this without an additional option, see #70 |
## Description Alternative to the unconditional allowance in #69, stacked on that branch for a focused comparison. No explicit `allow_deprecated` macro option. Follow [Serde's detection](https://github.com/serde-rs/serde/blob/master/serde_derive/src/deprecated.rs): if the type or one of its enum variants has `#[deprecated]` or `#[allow(deprecated)]`, allow deprecation in the generated impls. Otherwise leave the lint unchanged. This keeps ordinary enums working under `forbid(deprecated)` and preserves warnings for deprecated functions used in their `#[error("...", expression)]` formatting arguments. Deprecated types and variants still derive without internal warnings; direct uses in user code still warn. The implementation adds 34 lines around derive expansion and removes the unconditional allowances. A scoped anonymous const holds the generated impls, avoiding an extra flag passed through each generator. This also handles deprecated structs and whole enums. No new dependencies. ## Breaking Changes Compared with #69, deprecated formatting helpers on otherwise ordinary enums warn again. This preserves the behavior before the proposed unconditional allowance. ## Notes & open questions As with Serde, once detection triggers, the allowance covers all generated impls, including user-provided formatting expressions inside them. It is conditional, not scoped to individual expressions. Validation: 26 unit tests and 4 doctests pass, including two compile-fail regressions. `cargo fmt --check` passes. Clippy passes with the existing `clippy::useless_format` warning at `src/tests.rs:110` allowed; strict Clippy fails on that unchanged line. ## Change checklist - [x] Self-review. - [x] Documentation updates. - [x] Tests, including deprecated types/variants, explicit allowances, `forbid(deprecated)`, and retained user-code diagnostics. - [x] All breaking changes documented.
Description
The
stack_errormacro expands to impls that match on and construct every enum variant, including deprecated ones. With#[deny(deprecated)], an enum with a#[deprecated]variant therefore fails to compile at the#[stack_error(..)]call site.StackErrornow allows thedeprecatedlint in its generated impls when the type or one of its variants has#[deprecated]or#[allow(deprecated)]. This needs no option. Serde handles deprecation the same way.#[allow(deprecated)] const _: () = { ... };. User code that constructs a deprecated variant still warns.#[error("{}", old_formatter())]. Twocompile_faildoctests intests/deprecated.mdcheck this, together with the case above.#[allow(deprecated)]on the item or on a variant, and an enum without deprecation under#[forbid(deprecated)].Breaking Changes
None.
Notes & open questions
The first version of this PR added an opt-in
allow_deprecatedoption tostack_error. It is gone; the allowance is now inferred from the attributes (#69, #70).On per-arm allowances: the allowance is scoped to the generated impls, and those only touch the type's own variants, so allowing it on the whole impl hides no warning in user code.
Change checklist