Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lang: fix error mod exports #1426

Merged
merged 5 commits into from Feb 10, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 7 additions & 7 deletions lang/attribute/account/src/lib.rs
Expand Up @@ -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<Self, ProgramError> {
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)
}
Expand All @@ -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<W: std::io::Write>(&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(())
}
}
Expand All @@ -190,19 +190,19 @@ 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<Self, ProgramError> {
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)
}

fn try_deserialize_unchecked(buf: &mut &[u8]) -> std::result::Result<Self, ProgramError> {
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())
}
}

Expand Down
2 changes: 1 addition & 1 deletion lang/attribute/interface/src/lib.rs
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion lang/attribute/state/src/lib.rs
Expand Up @@ -45,7 +45,7 @@ pub fn state(
fn size(&self) -> std::result::Result<u64, anchor_lang::solana_program::program_error::ProgramError> {
Ok(8 + self
.try_to_vec()
.map_err(|_| anchor_lang::__private::ErrorCode::AccountDidNotSerialize)?
.map_err(|_| anchor_lang::error::ErrorCode::AccountDidNotSerialize)?
.len() as u64)
}
}
Expand Down
5 changes: 4 additions & 1 deletion 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.
///
/// - &gt;= 100 Instruction error codes
Expand All @@ -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
Expand Down
9 changes: 0 additions & 9 deletions lang/src/lib.rs
Expand Up @@ -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;
Expand Down
52 changes: 26 additions & 26 deletions lang/syn/src/codegen/accounts/constraints.rs
Expand Up @@ -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
};
Expand All @@ -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());
}
}
}
Expand Down Expand Up @@ -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());
}
}
}
Expand Down Expand Up @@ -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());
}
},
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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());
}
}
}
Expand Down Expand Up @@ -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());
}
}
}
Expand All @@ -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());
}
}
}
Expand All @@ -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! {
Expand All @@ -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());
}
}
}
Expand All @@ -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());
}
}
}
Expand Down Expand Up @@ -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());
}
}
}
Expand All @@ -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());
}
}
}
Expand All @@ -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() },
}
}
2 changes: 1 addition & 1 deletion lang/syn/src/codegen/accounts/try_accounts.rs
Expand Up @@ -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)?;
}
}
};
Expand Down
2 changes: 1 addition & 1 deletion lang/syn/src/codegen/error.rs
Expand Up @@ -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 }
Expand Down
2 changes: 1 addition & 1 deletion lang/syn/src/codegen/program/cpi.rs
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion lang/syn/src/codegen/program/dispatch.rs
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion lang/syn/src/codegen/program/entry.rs
Expand Up @@ -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"))]
Expand Down