Skip to content

Only determine impl-trait desugaring kind once #96529

Open
@cjgillot

Description

@cjgillot

impl-trait can either be desugared as a generic parameter (universal), as an opaque type (existential), or forbidden. This desugaring is currently on the AST in two places: def_collector and ast_lowering. The two logic are not consistent with one another. When this happens, ast_lowering is right.

dyn Trait<T: Bound> sometimes desugars to dyn Trait<T = impl Bound>, and sometimes does not.

We should only determine the desugaring once. This can be achieved by:

  • saving the desugaring from def_collector into an impl_trait_context: FxHashMap<LocalDefId, ImplTraitContext> in Resolver;
  • add impl_trait_id: NodeId in rustc_ast::AssocConstraint and use it to create the impl-trait's LocalDefId;
  • introduce UniversalInDyn to handle the dyn Trait<T: Bound> case in def_collector and create the definition there, desugar_to_impl_trait should be replaced by resolver.opt_local_def_id(constraint.impl_trait_id).is_some();
  • move the &mut Vec in rustc_ast_lowering::ImplTraitContext to LoweringContext, the new field should be saved and restored by with_hir_id_owner;
  • merge rustc_ast_lowering::ImplTraitContext and rustc_resolve::ImplTraitContext, introducing a ResolverAstLowering::get_impl_trait_context(LocalDefId) -> Option<ImplTraitContext> to access the information.

Please reach out on Zulip for any question.

Metadata

Metadata

Assignees

Labels

A-HIRArea: The high-level intermediate representation (HIR)A-astArea: ASTC-cleanupCategory: PRs that clean code up or issues documenting cleanup.E-mediumCall for participation: Medium difficulty. Experience needed to fix: Intermediate.E-mentorCall for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions