Skip to content

Elision in method can take 'static from self, perhaps surprising #48686

Closed

Description

@cramertj

rustc currently compiles the following with no warnings or errors:

struct Foo;

impl Foo {
    pub fn get_mut(&'static self, x: &mut u8) -> &mut u8 {
        unsafe { &mut *(x as *mut _) }
    }
}

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:

struct Foo;

impl Foo {
    pub fn get_mut(&'static self, x: &mut u8) -> &mut u8 {
        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.

cc @aturon

Activity

  1. leoyvens commented on Mar 2, 2018

    @leoyvens
    Contributor

    Sounds like a case of "disallow elided lifetimes that refer to explicit lifetimes", I recall @nikomatsakis being in favor of forbidding this.

  2. nikomatsakis commented on Mar 6, 2018

    @nikomatsakis
    Contributor

    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.

  3. changed the title [-]Invalid elision in methods[/-] [+]Elision in method can take `'static` from `self`, perhaps surprising[/+] on Mar 6, 2018
  4. nikomatsakis commented on Mar 6, 2018

    @nikomatsakis
    Contributor

    I retitled the issue to be less dramatic =)

  5. added
    C-bugCategory: This is a bug.
    A-lifetimesArea: Lifetimes / regions
    T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
    on Mar 6, 2018
  6. Enselic commented on Nov 18, 2023

    @Enselic
    Member

    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.

  7. added
    C-enhancementCategory: An issue proposing an enhancement or a PR with one.
    A-lintsArea: Lints (warnings about flaws in source code) such as unused_mut.
    E-help-wantedCall for participation: Help is requested to fix this issue.
    and removed
    C-bugCategory: This is a bug.
    on Nov 18, 2023
  8. GrigorenkoPV commented on Aug 15, 2024

    @GrigorenkoPV
    Contributor

    @rustbot claim

  9. added a commit that references this issue on Aug 25, 2024
  10. 1 remaining item

  11. added a commit that references this issue on Sep 1, 2024
  12. added
    A-lintsArea: Lints (warnings about flaws in source code) such as unused_mut.
    and removed
    A-lintsArea: Lints (warnings about flaws in source code) such as unused_mut.
    on Dec 21, 2024
  13. added 3 commits that reference this issue on Jun 2, 2025
  14. added a commit that references this issue on Jun 5, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

A-lifetimesArea: Lifetimes / regionsA-lintsArea: Lints (warnings about flaws in source code) such as unused_mut.C-enhancementCategory: An issue proposing an enhancement or a PR with one.E-help-wantedCall for participation: Help is requested to fix this issue.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions