You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Elision in method can take 'static from self, perhaps surprising #48686
However, attempting to make this function safe reveals that the elided lifetime of x isn't the lifetime in the return type-- it's instead copying over the 'static lifetime into the return type! Rustdoc actually documents that the signature of this method is fn get_mut(&'static self, x: &mut u8) -> &'static mut u8.
The following code produces an error:
structFoo;implFoo{pubfnget_mut(&'staticself,x:&mutu8) -> &mutu8{
x
}}
error[E0312]: lifetime of reference outlives lifetime of borrowed content...
--> src/main.rs:7:9
|
7 | x
| ^
|
= note: ...the reference is valid for the static lifetime...
note: ...but the borrowed content is only valid for the anonymous lifetime #1 defined on the method body at 6:5
--> src/main.rs:6:5
|
6 | / pub fn get_mut(&'static self, x: &mut u8) -> &mut u8 {
7 | | x
8 | | }
| |_____^
error: aborting due to previous error
error: Could not compile `playground`.
This seems like it could be fixed by disallowing elision in this case via a lint.
I agree this is undesirable, but as @leodasvacas notes, I would say 'no more undesirable than any other named lifetime'. We should implement that lint.
Triage: Marking as E-help-wanted to implement a lint that warns when an elided lifetime ends up being a named lifetime. There seems to be a broad consensus that this would be good to lint for.
Activity
leoyvens commentedon Mar 2, 2018
Sounds like a case of "disallow elided lifetimes that refer to explicit lifetimes", I recall @nikomatsakis being in favor of forbidding this.
nikomatsakis commentedon Mar 6, 2018
I agree this is undesirable, but as @leodasvacas notes, I would say 'no more undesirable than any other named lifetime'. We should implement that lint.
[-]Invalid elision in methods[/-][+]Elision in method can take `'static` from `self`, perhaps surprising[/+]nikomatsakis commentedon Mar 6, 2018
I retitled the issue to be less dramatic =)
Enselic commentedon Nov 18, 2023
Triage: Marking as E-help-wanted to implement a lint that warns when an elided lifetime ends up being a named lifetime. There seems to be a broad consensus that this would be good to lint for.
GrigorenkoPV commentedon Aug 15, 2024
@rustbot claim
Rollup merge of rust-lang#129207 - GrigorenkoPV:elided-is-named, r=cj…
1 remaining item
Unrolled build for rust-lang#129207
Rollup merge of #129207 - GrigorenkoPV:elided-is-named, r=cjgillot
Rollup merge of #129207 - GrigorenkoPV:elided-is-named, r=cjgillot
Rollup merge of #129207 - GrigorenkoPV:elided-is-named, r=cjgillot
Auto merge of #138677 - shepmaster:consistent-elided-lifetime-syntax,…
Auto merge of #138677 - shepmaster:consistent-elided-lifetime-syntax,…
Auto merge of #138677 - shepmaster:consistent-elided-lifetime-syntax,…
Auto merge of #138677 - shepmaster:consistent-elided-lifetime-syntax,…
Auto merge of #138677 - shepmaster:consistent-elided-lifetime-syntax,…
Auto merge of #138677 - shepmaster:consistent-elided-lifetime-syntax,…
Auto merge of #138677 - shepmaster:consistent-elided-lifetime-syntax,…
Auto merge of #138677 - shepmaster:consistent-elided-lifetime-syntax,…