Skip to content

Conversation

@soareschen
Copy link
Collaborator

@soareschen soareschen commented Oct 26, 2025

Summary

This PR fixes a bug in #[cgp_component] when the consumer trait contains default method implementation.

Previously, this would cause compile errors in the generated code, as the provider trait would contain the default implementation that refers to self.

Example

With the fix, we can now write trait implementations such as follows:

#[cgp_component(Greeter)]
pub trait CanGreet: HasName {
    fn greet(&self) -> String {
        format!("Hello, {}!", self.name())
    }
}

which would correctly generate the following provider trait:

pub trait Greeter<Context>: IsProviderFor<GreeterComponent, Context, ()>
where
    Context: HasName,
{
    fn greet(context: &Context) -> String {
        format!("Hello, {}!", context.name())
    }
}

Previously, the following would have been generated, causing compile error with the dangling reference to self:

pub trait Greeter<Context>: IsProviderFor<GreeterComponent, Context, ()>
where
    Context: HasName,
{
    fn greet(context: &Context) -> String {
        format!("Hello, {}!", self.name())
    }
}

@soareschen soareschen merged commit a7eeef2 into main Oct 26, 2025
5 checks passed
@soareschen soareschen deleted the fix-consumer-default-impl branch October 26, 2025 10:04
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