Skip to content

self: &Box<Self> produces confusing error due to failure to spot elided lifetime #117715

Closed
@adetaylor

Description

Code

struct Concrete(u32);

impl Concrete {
    fn m(self: &std::boxed::Box<Self>) -> &u32 {
        &self.0
    }
}

Current output

error[E0106]: missing lifetime specifier
 --> src/lib.rs:4:43
  |
4 |     fn m(self: &std::boxed::Box<Self>) -> &u32 {
  |                ----------------------     ^ expected named lifetime parameter
  |
  = help: this function's return type contains a borrowed value, but the signature does not say which `self` it is borrowed from
help: consider introducing a named lifetime parameter
  |
4 |     fn m<'a>(self: &'a std::boxed::Box<Self>) -> &'a u32 {
  |         ++++        ++                            ++

For more information about this error, try `rustc --explain E0106`.
error: could not compile `playground` (lib) due to previous error

Desired output

Ideally, rustc recognizes the elided lifetime in the `self` parameter and uses that.

Alternatively, "the signature does not say which `self` it is borrowed from" is reworded to more accurately describe the error.

Rationale and extra context

No response

Other cases

No response

Anything else?

fn n(self: &std::boxed::Box<Self>, _other: &u32) -> &u32 {
    &self.0
}

results in an error where rustc assumes we're returning the _other parameter, which presumably results from the same failure to spot a lifetime attached to self.

This issue was identified during discussion about a new Rust RFC, here.

I suspect this is caused by something within find_lifetime_for_self which isn't aware of the need to support things like Box as a self type: I haven't spotted the bug yet, I thought it was more important to get it reported properly first.

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsA-lifetimesArea: Lifetimes / regionsC-bugCategory: This is a bug.D-confusingDiagnostics: Confusing error or lint that should be reworked.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