Ver código fonte

lang: Make bumps of optional accounts `Option<u8>` rather than `u8` (#2730)

Jean Marchand (Exotic Markets) 1 ano atrás
pai
commit
c402972bf1

+ 1 - 0
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<u8>` rather than `u8` ([#2730](https://github.com/coral-xyz/anchor/pull/2730)).
 
 ## [0.29.0] - 2023-10-16
 

+ 5 - 2
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<u8>), quote!(#ident: None))
+                    } else {
+                        (quote!(pub #ident: u8), quote!(#ident: u8::MAX))
+                    };
 
                     for c in constraints.iter() {
                         // Verify this in super::constraints

+ 12 - 2
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! {

+ 1 - 1
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<TestPdaInitZeroCopy>) -> 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(())
     }