Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

relating higher-ranked projections can unexpectedly constrain inference #107268

Open
aliemjay opened this issue Jan 24, 2023 · 4 comments
Open

relating higher-ranked projections can unexpectedly constrain inference #107268

aliemjay opened this issue Jan 24, 2023 · 4 comments
Labels
A-higher-ranked Area: Higher-ranked things (e.g., lifetimes, types, trait bounds aka HRTBs) A-inference Area: Type inference C-bug Category: This is a bug. P-medium Medium priority T-types Relevant to the types team, which will review and decide on the PR/issue.

Comments

@aliemjay
Copy link
Member

aliemjay commented Jan 24, 2023

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

@rustbot rustbot added A-inference Area: Type inference C-bug Category: This is a bug. I-unsound Issue: A soundness hole (worst kind of bug), see: https://en.wikipedia.org/wiki/Soundness T-types Relevant to the types team, which will review and decide on the PR/issue. I-prioritize Issue: Indicates that prioritization has been requested for this issue. labels Jan 24, 2023
@aliemjay
Copy link
Member Author

aliemjay commented Jan 24, 2023

Here is a variant that shows why it's unsound; Inference ambiguity is accepted here:

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

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

fn test<A, B>() -> impl Fn(A::Ty<'static>)
where
    A: Trait,
    B: for<'a> Trait<Ty<'a> = A::Ty<'a>>,
{
    my_fn::<_> // A and B are both valid options; We pick A!
}

@compiler-errors
Copy link
Member

compiler-errors commented Jan 24, 2023

I think this needs a better example for the unsoundness. The inference variable being constrained by one where-clause predicate over the other is not unsound, regardless of whether we think that the behavior is surprising.

At least, this is totally fine to do inside of a function body, I thought?

@aliemjay
Copy link
Member Author

After the discussion in a zulip thread, I agree that this is not necessarily a soundness bug. 😅

@rustbot label -I-unsound

@rustbot rustbot removed the I-unsound Issue: A soundness hole (worst kind of bug), see: https://en.wikipedia.org/wiki/Soundness label Jan 27, 2023
@aliemjay aliemjay changed the title relating higher-ranked projections can unsoundly constrain inference relating higher-ranked projections can unexpectedly constrain inference Jan 27, 2023
@apiraino
Copy link
Contributor

WG-prioritization assigning priority (Zulip discussion).

@rustbot label -I-prioritize +P-medium

@rustbot rustbot added P-medium Medium priority and removed I-prioritize Issue: Indicates that prioritization has been requested for this issue. labels Jan 31, 2023
@fmease fmease added the A-higher-ranked Area: Higher-ranked things (e.g., lifetimes, types, trait bounds aka HRTBs) label Sep 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-higher-ranked Area: Higher-ranked things (e.g., lifetimes, types, trait bounds aka HRTBs) A-inference Area: Type inference C-bug Category: This is a bug. P-medium Medium priority T-types Relevant to the types team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

5 participants