diff --git a/CHANGELOG.md b/CHANGELOG.md index 468c113987..1740aeb48a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -33,6 +33,7 @@ The minor version will be incremented upon a breaking change and the patch versi - cli: Require explicit `overflow-checks` flag ([#2716](https://github.com/coral-xyz/anchor/pull/2716)). - ts: Remove `anchor-deprecated-state` feature ([#2717](https://github.com/coral-xyz/anchor/pull/2717)). - lang: Remove `CLOSED_ACCOUNT_DISCRIMINATOR` ([#2726](https://github.com/coral-xyz/anchor/pull/2726)). +- lang: Make bumps of optional accounts `Option` rather than `u8` ([#2730](https://github.com/coral-xyz/anchor/pull/2730)). ## [0.29.0] - 2023-10-16 diff --git a/lang/syn/src/codegen/accounts/bumps.rs b/lang/syn/src/codegen/accounts/bumps.rs index 762907356d..bd895410b9 100644 --- a/lang/syn/src/codegen/accounts/bumps.rs +++ b/lang/syn/src/codegen/accounts/bumps.rs @@ -32,8 +32,11 @@ pub fn generate(accs: &AccountsStruct) -> proc_macro2::TokenStream { match af { AccountField::Field(f) => { let constraints = constraints::linearize(&f.constraints); - let bump_field = quote!(pub #ident: u8); - let bump_default_field = quote!(#ident: u8::MAX); + let (bump_field, bump_default_field) = if f.is_optional { + (quote!(pub #ident: Option), quote!(#ident: None)) + } else { + (quote!(pub #ident: u8), quote!(#ident: u8::MAX)) + }; for c in constraints.iter() { // Verify this in super::constraints diff --git a/lang/syn/src/codegen/accounts/constraints.rs b/lang/syn/src/codegen/accounts/constraints.rs index cfb10a5365..0418501df8 100644 --- a/lang/syn/src/codegen/accounts/constraints.rs +++ b/lang/syn/src/codegen/accounts/constraints.rs @@ -473,6 +473,11 @@ fn generate_constraint_init_group( } } }; + let bump = if f.is_optional { + quote!(Some(__bump)) + } else { + quote!(__bump) + }; ( quote! { @@ -480,7 +485,7 @@ fn generate_constraint_init_group( &[#maybe_seeds_plus_comma], __program_id, ); - __bumps.#field = __bump; + __bumps.#field = #bump; #validate_pda }, quote! { @@ -871,6 +876,11 @@ fn generate_constraint_seeds(f: &Field, c: &ConstraintSeedsGroup) -> proc_macro2 let maybe_seeds_plus_comma = (!s.is_empty()).then(|| { quote! { #s, } }); + let bump = if f.is_optional { + quote!(Some(__bump)) + } else { + quote!(__bump) + }; // Not init here, so do all the checks. let define_pda = match c.bump.as_ref() { @@ -880,7 +890,7 @@ fn generate_constraint_seeds(f: &Field, c: &ConstraintSeedsGroup) -> proc_macro2 &[#maybe_seeds_plus_comma], &#deriving_program_id, ); - __bumps.#name = __bump; + __bumps.#name = #bump; }, // Bump target given. Use it. Some(b) => quote! { diff --git a/tests/misc/programs/misc-optional/src/lib.rs b/tests/misc/programs/misc-optional/src/lib.rs index 4e447acfdc..054f44ced5 100644 --- a/tests/misc/programs/misc-optional/src/lib.rs +++ b/tests/misc/programs/misc-optional/src/lib.rs @@ -112,7 +112,7 @@ pub mod misc_optional { pub fn test_pda_init_zero_copy(ctx: Context) -> Result<()> { let mut acc = ctx.accounts.my_pda.as_ref().unwrap().load_init()?; acc.data = 9; - acc.bump = ctx.bumps.my_pda; + acc.bump = ctx.bumps.my_pda.unwrap(); Ok(()) }