Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions crates/flux-utils/src/arrayvec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,16 @@ impl<T: TypeHash + Copy, const N: usize> TypeHash for ArrayVec<T, N> {
};
}

impl<const N: usize> TypeHash for ArrayStr<N> {
const TYPE_HASH: u64 = {
let mut h = 0xcbf2_9ce4_8422_2325u64;
h = fnv1a64_str(h, "ArrayStr");
h = hash_u64(h, N as u64);
h = hash_layout_of::<Self>(h);
h
};
}

mod serde_impl {
use core::fmt;

Expand Down
22 changes: 16 additions & 6 deletions crates/flux-versioned-types-macros/src/evolve/generate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use rustc_hash::FxHashMap;
use syn::{Attribute, Ident, Type, Visibility};

use super::parse::{AddField, EvolutionOp, EvolveInput, EvolveStruct, ModifyField, StructField};
use crate::shared::is_closure;
use crate::shared::{is_closure, without_schema_attrs};

pub(crate) struct FieldInfo {
pub attrs: Vec<Attribute>,
Expand Down Expand Up @@ -73,12 +73,13 @@ pub(crate) fn generate_struct_def(
}

pub(crate) fn generate_base_struct(input: &EvolveInput) -> (TokenStream2, Vec<FieldInfo>) {
let is_final = input.evolutions.is_empty();
let fields: Vec<_> = input
.base
.items
.iter()
.map(|f| {
let attrs = &f.attrs;
let attrs = emitted_attrs(&f.attrs, is_final);
let vis = &f.vis;
let name = &f.name;
let ty = &f.ty;
Expand All @@ -94,9 +95,17 @@ pub(crate) fn generate_base_struct(input: &EvolveInput) -> (TokenStream2, Vec<Fi
(output, field_infos)
}

/// Field attributes for an emitted version: schema-only attributes survive
/// only on the final, queryable version, while the accumulated `FieldInfo`
/// keeps them for later steps. Version-level attributes are never stripped.
fn emitted_attrs(attrs: &[Attribute], is_final: bool) -> Vec<Attribute> {
if is_final { attrs.to_vec() } else { without_schema_attrs(attrs) }
}

fn generate_evolved_struct_fields(
kept_fields: &[&FieldInfo],
ctx: &EvolutionContext,
is_final: bool,
) -> Vec<TokenStream2> {
kept_fields
.iter()
Expand All @@ -106,21 +115,21 @@ fn generate_evolved_struct_fields(

ctx.modify_map.get(&name_str).map_or_else(
|| {
let attrs = &f.attrs;
let attrs = emitted_attrs(&f.attrs, is_final);
let vis = &f.vis;
let ty = &f.ty;
quote! { #(#attrs)* #vis #name: #ty }
},
|modify| {
let attrs = &modify.attrs;
let attrs = emitted_attrs(&modify.attrs, is_final);
let vis = &f.vis;
let ty = &modify.new_ty;
quote! { #(#attrs)* #vis #name: #ty }
},
)
})
.chain(ctx.add_fields.iter().map(|f| {
let attrs = &f.attrs;
let attrs = emitted_attrs(&f.attrs, is_final);
let vis = &f.vis;
let name = &f.name;
let ty = &f.ty;
Expand Down Expand Up @@ -222,13 +231,14 @@ pub(crate) fn generate_evolution(
default_attrs: &[Attribute],
current_fields: &[FieldInfo],
prev_name: &Ident,
is_final: bool,
) -> (TokenStream2, Vec<FieldInfo>) {
let ctx = EvolutionContext::from_evolution(evolution);

let kept_fields: Vec<_> =
current_fields.iter().filter(|f| !ctx.remove_names.contains(&f.name.to_string())).collect();

let struct_fields = generate_evolved_struct_fields(&kept_fields, &ctx);
let struct_fields = generate_evolved_struct_fields(&kept_fields, &ctx, is_final);
let struct_def =
generate_struct_def(&evolution.name, default_attrs, &evolution.attrs, &struct_fields);
let into_impl = generate_into_impl(prev_name, &evolution.name, &kept_fields, &ctx);
Expand Down
21 changes: 16 additions & 5 deletions crates/flux-versioned-types-macros/src/evolve_enum/generate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,22 @@ use syn::{Attribute, Expr, Ident, Index};
use super::parse::{
EnumVariant, EvolveEnum, EvolveEnumInput, EvolveEnumOp, ModifyVariant, RemoveVariant,
};
use crate::shared::is_closure;
use crate::shared::{is_closure, without_schema_attrs};

fn generate_enum_def(
name: &Ident,
default_attrs: &[Attribute],
enum_attrs: &[Attribute],
variants: &[EnumVariant],
is_final: bool,
) -> TokenStream2 {
let emit_attrs = |attrs: &[Attribute]| {
if is_final { attrs.to_vec() } else { without_schema_attrs(attrs) }
};
let variant_tokens: Vec<_> = variants
.iter()
.map(|v| {
let vattrs = &v.attrs;
let vattrs = emit_attrs(&v.attrs);
let vname = &v.name;
let disc = v.discriminant.as_ref().map(|d| quote! { = #d });
if v.is_unit() {
Expand All @@ -27,7 +31,7 @@ fn generate_enum_def(
.fields
.iter()
.map(|f| {
let fattrs = &f.attrs;
let fattrs = emit_attrs(&f.attrs);
let ty = &f.ty;
quote! { #(#fattrs)* #ty }
})
Expand Down Expand Up @@ -214,6 +218,7 @@ pub(crate) fn generate_base_enum(input: &EvolveEnumInput) -> (TokenStream2, Vec<
&input.default_attrs,
&input.base.attrs,
&input.base.items,
input.evolutions.is_empty(),
);

(output, input.base.items.clone())
Expand All @@ -224,6 +229,7 @@ pub(crate) fn generate_evolution(
default_attrs: &[Attribute],
current_variants: &[EnumVariant],
prev_name: &Ident,
is_final: bool,
) -> (TokenStream2, Vec<EnumVariant>) {
let mut add_variants = Vec::new();
let mut remove_map = FxHashMap::default();
Expand Down Expand Up @@ -274,8 +280,13 @@ pub(crate) fn generate_evolution(
new_variants.push(av.clone());
}

let enum_def =
generate_enum_def(&evolution.name, default_attrs, &evolution.attrs, &new_variants);
let enum_def = generate_enum_def(
&evolution.name,
default_attrs,
&evolution.attrs,
&new_variants,
is_final,
);
let into_impl = generate_into_impl(
prev_name,
&evolution.name,
Expand Down
16 changes: 13 additions & 3 deletions crates/flux-versioned-types-macros/src/shared/generate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,19 @@ impl<Op> Named for EvolveBlock<Op> {
}
}

/// Schema-only attributes on fields and variants describe the final,
/// queryable shape, so they are stripped from every older version's
/// expansion: only the version carrying the schema derive may name them.
/// Version-level attributes are left alone, so an explicit whole-type proxy
/// keeps working on any version.
pub(crate) fn without_schema_attrs(attrs: &[Attribute]) -> Vec<Attribute> {
attrs.iter().filter(|attr| !attr.path().is_ident("telemetry_schema")).cloned().collect()
}

pub(crate) fn generate_evolving<B: Named, E: Named, Item>(
input: &mut EvolveInputGeneric<B, E>,
generate_base: impl FnOnce(&EvolveInputGeneric<B, E>) -> (TokenStream2, Vec<Item>),
generate_step: impl Fn(&E, &[Attribute], &[Item], &Ident) -> (TokenStream2, Vec<Item>),
generate_step: impl Fn(&E, &[Attribute], &[Item], &Ident, bool) -> (TokenStream2, Vec<Item>),
) -> TokenStream2 {
if input.evolutions.is_empty() {
input.base.attrs_mut().extend(input.final_attrs.clone());
Expand Down Expand Up @@ -84,9 +93,10 @@ pub(crate) fn generate_evolving<B: Named, E: Named, Item>(
let mut output = base_output;
let mut prev_name = input.base.name().clone();

for evolution in &input.evolutions {
for (index, evolution) in input.evolutions.iter().enumerate() {
let is_final = index + 1 == input.evolutions.len();
let (ev_output, new_items) =
generate_step(evolution, &input.default_attrs, &current, &prev_name);
generate_step(evolution, &input.default_attrs, &current, &prev_name, is_final);
output.extend(ev_output);
current = new_items;
prev_name = evolution.name().clone();
Expand Down
2 changes: 1 addition & 1 deletion crates/flux-versioned-types-macros/src/shared/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ mod generate;
mod parse;

pub(crate) use generate::{
default_enum_attrs, default_struct_attrs, generate_evolving, is_closure,
default_enum_attrs, default_struct_attrs, generate_evolving, is_closure, without_schema_attrs,
};
pub(crate) use parse::{BaseBlock, EvolveBlock, EvolveInputGeneric, ParseEvolveOp};
4 changes: 4 additions & 0 deletions crates/flux-versioned-types/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,10 @@ macro_rules! __versioned_enum_inner {
/// Like [`versioned_struct`], but the latest version also derives
/// [`TelemetrySchema`] so it is queryable. With `persist = "dir"` the type
/// also gets a [`VersionedPersistable`] home under that directory.
///
/// Schema-only field attributes (`#[telemetry_schema(..)]`) may be written
/// on any version; they describe the final shape and are stripped from older
/// versions' expansions.
#[macro_export]
macro_rules! versioned_telemetry {
($name:ident, persist = $dir:expr => $($tokens:tt)*) => {
Expand Down
Loading