From 1657ce1b31ffefe9a75900f243f0bf0095588fac Mon Sep 17 00:00:00 2001 From: aiongg Date: Thu, 19 Nov 2026 11:00:57 +0000 Subject: [PATCH] craft-ui-macros: a type parameter only hash-escaped memo props fields name is bound by Hash + Sync A dock's chrome props hold the host's panel type through #[memo_props(hash)]; the derive bound it by MemoProps, which a plain panel id does not implement. --- crates/ui-macros/src/memo.rs | 11 +++++---- crates/ui-macros/src/plain.rs | 44 ++++++++++++++++++++++++++++++----- 2 files changed, 45 insertions(+), 10 deletions(-) diff --git a/crates/ui-macros/src/memo.rs b/crates/ui-macros/src/memo.rs index 2e7e3c859..adc430512 100644 --- a/crates/ui-macros/src/memo.rs +++ b/crates/ui-macros/src/memo.rs @@ -12,8 +12,11 @@ use quote::{format_ident, quote}; use syn::spanned::Spanned; use syn::{Data, DeriveInput, Fields}; -/// The escape for a foreign plain type: the field feeds the key through its `Hash`. -const ESCAPE: Escape = Escape { attr: "memo_props", keyword: "hash" }; +/// The escape for a foreign plain type: the field feeds the key through its `Hash`. A type parameter only such fields +/// name (a dock's panel type) is bound by what `MemoKey::feed_hash` needs. +fn escape() -> Escape { + Escape { attr: "memo_props", keyword: "hash", bound: Some(quote!(::core::hash::Hash + ::core::marker::Sync)) } +} /// The escape for a callback's type parameter (`Stateless`): bound by what `Stateless: MemoProps` needs. fn stateless() -> ParamEscape { @@ -39,7 +42,7 @@ fn feed(fields: &Fields, k: &TokenStream) -> syn::Result<(TokenStream, Vec syn::Result { let input: DeriveInput = syn::parse2(input)?; let k = ui_crate(); let trait_path = quote!(#k::contract::MemoProps); - let FieldChecks { generics, .. } = field_checks(&input, &trait_path, Some(&ESCAPE), Some(&stateless()))?; + let FieldChecks { generics, .. } = field_checks(&input, &trait_path, Some(&escape()), Some(&stateless()))?; let body = match &input.data { Data::Struct(data) => { let (pattern, statements) = feed(&data.fields, &k)?; diff --git a/crates/ui-macros/src/plain.rs b/crates/ui-macros/src/plain.rs index 1328b1415..31c4b2f6e 100644 --- a/crates/ui-macros/src/plain.rs +++ b/crates/ui-macros/src/plain.rs @@ -37,6 +37,9 @@ pub(crate) struct Escape { pub(crate) attr: &'static str, /// The one key it takes (`assume`). pub(crate) keyword: &'static str, + /// What a type parameter that only escaped fields name is bound by instead of the derive's trait (`Hash + Sync` + /// for a field that feeds a key through its `Hash`); `None` keeps the trait. + pub(crate) bound: Option, } /// The escape a derive allows for one type parameter, `#[()]`: the parameter is bound by `bound` @@ -55,8 +58,9 @@ pub(crate) struct ParamEscape { /// `check::()` spanned at the field's type, so a rejected field (`Cell` included) is an error at the derive /// that names the field, and a recursive type (a node holding `Vec` or `Vec>`) does not make its impl /// require itself. A field type written with `Self` is checked as the type itself. A field carrying `escape` is not -/// checked. A type parameter carrying `param_escape` is bound by its `bound` instead of `trait_path`; its fields are -/// still checked under that bound. +/// checked; a type parameter that only escaped fields name is bound by the escape's `bound`, when it has one. A type +/// parameter carrying `param_escape` is bound by its `bound` instead of `trait_path`; its fields are still checked under +/// that bound. pub(crate) fn field_checks( input: &DeriveInput, trait_path: &TokenStream, @@ -82,6 +86,16 @@ pub(crate) fn field_checks( } } } + // The type parameters each kind of field names: a parameter only escaped fields name is bound by the escape's + // bound, when it has one. + let (mut named, mut named_escaped) = (Named::default(), Named::default()); + for field in &fields { + let is_escaped = match escape { + Some(escape) => escaped(field, escape)?, + None => false, + }; + syn::visit::Visit::visit_type(if is_escaped { &mut named_escaped } else { &mut named }, &field.ty); + } let mut bounds = Vec::new(); for param in generics.type_params_mut() { let escaped = match param_escape { @@ -91,8 +105,10 @@ pub(crate) fn field_checks( // A derive's helper attributes are not attributes of the impl. param.attrs.clear(); let ident = ¶m.ident; - bounds.push(match param_escape { - Some(ParamEscape { bound, .. }) if escaped => quote!(#ident: #bound), + let only_escaped = named_escaped.0.contains(ident) && !named.0.contains(ident); + bounds.push(match (param_escape, escape.and_then(|escape| escape.bound.as_ref())) { + (Some(ParamEscape { bound, .. }), _) if escaped => quote!(#ident: #bound), + (_, Some(bound)) if only_escaped => quote!(#ident: #bound), _ => quote!(#ident: #trait_path), }); } @@ -128,6 +144,21 @@ pub(crate) fn field_checks( Ok(FieldChecks { generics, checks }) } +/// The single-segment paths a type names, among them the type parameters it uses. +#[derive(Default)] +struct Named(Vec); + +impl<'ast> syn::visit::Visit<'ast> for Named { + fn visit_type_path(&mut self, path: &'ast syn::TypePath) { + if path.qself.is_none() + && let Some(ident) = path.path.get_ident() + { + self.0.push(ident.clone()); + } + syn::visit::visit_type_path(self, path); + } +} + /// Replaces the type `Self` with the deriving type, which the generated free function can't name as `Self`. struct ReplaceSelf<'t>(&'t syn::Type); @@ -162,7 +193,7 @@ fn param_escaped(param: &syn::TypeParam, escape: &ParamEscape) -> syn::Result( = "reason")]`. Any other content of the attribute, or an empty /// reason, is an error. pub(crate) fn escaped(field: &syn::Field, escape: &Escape) -> syn::Result { - let Escape { attr: name, keyword } = *escape; + let Escape { attr: name, keyword, .. } = escape; let mut found = false; for attr in field.attrs.iter().filter(|attr| attr.path().is_ident(name)) { attr.parse_nested_meta(|meta| { @@ -202,7 +233,8 @@ pub(crate) fn derive(input: TokenStream) -> syn::Result { let input: DeriveInput = syn::parse2(input)?; let k = tracked_crate(); let trait_path = quote!(#k::PlainData); - let FieldChecks { generics, checks } = field_checks(&input, &trait_path, Some(&Escape { attr: "plain_data", keyword: "assume" }), None)?; + let FieldChecks { generics, checks } = + field_checks(&input, &trait_path, Some(&Escape { attr: "plain_data", keyword: "assume", bound: None }), None)?; let ident = &input.ident; let (impl_generics, _, where_clause) = generics.split_for_impl(); let (_, type_generics, _) = input.generics.split_for_impl();