-
-
Notifications
You must be signed in to change notification settings - Fork 14.7k
Unclear compiler error when impl Trait return value captures non-'static argument #82171
Copy link
Copy link
Closed
Closed
Copy link
Labels
A-diagnosticsArea: Messages for errors, warnings, and lintsArea: Messages for errors, warnings, and lintsA-impl-traitArea: `impl Trait`. Universally / existentially quantified anonymous types with static dispatch.Area: `impl Trait`. Universally / existentially quantified anonymous types with static dispatch.A-lifetimesArea: Lifetimes / regionsArea: Lifetimes / regionsC-bugCategory: This is a bug.Category: This is a bug.D-newcomer-roadblockDiagnostics: Confusing error or lint; hard to understand for new users.Diagnostics: Confusing error or lint; hard to understand for new users.D-papercutDiagnostics: An error or lint that needs small tweaks.Diagnostics: An error or lint that needs small tweaks.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.Relevant to the compiler team, which will review and decide on the PR/issue.fixed-by-TAITFixed by the feature `type_alias_impl_trait`.Fixed by the feature `type_alias_impl_trait`.
Metadata
Metadata
Assignees
Labels
A-diagnosticsArea: Messages for errors, warnings, and lintsArea: Messages for errors, warnings, and lintsA-impl-traitArea: `impl Trait`. Universally / existentially quantified anonymous types with static dispatch.Area: `impl Trait`. Universally / existentially quantified anonymous types with static dispatch.A-lifetimesArea: Lifetimes / regionsArea: Lifetimes / regionsC-bugCategory: This is a bug.Category: This is a bug.D-newcomer-roadblockDiagnostics: Confusing error or lint; hard to understand for new users.Diagnostics: Confusing error or lint; hard to understand for new users.D-papercutDiagnostics: An error or lint that needs small tweaks.Diagnostics: An error or lint that needs small tweaks.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.Relevant to the compiler team, which will review and decide on the PR/issue.fixed-by-TAITFixed by the feature `type_alias_impl_trait`.Fixed by the feature `type_alias_impl_trait`.
Type
Fields
Give feedbackNo fields configured for issues without a type.
(cc @estebank)
I tried this code (the explicit
'staticlifetime is unnecessary but emphasizes the implicit'staticrequirement ofimpl Trait):See playground.
I expected to see this happen:
Successful compilation or a clear error message suggesting that
impl Traitcan't be used in this way due to the non-'staticlifetime offoo's argumenta.Instead, this happened:
foo's return value isn't dependent ona's lifetime sinceto_stringconverts it to an ownedString. ReturningBox<dyn Iterator<Item = String>>instead of usingimpl Traitfixes the error.Neither of the compiler's suggestions are the "right" fix here, since I'd like the return value to have a
'staticlifetime but don't want to requireato have one.I think the ideal fix (to the error message) would be something along the lines of:
impl Traitdoesn't allow the return value not to depend on the lifetime ofa(maybe with a tracking issue if this limitation is being addressed)Box<dyn Trait>and/or a concrete typeMeta
rustc --version --verbose: