Skip to content

lifetime misformatted (leading &) in rustc note output #10291

Closed
@pnkfelix

Description

@pnkfelix

Consider compile_fail/regions-nested-fns.rs:

fn ignore<T>(t: T) {}

fn nested<'x>(x: &'x int) {
    let y = 3;
    let mut ay = &y; //~ ERROR cannot infer an appropriate lifetime

    ignore::< <'z>|&'z int|>(|z| {
        ay = x;
        ay = &y;
        ay = z;
    });

    ignore::< <'z>|&'z int| -> &'z int>(|z| {
        if false { return x; }  //~ ERROR mismatched types
        //~^ ERROR cannot infer an appropriate lifetime
        if false { return ay; }
        return z;
    });
}

fn main() {}

When you run this, in addition to various error output, you will also see messages prefixed by note:. In those messages, when a lifetime is printed, it is printed with a leading & in addition to the ' that is part of its name. For example:

[...] the lifetime cannot outlive the lifetime &'z1 as defined [...].

In my opinion, this is confusing. 'z is a lifetime, while &'z are the first three characters of some (incomplete) type expression that is missing the pointed-to type (along with a potential mut qualifier).

I suspect the leading & is arising probably some artifact of how the code is implementing the fmt::Default trait; it might also involve the use of bound_region_ptr_to_str as the implementation of how lifetimes are printed (even though the former says it is for printing ptrs, not lifetimes).


Also, there is an extra space between &'z and as defined above.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-lifetimesArea: Lifetimes / regionsE-easyCall for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions