Skip to content

Conversation

@hanbings-agent
Copy link
Owner

feat: Introduce experimental impl shards syntax

This PR introduces an experimental impl shards syntax, allowing a trait implementation to be split into multiple impl items, each defining a single associated item.

Why:
To enable more granular organization of trait implementations, potentially across different parts of a module. This is a foundational step for exploring "impl shards" as requested by the user.

How it works:

  1. New Syntax: impl Trait for Type fn method() { ... } is parsed under the #[feature(impl_shards)] gate.
  2. Pre-expansion Merging: A new AST pass (rustc_ast_passes::impl_shards) merges these single-item impl shards into a single, conventional impl Trait for Type { ... } block. This occurs early in the compilation pipeline, before macro expansion and name resolution.
  3. Scope: Merging is currently limited to impl shards within the same module that share identical impl headers (trait, self type, generics, where clause, modifiers).
  4. Compatibility: Existing compiler checks for duplicate associated items and incomplete trait implementations are preserved, as they operate on the merged impl block.

Current Limitations:

  • Shards generated via include!() or macros across different files/modules will not be merged, as the merging pass is module-local and pre-expansion. This means cross-module shards will still trigger existing coherence errors.

Tests:

  • tests/ui/impl-shards/split.rs: Verifies successful compilation of split impls.
  • tests/ui/impl-shards/feature-gate.rs: Ensures E0658 is emitted without the feature gate.
  • tests/ui/impl-shards/duplicate.rs: Confirms E0201 for duplicate method definitions after merging.

Open in Cursor Open in Web

Co-authored-by: agent <agent@hanbings.io>
@cursor
Copy link

cursor bot commented Dec 22, 2025

Cursor Agent can help with this pull request. Just @cursor in comments and I'll start working on changes in this branch.
Learn more about Cursor Agents

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants