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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ The minor version will be incremented upon a breaking change and the patch versi

### Features

- lang: Add `#[error]` attribute to `declare_program!` ([#3757](https://github.com/coral-xyz/anchor/pull/3757)).

### Fixes

- docker: Upgrade `node` to 20.18.0 LTS ([#3687](https://github.com/solana-foundation/anchor/pull/3687)).
Expand Down
1 change: 1 addition & 0 deletions docs/content/docs/features/declare-program.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ The following modules are generated by the `declare_program!()` macro:
| [`constants`](https://github.com/coral-xyz/anchor/blob/0e5285aecdf410fa0779b7cd09a47f235882c156/lang/attribute/program/src/declare_program/mods/constants.rs) | Program constants defined in the program |
| [`events`](https://github.com/coral-xyz/anchor/blob/0e5285aecdf410fa0779b7cd09a47f235882c156/lang/attribute/program/src/declare_program/mods/events.rs) | Program events defined in the program |
| [`types`](https://github.com/coral-xyz/anchor/blob/0e5285aecdf410fa0779b7cd09a47f235882c156/lang/attribute/program/src/declare_program/mods/types.rs) | Program types defined in the program |
| [`errors`](https://github.com/coral-xyz/anchor/blob/0e5285aecdf410fa0779b7cd09a47f235882c156/lang/attribute/program/src/declare_program/mods/errors.rs) | Program errors defined in the program |

## Examples

Expand Down
6 changes: 4 additions & 2 deletions lang/attribute/program/src/declare_program/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ use syn::parse::{Parse, ParseStream};
use common::gen_docs;
use mods::{
accounts::gen_accounts_mod, client::gen_client_mod, constants::gen_constants_mod,
cpi::gen_cpi_mod, events::gen_events_mod, internal::gen_internal_mod, program::gen_program_mod,
types::gen_types_mod, utils::gen_utils_mod,
cpi::gen_cpi_mod, errors::gen_errors_mod, events::gen_events_mod, internal::gen_internal_mod,
program::gen_program_mod, types::gen_types_mod, utils::gen_utils_mod,
};

pub struct DeclareProgram {
Expand Down Expand Up @@ -61,6 +61,7 @@ fn gen_program(idl: &Idl, name: &syn::Ident) -> proc_macro2::TokenStream {
let accounts_mod = gen_accounts_mod(idl);
let events_mod = gen_events_mod(idl);
let types_mod = gen_types_mod(idl);
let errors_mod = gen_errors_mod(idl);

// Clients
let cpi_mod = gen_cpi_mod(idl);
Expand All @@ -85,6 +86,7 @@ fn gen_program(idl: &Idl, name: &syn::Ident) -> proc_macro2::TokenStream {
#accounts_mod
#events_mod
#types_mod
#errors_mod

#cpi_mod
#client_mod
Expand Down
30 changes: 30 additions & 0 deletions lang/attribute/program/src/declare_program/mods/errors.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
use anchor_lang_idl::types::Idl;
use quote::{format_ident, quote};

pub fn gen_errors_mod(idl: &Idl) -> proc_macro2::TokenStream {
let errors = idl.errors.iter().map(|e| {
let name = format_ident!("{}", e.name);
quote! {
#name,
}
});

if errors.len() == 0 {
return quote! {
/// Program error type definitions.
pub mod errors {
}
};
}

quote! {
/// Program error type definitions.
pub mod errors {

#[anchor_lang::error_code]

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This might make the errors field of the IDL inconsistent during generation i.e. programs that use declare_program! might get external program errors in their own IDL. This is because the errors field of the IDL is populated via the same #[error_code] macro, meaning these declarations might conflict when there are multiple declarations in the same crate.

This is also why we manually declare the program ID in our declare_program! impl instead of just using declare_id! (#3555 (comment)).

Similar issue: #3438

pub enum ProgramError {
#(#errors)*
}
}
}
}
1 change: 1 addition & 0 deletions lang/attribute/program/src/declare_program/mods/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ pub mod accounts;
pub mod client;
pub mod constants;
pub mod cpi;
pub mod errors;
pub mod events;
pub mod internal;
pub mod program;
Expand Down