From 334a44ab7b2ea951af8001343e05244520d4e71c Mon Sep 17 00:00:00 2001 From: acheron <98934430+acheroncrypto@users.noreply.github.com> Date: Tue, 21 May 2024 22:04:19 +0200 Subject: [PATCH] lang: Fix instruction return type generation with `declare_program!` (#2977) --- CHANGELOG.md | 1 + .../program/src/declare_program/mods/cpi.rs | 2 +- tests/declare-program/idls/external.json | 91 ++++++++++--------- .../programs/external/src/lib.rs | 16 +++- 4 files changed, 66 insertions(+), 44 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0217de884..c7c1155da 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -32,6 +32,7 @@ The minor version will be incremented upon a breaking change and the patch versi - lang: Fix using `Vec` type with `declare_program!` ([#2966](https://github.com/coral-xyz/anchor/pull/2966)). - lang: Fix `ProgramError::ArithmeticOverflow` not found error ([#2975](https://github.com/coral-xyz/anchor/pull/2975)). - lang: Fix using optional accounts with `declare_program!` ([#2967](https://github.com/coral-xyz/anchor/pull/2967)). +- lang: Fix instruction return type generation with `declare_program!` ([#2977](https://github.com/coral-xyz/anchor/pull/2977)). ### Breaking diff --git a/lang/attribute/program/src/declare_program/mods/cpi.rs b/lang/attribute/program/src/declare_program/mods/cpi.rs index 50452fe73..05e1016a2 100644 --- a/lang/attribute/program/src/declare_program/mods/cpi.rs +++ b/lang/attribute/program/src/declare_program/mods/cpi.rs @@ -50,7 +50,7 @@ fn gen_cpi_instructions(idl: &Idl) -> proc_macro2::TokenStream { let ty = convert_idl_type_to_syn_type(ty); ( quote! { anchor_lang::Result> }, - quote! { Ok(Return::<#ty> { phantom:: std::marker::PhantomData }) }, + quote! { Ok(Return::<#ty> { phantom: std::marker::PhantomData }) }, ) }, None => ( diff --git a/tests/declare-program/idls/external.json b/tests/declare-program/idls/external.json index c0d1faddc..676532e24 100644 --- a/tests/declare-program/idls/external.json +++ b/tests/declare-program/idls/external.json @@ -44,6 +44,56 @@ ], "args": [] }, + { + "name": "test_compilation_defined_type_param", + "discriminator": [ + 61, + 118, + 87, + 242, + 137, + 97, + 90, + 223 + ], + "accounts": [ + { + "name": "signer", + "signer": true + } + ], + "args": [ + { + "name": "_my_account", + "type": { + "defined": { + "name": "MyAccount" + } + } + } + ] + }, + { + "name": "test_compilation_return_type", + "discriminator": [ + 174, + 51, + 51, + 121, + 52, + 61, + 38, + 28 + ], + "accounts": [ + { + "name": "signer", + "signer": true + } + ], + "args": [], + "returns": "bool" + }, { "name": "update", "discriminator": [ @@ -81,47 +131,6 @@ } ] }, - { - "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 6c0ce31e2..71dc571db 100644 --- a/tests/declare-program/programs/external/src/lib.rs +++ b/tests/declare-program/programs/external/src/lib.rs @@ -21,10 +21,22 @@ pub mod external { } // 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; + pub fn test_compilation_defined_type_param( + _ctx: Context, + _my_account: MyAccount, + ) -> Result<()> { Ok(()) } + + // Compilation test for whether a custom return type can be specified in `cpi` client + pub fn test_compilation_return_type(_ctx: Context) -> Result { + Ok(true) + } +} + +#[derive(Accounts)] +pub struct TestCompilation<'info> { + pub signer: Signer<'info>, } #[derive(Accounts)]