Skip to content

opaque type leakage and RPITIT normalization #139788

@lcnr

Description

@lcnr

related to rust-lang/trait-system-refactor-initiative#173, see added tests in #139789

trait Trait { // desugars to // type Assoc: Sized + Send; // fn foo(b: bool) -> Self::Assoc; fn foo(b: bool) -> impl Sized + Send; } impl Trait for u32 { // desugars to // type Assoc = impl_rpit::<Self>; // fn foo(b: bool) -> Self::Assoc { .. } fn foo(b: bool) -> impl Sized { if b { u32::foo(false) } else { 1u32 } } }

This currently results in a query cycle:

  • type_of(impl::Assoc)
  • collect_return_position_impl_trait_in_trait_tys
  • impl_rpit: Send
  • type_of(impl_rpit) // auto trait leakage
  • typeck(impl::foo)
  • normalize(<u32 as Trait>::Assoc)
  • type_of(impl::Assoc)

I believe that this query cycle should not be there and can be avoided.

collect_return_position_impl_trait_in_trait_tys currently adds the item bounds of the RPITIT when replacing it with fresh infer vars. I believe this is not necessary to guide inference as the method signature is fully concrete.

We could therefore split this in two:

  • collect_return_position_impl_trait_in_trait_tys instantiates RPITIT with infer vars but does not check the item bounds of the RPITIT trait assoc type
  • compare_type_predicate_entailment (or a separate query, idk and idc :3) then uses collect_return_position_impl_trait_in_trait_tys and actually checks the item bounds

This means normalizing impl::Assoc no longer has to prove the item bounds of the RPITIT, allowing the above example to compile and fixing rust-lang/trait-system-refactor-initiative#173

cc @compiler-errors

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-impl-traitArea: `impl Trait`. Universally / existentially quantified anonymous types with static dispatch.F-return_position_impl_trait_in_trait`#![feature(return_position_impl_trait_in_trait)]`WG-trait-system-refactorThe Rustc Trait System Refactor Initiative (-Znext-solver)

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions