Skip to content

Commit

Permalink
lang: Fix instructions with no accounts causing compilation errors wh…
Browse files Browse the repository at this point in the history
…en using `declare_program!` (#3567)
  • Loading branch information
acheroncrypto authored Feb 20, 2025
1 parent 0d18bb7 commit b26f7eb
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ The minor version will be incremented upon a breaking change and the patch versi
- idl: Fix using constant identifiers as generic arguments ([#3522](https://github.com/coral-xyz/anchor/pull/3522)).
- client: Remove `std::process::exit` usage ([#3544](https://github.com/coral-xyz/anchor/pull/3544)).
- idl: Fix using `Pubkey` constants with `seeds::program` ([#3559](https://github.com/coral-xyz/anchor/pull/3559)).
- lang: Fix instructions with no accounts causing compilation errors when using `declare_program!` ([#3567](https://github.com/coral-xyz/anchor/pull/3567)).

### Breaking

Expand Down
8 changes: 7 additions & 1 deletion lang/attribute/program/src/declare_program/mods/cpi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ fn gen_cpi_instructions(idl: &Idl) -> proc_macro2::TokenStream {
let method_name = format_ident!("{}", ix.name);
let accounts_ident = format_ident!("{}", ix.name.to_camel_case());

let accounts_generic = if ix.accounts.is_empty() {
quote!()
} else {
quote!(<'info>)
};

let args = ix.args.iter().map(|arg| {
let name = format_ident!("{}", arg.name);
let ty = convert_idl_type_to_syn_type(&arg.ty);
Expand Down Expand Up @@ -61,7 +67,7 @@ fn gen_cpi_instructions(idl: &Idl) -> proc_macro2::TokenStream {

quote! {
pub fn #method_name<'a, 'b, 'c, 'info>(
ctx: anchor_lang::context::CpiContext<'a, 'b, 'c, 'info, accounts::#accounts_ident<'info>>,
ctx: anchor_lang::context::CpiContext<'a, 'b, 'c, 'info, accounts::#accounts_ident #accounts_generic>,
#(#args),*
) -> #ret_type {
let ix = {
Expand Down
15 changes: 15 additions & 0 deletions tests/declare-program/idls/external.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,21 @@
}
]
},
{
"name": "test_compilation_no_accounts",
"discriminator": [
194,
91,
205,
217,
51,
10,
247,
201
],
"accounts": [],
"args": []
},
{
"name": "test_compilation_return_type",
"discriminator": [
Expand Down
8 changes: 8 additions & 0 deletions tests/declare-program/programs/external/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,21 @@ pub mod external {
pub fn test_compilation_return_type(_ctx: Context<TestCompilation>) -> Result<bool> {
Ok(true)
}

// Compilation test for an instruction with no accounts
pub fn test_compilation_no_accounts(_ctx: Context<TestCompilationNoAccounts>) -> Result<()> {
Ok(())
}
}

#[derive(Accounts)]
pub struct TestCompilation<'info> {
pub signer: Signer<'info>,
}

#[derive(Accounts)]
pub struct TestCompilationNoAccounts {}

#[derive(Accounts)]
pub struct Init<'info> {
#[account(mut)]
Expand Down

0 comments on commit b26f7eb

Please sign in to comment.