-
-
Notifications
You must be signed in to change notification settings - Fork 14.9k
impl Trait returned from trait method without arguments not detected as 'static #119773
Copy link
Copy link
Open
Labels
A-diagnosticsArea: Messages for errors, warnings, and lintsArea: Messages for errors, warnings, and lintsA-lifetimesArea: Lifetimes / regionsArea: Lifetimes / regionsC-bugCategory: This is a bug.Category: This is a bug.D-invalid-suggestionDiagnostics: A structured suggestion resulting in incorrect code.Diagnostics: A structured suggestion resulting in incorrect code.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.
Metadata
Metadata
Assignees
Labels
A-diagnosticsArea: Messages for errors, warnings, and lintsArea: Messages for errors, warnings, and lintsA-lifetimesArea: Lifetimes / regionsArea: Lifetimes / regionsC-bugCategory: This is a bug.Category: This is a bug.D-invalid-suggestionDiagnostics: A structured suggestion resulting in incorrect code.Diagnostics: A structured suggestion resulting in incorrect code.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.
Type
Fields
Give feedbackNo fields configured for issues without a type.
I tried this code:
Playground
I expected the code to compile successfully, but it failed on Rust 1.75.0 with the following error:
I understand that
Box<dyn Fn()>is sugar forBox<dyn Fn() + 'static>, but in this case theimpl Fn()return is'static. It has to be, becauseOriginal::f()takes no arguments, so the value it returns cannot reference any non-static lifetime.The workaround is to change the return type of
Erased::f()fromBox<dyn Fn()>toBox<dyn Fn() + '_>, which is allows the code to compile. (Playground.) (Another workaround is to change the definition ofOriginal::f()to returnimpl Fn() + 'static, but in the original codeOriginalis provided by a different crate and cannot be modified.)A second, and minor issue is that the compiler's suggestion is not actionable because the opaque type returned from
Original::f()cannot be named.