diff --git a/CHANGELOG.md b/CHANGELOG.md index 37e12d9f3..10196a3b4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/lang/attribute/program/src/declare_program/mod.rs b/lang/attribute/program/src/declare_program/mod.rs index d414c0596..bca2c15ec 100644 --- a/lang/attribute/program/src/declare_program/mod.rs +++ b/lang/attribute/program/src/declare_program/mod.rs @@ -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 diff --git a/lang/attribute/program/src/declare_program/mods/accounts.rs b/lang/attribute/program/src/declare_program/mods/accounts.rs index e55de414a..d2b150bcd 100644 --- a/lang/attribute/program/src/declare_program/mods/accounts.rs +++ b/lang/attribute/program/src/declare_program/mods/accounts.rs @@ -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)* } diff --git a/lang/attribute/program/src/declare_program/mods/events.rs b/lang/attribute/program/src/declare_program/mods/events.rs index bdd51abfe..8d085d9f8 100644 --- a/lang/attribute/program/src/declare_program/mods/events.rs +++ b/lang/attribute/program/src/declare_program/mods/events.rs @@ -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)* } diff --git a/lang/attribute/program/src/declare_program/mods/utils.rs b/lang/attribute/program/src/declare_program/mods/utils.rs index 414fc5c8f..65c6c95fe 100644 --- a/lang/attribute/program/src/declare_program/mods/utils.rs +++ b/lang/attribute/program/src/declare_program/mods/utils.rs @@ -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. /// diff --git a/tests/declare-program/idls/external.json b/tests/declare-program/idls/external.json index 7329c7c55..c0d1faddc 100644 --- a/tests/declare-program/idls/external.json +++ b/tests/declare-program/idls/external.json @@ -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": [ diff --git a/tests/declare-program/programs/external/src/lib.rs b/tests/declare-program/programs/external/src/lib.rs index 4897ba8ad..6c0ce31e2 100644 --- a/tests/declare-program/programs/external/src/lib.rs +++ b/tests/declare-program/programs/external/src/lib.rs @@ -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, my_account: MyAccount) -> Result<()> { + *ctx.accounts.my_account = my_account; + Ok(()) + } } #[derive(Accounts)]