From 629b704edb8eb11ac55110d715175c978d206f62 Mon Sep 17 00:00:00 2001 From: Techassi Date: Fri, 6 Sep 2024 16:31:27 +0200 Subject: [PATCH 1/3] fix(stackable-versioned): Emit right type for unchanged fields Previously, the type used in fields marked as NoChange in a particular version was incorrect. It used the type of the definition container. The NoChange action now also tracks the field type and can thus generate the correct type. --- .../src/codegen/common/item.rs | 78 ++++++++++++++----- .../src/codegen/venum/variant.rs | 2 +- .../src/codegen/vstruct/field.rs | 4 +- .../tests/good/basic.rs | 2 +- 4 files changed, 61 insertions(+), 25 deletions(-) diff --git a/crates/stackable-versioned-macros/src/codegen/common/item.rs b/crates/stackable-versioned-macros/src/codegen/common/item.rs index aa40d5abb..65c0b1f96 100644 --- a/crates/stackable-versioned-macros/src/codegen/common/item.rs +++ b/crates/stackable-versioned-macros/src/codegen/common/item.rs @@ -297,36 +297,69 @@ where ItemStatus::Addition { .. } => { chain.insert(version.inner, ItemStatus::NotPresent) } - ItemStatus::Change { from_ident, .. } => { - chain.insert(version.inner, ItemStatus::NoChange(from_ident.clone())) - } - ItemStatus::Deprecation { previous_ident, .. } => chain - .insert(version.inner, ItemStatus::NoChange(previous_ident.clone())), - ItemStatus::NoChange(ident) => { - chain.insert(version.inner, ItemStatus::NoChange(ident.clone())) - } + ItemStatus::Change { + from_ident, + from_type, + .. + } => chain.insert( + version.inner, + ItemStatus::NoChange { + ident: from_ident.clone(), + ty: from_type.clone(), + }, + ), + ItemStatus::Deprecation { previous_ident, .. } => chain.insert( + version.inner, + ItemStatus::NoChange { + ident: previous_ident.clone(), + ty: self.inner.ty(), + }, + ), + ItemStatus::NoChange { ident, ty } => chain.insert( + version.inner, + ItemStatus::NoChange { + ident: ident.clone(), + ty: ty.clone(), + }, + ), ItemStatus::NotPresent => unreachable!(), }, (Some(status), None) => { - let ident = match status { - ItemStatus::Addition { ident, .. } => ident, - ItemStatus::Change { to_ident, .. } => to_ident, - ItemStatus::Deprecation { ident, .. } => ident, - ItemStatus::NoChange(ident) => ident, + let (ident, ty) = match status { + ItemStatus::Addition { ident, ty, .. } => (ident, ty), + ItemStatus::Change { + to_ident, to_type, .. + } => (to_ident, to_type), + ItemStatus::Deprecation { ident, .. } => (ident, &self.inner.ty()), + ItemStatus::NoChange { ident, ty } => (ident, ty), ItemStatus::NotPresent => unreachable!(), }; - chain.insert(version.inner, ItemStatus::NoChange(ident.clone())) + chain.insert( + version.inner, + ItemStatus::NoChange { + ident: ident.clone(), + ty: ty.clone(), + }, + ) } (Some(status), Some(_)) => { - let ident = match status { - ItemStatus::Addition { ident, .. } => ident, - ItemStatus::Change { to_ident, .. } => to_ident, - ItemStatus::NoChange(ident) => ident, + let (ident, ty) = match status { + ItemStatus::Addition { ident, ty, .. } => (ident, ty), + ItemStatus::Change { + to_ident, to_type, .. + } => (to_ident, to_type), + ItemStatus::NoChange { ident, ty, .. } => (ident, ty), _ => unreachable!(), }; - chain.insert(version.inner, ItemStatus::NoChange(ident.clone())) + chain.insert( + version.inner, + ItemStatus::NoChange { + ident: ident.clone(), + ty: ty.clone(), + }, + ) } _ => unreachable!(), }; @@ -365,7 +398,10 @@ pub(crate) enum ItemStatus { note: Option, ident: Ident, }, - NoChange(Ident), + NoChange { + ident: Ident, + ty: Type, + }, NotPresent, } @@ -375,7 +411,7 @@ impl ItemStatus { ItemStatus::Addition { ident, .. } => Some(ident), ItemStatus::Change { to_ident, .. } => Some(to_ident), ItemStatus::Deprecation { ident, .. } => Some(ident), - ItemStatus::NoChange(ident) => Some(ident), + ItemStatus::NoChange { ident, .. } => Some(ident), ItemStatus::NotPresent => None, } } diff --git a/crates/stackable-versioned-macros/src/codegen/venum/variant.rs b/crates/stackable-versioned-macros/src/codegen/venum/variant.rs index 3224b08f6..c6ec34486 100644 --- a/crates/stackable-versioned-macros/src/codegen/venum/variant.rs +++ b/crates/stackable-versioned-macros/src/codegen/venum/variant.rs @@ -142,7 +142,7 @@ impl VersionedVariant { #ident, }) } - ItemStatus::NoChange(ident) => Some(quote! { + ItemStatus::NoChange { ident, .. } => Some(quote! { #(#original_attributes)* #ident, }), diff --git a/crates/stackable-versioned-macros/src/codegen/vstruct/field.rs b/crates/stackable-versioned-macros/src/codegen/vstruct/field.rs index fa6c48530..81a9a9844 100644 --- a/crates/stackable-versioned-macros/src/codegen/vstruct/field.rs +++ b/crates/stackable-versioned-macros/src/codegen/vstruct/field.rs @@ -157,9 +157,9 @@ impl VersionedField { }) } ItemStatus::NotPresent => None, - ItemStatus::NoChange(field_ident) => Some(quote! { + ItemStatus::NoChange { ident, ty } => Some(quote! { #(#original_attributes)* - pub #field_ident: #field_type, + pub #ident: #ty, }), } } diff --git a/crates/stackable-versioned-macros/tests/good/basic.rs b/crates/stackable-versioned-macros/tests/good/basic.rs index 9bf3d38ce..4f1faecd5 100644 --- a/crates/stackable-versioned-macros/tests/good/basic.rs +++ b/crates/stackable-versioned-macros/tests/good/basic.rs @@ -13,7 +13,7 @@ use stackable_versioned_macros::versioned; )] pub(crate) struct Foo { #[versioned( - added(since = "v1alpha1"), + // added(since = "v1alpha1"), changed(since = "v1beta1", from_name = "jjj", from_type = "u8"), changed(since = "v1", from_type = "u16"), deprecated(since = "v2", note = "not empty") From 2748457f0b20fb022f46678de87a0c3400cd4b12 Mon Sep 17 00:00:00 2001 From: Techassi Date: Fri, 6 Sep 2024 16:33:55 +0200 Subject: [PATCH 2/3] chore: Remove superfluous 'added' action --- crates/stackable-versioned-macros/tests/good/basic.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/crates/stackable-versioned-macros/tests/good/basic.rs b/crates/stackable-versioned-macros/tests/good/basic.rs index 4f1faecd5..ea86f8814 100644 --- a/crates/stackable-versioned-macros/tests/good/basic.rs +++ b/crates/stackable-versioned-macros/tests/good/basic.rs @@ -13,7 +13,6 @@ use stackable_versioned_macros::versioned; )] pub(crate) struct Foo { #[versioned( - // added(since = "v1alpha1"), changed(since = "v1beta1", from_name = "jjj", from_type = "u8"), changed(since = "v1", from_type = "u16"), deprecated(since = "v2", note = "not empty") From 5aa53f40a03cc576d88855285beeaf5148b05092 Mon Sep 17 00:00:00 2001 From: Techassi Date: Fri, 6 Sep 2024 16:35:30 +0200 Subject: [PATCH 3/3] chore: Update changelog --- crates/stackable-versioned/CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/crates/stackable-versioned/CHANGELOG.md b/crates/stackable-versioned/CHANGELOG.md index 2c8f05c38..a760544d8 100644 --- a/crates/stackable-versioned/CHANGELOG.md +++ b/crates/stackable-versioned/CHANGELOG.md @@ -22,12 +22,14 @@ All notable changes to this project will be documented in this file. - Report variant rename validation error at the correct span and trim underscores from variants not using PascalCase ([#842]). +- Emit correct struct field types for fields with no changes (NoChange) ([#860]). [#842]: https://github.com/stackabletech/operator-rs/pull/842 [#844]: https://github.com/stackabletech/operator-rs/pull/844 [#847]: https://github.com/stackabletech/operator-rs/pull/847 [#850]: https://github.com/stackabletech/operator-rs/pull/850 [#859]: https://github.com/stackabletech/operator-rs/pull/859 +[#860]: https://github.com/stackabletech/operator-rs/pull/860 ## [0.1.1] - 2024-07-10