Skip to content

Higher-ranked lifetime bounds give confusing errors #29061

Open
@wthrowe

Description

@wthrowe
#![allow(dead_code)]

fn x(_: &()) {}

trait HR {}
impl HR for fn(&()) {}
fn hr<T: HR>(_: T) {}

trait NotHR {}
impl<'a> NotHR for fn(&'a ()) {}
fn not_hr<T: NotHR>(_: T) {}

fn a<'a>() {
    let not_hr_func: fn(&'a ()) = x;
    let hr_func: fn(&()) = x;
    let hr_func2: for<'b> fn(&'b ()) = x;
    hr(not_hr_func);
    not_hr(hr_func);
    not_hr(hr_func2);
}

fn main() {}

gives

<anon>:17:5: 17:7 error: the trait `HR` is not implemented for the type `fn(&())` [E0277]
<anon>:17     hr(not_hr_func);
              ^~
<anon>:17:5: 17:7 help: see the detailed explanation for E0277
<anon>:17:5: 17:7 note: required by `hr`
<anon>:18:5: 18:11 error: the trait `NotHR` is not implemented for the type `fn(&())` [E0277]
<anon>:18     not_hr(hr_func);
              ^~~~~~
<anon>:18:5: 18:11 help: see the detailed explanation for E0277
<anon>:18:5: 18:11 note: required by `not_hr`
<anon>:19:5: 19:11 error: the trait `NotHR` is not implemented for the type `fn(&'b ())` [E0277]
<anon>:19     not_hr(hr_func2);
              ^~~~~~
<anon>:19:5: 19:11 help: see the detailed explanation for E0277
<anon>:19:5: 19:11 note: required by `not_hr`
error: aborting due to 3 previous errors
playpen: application terminated with error code 101

Summarizing, given the type declaration, we have the printed form:

Declaration Display
fn(&'a ()) fn(&())
fn(&()) fn(&())
for<'b> fn(&'b ()) fn(&'b ())

So the non-higher-ranked type has its lifetime elided and is printed like a higher-ranked type, and higher-ranked types are sometimes printed without the quantifier but with the lifetime, looking like non-higher-ranked types.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsA-higher-rankedArea: Higher-ranked things (e.g., lifetimes, types, trait bounds aka HRTBs)A-lifetimesArea: Lifetimes / regionsC-enhancementCategory: An issue proposing an enhancement or a PR with one.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