Skip to content

Failure to assume GAT normalization holds when proving GAT bounds in impl #117606

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

Open
compiler-errors opened this issue Nov 5, 2023 · 1 comment
Labels
A-GATs Area: Generic associated types (GATs) F-associated_type_defaults `#![feature(associated_type_defaults)]` T-types Relevant to the types team, which will review and decide on the PR/issue.

Comments

@compiler-errors
Copy link
Member

#![feature(associated_type_defaults)]

trait Foo {
    type Bar<T>: Baz<Self> = i32;
    // We should be able to prove that `i32: Baz<Self>` because of
    // the impl below, which requires that `Self::Bar<()>: Eq<i32>`
    // which is true, because we assume `for<T> Self::Bar<T> = i32`.
}

trait Baz<T: ?Sized> {}
impl<T: Foo + ?Sized> Baz<T> for i32 where T::Bar<()>: Eq<i32> {}
trait Eq<T> {}
impl<T> Eq<T> for T {}

fn main() {}

This code should pass, but it doesn't after #117542.

Instead, it fails with:

error[E0277]: the trait bound `<Self as Foo>::Bar<()>: Eq<i32>` is not satisfied
  --> $DIR/assume-gat-normalization-for-nested-goals.rs:6:30
   |
LL |     type Bar<T>: Baz<Self> = i32;
   |                              ^^^ the trait `Eq<i32>` is not implemented for `<Self as Foo>::Bar<()>`
   |
note: required for `i32` to implement `Baz<Self>`
  --> $DIR/assume-gat-normalization-for-nested-goals.rs:13:23
   |
LL | impl<T: Foo + ?Sized> Baz<T> for i32 where T::Bar<()>: Eq<i32> {}
   |                       ^^^^^^     ^^^                   ------- unsatisfied trait bound introduced here
note: required by a bound in `Foo::Bar`
  --> $DIR/assume-gat-normalization-for-nested-goals.rs:6:18
   |
LL |     type Bar<T>: Baz<Self> = i32;
   |                  ^^^^^^^^^ required by this bound in `Foo::Bar`
help: consider further restricting the associated type
   |
LL | trait Foo where <Self as Foo>::Bar<()>: Eq<i32> {
   |           +++++++++++++++++++++++++++++++++++++

error: aborting due to previous error

For more information about this error, try `rustc --explain E0277`.
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Nov 5, 2023
@compiler-errors
Copy link
Member Author

To fix this, we need to quantify the GAT universally over all of its (impl+gat) vars here:

// Extend the impl's identity args with late-bound GAT vars
let normalize_impl_ty_args = ty::GenericArgs::identity_for_item(tcx, container_id)
.extend_to(tcx, impl_ty.def_id, |param, _| match param.kind {
GenericParamDefKind::Type { .. } => {
let kind = ty::BoundTyKind::Param(param.def_id, param.name);
let bound_var = ty::BoundVariableKind::Ty(kind);
bound_vars.push(bound_var);
Ty::new_bound(
tcx,
ty::INNERMOST,
ty::BoundTy { var: ty::BoundVar::from_usize(bound_vars.len() - 1), kind },
)
.into()
}
GenericParamDefKind::Lifetime => {
let kind = ty::BoundRegionKind::BrNamed(param.def_id, param.name);
let bound_var = ty::BoundVariableKind::Region(kind);
bound_vars.push(bound_var);
ty::Region::new_late_bound(
tcx,
ty::INNERMOST,
ty::BoundRegion {
var: ty::BoundVar::from_usize(bound_vars.len() - 1),
kind,
},
)
.into()
}
GenericParamDefKind::Const { .. } => {
let bound_var = ty::BoundVariableKind::Const;
bound_vars.push(bound_var);
ty::Const::new_bound(
tcx,
ty::INNERMOST,
ty::BoundVar::from_usize(bound_vars.len() - 1),
tcx.type_of(param.def_id)
.no_bound_vars()
.expect("const parameter types cannot be generic"),
)
.into()
}
});

...rather than just its gat vars.

That's obviously not sound unless the projection predicate requires the where clauses of the impl to hold, which requires having where clauses on binders.

@saethlin saethlin added F-associated_type_defaults `#![feature(associated_type_defaults)]` T-types Relevant to the types team, which will review and decide on the PR/issue. and removed needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels Nov 18, 2023
@fmease fmease added the A-GATs Area: Generic associated types (GATs) label Sep 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-GATs Area: Generic associated types (GATs) F-associated_type_defaults `#![feature(associated_type_defaults)]` 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

4 participants