Skip to content

relating higher-ranked projections can unexpectedly constrain inference #107268

Open
@aliemjay

Description

@aliemjay

The following compiles (although it shouldn't, arguably, see below for reasoning): https://play.rust-lang.org/?version=nightly&mode=debug&edition=2021&gist=05d31c1cc2a9e8752d7a3fc05e9446ea

pub trait Trait {
    type Ty<'a>;
}

fn my_fn<T: Trait>(_: T::Ty<'_>) {}

fn test<A: Trait, B: Trait>() -> impl Fn(A::Ty<'static>) {
    my_fn
}

It stops compiling with a trivial change that gets rid of higher-ranked regions:

- fn my_fn<T: Trait>(_: T::Ty<'_>) {}
+ fn my_fn<T: Trait>(_: T::Ty<'static>) {}

fn test<A: Trait, B: Trait>() -> impl Fn(A::Ty<'static>) {
    my_fn
+   //~^ ERROR type annotation needed
}

This was discovered by @BoxyUwU in #96912.

@rustbot label T-types C-bug I-unsound A-inference

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-higher-rankedArea: Higher-ranked things (e.g., lifetimes, types, trait bounds aka HRTBs)A-inferenceArea: Type inferenceC-bugCategory: This is a bug.P-mediumMedium priorityT-typesRelevant to the types team, which will review and decide on the PR/issue.fixed-by-next-solverFixed by the next-generation trait solver, `-Znext-solver`.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions