Browse Source

lang: Fix constant bytes declarations when using `declare_program!` (#3287)

acheron 1 năm trước cách đây
mục cha
commit
143893f67e

+ 1 - 0
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
 

+ 8 - 9
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::<syn::Expr>(&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::<syn::Expr>(s).unwrap().to_token_stream()
-}

+ 7 - 0
tests/declare-program/idls/external.json

@@ -229,5 +229,12 @@
         ]
       }
     }
+  ],
+  "constants": [
+    {
+      "name": "MASTER_SEED",
+      "type": "bytes",
+      "value": "[109, 97, 115, 116, 101, 114]"
+    }
   ]
 }

+ 93 - 18
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"
+  }
 }

+ 3 - 0
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::*;