From 9b066760a18de20ebfbad2edec4c48b9ea9fcb8f Mon Sep 17 00:00:00 2001 From: Soares Chen Date: Sat, 14 Nov 2026 22:18:40 +0100 Subject: [PATCH 1/4] Derive a provider's component key from its trait's path `#[cgp_impl]`, `#[cgp_provider]`, and `#[cgp_new_provider]` named the component key after the provider trait's last segment, so a header written as `impl crate::components::Greeter` still needed `GreeterComponent` imported. The key now keeps the header's path and appends `Component` to its last segment, dropping the trait's generic arguments, so such a header resolves the key without an import. The key's last segment carries the trait identifier's span instead of the macro's call site. An unresolved key is then reported on the trait's name in the header rather than on the whole attribute, and with both names in scope Rust Analyzer's hover on the trait shows the key too. Go-to-definition on the trait still gives one target, since the marker struct is spanned on the same provider identifier as the trait. Co-Authored-By: Claude Opus 5.5 --- .../src/types/cgp_provider/item.rs | 37 ++-- .../cgp-tests/tests/basic_delegation/mod.rs | 1 + .../basic_delegation/provider_trait_path.rs | 176 ++++++++++++++++++ 3 files changed, 194 insertions(+), 20 deletions(-) create mode 100644 crates/tests/cgp-tests/tests/basic_delegation/provider_trait_path.rs diff --git a/crates/macros/cgp-macro-core/src/types/cgp_provider/item.rs b/crates/macros/cgp-macro-core/src/types/cgp_provider/item.rs index e9e3998f..9eae1723 100644 --- a/crates/macros/cgp-macro-core/src/types/cgp_provider/item.rs +++ b/crates/macros/cgp-macro-core/src/types/cgp_provider/item.rs @@ -1,7 +1,6 @@ -use proc_macro2::Span; use quote::ToTokens; use syn::spanned::Spanned; -use syn::{Error, Ident, ItemImpl, Type}; +use syn::{Error, Ident, ItemImpl, Path, Type}; use crate::functions::parse_internal; use crate::types::cgp_provider::{LoweredCgpProvider, ProviderArgs}; @@ -54,24 +53,22 @@ impl ItemCgpProvider { let provider_trait: PathWithTypeArgs = parse_internal(provider_trait_path.to_token_stream())?; - // Span the synthesized `{Provider}Component` reference on `call_site`, not - // the provider trait's span. The derived component appears only as the - // first type argument of the generated `IsProviderFor<..>` impl — an - // interior token that anchors no error caret — so giving it the provider - // trait's span buys nothing at the compiler, and it actively harms the - // editor: rust-analyzer maps a source token to its expansion by source - // range, so a component reference sharing the provider trait's range makes - // go-to-definition on the provider trait offer the component struct too. - // A `call_site` span shares no narrow user token's range, so it stays out - // of the way. (This is the reference-side dual of the `#[cgp_component]` - // marker struct, whose *definition* is instead spanned on the provider - // identifier so navigation lands there cleanly.) - let component_ident = Ident::new( - &format!("{}Component", provider_trait.ident()), - Span::call_site(), - ); - - parse_internal(component_ident.to_token_stream()) + // The key keeps the trait's path, so `impl crate::components::Greeter` + // names `crate::components::GreeterComponent` and needs no import. Its last + // segment takes the trait identifier's span, so an unresolved key is reported + // on the trait's name in the header rather than on the whole attribute; with + // both names resolved, Rust Analyzer's hover there shows the key beside the + // trait. See cgp-knowledge-base/cgp/reference/macros/cgp_impl.md. + let provider_ident = provider_trait.ident(); + + let mut component_path: Path = provider_trait.path.clone(); + + if let Some(last_segment) = component_path.segments.last_mut() { + last_segment.ident = + Ident::new(&format!("{provider_ident}Component"), provider_ident.span()); + } + + parse_internal(component_path.to_token_stream()) } /// Builds the provider struct, when the attribute declares one. diff --git a/crates/tests/cgp-tests/tests/basic_delegation/mod.rs b/crates/tests/cgp-tests/tests/basic_delegation/mod.rs index 4f29d51d..eb226c78 100644 --- a/crates/tests/cgp-tests/tests/basic_delegation/mod.rs +++ b/crates/tests/cgp-tests/tests/basic_delegation/mod.rs @@ -11,6 +11,7 @@ pub mod delegate_components_macro; pub mod delegate_generic_table; pub mod provider_component_override; pub mod provider_macro; +pub mod provider_trait_path; // Behavioral wiring: a consumer trait becomes usable once its component is wired. pub mod consumer_delegate_generic; diff --git a/crates/tests/cgp-tests/tests/basic_delegation/provider_trait_path.rs b/crates/tests/cgp-tests/tests/basic_delegation/provider_trait_path.rs new file mode 100644 index 00000000..0721d16a --- /dev/null +++ b/crates/tests/cgp-tests/tests/basic_delegation/provider_trait_path.rs @@ -0,0 +1,176 @@ +//! A provider header that names its provider trait by path names the component key by the +//! same path, so the module needs no import of the key. +//! +//! `#[cgp_impl]`, `#[cgp_provider]`, and `#[cgp_new_provider]` derive the key from the +//! header's trait path, replacing its last segment with `{Trait}Component` and dropping the +//! trait's generic arguments. No module below imports `GreeterComponent` or +//! `ComputerComponent`: each header reaches the key through a path starting with `self::`, +//! `super::`, `crate::`, a crate name, or `::`. +//! +//! See cgp-knowledge-base/cgp/reference/macros/cgp_impl.md. + +use cgp::prelude::*; +use cgp_macro_test_util::snapshot_cgp_impl; + +pub mod components { + use cgp::prelude::*; + + #[cgp_component(Greeter)] + pub trait CanGreet { + fn greet(&self) -> String; + } +} + +snapshot_cgp_impl! { + #[cgp_impl(new GreetSelfPath)] + impl self::components::Greeter { + fn greet(&self) -> String { + String::from("self") + } + } + + expand_greet_self_path(output) { + insta::assert_snapshot!(output, @r#" + impl<__Context__> self::components::Greeter<__Context__> for GreetSelfPath { + fn greet(__context__: &__Context__) -> String { + String::from("self") + } + } + impl<__Context__> IsProviderFor + for GreetSelfPath {} + ///Provider `GreetSelfPath` for [`Greeter`](self::components::Greeter). + pub struct GreetSelfPath; + "#) + } +} + +pub mod super_path { + use cgp::prelude::*; + + #[cgp_impl(new GreetSuperPath)] + impl super::components::Greeter { + fn greet(&self) -> String { + String::from("super") + } + } +} + +pub mod crate_path { + use cgp::prelude::*; + + #[cgp_impl(new GreetCratePath)] + impl crate::basic_delegation::provider_trait_path::components::Greeter { + fn greet(&self) -> String { + String::from("crate") + } + } +} + +pub mod raw_provider_path { + use cgp::prelude::*; + + pub struct GreetRawPath; + + #[cgp_provider] + impl super::components::Greeter for GreetRawPath { + fn greet(_context: &Context) -> String { + String::from("raw") + } + } + + #[cgp_new_provider] + impl super::components::Greeter for GreetNewRawPath { + fn greet(_context: &Context) -> String { + String::from("new raw") + } + } +} + +// A dependency's provider trait, named by path with its generic arguments, once through +// the crate name and once from the root with a leading `::`. +snapshot_cgp_impl! { + #[cgp_impl(new DoubleByPath)] + impl cgp::extra::handler::Computer { + type Output = u64; + + fn compute(&self, _code: PhantomData, input: u64) -> u64 { + input * 2 + } + } + + expand_double_by_path(output) { + insta::assert_snapshot!(output, @" + impl<__Context__, Code> cgp::extra::handler::Computer<__Context__, Code, u64> + for DoubleByPath { + type Output = u64; + fn compute(__context__: &__Context__, _code: PhantomData, input: u64) -> u64 { + input * 2 + } + } + impl< + __Context__, + Code, + > IsProviderFor + for DoubleByPath {} + ///Provider `DoubleByPath` for [`Computer`](cgp::extra::handler::Computer). + pub struct DoubleByPath; + ") + } +} + +#[cgp_impl(new TripleByRootPath)] +impl ::cgp::extra::handler::Computer { + type Output = u64; + + fn compute(&self, _code: PhantomData, input: u64) -> u64 { + input * 3 + } +} + +macro_rules! greeting_app { + ($app:ident, $provider:ty) => { + pub struct $app; + + delegate_components! { + $app { + components::GreeterComponent: $provider, + } + } + }; +} + +greeting_app!(SelfPathApp, GreetSelfPath); +greeting_app!(SuperPathApp, super_path::GreetSuperPath); +greeting_app!(CratePathApp, crate_path::GreetCratePath); +greeting_app!(RawPathApp, raw_provider_path::GreetRawPath); +greeting_app!(NewRawPathApp, raw_provider_path::GreetNewRawPath); + +pub struct DoubleApp; + +delegate_components! { + DoubleApp { + cgp::extra::handler::ComputerComponent: DoubleByPath, + } +} + +pub struct TripleApp; + +delegate_components! { + TripleApp { + cgp::extra::handler::ComputerComponent: TripleByRootPath, + } +} + +#[test] +fn test_provider_trait_path() { + use cgp::extra::handler::CanCompute; + use components::CanGreet; + + assert_eq!(SelfPathApp.greet(), "self"); + assert_eq!(SuperPathApp.greet(), "super"); + assert_eq!(CratePathApp.greet(), "crate"); + assert_eq!(RawPathApp.greet(), "raw"); + assert_eq!(NewRawPathApp.greet(), "new raw"); + assert_eq!(DoubleApp.compute(PhantomData::<()>, 21), 42); + assert_eq!(TripleApp.compute(PhantomData::<()>, 14), 42); +} From a4efc6ab7e48ec373d07b7d928c1b60edf5860f8 Mon Sep 17 00:00:00 2001 From: Soares Chen Date: Sat, 14 Nov 2026 22:25:06 +0100 Subject: [PATCH 2/4] Test a higher-order provider named by its trait's path A higher-order provider's inner bound gains an `IsProviderFor` counterpart naming the provider's key. With the header written by path, that counterpart names the key by the same path; a snapshot pins it and a context wires the provider over an inner one. Co-Authored-By: Claude Opus 5.5 --- .../basic_delegation/provider_trait_path.rs | 41 +++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/crates/tests/cgp-tests/tests/basic_delegation/provider_trait_path.rs b/crates/tests/cgp-tests/tests/basic_delegation/provider_trait_path.rs index 0721d16a..197aafd4 100644 --- a/crates/tests/cgp-tests/tests/basic_delegation/provider_trait_path.rs +++ b/crates/tests/cgp-tests/tests/basic_delegation/provider_trait_path.rs @@ -86,6 +86,45 @@ pub mod raw_provider_path { } } +// A higher-order provider named by path: the inner provider's `IsProviderFor` counterpart names +// the key by the header's path too. +pub mod higher_order_path { + use cgp_macro_test_util::snapshot_cgp_impl; + + snapshot_cgp_impl! { + #[cgp_impl(new Loud)] + #[use_provider(Inner: super::components::Greeter)] + impl super::components::Greeter { + fn greet(&self) -> String { + Inner::greet(self).to_uppercase() + } + } + + expand_loud(output) { + insta::assert_snapshot!(output, @" + impl<__Context__, Inner> super::components::Greeter<__Context__> for Loud + where + Inner: super::components::Greeter<__Context__>, + { + fn greet(__context__: &__Context__) -> String { + Inner::greet(__context__).to_uppercase() + } + } + impl< + __Context__, + Inner, + > IsProviderFor for Loud + where + Inner: IsProviderFor + + super::components::Greeter<__Context__>, + {} + ///Provider `Loud` for [`Greeter`](super::components::Greeter). + pub struct Loud(pub ::core::marker::PhantomData); + ") + } + } +} + // A dependency's provider trait, named by path with its generic arguments, once through // the crate name and once from the root with a leading `::`. snapshot_cgp_impl! { @@ -144,6 +183,7 @@ greeting_app!(SuperPathApp, super_path::GreetSuperPath); greeting_app!(CratePathApp, crate_path::GreetCratePath); greeting_app!(RawPathApp, raw_provider_path::GreetRawPath); greeting_app!(NewRawPathApp, raw_provider_path::GreetNewRawPath); +greeting_app!(LoudApp, higher_order_path::Loud); pub struct DoubleApp; @@ -171,6 +211,7 @@ fn test_provider_trait_path() { assert_eq!(CratePathApp.greet(), "crate"); assert_eq!(RawPathApp.greet(), "raw"); assert_eq!(NewRawPathApp.greet(), "new raw"); + assert_eq!(LoudApp.greet(), "SELF"); assert_eq!(DoubleApp.compute(PhantomData::<()>, 21), 42); assert_eq!(TripleApp.compute(PhantomData::<()>, 14), 42); } From ac0c98cc502a16d0720fd0fe8c6fc5f7a097b4ce Mon Sep 17 00:00:00 2001 From: Soares Chen Date: Sat, 14 Nov 2026 23:17:52 +0100 Subject: [PATCH 3/4] Name the component key in the provider trait's doc The provider trait that `#[cgp_component]`, `#[cgp_type]`, and `#[cgp_getter]` generate gains a paragraph after the consumer trait's copied doc: Provider trait for [`CanGreet`]. A provider imports this trait's component key, [`GreeterComponent`], beside it. The hover on a provider trait in a `use` line shows only the trait, so its doc is where a reader learns which key to import beside it. The link names the key that `name:` sets, so it resolves for a renamed key. The line is added where the trait is emitted, not to the stored trait, whose attributes the generated impls copy. Co-Authored-By: Claude Opus 5.5 --- .../src/types/cgp_component/evaluated/item.rs | 32 +++++++++++++++++-- .../tests/abstract_types/cgp_type_bounded.rs | 1 + .../tests/abstract_types/cgp_type_macro.rs | 1 + .../cgp_type_self_referential.rs | 1 + .../tests/abstract_types/cgp_type_unsized.rs | 1 + .../tests/async_and_send/component_async.rs | 1 + .../basic_delegation/component_assoc_const.rs | 1 + .../tests/basic_delegation/component_macro.rs | 1 + .../tests/basic_delegation/default_methods.rs | 1 + .../doc_comment_on_cgp_component.rs | 2 ++ .../tests/dispatching/use_delegate_getter.rs | 2 ++ .../generic_components/component_lifetime.rs | 1 + .../component_type_param.rs | 1 + .../tests/getters/abstract_type_extend.rs | 1 + .../tests/getters/abstract_type_use_type.rs | 1 + .../tests/getters/assoc_type_getter.rs | 1 + .../getters/assoc_type_self_referential.rs | 1 + crates/tests/cgp-tests/tests/getters/clone.rs | 1 + crates/tests/cgp-tests/tests/getters/mref.rs | 1 + .../cgp-tests/tests/getters/mut_getter.rs | 1 + .../cgp-tests/tests/getters/mut_slice.rs | 1 + .../tests/cgp-tests/tests/getters/non_self.rs | 1 + .../tests/cgp-tests/tests/getters/option.rs | 1 + crates/tests/cgp-tests/tests/getters/slice.rs | 1 + .../tests/cgp-tests/tests/getters/string.rs | 1 + .../tests/getters/string_custom_name.rs | 1 + .../tests/getters/string_custom_spec.rs | 1 + .../tests/namespaces/default_impls.rs | 1 + .../tests/namespaces/namespace_basic.rs | 1 + .../tests/namespaces/namespace_multi.rs | 1 + .../tests/namespaces/namespace_symbol_path.rs | 1 + .../tests/namespaces/namespace_type_path.rs | 1 + .../namespaces/prefix_default_namespace.rs | 1 + .../tests/namespaces/redirect_lookup.rs | 1 + 34 files changed, 65 insertions(+), 2 deletions(-) diff --git a/crates/macros/cgp-macro-core/src/types/cgp_component/evaluated/item.rs b/crates/macros/cgp-macro-core/src/types/cgp_component/evaluated/item.rs index 4b014ddd..9565a9c9 100644 --- a/crates/macros/cgp-macro-core/src/types/cgp_component/evaluated/item.rs +++ b/crates/macros/cgp-macro-core/src/types/cgp_component/evaluated/item.rs @@ -1,4 +1,4 @@ -use syn::{Item, ItemImpl, ItemTrait}; +use syn::{Item, ItemImpl, ItemTrait, parse_quote}; use crate::types::attributes::CgpComponentAttributes; use crate::types::cgp_component::CgpComponentArgs; @@ -33,7 +33,7 @@ impl EvaluatedCgpComponent { let mut items = vec![ Item::Trait(self.consumer_trait.clone()), Item::Impl(self.consumer_impl.clone()), - Item::Trait(self.provider_trait.clone()), + Item::Trait(self.to_documented_provider_trait()), Item::Impl(self.provider_impl.clone()), Item::Struct(self.component_struct.to_item_struct()), ]; @@ -45,6 +45,34 @@ impl EvaluatedCgpComponent { Ok(items) } + /// The provider trait with a doc line naming its component key, so a reader + /// who hovers the trait in a `use` line learns which key to import beside it. + /// The line follows the consumer trait's copied doc as its own paragraph. It + /// is added here rather than to `provider_trait`, whose attributes the + /// generated impls copy. + pub fn to_documented_provider_trait(&self) -> ItemTrait { + let mut provider_trait = self.provider_trait.clone(); + + let consumer_ident = &self.consumer_trait.ident; + let component_ident = &self.args.component_name.ident; + let key_doc = format!( + "Provider trait for [`{consumer_ident}`]. A provider imports this trait's \ + component key, [`{component_ident}`], beside it." + ); + + if provider_trait + .attrs + .iter() + .any(|attr| attr.path().is_ident("doc")) + { + provider_trait.attrs.push(parse_quote!(#[doc = ""])); + } + + provider_trait.attrs.push(parse_quote!(#[doc = #key_doc])); + + provider_trait + } + /// The standard provider impls (`UseContext`, `RedirectLookup`, per-attribute /// `UseDelegate`) followed by one namespace prefix impl per `#[prefix]`. /// Emits the component's provider impls. diff --git a/crates/tests/cgp-tests/tests/abstract_types/cgp_type_bounded.rs b/crates/tests/cgp-tests/tests/abstract_types/cgp_type_bounded.rs index 007a8b61..7659e752 100644 --- a/crates/tests/cgp-tests/tests/abstract_types/cgp_type_bounded.rs +++ b/crates/tests/cgp-tests/tests/abstract_types/cgp_type_bounded.rs @@ -35,6 +35,7 @@ snapshot_cgp_type! { { type Types = <__Context__ as TypesTypeProvider<__Context__>>::Types; } + ///Provider trait for [`HasTypes`]. A provider imports this trait's component key, [`TypesTypeProviderComponent`], beside it. pub trait TypesTypeProvider< __Context__, >: IsProviderFor { diff --git a/crates/tests/cgp-tests/tests/abstract_types/cgp_type_macro.rs b/crates/tests/cgp-tests/tests/abstract_types/cgp_type_macro.rs index e34b8f5c..e7d1a487 100644 --- a/crates/tests/cgp-tests/tests/abstract_types/cgp_type_macro.rs +++ b/crates/tests/cgp-tests/tests/abstract_types/cgp_type_macro.rs @@ -31,6 +31,7 @@ snapshot_cgp_type! { { type Scalar = <__Context__ as ScalarTypeProvider<__Context__>>::Scalar; } + ///Provider trait for [`HasScalarType`]. A provider imports this trait's component key, [`ScalarTypeProviderComponent`], beside it. pub trait ScalarTypeProvider< __Context__, >: IsProviderFor { diff --git a/crates/tests/cgp-tests/tests/abstract_types/cgp_type_self_referential.rs b/crates/tests/cgp-tests/tests/abstract_types/cgp_type_self_referential.rs index 5a7da0ba..3a89a973 100644 --- a/crates/tests/cgp-tests/tests/abstract_types/cgp_type_self_referential.rs +++ b/crates/tests/cgp-tests/tests/abstract_types/cgp_type_self_referential.rs @@ -33,6 +33,7 @@ snapshot_cgp_type! { { type Scalar = <__Context__ as ScalarTypeProvider<__Context__>>::Scalar; } + ///Provider trait for [`HasScalarType`]. A provider imports this trait's component key, [`ScalarTypeProviderComponent`], beside it. pub trait ScalarTypeProvider< __Context__, >: IsProviderFor { diff --git a/crates/tests/cgp-tests/tests/abstract_types/cgp_type_unsized.rs b/crates/tests/cgp-tests/tests/abstract_types/cgp_type_unsized.rs index 9584d93d..9b276918 100644 --- a/crates/tests/cgp-tests/tests/abstract_types/cgp_type_unsized.rs +++ b/crates/tests/cgp-tests/tests/abstract_types/cgp_type_unsized.rs @@ -31,6 +31,7 @@ snapshot_cgp_type! { { type Foo = <__Context__ as ProvideFooType<__Context__, T>>::Foo; } + ///Provider trait for [`HasFooType`]. A provider imports this trait's component key, [`ProvideFooTypeComponent`], beside it. pub trait ProvideFooType< __Context__, T: ?Sized, diff --git a/crates/tests/cgp-tests/tests/async_and_send/component_async.rs b/crates/tests/cgp-tests/tests/async_and_send/component_async.rs index 47166db7..7a30c860 100644 --- a/crates/tests/cgp-tests/tests/async_and_send/component_async.rs +++ b/crates/tests/cgp-tests/tests/async_and_send/component_async.rs @@ -34,6 +34,7 @@ snapshot_cgp_component! { } } #[async_trait] + ///Provider trait for [`CanFetch`]. A provider imports this trait's component key, [`FetcherComponent`], beside it. pub trait Fetcher<__Context__>: IsProviderFor { async fn fetch(__context__: &__Context__, id: u32) -> String; } diff --git a/crates/tests/cgp-tests/tests/basic_delegation/component_assoc_const.rs b/crates/tests/cgp-tests/tests/basic_delegation/component_assoc_const.rs index 6c831eb7..6c31b931 100644 --- a/crates/tests/cgp-tests/tests/basic_delegation/component_assoc_const.rs +++ b/crates/tests/cgp-tests/tests/basic_delegation/component_assoc_const.rs @@ -34,6 +34,7 @@ snapshot_cgp_component! { __Context__::limit(self) } } + ///Provider trait for [`HasLimit`]. A provider imports this trait's component key, [`LimitProviderComponent`], beside it. pub trait LimitProvider< __Context__, >: IsProviderFor { diff --git a/crates/tests/cgp-tests/tests/basic_delegation/component_macro.rs b/crates/tests/cgp-tests/tests/basic_delegation/component_macro.rs index e5f0ea92..a441a3ac 100644 --- a/crates/tests/cgp-tests/tests/basic_delegation/component_macro.rs +++ b/crates/tests/cgp-tests/tests/basic_delegation/component_macro.rs @@ -32,6 +32,7 @@ snapshot_cgp_component! { __Context__::foo(self, value) } } + ///Provider trait for [`CanDoFoo`]. A provider imports this trait's component key, [`FooProviderComponent`], beside it. pub trait FooProvider< __Context__, >: IsProviderFor { diff --git a/crates/tests/cgp-tests/tests/basic_delegation/default_methods.rs b/crates/tests/cgp-tests/tests/basic_delegation/default_methods.rs index 4901b18a..ad3a0877 100644 --- a/crates/tests/cgp-tests/tests/basic_delegation/default_methods.rs +++ b/crates/tests/cgp-tests/tests/basic_delegation/default_methods.rs @@ -44,6 +44,7 @@ snapshot_cgp_component! { __Context__::greet(self) } } + ///Provider trait for [`CanGreet`]. A provider imports this trait's component key, [`GreeterComponent`], beside it. pub trait Greeter<__Context__>: IsProviderFor where __Context__: HasName, diff --git a/crates/tests/cgp-tests/tests/basic_delegation/doc_comment_on_cgp_component.rs b/crates/tests/cgp-tests/tests/basic_delegation/doc_comment_on_cgp_component.rs index 9c697389..f92885b7 100644 --- a/crates/tests/cgp-tests/tests/basic_delegation/doc_comment_on_cgp_component.rs +++ b/crates/tests/cgp-tests/tests/basic_delegation/doc_comment_on_cgp_component.rs @@ -39,6 +39,8 @@ snapshot_cgp_component! { } } /// The user's doc for the consumer trait. + /// + ///Provider trait for [`CanGreet`]. A provider imports this trait's component key, [`GreeterComponent`], beside it. pub trait Greeter<__Context__>: IsProviderFor { /// The user's doc for the method. fn greet(__context__: &__Context__) -> String; diff --git a/crates/tests/cgp-tests/tests/dispatching/use_delegate_getter.rs b/crates/tests/cgp-tests/tests/dispatching/use_delegate_getter.rs index 53f0a878..ef28d38c 100644 --- a/crates/tests/cgp-tests/tests/dispatching/use_delegate_getter.rs +++ b/crates/tests/cgp-tests/tests/dispatching/use_delegate_getter.rs @@ -42,6 +42,7 @@ snapshot_cgp_type! { { type Foo = <__Context__ as FooTypeProviderAt<__Context__, I, J>>::Foo; } + ///Provider trait for [`HasFooTypeAt`]. A provider imports this trait's component key, [`FooTypeProviderAtComponent`], beside it. pub trait FooTypeProviderAt< __Context__, I, @@ -213,6 +214,7 @@ snapshot_cgp_getter! { __Context__::foo_at(self, _tag) } } + ///Provider trait for [`HasFooAt`]. A provider imports this trait's component key, [`FooGetterAtComponent`], beside it. pub trait FooGetterAt< __Context__, I, diff --git a/crates/tests/cgp-tests/tests/generic_components/component_lifetime.rs b/crates/tests/cgp-tests/tests/generic_components/component_lifetime.rs index 36a4c208..e56938f0 100644 --- a/crates/tests/cgp-tests/tests/generic_components/component_lifetime.rs +++ b/crates/tests/cgp-tests/tests/generic_components/component_lifetime.rs @@ -39,6 +39,7 @@ snapshot_cgp_component! { __Context__::get_reference(self) } } + ///Provider trait for [`HasReference`]. A provider imports this trait's component key, [`ReferenceGetterComponent`], beside it. pub trait ReferenceGetter< 'a, __Context__, diff --git a/crates/tests/cgp-tests/tests/generic_components/component_type_param.rs b/crates/tests/cgp-tests/tests/generic_components/component_type_param.rs index ff4a3c0f..d3f09026 100644 --- a/crates/tests/cgp-tests/tests/generic_components/component_type_param.rs +++ b/crates/tests/cgp-tests/tests/generic_components/component_type_param.rs @@ -34,6 +34,7 @@ snapshot_cgp_component! { __Context__::calculate_area(self, shape) } } + ///Provider trait for [`CanCalculateArea`]. A provider imports this trait's component key, [`AreaCalculatorComponent`], beside it. pub trait AreaCalculator< __Context__, Shape, diff --git a/crates/tests/cgp-tests/tests/getters/abstract_type_extend.rs b/crates/tests/cgp-tests/tests/getters/abstract_type_extend.rs index db90b761..6e2be8ce 100644 --- a/crates/tests/cgp-tests/tests/getters/abstract_type_extend.rs +++ b/crates/tests/cgp-tests/tests/getters/abstract_type_extend.rs @@ -110,6 +110,7 @@ snapshot_cgp_getter! { __Context__::height(self) } } + ///Provider trait for [`HasRectangleFields`]. A provider imports this trait's component key, [`RectangleFieldsGetterComponent`], beside it. pub trait RectangleFieldsGetter< __Context__, >: IsProviderFor diff --git a/crates/tests/cgp-tests/tests/getters/abstract_type_use_type.rs b/crates/tests/cgp-tests/tests/getters/abstract_type_use_type.rs index 4bc7746e..a493212e 100644 --- a/crates/tests/cgp-tests/tests/getters/abstract_type_use_type.rs +++ b/crates/tests/cgp-tests/tests/getters/abstract_type_use_type.rs @@ -111,6 +111,7 @@ snapshot_cgp_getter! { __Context__::height(self) } } + ///Provider trait for [`HasRectangleFields`]. A provider imports this trait's component key, [`RectangleFieldsGetterComponent`], beside it. pub trait RectangleFieldsGetter< __Context__, >: IsProviderFor diff --git a/crates/tests/cgp-tests/tests/getters/assoc_type_getter.rs b/crates/tests/cgp-tests/tests/getters/assoc_type_getter.rs index 082257f6..e2ecd5da 100644 --- a/crates/tests/cgp-tests/tests/getters/assoc_type_getter.rs +++ b/crates/tests/cgp-tests/tests/getters/assoc_type_getter.rs @@ -36,6 +36,7 @@ snapshot_cgp_getter! { __Context__::scalar(self) } } + ///Provider trait for [`HasScalar`]. A provider imports this trait's component key, [`ScalarGetterComponent`], beside it. pub trait ScalarGetter< __Context__, >: IsProviderFor { diff --git a/crates/tests/cgp-tests/tests/getters/assoc_type_self_referential.rs b/crates/tests/cgp-tests/tests/getters/assoc_type_self_referential.rs index 1b6ba2ab..d6734f1e 100644 --- a/crates/tests/cgp-tests/tests/getters/assoc_type_self_referential.rs +++ b/crates/tests/cgp-tests/tests/getters/assoc_type_self_referential.rs @@ -35,6 +35,7 @@ snapshot_cgp_getter! { __Context__::name(self) } } + ///Provider trait for [`HasName`]. A provider imports this trait's component key, [`NameGetterComponent`], beside it. pub trait NameGetter<__Context__>: IsProviderFor { type Name: Display; fn name(__context__: &__Context__) -> &Self::Name; diff --git a/crates/tests/cgp-tests/tests/getters/clone.rs b/crates/tests/cgp-tests/tests/getters/clone.rs index 8c8b2b71..00e79794 100644 --- a/crates/tests/cgp-tests/tests/getters/clone.rs +++ b/crates/tests/cgp-tests/tests/getters/clone.rs @@ -36,6 +36,7 @@ snapshot_cgp_getter! { __Context__::name(self) } } + ///Provider trait for [`HasName`]. A provider imports this trait's component key, [`NameGetterComponent`], beside it. pub trait NameGetter<__Context__>: IsProviderFor where __Context__: HasNameType, diff --git a/crates/tests/cgp-tests/tests/getters/mref.rs b/crates/tests/cgp-tests/tests/getters/mref.rs index ea2a3014..5c74ee79 100644 --- a/crates/tests/cgp-tests/tests/getters/mref.rs +++ b/crates/tests/cgp-tests/tests/getters/mref.rs @@ -29,6 +29,7 @@ snapshot_cgp_getter! { __Context__::foo(self) } } + ///Provider trait for [`HasFoo`]. A provider imports this trait's component key, [`FooGetterComponent`], beside it. pub trait FooGetter<__Context__>: IsProviderFor { fn foo(__context__: &__Context__) -> MRef<'_, String>; } diff --git a/crates/tests/cgp-tests/tests/getters/mut_getter.rs b/crates/tests/cgp-tests/tests/getters/mut_getter.rs index e8ff5696..c207047b 100644 --- a/crates/tests/cgp-tests/tests/getters/mut_getter.rs +++ b/crates/tests/cgp-tests/tests/getters/mut_getter.rs @@ -31,6 +31,7 @@ snapshot_cgp_getter! { __Context__::count(self) } } + ///Provider trait for [`HasCount`]. A provider imports this trait's component key, [`CountGetterComponent`], beside it. pub trait CountGetter< __Context__, >: IsProviderFor { diff --git a/crates/tests/cgp-tests/tests/getters/mut_slice.rs b/crates/tests/cgp-tests/tests/getters/mut_slice.rs index c6094001..73920a2c 100644 --- a/crates/tests/cgp-tests/tests/getters/mut_slice.rs +++ b/crates/tests/cgp-tests/tests/getters/mut_slice.rs @@ -33,6 +33,7 @@ snapshot_cgp_getter! { __Context__::items(self) } } + ///Provider trait for [`HasItems`]. A provider imports this trait's component key, [`ItemsGetterComponent`], beside it. pub trait ItemsGetter< __Context__, >: IsProviderFor { diff --git a/crates/tests/cgp-tests/tests/getters/non_self.rs b/crates/tests/cgp-tests/tests/getters/non_self.rs index 58045d07..245e2a1f 100644 --- a/crates/tests/cgp-tests/tests/getters/non_self.rs +++ b/crates/tests/cgp-tests/tests/getters/non_self.rs @@ -42,6 +42,7 @@ snapshot_cgp_getter! { __Context__::foo_bar(foo) } } + ///Provider trait for [`HasFooBar`]. A provider imports this trait's component key, [`FooBarGetterComponent`], beside it. pub trait FooBarGetter< __Context__, >: IsProviderFor diff --git a/crates/tests/cgp-tests/tests/getters/option.rs b/crates/tests/cgp-tests/tests/getters/option.rs index ace26243..d71f8eca 100644 --- a/crates/tests/cgp-tests/tests/getters/option.rs +++ b/crates/tests/cgp-tests/tests/getters/option.rs @@ -28,6 +28,7 @@ snapshot_cgp_getter! { __Context__::foo(self) } } + ///Provider trait for [`HasFoo`]. A provider imports this trait's component key, [`FooGetterComponent`], beside it. pub trait FooGetter<__Context__>: IsProviderFor { fn foo(__context__: &__Context__) -> Option<&String>; } diff --git a/crates/tests/cgp-tests/tests/getters/slice.rs b/crates/tests/cgp-tests/tests/getters/slice.rs index 7bda4867..e7b80b02 100644 --- a/crates/tests/cgp-tests/tests/getters/slice.rs +++ b/crates/tests/cgp-tests/tests/getters/slice.rs @@ -29,6 +29,7 @@ snapshot_cgp_getter! { __Context__::foo(self) } } + ///Provider trait for [`HasFoo`]. A provider imports this trait's component key, [`FooGetterComponent`], beside it. pub trait FooGetter<__Context__>: IsProviderFor { fn foo(__context__: &__Context__) -> &[u8]; } diff --git a/crates/tests/cgp-tests/tests/getters/string.rs b/crates/tests/cgp-tests/tests/getters/string.rs index 93f9a889..bc22fdf9 100644 --- a/crates/tests/cgp-tests/tests/getters/string.rs +++ b/crates/tests/cgp-tests/tests/getters/string.rs @@ -28,6 +28,7 @@ snapshot_cgp_getter! { __Context__::foo(self) } } + ///Provider trait for [`HasFoo`]. A provider imports this trait's component key, [`FooGetterComponent`], beside it. pub trait FooGetter<__Context__>: IsProviderFor { fn foo(__context__: &__Context__) -> &str; } diff --git a/crates/tests/cgp-tests/tests/getters/string_custom_name.rs b/crates/tests/cgp-tests/tests/getters/string_custom_name.rs index 0f3e3f69..b05b8fcc 100644 --- a/crates/tests/cgp-tests/tests/getters/string_custom_name.rs +++ b/crates/tests/cgp-tests/tests/getters/string_custom_name.rs @@ -28,6 +28,7 @@ snapshot_cgp_getter! { __Context__::foo(self) } } + ///Provider trait for [`HasFoo`]. A provider imports this trait's component key, [`GetStringComponent`], beside it. pub trait GetString<__Context__>: IsProviderFor { fn foo(__context__: &__Context__) -> &str; } diff --git a/crates/tests/cgp-tests/tests/getters/string_custom_spec.rs b/crates/tests/cgp-tests/tests/getters/string_custom_spec.rs index 4f8b8ea6..87518ca3 100644 --- a/crates/tests/cgp-tests/tests/getters/string_custom_spec.rs +++ b/crates/tests/cgp-tests/tests/getters/string_custom_spec.rs @@ -31,6 +31,7 @@ snapshot_cgp_getter! { __Context__::foo(self) } } + ///Provider trait for [`HasFoo`]. A provider imports this trait's component key, [`GetStringComp`], beside it. pub trait GetString<__Context__>: IsProviderFor { fn foo(__context__: &__Context__) -> &str; } diff --git a/crates/tests/cgp-tests/tests/namespaces/default_impls.rs b/crates/tests/cgp-tests/tests/namespaces/default_impls.rs index c2fc4f5e..a2083b35 100644 --- a/crates/tests/cgp-tests/tests/namespaces/default_impls.rs +++ b/crates/tests/cgp-tests/tests/namespaces/default_impls.rs @@ -38,6 +38,7 @@ snapshot_cgp_component! { __Context__::show(self, value) } } + ///Provider trait for [`Show`]. A provider imports this trait's component key, [`ShowImplComponent`], beside it. pub trait ShowImpl<__Context__, T>: IsProviderFor { fn show(__context__: &__Context__, value: &T) -> String; } diff --git a/crates/tests/cgp-tests/tests/namespaces/namespace_basic.rs b/crates/tests/cgp-tests/tests/namespaces/namespace_basic.rs index 57e0e37c..30e812c3 100644 --- a/crates/tests/cgp-tests/tests/namespaces/namespace_basic.rs +++ b/crates/tests/cgp-tests/tests/namespaces/namespace_basic.rs @@ -67,6 +67,7 @@ snapshot_cgp_component! { __Context__::bar(self) } } + ///Provider trait for [`Bar`]. A provider imports this trait's component key, [`BarProviderComponent`], beside it. pub trait BarProvider< __Context__, >: IsProviderFor { diff --git a/crates/tests/cgp-tests/tests/namespaces/namespace_multi.rs b/crates/tests/cgp-tests/tests/namespaces/namespace_multi.rs index b4980481..45bd5583 100644 --- a/crates/tests/cgp-tests/tests/namespaces/namespace_multi.rs +++ b/crates/tests/cgp-tests/tests/namespaces/namespace_multi.rs @@ -109,6 +109,7 @@ snapshot_cgp_component! { __Context__::bar(self) } } + ///Provider trait for [`Bar`]. A provider imports this trait's component key, [`BarProviderComponent`], beside it. pub trait BarProvider< __Context__, >: IsProviderFor { diff --git a/crates/tests/cgp-tests/tests/namespaces/namespace_symbol_path.rs b/crates/tests/cgp-tests/tests/namespaces/namespace_symbol_path.rs index c3c31afd..c9ff4eb9 100644 --- a/crates/tests/cgp-tests/tests/namespaces/namespace_symbol_path.rs +++ b/crates/tests/cgp-tests/tests/namespaces/namespace_symbol_path.rs @@ -77,6 +77,7 @@ snapshot_cgp_component! { __Context__::bar(self) } } + ///Provider trait for [`Bar`]. A provider imports this trait's component key, [`BarProviderComponent`], beside it. pub trait BarProvider< __Context__, >: IsProviderFor { diff --git a/crates/tests/cgp-tests/tests/namespaces/namespace_type_path.rs b/crates/tests/cgp-tests/tests/namespaces/namespace_type_path.rs index fcb12b9e..90cf72ba 100644 --- a/crates/tests/cgp-tests/tests/namespaces/namespace_type_path.rs +++ b/crates/tests/cgp-tests/tests/namespaces/namespace_type_path.rs @@ -70,6 +70,7 @@ snapshot_cgp_component! { __Context__::bar(self) } } + ///Provider trait for [`Bar`]. A provider imports this trait's component key, [`BarProviderComponent`], beside it. pub trait BarProvider< __Context__, >: IsProviderFor { diff --git a/crates/tests/cgp-tests/tests/namespaces/prefix_default_namespace.rs b/crates/tests/cgp-tests/tests/namespaces/prefix_default_namespace.rs index 2e0a3bb0..007dc944 100644 --- a/crates/tests/cgp-tests/tests/namespaces/prefix_default_namespace.rs +++ b/crates/tests/cgp-tests/tests/namespaces/prefix_default_namespace.rs @@ -39,6 +39,7 @@ snapshot_cgp_component! { __Context__::foo(self) } } + ///Provider trait for [`Foo`]. A provider imports this trait's component key, [`FooProviderComponent`], beside it. pub trait FooProvider< __Context__, >: IsProviderFor { diff --git a/crates/tests/cgp-tests/tests/namespaces/redirect_lookup.rs b/crates/tests/cgp-tests/tests/namespaces/redirect_lookup.rs index 7e943aa1..fadf96b1 100644 --- a/crates/tests/cgp-tests/tests/namespaces/redirect_lookup.rs +++ b/crates/tests/cgp-tests/tests/namespaces/redirect_lookup.rs @@ -34,6 +34,7 @@ snapshot_cgp_component! { __Context__::foo() } } + ///Provider trait for [`CanDoFoo`]. A provider imports this trait's component key, [`FooProviderComponent`], beside it. pub trait FooProvider< __Context__, >: IsProviderFor { From 5cac7f0a6467020b70d1c463a880a3d2f389d51a Mon Sep 17 00:00:00 2001 From: Soares Chen Date: Sat, 14 Nov 2026 23:26:36 +0100 Subject: [PATCH 4/4] Separate the key doc line only from a copied doc string The provider trait's key doc line follows the consumer trait's doc after an empty line. A `#[doc(hidden)]` or other `#[doc(...)]` list carries no text, so it no longer counts as a doc to separate from, and a hidden trait's line opens its doc. A new snapshot pins that case, and the doc comment test's header now says the key line meets the user's doc on the provider trait. Co-Authored-By: Claude Opus 5.5 --- .../src/types/cgp_component/evaluated/item.rs | 5 +- .../doc_comment_on_cgp_component.rs | 6 +- .../cgp-tests/tests/basic_delegation/mod.rs | 1 + .../provider_trait_doc_hidden.rs | 89 +++++++++++++++++++ 4 files changed, 96 insertions(+), 5 deletions(-) create mode 100644 crates/tests/cgp-tests/tests/basic_delegation/provider_trait_doc_hidden.rs diff --git a/crates/macros/cgp-macro-core/src/types/cgp_component/evaluated/item.rs b/crates/macros/cgp-macro-core/src/types/cgp_component/evaluated/item.rs index 9565a9c9..cf3c77de 100644 --- a/crates/macros/cgp-macro-core/src/types/cgp_component/evaluated/item.rs +++ b/crates/macros/cgp-macro-core/src/types/cgp_component/evaluated/item.rs @@ -1,4 +1,4 @@ -use syn::{Item, ItemImpl, ItemTrait, parse_quote}; +use syn::{Item, ItemImpl, ItemTrait, Meta, parse_quote}; use crate::types::attributes::CgpComponentAttributes; use crate::types::cgp_component::CgpComponentArgs; @@ -60,10 +60,11 @@ impl EvaluatedCgpComponent { component key, [`{component_ident}`], beside it." ); + // Only a `#[doc = "…"]` string needs separating; `#[doc(hidden)]` adds no text. if provider_trait .attrs .iter() - .any(|attr| attr.path().is_ident("doc")) + .any(|attr| matches!(&attr.meta, Meta::NameValue(meta) if meta.path.is_ident("doc"))) { provider_trait.attrs.push(parse_quote!(#[doc = ""])); } diff --git a/crates/tests/cgp-tests/tests/basic_delegation/doc_comment_on_cgp_component.rs b/crates/tests/cgp-tests/tests/basic_delegation/doc_comment_on_cgp_component.rs index f92885b7..eb97bce9 100644 --- a/crates/tests/cgp-tests/tests/basic_delegation/doc_comment_on_cgp_component.rs +++ b/crates/tests/cgp-tests/tests/basic_delegation/doc_comment_on_cgp_component.rs @@ -2,9 +2,9 @@ //! //! The consumer trait's doc is copied onto every item the macro derives, so the //! provider trait and the blanket impls carry it too, and the marker struct -//! carries a generated line naming the consumer trait. The two never meet on one -//! item: the marker struct has only the generated line, and the derived items have -//! only the user's doc. +//! carries a generated line naming the consumer trait. The two meet only on the +//! provider trait, whose generated line naming its component key follows the +//! user's doc after an empty line; the impls carry only the user's doc. //! //! See cgp-knowledge-base/cgp/implementation/README.md#doc-comments-on-generated-items. diff --git a/crates/tests/cgp-tests/tests/basic_delegation/mod.rs b/crates/tests/cgp-tests/tests/basic_delegation/mod.rs index eb226c78..fc7c8b95 100644 --- a/crates/tests/cgp-tests/tests/basic_delegation/mod.rs +++ b/crates/tests/cgp-tests/tests/basic_delegation/mod.rs @@ -36,3 +36,4 @@ pub mod delegate_new_struct; // Where a user's doc comment lands: copied onto the derived items, with a generated // line on the marker struct. pub mod doc_comment_on_cgp_component; +pub mod provider_trait_doc_hidden; diff --git a/crates/tests/cgp-tests/tests/basic_delegation/provider_trait_doc_hidden.rs b/crates/tests/cgp-tests/tests/basic_delegation/provider_trait_doc_hidden.rs new file mode 100644 index 00000000..98fd7a11 --- /dev/null +++ b/crates/tests/cgp-tests/tests/basic_delegation/provider_trait_doc_hidden.rs @@ -0,0 +1,89 @@ +//! The provider trait's generated doc line on a consumer trait that carries +//! `#[doc(hidden)]` and no doc text. +//! +//! `#[doc(hidden)]` adds no text, so the generated line naming the component key +//! opens the provider trait's doc without an empty line before it. +//! +//! See cgp-knowledge-base/cgp/implementation/entrypoints/cgp_component.md. + +use cgp_macro_test_util::snapshot_cgp_component; + +snapshot_cgp_component! { + #[cgp_component(Hider)] + #[doc(hidden)] + pub trait CanHide { + fn hide(&self); + } + + expand_can_hide(output) { + insta::assert_snapshot!(output, @" + #[doc(hidden)] + pub trait CanHide { + fn hide(&self); + } + #[doc(hidden)] + impl<__Context__> CanHide for __Context__ + where + __Context__: Hider<__Context__>, + { + fn hide(&self) { + __Context__::hide(self) + } + } + #[doc(hidden)] + ///Provider trait for [`CanHide`]. A provider imports this trait's component key, [`HiderComponent`], beside it. + pub trait Hider<__Context__>: IsProviderFor { + fn hide(__context__: &__Context__); + } + #[doc(hidden)] + impl<__Provider__, __Context__> Hider<__Context__> for __Provider__ + where + __Provider__: DelegateComponent + + IsProviderFor, + <__Provider__ as DelegateComponent>::Delegate: Hider<__Context__>, + { + fn hide(__context__: &__Context__) { + <__Provider__ as DelegateComponent>::Delegate::hide(__context__) + } + } + ///Component marker for [`CanHide`]. + pub struct HiderComponent; + #[doc(hidden)] + impl<__Context__> Hider<__Context__> for UseContext + where + __Context__: CanHide, + { + fn hide(__context__: &__Context__) { + __Context__::hide(__context__) + } + } + impl<__Context__> IsProviderFor for UseContext + where + __Context__: CanHide, + {} + #[doc(hidden)] + impl<__Context__, __Components__, __Path__> Hider<__Context__> + for RedirectLookup<__Components__, __Path__> + where + __Components__: DelegateComponent<__Path__>, + <__Components__ as DelegateComponent<__Path__>>::Delegate: Hider<__Context__>, + { + fn hide(__context__: &__Context__) { + <__Components__ as DelegateComponent<__Path__>>::Delegate::hide(__context__) + } + } + impl< + __Context__, + __Components__, + __Path__, + > IsProviderFor + for RedirectLookup<__Components__, __Path__> + where + __Components__: DelegateComponent<__Path__>, + <__Components__ as DelegateComponent< + __Path__, + >>::Delegate: IsProviderFor + Hider<__Context__>, + {} + "); + } +}