Skip to content

Experiment with replacing specialization in libcore with downcasting #904

@oli-obk

Description

@oli-obk

Proposal

Now that we have an impl for rust-lang/rust#144361, we can both downcast to types (we always could do that), and downcast to dyn Trait objects. This means we can share logic between many types by having them implement the helper trait without needing that trait to be implemented for all types.

Just like downcasting to types, all of this is trivially optimizable by LLVM (it sees a None constant for types that do not implement the trait), so I do not forsee even any compilation time regressions.

If there are indeed no practical issues like runtime perf regressions or compile-time perf regressions, I would like to apply this to as many uses of specialization as possible.

One major issue with the current scheme is that it doesn't fully work in const contexts, as we cannot call methods on the downcasted traits. Many specialization schemes do not rely on this however, they just use marker traits to decide whether to use a different code path.

Mentors or Reviewers

Process

The main points of the Major Change Process are as follows:

  • File an issue describing the proposal.
  • A compiler team member who is knowledgeable in the area can second by writing @rustbot second or kickoff a team FCP with @rfcbot fcp $RESOLUTION.
  • Once an MCP is seconded, the Final Comment Period begins.
    • Final Comment Period lasts for 10 days after all outstanding concerns are solved.
    • Outstanding concerns will block the Final Comment Period from finishing. Once all concerns are resolved, the 10 day countdown is restarted.
    • If no concerns are raised after 10 days since the resolution of the last outstanding concern, the MCP is considered approved.

You can read more about Major Change Proposals on forge.

Metadata

Metadata

Assignees

No one assigned

    Labels

    T-compilerAdd this label so rfcbot knows to poll the compiler teammajor-changeA proposal to make a major change to rustc

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions