Skip to content

Use typed derive providers in with blocks - #2053

Draft
micahscopes wants to merge 9 commits into
feat/written-typed-derivefrom
feat/scoped-typed-derive
Draft

micahscopes wants to merge 9 commits into
feat/written-typed-derivefrom
feat/scoped-typed-derive

Conversation

@micahscopes

Copy link
Copy Markdown
Collaborator
use core::derive::Derive
use core::meta::{Code, Item, Record}

trait Read<A> { const fn read() -> u256 }
struct Packet {}
struct Small {}
struct Large {}
struct Via<P> {}

impl Read<u8> for Packet { const fn read() -> u256 { 1 } }
impl Read<u16> for Packet { const fn read() -> u256 { 2 } }

impl Derive<trait Read<u8>> for Small {
    const fn derive(_ record: Record) -> Code<Item> {
        quote { const fn read() -> u256 { 10 } }
    }
}
impl Derive<trait Read<u16>> for Large {
    const fn derive(_ record: Record) -> Code<Item> {
        quote { const fn read() -> u256 { 30 } }
    }
}
impl<G: * -> Constraint, P: Derive<G>> Derive<G> for Via<P> {
    const fn derive(_ record: Record) -> Code<Item> {
        <P as Derive<G>>::derive(record)
    }
}

const fn scoped_sum() -> u256 {
    with (Via<Small>, Large) {
        <Packet as Read<u8>>::read() + <Packet as Read<u16>>::read()
    }
}

#[test]
fn inside_and_outside_the_scope() {
    assert!(scoped_sum() == 40)
    assert!(<Packet as Read<u8>>::read() + <Packet as Read<u16>>::read() == 3)
}

The existing with block can now open typed derive providers. Inside the block, Via<Small> forwards the provider for Read<u8>, and Large supplies Read<u16>. The calls return 10 and 30. The same two calls outside the block return 1 and 2 from the ordinary implementations.

The compiler compares the full trait applications before running a provider. If two providers offer the same application in a block, either order reports the overlap. A selected provider's prerequisites still have to hold. A dot call can use its argument types to finish identifying the application before generation starts.

This builds on typed written derive. The implementation call keeps its full trait application and the proof of its selected provider. If checking reaches an implementation that still needs generation, the compiler records the request and checks again once the generated member has been admitted.

The scoped form applies to a concrete authored record and a trait declared in the requesting package. It requires settled trait arguments and receiver mode. Use a written header for an implementation that supplies an associated type or constant.

The standalone driver checks covered 58 cases. The CLI also checked a projected provider argument that resolves to the same trait application as a directly written argument.

@micahscopes
micahscopes added this pull request to stack #2051 November 20, 2026 21:59
@micahscopes
micahscopes force-pushed the feat/scoped-typed-derive branch from 0d02be2 to 29698c8 Compare November 20, 2026 22:14

This branch has not been deployed

No deployments
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.

1 participant