Skip to content

Support where clauses on traits and generic impl blocks#24

Merged
Kab1r merged 1 commit intomainfrom
claude/where-clauses-generic-impls
Apr 17, 2026
Merged

Support where clauses on traits and generic impl blocks#24
Kab1r merged 1 commit intomainfrom
claude/where-clauses-generic-impls

Conversation

@Kab1r
Copy link
Copy Markdown
Owner

@Kab1r Kab1r commented Apr 15, 2026

Summary

  • Remove the where-clause rejection on traits in validate_trait and forward the where clause to the generated inner trait, public marker trait, and blanket impl
  • Remove the generic-impl and where-clause-on-impl rejections in expand_impl and forward the generics via split_for_impl
  • Improve rejection messages for associated types and associated constants to explain why they're rejected

Generic impls are always cold (can't appear in the hot list), but fall back to vtable dispatch correctly via the existing shim.

Changes

crates/macros/src/lib.rs

  • validate_trait: drop the where_clause rejection; leave it for emit_trait_expansion to forward
  • emit_trait_expansion: extract where_clause and emit it on the inner trait, public trait, and blanket impl
  • expand_impl: drop generic-impl and where-clause-on-impl rejections; use split_for_impl to forward impl_generics and where_clause to the __TraitImpl for T block
  • Associated types/constants error messages now explain the reason (type parameterization / dyn-compatibility)

New trybuild pass tests

  • attr_where_clause.rstrait Inspectable where Self: Debug with a default body using format! and Debug
  • attr_generic_impl.rsScaled<T: Shape> using (&self.inner as &dyn Shape).area() for devirt dispatch
  • attr_where_impl.rsimpl<T> Shape for Named<T> where T: Shape + Display

Equivalence test additions

  • attr_where_clause_dispatch — dispatch through &dyn Inspectable where Self: Debug
  • attr_generic_impl_dispatch — covers Scaled<GHot>, nested Scaled<Scaled<GHot>>, Pair<A, B> with a where clause, and + Send / + Send + Sync flavors

Test plan

  • cargo test --workspace --exclude devirt-fuzz — all unit, equivalence, UI, and UI-attr tests pass
  • cargo clippy --workspace --exclude devirt-fuzz --all-targets -- -D warnings — clean
  • 3 new trybuild pass tests (attr_where_clause, attr_generic_impl, attr_where_impl)
  • 2 new equivalence tests (attr_where_clause_dispatch, attr_generic_impl_dispatch)

Remove rejections in expand_trait and expand_impl, forwarding the
generics and where clauses to the generated inner trait, public marker
trait, blanket impl, and impl block. Generic impls are always cold
(can't be listed as hot types) but fall back to vtable dispatch
correctly via the existing shim.

Improve rejection messages for associated types and associated
constants to explain *why* they're rejected (type parameterization
requirement and dyn-compatibility respectively).

Add trybuild tests (attr_where_clause, attr_generic_impl,
attr_where_impl) and equivalence tests (attr_where_clause_dispatch,
attr_generic_impl_dispatch) covering nested generic impls and
where-clause-constrained impls through &dyn, &(dyn + Send), and
&(dyn + Send + Sync).

https://claude.ai/code/session_01BJen3TbKCFohcjNFeYwBiA
@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Apr 15, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: ASSERTIVE

Plan: Pro

Run ID: b1fb7990-7816-4ddf-9851-06e95a17e450

📥 Commits

Reviewing files that changed from the base of the PR and between d45181f and 9dee07f.

📒 Files selected for processing (6)
  • crates/core/tests/equivalence.rs
  • crates/core/tests/ui_attr.rs
  • crates/core/tests/ui_attr/attr_generic_impl.rs
  • crates/core/tests/ui_attr/attr_where_clause.rs
  • crates/core/tests/ui_attr/attr_where_impl.rs
  • crates/macros/src/lib.rs

📝 Walkthrough

Summary by CodeRabbit

  • New Features

    • The devirt macro now supports where clauses on trait definitions, enabling more flexible trait constraints.
    • The devirt macro now supports generic impl blocks with where clauses, expanding devirtualization capabilities for generic types.
  • Tests

    • Added comprehensive test coverage for where clause handling in traits and generic implementations.

Walkthrough

The PR extends the devirt macro system to support trait where-clauses and generic trait implementations, adding comprehensive test coverage via equivalence and UI tests to validate the new functionality.

Changes

Cohort / File(s) Summary
Macro Implementation
crates/macros/src/lib.rs
Updated trait and impl validation to accept where clauses on trait definitions and generic impl blocks; where-clauses are now captured and propagated to the emitted inner dispatch trait, marker trait, and blanket impl.
Equivalence Tests
crates/core/tests/equivalence.rs
Added two macro-gated test modules: attr_where_clause with Inspectable trait (bounded by where Self: Debug) and implementations for WcHot/WcCold; attr_generic_impl with Scale trait and generic impls for Scaled<T> and Pair<A, B>.
UI Test Registration
crates/core/tests/ui_attr.rs
Registered three new trybuild success test cases: attr_where_clause.rs, attr_generic_impl.rs, attr_where_impl.rs.
UI Test Cases
crates/core/tests/ui_attr/attr_where_clause.rs, crates/core/tests/ui_attr/attr_generic_impl.rs, crates/core/tests/ui_attr/attr_where_impl.rs
New test files validating where-clause support on traits and generic impl blocks; include concrete types, devirtualized trait implementations, dynamic dispatch via trait objects, and assertion-based validation of computed values.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

  • PR #16: Modifies proc-macro expansion in crates/macros/src/lib.rs to support trait/impl where-clauses and generic impls with corresponding test coverage.
  • PR #14: Updates the devirt proc-macro implementation and macro wiring; related through proc-macro modifications and test infrastructure changes.
  • PR #17: Adds auto-trait dispatch shims and signature-rewriting helpers to crates/macros/src/lib.rs alongside this PR's where-clause and generic impl support.

Poem

🐰 Hopping through traits with where-clause grace,
Generics now sprint at devirtualization's pace,
Where bounds and impls dance without a trace,
Dynamic dispatch finds its rightful place,
Tests validate each bound-checked case! ✨

🚥 Pre-merge checks | ✅ 2 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 28.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately describes the main change: adding support for where clauses on traits and generic impl blocks in the devirt macro system.
Description check ✅ Passed The description is well-related to the changeset, providing a clear summary of the changes, implementation details, and comprehensive test coverage.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch claude/where-clauses-generic-impls

Comment @coderabbitai help to get the list of available commands and usage tips.

@Kab1r Kab1r merged commit 6ef5435 into main Apr 17, 2026
7 checks passed
@Kab1r Kab1r deleted the claude/where-clauses-generic-impls branch April 17, 2026 03:23
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.

2 participants