From 77ca89f99d3e3df41db95b3af0058baffc884313 Mon Sep 17 00:00:00 2001 From: acheron Date: Mon, 30 Sep 2024 11:34:58 +0200 Subject: [PATCH 1/4] lang: Fix constant bytes declarations when using `declare_program!` --- .../src/declare_program/mods/constants.rs | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) 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() -} From 0a06ed6fafef95e63ebc30e28c59a7450eb35e6d Mon Sep 17 00:00:00 2001 From: acheron Date: Mon, 30 Sep 2024 11:36:57 +0200 Subject: [PATCH 2/4] tests: Use constant bytes with `declare_program!` --- tests/declare-program/idls/external.json | 7 +++++++ tests/declare-program/programs/external/src/lib.rs | 3 +++ 2 files changed, 10 insertions(+) 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/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::*; From 07a31b5d73580b64f3c33b63f6adbf28bc9d2089 Mon Sep 17 00:00:00 2001 From: acheron Date: Mon, 30 Sep 2024 11:40:24 +0200 Subject: [PATCH 3/4] tests: Update the external legacy IDL --- .../declare-program/idls/external_legacy.json | 111 +++++++++++++++--- 1 file changed, 93 insertions(+), 18 deletions(-) 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" + } } From b7af1c2781b50b8fda198defa1a7609fd395de4f Mon Sep 17 00:00:00 2001 From: acheron Date: Mon, 30 Sep 2024 17:26:58 +0200 Subject: [PATCH 4/4] Update CHANGELOG --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) 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