From 3838bdeaf00eb1a3e0bc148cfda6a96bce687533 Mon Sep 17 00:00:00 2001 From: Daniel Marin Date: Thu, 10 Feb 2022 15:46:44 +0100 Subject: [PATCH 1/3] ts: allow nullable types for option fields (#1428) --- CHANGELOG.md | 4 ++++ ts/src/program/namespace/types.ts | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1af4996d2b..f64a2f3b54 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,10 @@ incremented for features. ## [Unreleased] +### Fixes + +* ts: Allow nullable types for `Option` mapped types ([#1428](https://github.com/project-serum/anchor/pull/1428)). + ### Breaking * lang: Enforce that the payer for an init-ed account be marked `mut` ([#1271](https://github.com/project-serum/anchor/pull/1271)). diff --git a/ts/src/program/namespace/types.ts b/ts/src/program/namespace/types.ts index 5c80411abc..be278dfde6 100644 --- a/ts/src/program/namespace/types.ts +++ b/ts/src/program/namespace/types.ts @@ -107,7 +107,7 @@ export type DecodeType = T extends keyof TypeMap : T extends { option: { defined: keyof Defined } } ? Defined[T["option"]["defined"]] | null : T extends { option: keyof TypeMap } - ? TypeMap[T["option"]] + ? TypeMap[T["option"]] | null : T extends { vec: keyof TypeMap } ? TypeMap[T["vec"]][] : T extends { array: [defined: keyof TypeMap, size: number] } From 2390a4f16791b40c63efe621ffbd558e354d5303 Mon Sep 17 00:00:00 2001 From: Paul Date: Thu, 10 Feb 2022 21:54:12 +0100 Subject: [PATCH 2/3] lang: fix error mod exports (#1426) --- lang/attribute/account/src/lib.rs | 14 ++--- lang/attribute/interface/src/lib.rs | 2 +- lang/attribute/state/src/lib.rs | 2 +- lang/src/error.rs | 5 +- lang/src/lib.rs | 9 ---- lang/syn/src/codegen/accounts/constraints.rs | 52 +++++++++---------- lang/syn/src/codegen/accounts/try_accounts.rs | 2 +- lang/syn/src/codegen/error.rs | 2 +- lang/syn/src/codegen/program/cpi.rs | 2 +- lang/syn/src/codegen/program/dispatch.rs | 2 +- lang/syn/src/codegen/program/entry.rs | 2 +- lang/syn/src/codegen/program/handlers.rs | 24 ++++----- spl/src/governance.rs | 8 +-- 13 files changed, 60 insertions(+), 66 deletions(-) diff --git a/lang/attribute/account/src/lib.rs b/lang/attribute/account/src/lib.rs index 4ea13811f5..ff863c0df7 100644 --- a/lang/attribute/account/src/lib.rs +++ b/lang/attribute/account/src/lib.rs @@ -148,11 +148,11 @@ pub fn account( impl #impl_gen anchor_lang::AccountDeserialize for #account_name #type_gen #where_clause { fn try_deserialize(buf: &mut &[u8]) -> std::result::Result { if buf.len() < #discriminator.len() { - return Err(anchor_lang::__private::ErrorCode::AccountDiscriminatorNotFound.into()); + return Err(anchor_lang::error::ErrorCode::AccountDiscriminatorNotFound.into()); } let given_disc = &buf[..8]; if &#discriminator != given_disc { - return Err(anchor_lang::__private::ErrorCode::AccountDiscriminatorMismatch.into()); + return Err(anchor_lang::error::ErrorCode::AccountDiscriminatorMismatch.into()); } Self::try_deserialize_unchecked(buf) } @@ -176,12 +176,12 @@ pub fn account( #[automatically_derived] impl #impl_gen anchor_lang::AccountSerialize for #account_name #type_gen #where_clause { fn try_serialize(&self, writer: &mut W) -> std::result::Result<(), ProgramError> { - writer.write_all(&#discriminator).map_err(|_| anchor_lang::__private::ErrorCode::AccountDidNotSerialize)?; + writer.write_all(&#discriminator).map_err(|_| anchor_lang::error::ErrorCode::AccountDidNotSerialize)?; AnchorSerialize::serialize( self, writer ) - .map_err(|_| anchor_lang::__private::ErrorCode::AccountDidNotSerialize)?; + .map_err(|_| anchor_lang::error::ErrorCode::AccountDidNotSerialize)?; Ok(()) } } @@ -190,11 +190,11 @@ pub fn account( impl #impl_gen anchor_lang::AccountDeserialize for #account_name #type_gen #where_clause { fn try_deserialize(buf: &mut &[u8]) -> std::result::Result { if buf.len() < #discriminator.len() { - return Err(anchor_lang::__private::ErrorCode::AccountDiscriminatorNotFound.into()); + return Err(anchor_lang::error::ErrorCode::AccountDiscriminatorNotFound.into()); } let given_disc = &buf[..8]; if &#discriminator != given_disc { - return Err(anchor_lang::__private::ErrorCode::AccountDiscriminatorMismatch.into()); + return Err(anchor_lang::error::ErrorCode::AccountDiscriminatorMismatch.into()); } Self::try_deserialize_unchecked(buf) } @@ -202,7 +202,7 @@ pub fn account( fn try_deserialize_unchecked(buf: &mut &[u8]) -> std::result::Result { let mut data: &[u8] = &buf[8..]; AnchorDeserialize::deserialize(&mut data) - .map_err(|_| anchor_lang::__private::ErrorCode::AccountDidNotDeserialize.into()) + .map_err(|_| anchor_lang::error::ErrorCode::AccountDidNotDeserialize.into()) } } diff --git a/lang/attribute/interface/src/lib.rs b/lang/attribute/interface/src/lib.rs index e6dd5c6187..e264b76464 100644 --- a/lang/attribute/interface/src/lib.rs +++ b/lang/attribute/interface/src/lib.rs @@ -208,7 +208,7 @@ pub fn interface( #(#args_no_tys),* }; let mut ix_data = anchor_lang::AnchorSerialize::try_to_vec(&ix) - .map_err(|_| anchor_lang::__private::ErrorCode::InstructionDidNotSerialize)?; + .map_err(|_| anchor_lang::error::ErrorCode::InstructionDidNotSerialize)?; let mut data = #sighash_tts.to_vec(); data.append(&mut ix_data); let accounts = ctx.to_account_metas(None); diff --git a/lang/attribute/state/src/lib.rs b/lang/attribute/state/src/lib.rs index f0549f1f58..671c36908b 100644 --- a/lang/attribute/state/src/lib.rs +++ b/lang/attribute/state/src/lib.rs @@ -45,7 +45,7 @@ pub fn state( fn size(&self) -> std::result::Result { Ok(8 + self .try_to_vec() - .map_err(|_| anchor_lang::__private::ErrorCode::AccountDidNotSerialize)? + .map_err(|_| anchor_lang::error::ErrorCode::AccountDidNotSerialize)? .len() as u64) } } diff --git a/lang/src/error.rs b/lang/src/error.rs index 5adca306ac..5e9a276cf0 100644 --- a/lang/src/error.rs +++ b/lang/src/error.rs @@ -1,5 +1,8 @@ use crate::error; +/// The starting point for user defined error codes. +pub const ERROR_CODE_OFFSET: u32 = 6000; + /// Error codes that can be returned by internal framework code. /// /// - >= 100 Instruction error codes @@ -10,7 +13,7 @@ use crate::error; /// - = 5000 deprecated error code /// /// The starting point for user-defined errors is defined -/// by the [ERROR_CODE_OFFSET](crate::__private::ERROR_CODE_OFFSET). +/// by the [ERROR_CODE_OFFSET](crate::error::ERROR_CODE_OFFSET). #[error(offset = 0)] pub enum ErrorCode { // Instructions diff --git a/lang/src/lib.rs b/lang/src/lib.rs index 9746c96fb3..e79f148b35 100644 --- a/lang/src/lib.rs +++ b/lang/src/lib.rs @@ -271,20 +271,11 @@ pub mod prelude { /// Internal module used by macros and unstable apis. #[doc(hidden)] pub mod __private { - // Modules with useful information for users - // don't use #[doc(hidden)] on these - pub use crate::error::ErrorCode; - /// The discriminator anchor uses to mark an account as closed. pub const CLOSED_ACCOUNT_DISCRIMINATOR: [u8; 8] = [255, 255, 255, 255, 255, 255, 255, 255]; - /// The starting point for user defined error codes. - pub const ERROR_CODE_OFFSET: u32 = 6000; - pub use crate::ctor::Ctor; - pub use crate::error::Error; - pub use anchor_attribute_account::ZeroCopyAccessor; pub use anchor_attribute_event::EventIndex; diff --git a/lang/syn/src/codegen/accounts/constraints.rs b/lang/syn/src/codegen/accounts/constraints.rs index 1754b88ee2..d5a6cdaefc 100644 --- a/lang/syn/src/codegen/accounts/constraints.rs +++ b/lang/syn/src/codegen/accounts/constraints.rs @@ -157,7 +157,7 @@ pub fn generate_constraint_zeroed(f: &Field, _c: &ConstraintZeroed) -> proc_macr __disc_bytes.copy_from_slice(&__data[..8]); let __discriminator = u64::from_le_bytes(__disc_bytes); if __discriminator != 0 { - return Err(anchor_lang::__private::ErrorCode::ConstraintZero.into()); + return Err(anchor_lang::error::ErrorCode::ConstraintZero.into()); } #from_account_info }; @@ -169,7 +169,7 @@ pub fn generate_constraint_close(f: &Field, c: &ConstraintClose) -> proc_macro2: let target = &c.sol_dest; quote! { if #field.key() == #target.key() { - return Err(anchor_lang::__private::ErrorCode::ConstraintClose.into()); + return Err(anchor_lang::error::ErrorCode::ConstraintClose.into()); } } } @@ -232,7 +232,7 @@ pub fn generate_constraint_literal(c: &ConstraintLiteral) -> proc_macro2::TokenS }; quote! { if !(#lit) { - return Err(anchor_lang::__private::ErrorCode::Deprecated.into()); + return Err(anchor_lang::error::ErrorCode::Deprecated.into()); } } } @@ -270,7 +270,7 @@ pub fn generate_constraint_rent_exempt( ConstraintRentExempt::Skip => quote! {}, ConstraintRentExempt::Enforce => quote! { if !__anchor_rent.is_exempt(#info.lamports(), #info.try_data_len()?) { - return Err(anchor_lang::__private::ErrorCode::ConstraintRentExempt.into()); + return Err(anchor_lang::error::ErrorCode::ConstraintRentExempt.into()); } }, } @@ -367,10 +367,10 @@ fn generate_constraint_init_group(f: &Field, c: &ConstraintInitGroup) -> proc_ma let pa: #ty_decl = #from_account_info; if #if_needed { if pa.mint != #mint.key() { - return Err(anchor_lang::__private::ErrorCode::ConstraintTokenMint.into()); + return Err(anchor_lang::error::ErrorCode::ConstraintTokenMint.into()); } if pa.owner != #owner.key() { - return Err(anchor_lang::__private::ErrorCode::ConstraintTokenOwner.into()); + return Err(anchor_lang::error::ErrorCode::ConstraintTokenOwner.into()); } } pa @@ -402,14 +402,14 @@ fn generate_constraint_init_group(f: &Field, c: &ConstraintInitGroup) -> proc_ma let pa: #ty_decl = #from_account_info; if #if_needed { if pa.mint != #mint.key() { - return Err(anchor_lang::__private::ErrorCode::ConstraintTokenMint.into()); + return Err(anchor_lang::error::ErrorCode::ConstraintTokenMint.into()); } if pa.owner != #owner.key() { - return Err(anchor_lang::__private::ErrorCode::ConstraintTokenOwner.into()); + return Err(anchor_lang::error::ErrorCode::ConstraintTokenOwner.into()); } if pa.key() != anchor_spl::associated_token::get_associated_token_address(&#owner.key(), &#mint.key()) { - return Err(anchor_lang::__private::ErrorCode::AccountNotAssociatedTokenAccount.into()); + return Err(anchor_lang::error::ErrorCode::AccountNotAssociatedTokenAccount.into()); } } pa @@ -455,16 +455,16 @@ fn generate_constraint_init_group(f: &Field, c: &ConstraintInitGroup) -> proc_ma let pa: #ty_decl = #from_account_info; if #if_needed { if pa.mint_authority != anchor_lang::solana_program::program_option::COption::Some(#owner.key()) { - return Err(anchor_lang::__private::ErrorCode::ConstraintMintMintAuthority.into()); + return Err(anchor_lang::error::ErrorCode::ConstraintMintMintAuthority.into()); } if pa.freeze_authority .as_ref() .map(|fa| #freeze_authority.as_ref().map(|expected_fa| fa != *expected_fa).unwrap_or(true)) .unwrap_or(#freeze_authority.is_some()) { - return Err(anchor_lang::__private::ErrorCode::ConstraintMintFreezeAuthority.into()); + return Err(anchor_lang::error::ErrorCode::ConstraintMintFreezeAuthority.into()); } if pa.decimals != #decimals { - return Err(anchor_lang::__private::ErrorCode::ConstraintMintDecimals.into()); + return Err(anchor_lang::error::ErrorCode::ConstraintMintDecimals.into()); } } pa @@ -540,17 +540,17 @@ fn generate_constraint_init_group(f: &Field, c: &ConstraintInitGroup) -> proc_ma // Assert the account was created correctly. if #if_needed { if space != actual_field.data_len() { - return Err(anchor_lang::__private::ErrorCode::ConstraintSpace.into()); + return Err(anchor_lang::error::ErrorCode::ConstraintSpace.into()); } if actual_owner != #owner { - return Err(anchor_lang::__private::ErrorCode::ConstraintOwner.into()); + return Err(anchor_lang::error::ErrorCode::ConstraintOwner.into()); } { let required_lamports = __anchor_rent.minimum_balance(space); if pa.to_account_info().lamports() < required_lamports { - return Err(anchor_lang::__private::ErrorCode::ConstraintRentExempt.into()); + return Err(anchor_lang::error::ErrorCode::ConstraintRentExempt.into()); } } } @@ -592,10 +592,10 @@ fn generate_constraint_seeds(f: &Field, c: &ConstraintSeedsGroup) -> proc_macro2 let b = c.bump.as_ref().unwrap(); quote! { if #name.key() != __pda_address { - return Err(anchor_lang::__private::ErrorCode::ConstraintSeeds.into()); + return Err(anchor_lang::error::ErrorCode::ConstraintSeeds.into()); } if __bump != #b { - return Err(anchor_lang::__private::ErrorCode::ConstraintSeeds.into()); + return Err(anchor_lang::error::ErrorCode::ConstraintSeeds.into()); } } } @@ -607,7 +607,7 @@ fn generate_constraint_seeds(f: &Field, c: &ConstraintSeedsGroup) -> proc_macro2 else if c.is_init { quote! { if #name.key() != __pda_address { - return Err(anchor_lang::__private::ErrorCode::ConstraintSeeds.into()); + return Err(anchor_lang::error::ErrorCode::ConstraintSeeds.into()); } } } @@ -630,7 +630,7 @@ fn generate_constraint_seeds(f: &Field, c: &ConstraintSeedsGroup) -> proc_macro2 let __pda_address = Pubkey::create_program_address( &[#maybe_seeds_plus_comma &[#b][..]], &#deriving_program_id, - ).map_err(|_| anchor_lang::__private::ErrorCode::ConstraintSeeds)?; + ).map_err(|_| anchor_lang::error::ErrorCode::ConstraintSeeds)?; }, }; quote! { @@ -639,7 +639,7 @@ fn generate_constraint_seeds(f: &Field, c: &ConstraintSeedsGroup) -> proc_macro2 // Check it. if #name.key() != __pda_address { - return Err(anchor_lang::__private::ErrorCode::ConstraintSeeds.into()); + return Err(anchor_lang::error::ErrorCode::ConstraintSeeds.into()); } } } @@ -654,11 +654,11 @@ fn generate_constraint_associated_token( let spl_token_mint_address = &c.mint; quote! { if #name.owner != #wallet_address.key() { - return Err(anchor_lang::__private::ErrorCode::ConstraintTokenOwner.into()); + return Err(anchor_lang::error::ErrorCode::ConstraintTokenOwner.into()); } let __associated_token_address = anchor_spl::associated_token::get_associated_token_address(&#wallet_address.key(), &#spl_token_mint_address.key()); if #name.key() != __associated_token_address { - return Err(anchor_lang::__private::ErrorCode::ConstraintAssociated.into()); + return Err(anchor_lang::error::ErrorCode::ConstraintAssociated.into()); } } } @@ -753,7 +753,7 @@ pub fn generate_constraint_executable( let name = &f.ident; quote! { if !#name.to_account_info().executable { - return Err(anchor_lang::__private::ErrorCode::ConstraintExecutable.into()); + return Err(anchor_lang::error::ErrorCode::ConstraintExecutable.into()); } } } @@ -769,10 +769,10 @@ pub fn generate_constraint_state(f: &Field, c: &ConstraintState) -> proc_macro2: // Checks the given state account is the canonical state account for // the target program. if #ident.key() != anchor_lang::accounts::cpi_state::CpiState::<#account_ty>::address(&#program_target.key()) { - return Err(anchor_lang::__private::ErrorCode::ConstraintState.into()); + return Err(anchor_lang::error::ErrorCode::ConstraintState.into()); } if #ident.as_ref().owner != &#program_target.key() { - return Err(anchor_lang::__private::ErrorCode::ConstraintState.into()); + return Err(anchor_lang::error::ErrorCode::ConstraintState.into()); } } } @@ -783,6 +783,6 @@ fn generate_custom_error( ) -> proc_macro2::TokenStream { match custom_error { Some(error) => quote! { #error.into() }, - None => quote! { anchor_lang::__private::ErrorCode::#error.into() }, + None => quote! { anchor_lang::error::ErrorCode::#error.into() }, } } diff --git a/lang/syn/src/codegen/accounts/try_accounts.rs b/lang/syn/src/codegen/accounts/try_accounts.rs index 47d582e4be..c02051138c 100644 --- a/lang/syn/src/codegen/accounts/try_accounts.rs +++ b/lang/syn/src/codegen/accounts/try_accounts.rs @@ -79,7 +79,7 @@ pub fn generate(accs: &AccountsStruct) -> proc_macro2::TokenStream { let __Args { #(#field_names),* } = __Args::deserialize(&mut ix_data) - .map_err(|_| anchor_lang::__private::ErrorCode::InstructionDidNotDeserialize)?; + .map_err(|_| anchor_lang::error::ErrorCode::InstructionDidNotDeserialize)?; } } }; diff --git a/lang/syn/src/codegen/error.rs b/lang/syn/src/codegen/error.rs index abc57885bc..a1857df557 100644 --- a/lang/syn/src/codegen/error.rs +++ b/lang/syn/src/codegen/error.rs @@ -33,7 +33,7 @@ pub fn generate(error: Error) -> proc_macro2::TokenStream { .collect(); let offset = match error.args { - None => quote! { anchor_lang::__private::ERROR_CODE_OFFSET}, + None => quote! { anchor_lang::error::ERROR_CODE_OFFSET}, Some(args) => { let offset = &args.offset; quote! { #offset } diff --git a/lang/syn/src/codegen/program/cpi.rs b/lang/syn/src/codegen/program/cpi.rs index df96d281d5..c363915340 100644 --- a/lang/syn/src/codegen/program/cpi.rs +++ b/lang/syn/src/codegen/program/cpi.rs @@ -78,7 +78,7 @@ pub fn generate(program: &Program) -> proc_macro2::TokenStream { let ix = { let ix = instruction::#ix_variant; let mut ix_data = AnchorSerialize::try_to_vec(&ix) - .map_err(|_| anchor_lang::__private::ErrorCode::InstructionDidNotSerialize)?; + .map_err(|_| anchor_lang::error::ErrorCode::InstructionDidNotSerialize)?; let mut data = #sighash_tts.to_vec(); data.append(&mut ix_data); let accounts = ctx.to_account_metas(None); diff --git a/lang/syn/src/codegen/program/dispatch.rs b/lang/syn/src/codegen/program/dispatch.rs index 53102616f8..93f2d625da 100644 --- a/lang/syn/src/codegen/program/dispatch.rs +++ b/lang/syn/src/codegen/program/dispatch.rs @@ -114,7 +114,7 @@ pub fn generate(program: &Program) -> proc_macro2::TokenStream { }) .collect(); let fallback_fn = gen_fallback(program).unwrap_or(quote! { - Err(anchor_lang::__private::ErrorCode::InstructionFallbackNotFound.into()) + Err(anchor_lang::error::ErrorCode::InstructionFallbackNotFound.into()) }); quote! { /// Performs method dispatch. diff --git a/lang/syn/src/codegen/program/entry.rs b/lang/syn/src/codegen/program/entry.rs index b5f8d979e9..8f9bcb7bd9 100644 --- a/lang/syn/src/codegen/program/entry.rs +++ b/lang/syn/src/codegen/program/entry.rs @@ -6,7 +6,7 @@ use quote::quote; pub fn generate(program: &Program) -> proc_macro2::TokenStream { let name: proc_macro2::TokenStream = program.name.to_string().to_camel_case().parse().unwrap(); let fallback_maybe = dispatch::gen_fallback(program).unwrap_or(quote! { - Err(anchor_lang::__private::ErrorCode::InstructionMissing.into()); + Err(anchor_lang::error::ErrorCode::InstructionMissing.into()); }); quote! { #[cfg(not(feature = "no-entrypoint"))] diff --git a/lang/syn/src/codegen/program/handlers.rs b/lang/syn/src/codegen/program/handlers.rs index 7a23fcf8be..76158dfa6f 100644 --- a/lang/syn/src/codegen/program/handlers.rs +++ b/lang/syn/src/codegen/program/handlers.rs @@ -21,7 +21,7 @@ pub fn generate(program: &Program) -> proc_macro2::TokenStream { let mut data: &[u8] = idl_ix_data; let ix = anchor_lang::idl::IdlInstruction::deserialize(&mut data) - .map_err(|_| anchor_lang::__private::ErrorCode::InstructionDidNotDeserialize)?; + .map_err(|_| anchor_lang::error::ErrorCode::InstructionDidNotDeserialize)?; match ix { anchor_lang::idl::IdlInstruction::Create { data_len } => { @@ -66,7 +66,7 @@ pub fn generate(program: &Program) -> proc_macro2::TokenStream { #[inline(never)] #[cfg(feature = "no-idl")] pub fn __idl_dispatch(program_id: &Pubkey, accounts: &[AccountInfo], idl_ix_data: &[u8]) -> ProgramResult { - Err(anchor_lang::__private::ErrorCode::IdlInstructionStub.into()) + Err(anchor_lang::error::ErrorCode::IdlInstructionStub.into()) } // One time IDL account initializer. Will faill on subsequent @@ -81,7 +81,7 @@ pub fn generate(program: &Program) -> proc_macro2::TokenStream { anchor_lang::prelude::msg!("Instruction: IdlCreateAccount"); if program_id != accounts.program.key { - return Err(anchor_lang::__private::ErrorCode::IdlInstructionInvalidProgram.into()); + return Err(anchor_lang::error::ErrorCode::IdlInstructionInvalidProgram.into()); } // Create the IDL's account. let from = accounts.from.key; @@ -212,7 +212,7 @@ pub fn generate(program: &Program) -> proc_macro2::TokenStream { // Deserialize instruction data. let ix = instruction::state::#ix_name::deserialize(&mut &ix_data[..]) - .map_err(|_| anchor_lang::__private::ErrorCode::InstructionDidNotDeserialize)?; + .map_err(|_| anchor_lang::error::ErrorCode::InstructionDidNotDeserialize)?; let instruction::state::#variant_arm = ix; let mut __bumps = std::collections::BTreeMap::new(); @@ -291,7 +291,7 @@ pub fn generate(program: &Program) -> proc_macro2::TokenStream { // Deserialize instruction data. let ix = instruction::state::#ix_name::deserialize(&mut &ix_data[..]) - .map_err(|_| anchor_lang::__private::ErrorCode::InstructionDidNotDeserialize)?; + .map_err(|_| anchor_lang::error::ErrorCode::InstructionDidNotDeserialize)?; let instruction::state::#variant_arm = ix; let mut __bumps = std::collections::BTreeMap::new(); @@ -399,7 +399,7 @@ pub fn generate(program: &Program) -> proc_macro2::TokenStream { // Deserialize instruction. let ix = instruction::state::#ix_name::deserialize(&mut &ix_data[..]) - .map_err(|_| anchor_lang::__private::ErrorCode::InstructionDidNotDeserialize)?; + .map_err(|_| anchor_lang::error::ErrorCode::InstructionDidNotDeserialize)?; let instruction::state::#variant_arm = ix; // Bump collector. @@ -408,7 +408,7 @@ pub fn generate(program: &Program) -> proc_macro2::TokenStream { // Load state. let mut remaining_accounts: &[AccountInfo] = accounts; if remaining_accounts.is_empty() { - return Err(anchor_lang::__private::ErrorCode::AccountNotEnoughKeys.into()); + return Err(anchor_lang::error::ErrorCode::AccountNotEnoughKeys.into()); } let loader: anchor_lang::accounts::loader::Loader<#mod_name::#name> = anchor_lang::accounts::loader::Loader::try_accounts(program_id, &mut remaining_accounts, &[], &mut __bumps)?; @@ -455,7 +455,7 @@ pub fn generate(program: &Program) -> proc_macro2::TokenStream { // Deserialize instruction. let ix = instruction::state::#ix_name::deserialize(&mut &ix_data[..]) - .map_err(|_| anchor_lang::__private::ErrorCode::InstructionDidNotDeserialize)?; + .map_err(|_| anchor_lang::error::ErrorCode::InstructionDidNotDeserialize)?; let instruction::state::#variant_arm = ix; // Bump collector. @@ -464,7 +464,7 @@ pub fn generate(program: &Program) -> proc_macro2::TokenStream { // Load state. let mut remaining_accounts: &[AccountInfo] = accounts; if remaining_accounts.is_empty() { - return Err(anchor_lang::__private::ErrorCode::AccountNotEnoughKeys.into()); + return Err(anchor_lang::error::ErrorCode::AccountNotEnoughKeys.into()); } let mut state: anchor_lang::accounts::state::ProgramState<#state_ty> = anchor_lang::accounts::state::ProgramState::try_accounts( program_id, @@ -566,7 +566,7 @@ pub fn generate(program: &Program) -> proc_macro2::TokenStream { let deserialize_instruction = quote! { #args_struct let ix = Args::deserialize(&mut &ix_data[..]) - .map_err(|_| anchor_lang::__private::ErrorCode::InstructionDidNotDeserialize)?; + .map_err(|_| anchor_lang::error::ErrorCode::InstructionDidNotDeserialize)?; let Args { #(#ix_arg_names),* } = ix; @@ -592,7 +592,7 @@ pub fn generate(program: &Program) -> proc_macro2::TokenStream { // Deserialize the program state account. let mut remaining_accounts: &[AccountInfo] = accounts; if remaining_accounts.is_empty() { - return Err(anchor_lang::__private::ErrorCode::AccountNotEnoughKeys.into()); + return Err(anchor_lang::error::ErrorCode::AccountNotEnoughKeys.into()); } let mut state: anchor_lang::accounts::state::ProgramState<#state_ty> = anchor_lang::accounts::state::ProgramState::try_accounts( program_id, @@ -705,7 +705,7 @@ pub fn generate(program: &Program) -> proc_macro2::TokenStream { // Deserialize data. let ix = instruction::#ix_name::deserialize(&mut &ix_data[..]) - .map_err(|_| anchor_lang::__private::ErrorCode::InstructionDidNotDeserialize)?; + .map_err(|_| anchor_lang::error::ErrorCode::InstructionDidNotDeserialize)?; let instruction::#variant_arm = ix; // Bump collector. diff --git a/spl/src/governance.rs b/spl/src/governance.rs index ef63e61f9b..d10ed15e71 100644 --- a/spl/src/governance.rs +++ b/spl/src/governance.rs @@ -11,9 +11,9 @@ macro_rules! vote_weight_record { let mut data = buf; let vwr: spl_governance_addin_api::voter_weight::VoterWeightRecord = anchor_lang::AnchorDeserialize::deserialize(&mut data) - .map_err(|_| anchor_lang::__private::ErrorCode::AccountDidNotDeserialize)?; + .map_err(|_| anchor_lang::error::ErrorCode::AccountDidNotDeserialize)?; if !solana_program::program_pack::IsInitialized::is_initialized(&vwr) { - return Err(anchor_lang::__private::ErrorCode::AccountDidNotSerialize.into()); + return Err(anchor_lang::error::ErrorCode::AccountDidNotSerialize.into()); } Ok(VoterWeightRecord(vwr)) } @@ -24,7 +24,7 @@ macro_rules! vote_weight_record { let mut data = buf; let vwr: spl_governance_addin_api::voter_weight::VoterWeightRecord = anchor_lang::AnchorDeserialize::deserialize(&mut data) - .map_err(|_| anchor_lang::__private::ErrorCode::AccountDidNotDeserialize)?; + .map_err(|_| anchor_lang::error::ErrorCode::AccountDidNotDeserialize)?; Ok(VoterWeightRecord(vwr)) } } @@ -35,7 +35,7 @@ macro_rules! vote_weight_record { writer: &mut W, ) -> std::result::Result<(), ProgramError> { anchor_lang::AnchorSerialize::serialize(&self.0, writer) - .map_err(|_| anchor_lang::__private::ErrorCode::AccountDidNotSerialize)?; + .map_err(|_| anchor_lang::error::ErrorCode::AccountDidNotSerialize)?; Ok(()) } } From d5e7e2a7c32770c1bc50ff0957105318047a0f31 Mon Sep 17 00:00:00 2001 From: Paul Date: Fri, 11 Feb 2022 11:35:09 +0100 Subject: [PATCH 3/3] docs: update changelog for #1426 (#1432) --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index f64a2f3b54..023b1a7524 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,6 +18,7 @@ incremented for features. ### Breaking * lang: Enforce that the payer for an init-ed account be marked `mut` ([#1271](https://github.com/project-serum/anchor/pull/1271)). +* lang: All error-related code is now in the error module ([#1426](https://github.com/project-serum/anchor/pull/1426)). ## [0.21.0] - 2022-02-07