Skip to content

nested RPITIT in default methods result in type mismatch #142

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

Closed
lcnr opened this issue Jan 21, 2025 · 5 comments · Fixed by rust-lang/rust#137000
Closed

nested RPITIT in default methods result in type mismatch #142

lcnr opened this issue Jan 21, 2025 · 5 comments · Fixed by rust-lang/rust#137000
Assignees

Comments

@lcnr
Copy link
Contributor

lcnr commented Jan 21, 2025

use std::ops::Deref;

trait Foo {
    fn bar() -> impl Deref<Target = impl Sized> {
        &()
    }
}

fn main() {}
error[E0271]: type mismatch resolving `<impl Deref<Target = impl Sized> as Deref>::Target == impl Sized`
 --> <source>:4:17
  |
4 |     fn bar() -> impl Deref<Target = impl Sized> {
  |                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ types differ
  |
note: required by a bound in `Foo::{synthetic#0}`
 --> <source>:4:28
  |
4 |     fn bar() -> impl Deref<Target = impl Sized> {
  |                            ^^^^^^^^^^^^^^^^^^^ required by this bound in `Foo::{synthetic#0}`

affected tests

  • tests/ui/impl-trait/in-trait/nested-rpitit-bounds.rs
  • tests/ui/impl-trait/in-trait/default-body-with-rpit.rs
@compiler-errors
Copy link
Member

compiler-errors commented Jan 22, 2025

Given:

trait Foo {
    fn bar() -> impl Deref<Target = impl Sized> {
        &()
    }
}

Let's partially desugar things here just to give them names:

type RPIT1 = impl Deref<Target = RPIT2>;
type RPIT2 = impl Sized;

trait Foo {
    type GAT1: Deref<Target = Self::GAT2> = RPIT1;
    type GAT2: Sized = RPIT2;

    fn bar() -> Self::GAT1 {
        &()
    }
}

We want to prove that the default value of GAT1, namely RPIT1, satisfies the item bounds for GAT1. This requires us to prove RPIT1: Deref<Target = Self::GAT2>. We can't normalize Self::GAT2 to RPIT2 since we have a param-env of Self: Foo which shadows the normalization.

This is side-stepped in the old solver by the construction of the normalize_param_env. See rust-lang/rust#117131 for more info. Since we don't actually normalize anything in the new solver, this is insufficient.

One way to fix this would be to augment the ReplaceTy folder to actually replace RPITITs eagerly too 🤔...

@lcnr
Copy link
Contributor Author

lcnr commented Feb 3, 2025

for now, we've agreed to deeply normalize in the normalize_param_env with the new solver as well

@compiler-errors
Copy link
Member

This is not as easy as deeply normalizing in the normalize_param_env, since there's ambiguity between using the impl to normalize and installing a projection predicate. Consider:

trait Ref<'a> {
    type Assoc;
}
impl<'a, T> Ref<'a> for T where T: 'a {
    type Assoc = &'a T;
}

Normalizing <T as Ref<'a>>::Assoc with the normalize-param-env will have two candidates that differ in their response region: via the impl, and via a projection predicate that we install. Since we don't also install an identity trait predicate (i.e. T: Ref<'a>), then there's no preference for the projection predicate. If we do that, though, then other things get fucked up.

@compiler-errors
Copy link
Member

  1. only install normalize_param_env only if associated item is overrideable
  2. change shadowing

@lcnr lcnr moved this from unknown to todo in -Znext-solver=globally Feb 13, 2025
@lcnr lcnr moved this from todo to in progress in -Znext-solver=globally Feb 17, 2025
Zalathar added a commit to Zalathar/rust that referenced this issue Feb 18, 2025
…tem-bounds, r=lcnr

Deeply normalize item bounds in new solver

Built on rust-lang#136863.

Fixes rust-lang/trait-system-refactor-initiative#142.
Fixes rust-lang/trait-system-refactor-initiative#151.

cc rust-lang/trait-system-refactor-initiative#116

First commit reworks candidate preference for projection bounds to prefer param-env projection clauses even if the corresponding trait ref doesn't come from the param-env.

Second commit adjusts the associated type item bounds check to deeply normalize in the new solver. This causes some test fallout which I will point out.

r? lcnr
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Feb 18, 2025
…tem-bounds, r=lcnr

Deeply normalize item bounds in new solver

Built on rust-lang#136863.

Fixes rust-lang/trait-system-refactor-initiative#142.
Fixes rust-lang/trait-system-refactor-initiative#151.

cc rust-lang/trait-system-refactor-initiative#116

First commit reworks candidate preference for projection bounds to prefer param-env projection clauses even if the corresponding trait ref doesn't come from the param-env.

Second commit adjusts the associated type item bounds check to deeply normalize in the new solver. This causes some test fallout which I will point out.

r? lcnr
GuillaumeGomez added a commit to GuillaumeGomez/rust that referenced this issue Feb 18, 2025
…tem-bounds, r=lcnr

Deeply normalize item bounds in new solver

Built on rust-lang#136863.

Fixes rust-lang/trait-system-refactor-initiative#142.
Fixes rust-lang/trait-system-refactor-initiative#151.

cc rust-lang/trait-system-refactor-initiative#116

First commit reworks candidate preference for projection bounds to prefer param-env projection clauses even if the corresponding trait ref doesn't come from the param-env.

Second commit adjusts the associated type item bounds check to deeply normalize in the new solver. This causes some test fallout which I will point out.

r? lcnr
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Feb 18, 2025
…tem-bounds, r=lcnr

Deeply normalize item bounds in new solver

Built on rust-lang#136863.

Fixes rust-lang/trait-system-refactor-initiative#142.
Fixes rust-lang/trait-system-refactor-initiative#151.

cc rust-lang/trait-system-refactor-initiative#116

First commit reworks candidate preference for projection bounds to prefer param-env projection clauses even if the corresponding trait ref doesn't come from the param-env.

Second commit adjusts the associated type item bounds check to deeply normalize in the new solver. This causes some test fallout which I will point out.

r? lcnr
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Feb 18, 2025
…tem-bounds, r=lcnr

Deeply normalize item bounds in new solver

Built on rust-lang#136863.

Fixes rust-lang/trait-system-refactor-initiative#142.
Fixes rust-lang/trait-system-refactor-initiative#151.

cc rust-lang/trait-system-refactor-initiative#116

First commit reworks candidate preference for projection bounds to prefer param-env projection clauses even if the corresponding trait ref doesn't come from the param-env.

Second commit adjusts the associated type item bounds check to deeply normalize in the new solver. This causes some test fallout which I will point out.

r? lcnr
rust-timer added a commit to rust-lang-ci/rust that referenced this issue Feb 19, 2025
Rollup merge of rust-lang#137000 - compiler-errors:deeply-normalize-item-bounds, r=lcnr

Deeply normalize item bounds in new solver

Built on rust-lang#136863.

Fixes rust-lang/trait-system-refactor-initiative#142.
Fixes rust-lang/trait-system-refactor-initiative#151.

cc rust-lang/trait-system-refactor-initiative#116

First commit reworks candidate preference for projection bounds to prefer param-env projection clauses even if the corresponding trait ref doesn't come from the param-env.

Second commit adjusts the associated type item bounds check to deeply normalize in the new solver. This causes some test fallout which I will point out.

r? lcnr
@lcnr
Copy link
Contributor Author

lcnr commented Feb 19, 2025

fixed by rust-lang/rust#137000

@lcnr lcnr closed this as completed Feb 19, 2025
@lcnr lcnr moved this from in progress to done in -Znext-solver=globally Feb 19, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Development

Successfully merging a pull request may close this issue.

2 participants