Skip to content

Compiler can't unify arg-position impl Trait into an HRTB #119045

Open
@Patryk27

Description

It looks like using arg: impl Trait makes the compiler unable to generalize the entire function-type into HRTB (?), that is:

fn foo(_: &str) {
    //
}

fn bar(_: impl AsRef<str>) {
    //
}

fn zar<T>(_: T) {
    //
}

fn check(_: impl Fn(&str)) {
    //
}

fn main() {
    check(foo); // ok
    check(bar); // err (implementation not general enough)
    check(zar::<&str>); // err (ditto)
}

Using a no-op closure helps:

fn main() {
    check(|x| bar(x));
}

... but is somewhat inconvenient (spotted in the wild at clap-rs/clap#4939).

Note that I'm reporting this is a bug, but I'm not really sure on the category here - it just feels like this code should pass type-checking, but maybe there's some deeper reason why it can't.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    A-higher-rankedArea: Higher-ranked things (e.g., lifetimes, types, trait bounds aka HRTBs)A-lifetimesArea: Lifetimes / regionsA-trait-systemArea: Trait systemC-discussionCategory: Discussion or questions that doesn't represent real issues.T-typesRelevant to the types 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