-
-
Notifications
You must be signed in to change notification settings - Fork 14.7k
Compiler doesn't explain that unfulfilled lifetime requirement comes from object lifetime default #103849
Copy link
Copy link
Open
Labels
A-diagnosticsArea: Messages for errors, warnings, and lintsArea: Messages for errors, warnings, and lintsA-dyn-traitArea: trait objects, vtable layoutArea: trait objects, vtable layoutA-lifetimesArea: Lifetimes / regionsArea: Lifetimes / regionsD-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.
Metadata
Metadata
Assignees
Labels
A-diagnosticsArea: Messages for errors, warnings, and lintsArea: Messages for errors, warnings, and lintsA-dyn-traitArea: trait objects, vtable layoutArea: trait objects, vtable layoutA-lifetimesArea: Lifetimes / regionsArea: Lifetimes / regionsD-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.
Type
Fields
Give feedbackNo fields configured for issues without a type.
Given the following code (Playground link):
The current output is:
Ideally the output should look like:
Rationale for the change:
I'm fairly new to Rust. When I first encountered this errors, there were three things about this message I didn't understand:
Do types have a lifetime?
The error message states that "the parameter type
Tmay not live long enough". In non-generic code, Rust's error messages talk about the lifetimes of concrete variables, not the lifetimes of types. My understanding is that there is that types themselves always exist and that there is no such thing as the lifetime of a type, only the lifetime of a variable of that type. So I guess "the parameter typeTmay not live long enough" is just shorthand for "variables of typeTmay not live long enough". The latter would have made the error message clearer to me.How can
valuenot live long enough?The only variable of type
Tisvalue. This variable is passed by value, not by reference, and thus moved. I just couldn't think of a scenario where this variable might not live long enough.The answer, as I now realize, is that
Tmay be a struct containing references, which in turn may have a limited lifetime. Given that this is the only problematic case, it would have helped me if the error message had explicitly mentioned it.Must I really limit my function to arguments with static lifetime?
The error message suggests adding the explicit lifetime bound
'statictoT. When I first came across this message, I didn't realize that "static" as a trait bound has a different meaning to "static" as a reference lifetime. I assumed that adding the trait bound'statictoTwould allow only arguments with a static lifetime to be passed, making the function useless. This is not the case. It would have helped me if the suggestion had mentioned this in some way.