Skip to content

Commit

Permalink
lang: Fix using defined types in instruction parameters with `declare…
Browse files Browse the repository at this point in the history
…_program!` (#2959)
  • Loading branch information
acheroncrypto authored May 11, 2024
1 parent d8d007e commit 460a161
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ The minor version will be incremented upon a breaking change and the patch versi
- cli: add filename to 'Unable to read keypair file' errors ([#2932](https://github.com/coral-xyz/anchor/pull/2932)).
- idl: Fix path resolution of the `Cargo.lock` of the project when generating idls for external types ([#2946](https://github.com/coral-xyz/anchor/pull/2946)).
- idl: Fix potential panic on external type resolution ([#2954](https://github.com/coral-xyz/anchor/pull/2954)).
- lang: Fix using defined types in instruction parameters with `declare_program!` ([#2959](https://github.com/coral-xyz/anchor/pull/2959)).

### Breaking

Expand Down
3 changes: 3 additions & 0 deletions lang/attribute/program/src/declare_program/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ fn gen_program(idl: &Idl, name: &syn::Ident) -> proc_macro2::TokenStream {
#docs
pub mod #name {
use anchor_lang::prelude::*;
use accounts::*;
use events::*;
use types::*;

#id
#program_mod
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ pub fn gen_accounts_mod(idl: &Idl) -> proc_macro2::TokenStream {
quote! {
/// Program account type definitions.
pub mod accounts {
use super::{*, types::*};
use super::*;

#(#accounts)*
}
Expand Down
2 changes: 1 addition & 1 deletion lang/attribute/program/src/declare_program/mods/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ pub fn gen_events_mod(idl: &Idl) -> proc_macro2::TokenStream {
quote! {
/// Program event type definitions.
pub mod events {
use super::{*, types::*};
use super::*;

#(#events)*
}
Expand Down
2 changes: 1 addition & 1 deletion lang/attribute/program/src/declare_program/mods/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ fn gen_event(idl: &Idl) -> proc_macro2::TokenStream {
});

quote! {
use super::{*, events::*};
use super::*;

/// An enum that includes all events of the declared program as a tuple variant.
///
Expand Down
41 changes: 41 additions & 0 deletions tests/declare-program/idls/external.json
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,47 @@
}
]
},
{
"name": "update_all",
"discriminator": [
205,
139,
239,
66,
134,
131,
110,
182
],
"accounts": [
{
"name": "authority",
"signer": true
},
{
"name": "my_account",
"writable": true,
"pda": {
"seeds": [
{
"kind": "account",
"path": "authority"
}
]
}
}
],
"args": [
{
"name": "my_account",
"type": {
"defined": {
"name": "MyAccount"
}
}
}
]
},
{
"name": "update_composite",
"discriminator": [
Expand Down
6 changes: 6 additions & 0 deletions tests/declare-program/programs/external/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ pub mod external {
ctx.accounts.update.my_account.field = value;
Ok(())
}

// Compilation test for whether a defined type (an account in this case) can be used in `cpi` client.
pub fn update_all(ctx: Context<Update>, my_account: MyAccount) -> Result<()> {
*ctx.accounts.my_account = my_account;
Ok(())
}
}

#[derive(Accounts)]
Expand Down

0 comments on commit 460a161

Please sign in to comment.