Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
manuthambi opened this issue Feb 11, 2020 · 2 comments
Open

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

manuthambi opened this issue Feb 11, 2020 · 2 comments
Labels
A-lifetimes Area: Lifetimes / regions C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-lang Relevant to the language team, which will review and decide on the PR/issue.

Comments

@manuthambi
Copy link

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

@manuthambi manuthambi added the C-bug Category: This is a bug. label Feb 11, 2020
@jonas-schievink jonas-schievink added A-lifetimes Area: Lifetimes / regions T-lang Relevant to the language team, which will review and decide on the PR/issue. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Feb 11, 2020
@estebank estebank added the F-arbitrary_self_types `#![feature(arbitrary_self_types)]` label Feb 12, 2020
@Centril
Copy link
Contributor

Centril commented Feb 17, 2020

cc @cramertj @nikomatsakis @taiki-e

@steffahn
Copy link
Member

steffahn commented Sep 19, 2021

I don’t see the relevance of F-arbitrary_self_types, as &Rc<Self> works on stable without any feature flags.

@rustbot label -F-arbitrary_self_types

@rustbot rustbot removed the F-arbitrary_self_types `#![feature(arbitrary_self_types)]` label Sep 19, 2021
bors added a commit to rust-lang/rust-clippy that referenced this issue Jan 22, 2022
…=xFrednet

`needless_lifetimes`: ignore lifetimes in explicit self types

changelog: false positive fix: [`needless_lifetimes`] no longer lints lifetimes in explicit self types

They're not currently elidable (rust-lang/rust#69064)

Fixes #7296
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lifetimes Area: Lifetimes / regions C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-lang Relevant to the language team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

6 participants