Skip to content

Lifetime elision doesn't work right with nested self receivers.  #69064

Open
@manuthambi

Description

@manuthambi

Seems like lifetime elision doesn't work quite right with nested self receivers. The error messages are also misleading. Of course this can be worked around by providing the annotation in the code

use std::rc::Rc;

struct S {
    val: String,
}

impl S {
    // error[E0623]: lifetime mismatch
    //  --> src/lib.rs:9:9
    //   |
    // 8 |     fn rc1(self: &Rc<Self>, _s: &str) -> &str {
    //   |                  ---------               ----
    //   |                  |
    //   |                  this parameter and the return type are declared with different lifetimes...
    // 9 |         self.val.as_ref()
    //   |         ^^^^^^^^^^^^^^^^^ ...but data from `self` is returned here
    fn rc1(self: &Rc<Self>, _s: &str) -> &str {
        self.val.as_ref()
    }

    //     error[E0106]: missing lifetime specifier
    //   --> src/lib.rs:11:32
    //    |
    // 11 |     fn rc2(self: &Rc<Self>) -> &str {
    //    |                                ^ help: consider giving it a 'static lifetime: `&'static`
    //    |
    //    = help: this function's return type contains a borrowed value, but there is no value for it to be borrowed from
    fn rc2(self: &Rc<Self>) -> &str {
        self.val.as_ref()
    }


    // Compiles fine
    fn ref1(&self, _s: &str) -> &str {
        self.val.as_ref()
    }

    // Compiles fine
    fn ref2(&self) -> &str {
        self.val.as_ref()
    }
}

#64325

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-lifetimesArea: Lifetimes / regionsC-bugCategory: This is a bug.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.T-langRelevant to the language 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