From 143893f67e34c756db379c036f8f869975906855 Mon Sep 17 00:00:00 2001 From: acheron <98934430+acheroncrypto@users.noreply.github.com> Date: Mon, 30 Sep 2024 17:28:33 +0200 Subject: [PATCH] lang: Fix constant bytes declarations when using `declare_program!` (#3287) --- CHANGELOG.md | 1 + .../src/declare_program/mods/constants.rs | 17 ++- tests/declare-program/idls/external.json | 7 ++ .../declare-program/idls/external_legacy.json | 111 +++++++++++++++--- .../programs/external/src/lib.rs | 3 + 5 files changed, 112 insertions(+), 27 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0606b11c6f..f7a7b52f2b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -78,6 +78,7 @@ The minor version will be incremented upon a breaking change and the patch versi - lang: Use closures for `init` constraints to reduce the stack usage of `try_accounts` ([#2939](https://github.com/coral-xyz/anchor/pull/2939)). - lang: Allow the `cfg` attribute above the instructions ([#2339](https://github.com/coral-xyz/anchor/pull/2339)). - idl: Log output with `ANCHOR_LOG` on failure and improve build error message ([#3284](https://github.com/coral-xyz/anchor/pull/3284)). +- lang: Fix constant bytes declarations when using `declare_program!` ([#3287](https://github.com/coral-xyz/anchor/pull/3287)). ### Breaking diff --git a/lang/attribute/program/src/declare_program/mods/constants.rs b/lang/attribute/program/src/declare_program/mods/constants.rs index 4cb6ec0ed3..74093c6bd4 100644 --- a/lang/attribute/program/src/declare_program/mods/constants.rs +++ b/lang/attribute/program/src/declare_program/mods/constants.rs @@ -1,16 +1,19 @@ use anchor_lang_idl::types::{Idl, IdlType}; use quote::{format_ident, quote, ToTokens}; -use super::common::convert_idl_type_to_str; +use super::common::convert_idl_type_to_syn_type; pub fn gen_constants_mod(idl: &Idl) -> proc_macro2::TokenStream { let constants = idl.constants.iter().map(|c| { let name = format_ident!("{}", c.name); - let ty = match &c.ty { - IdlType::String => quote!(&str), - _ => parse_expr_ts(&convert_idl_type_to_str(&c.ty)), + let val = syn::parse_str::(&c.value) + .unwrap() + .to_token_stream(); + let (ty, val) = match &c.ty { + IdlType::Bytes => (quote!(&[u8]), quote! { &#val }), + IdlType::String => (quote!(&str), val), + _ => (convert_idl_type_to_syn_type(&c.ty).to_token_stream(), val), }; - let val = parse_expr_ts(&c.value); // TODO: Docs quote! { pub const #name: #ty = #val; } @@ -23,7 +26,3 @@ pub fn gen_constants_mod(idl: &Idl) -> proc_macro2::TokenStream { } } } - -fn parse_expr_ts(s: &str) -> proc_macro2::TokenStream { - syn::parse_str::(s).unwrap().to_token_stream() -} diff --git a/tests/declare-program/idls/external.json b/tests/declare-program/idls/external.json index 676532e246..37ea354d1b 100644 --- a/tests/declare-program/idls/external.json +++ b/tests/declare-program/idls/external.json @@ -229,5 +229,12 @@ ] } } + ], + "constants": [ + { + "name": "MASTER_SEED", + "type": "bytes", + "value": "[109, 97, 115, 116, 101, 114]" + } ] } \ No newline at end of file diff --git a/tests/declare-program/idls/external_legacy.json b/tests/declare-program/idls/external_legacy.json index 20da7a7fa2..dcf367ffbc 100644 --- a/tests/declare-program/idls/external_legacy.json +++ b/tests/declare-program/idls/external_legacy.json @@ -1,26 +1,55 @@ { "version": "0.1.0", "name": "external", - "metadata": { - "address": "Externa111111111111111111111111111111111111" - }, + "constants": [ + { + "name": "MASTER_SEED", + "type": "bytes", + "value": "[109, 97, 115, 116, 101, 114]" + } + ], "instructions": [ { "name": "init", "accounts": [ - { "name": "authority", "isMut": true, "isSigner": true }, - { "name": "myAccount", "isMut": true, "isSigner": false }, - { "name": "systemProgram", "isMut": false, "isSigner": false } + { + "name": "authority", + "isMut": true, + "isSigner": true + }, + { + "name": "myAccount", + "isMut": true, + "isSigner": false + }, + { + "name": "systemProgram", + "isMut": false, + "isSigner": false + } ], "args": [] }, { "name": "update", "accounts": [ - { "name": "authority", "isMut": false, "isSigner": true }, - { "name": "myAccount", "isMut": true, "isSigner": false } + { + "name": "authority", + "isMut": false, + "isSigner": true + }, + { + "name": "myAccount", + "isMut": true, + "isSigner": false + } ], - "args": [{ "name": "value", "type": "u32" }] + "args": [ + { + "name": "value", + "type": "u32" + } + ] }, { "name": "updateComposite", @@ -28,21 +57,53 @@ { "name": "update", "accounts": [ - { "name": "authority", "isMut": false, "isSigner": true }, - { "name": "myAccount", "isMut": true, "isSigner": false } + { + "name": "authority", + "isMut": false, + "isSigner": true + }, + { + "name": "myAccount", + "isMut": true, + "isSigner": false + } ] } ], - "args": [{ "name": "value", "type": "u32" }] + "args": [ + { + "name": "value", + "type": "u32" + } + ] }, { "name": "testCompilationDefinedTypeParam", - "accounts": [{ "name": "signer", "isMut": false, "isSigner": true }], - "args": [{ "name": "myAccount", "type": { "defined": "MyAccount" } }] + "accounts": [ + { + "name": "signer", + "isMut": false, + "isSigner": true + } + ], + "args": [ + { + "name": "myAccount", + "type": { + "defined": "MyAccount" + } + } + ] }, { "name": "testCompilationReturnType", - "accounts": [{ "name": "signer", "isMut": false, "isSigner": true }], + "accounts": [ + { + "name": "signer", + "isMut": false, + "isSigner": true + } + ], "args": [], "returns": "bool" } @@ -52,14 +113,28 @@ "name": "MyAccount", "type": { "kind": "struct", - "fields": [{ "name": "field", "type": "u32" }] + "fields": [ + { + "name": "field", + "type": "u32" + } + ] } } ], "events": [ { "name": "MyEvent", - "fields": [{ "name": "value", "type": "u32", "index": false }] + "fields": [ + { + "name": "value", + "type": "u32", + "index": false + } + ] } - ] + ], + "metadata": { + "address": "Externa111111111111111111111111111111111111" + } } diff --git a/tests/declare-program/programs/external/src/lib.rs b/tests/declare-program/programs/external/src/lib.rs index 71dc571db7..b244ad2247 100644 --- a/tests/declare-program/programs/external/src/lib.rs +++ b/tests/declare-program/programs/external/src/lib.rs @@ -2,6 +2,9 @@ use anchor_lang::prelude::*; declare_id!("Externa111111111111111111111111111111111111"); +#[constant] +pub const MASTER_SEED: &[u8] = b"master"; + #[program] pub mod external { use super::*;