From 03544bd35c20d83df6831c33ac8e5cbf237dc908 Mon Sep 17 00:00:00 2001 From: Nicolas Schapeler Date: Mon, 13 May 2024 18:05:20 +0200 Subject: [PATCH 1/6] add support for const generics in types inside of accounts --- lang/attribute/program/src/declare_program/common.rs | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/lang/attribute/program/src/declare_program/common.rs b/lang/attribute/program/src/declare_program/common.rs index e276a4fe09..5cd08b87ed 100644 --- a/lang/attribute/program/src/declare_program/common.rs +++ b/lang/attribute/program/src/declare_program/common.rs @@ -104,8 +104,15 @@ pub fn convert_idl_type_def_to_ts( .generics .iter() .map(|generic| match generic { - IdlTypeDefGeneric::Type { name } => format_ident!("{name}"), - IdlTypeDefGeneric::Const { name, ty } => format_ident!("{name}: {ty}"), + IdlTypeDefGeneric::Type { name } => { + let name = format_ident!("{}", name); + quote! { #name } + } + IdlTypeDefGeneric::Const { name, ty } => { + let name = format_ident!("{}", name); + let ty = format_ident!("{}", ty); + quote! { const #name: #ty } + } }) .collect::>(); if generics.is_empty() { From ef4d5fbc3b23d482b027e51c1129b55e5cc46380 Mon Sep 17 00:00:00 2001 From: Nicolas Schapeler Date: Mon, 13 May 2024 19:24:01 +0200 Subject: [PATCH 2/6] fix bytes being converted incorrectly --- lang/attribute/program/src/declare_program/common.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/lang/attribute/program/src/declare_program/common.rs b/lang/attribute/program/src/declare_program/common.rs index e276a4fe09..fd687bd2ec 100644 --- a/lang/attribute/program/src/declare_program/common.rs +++ b/lang/attribute/program/src/declare_program/common.rs @@ -38,7 +38,10 @@ pub fn gen_accounts_common(idl: &Idl, prefix: &str) -> proc_macro2::TokenStream } pub fn convert_idl_type_to_syn_type(ty: &IdlType) -> syn::Type { - syn::parse_str(&convert_idl_type_to_str(ty)).unwrap() + match ty { + IdlType::Bytes => syn::parse_str("Vec").unwrap(), + _ => syn::parse_str(&convert_idl_type_to_str(ty)).unwrap(), + } } // TODO: Impl `ToString` for `IdlType` From 5fcb487dacca6ce99caef715677c8e4ad64177f5 Mon Sep 17 00:00:00 2001 From: cryptopapi997 <38372048+cryptopapi997@users.noreply.github.com> Date: Mon, 13 May 2024 19:27:09 +0200 Subject: [PATCH 3/6] Update CHANGELOG.md --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 10196a3b4f..0d3da911ca 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -26,6 +26,7 @@ The minor version will be incremented upon a breaking change and the patch versi - 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)). +- lang: Fix using const generics with `declare_program!` ([#2965](https://github.com/coral-xyz/anchor/pull/2965)). ### Breaking From a8adf4ba5ef7b6e689b1a27aca1ddfeadd070ac6 Mon Sep 17 00:00:00 2001 From: cryptopapi997 <38372048+cryptopapi997@users.noreply.github.com> Date: Mon, 13 May 2024 19:29:25 +0200 Subject: [PATCH 4/6] Update CHANGELOG.md --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 10196a3b4f..6f5d0b6915 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -26,6 +26,7 @@ The minor version will be incremented upon a breaking change and the patch versi - 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)). +- lang: Fix bytes being incorrectly converted from IDL to Rust with `declare_program!` ([#2966](https://github.com/coral-xyz/anchor/pull/2966)). ### Breaking From ce4b351ea0a5cd9fd426fc4467ef894826d6640a Mon Sep 17 00:00:00 2001 From: cryptopapi997 <38372048+cryptopapi997@users.noreply.github.com> Date: Tue, 14 May 2024 21:32:31 +0200 Subject: [PATCH 5/6] Update common.rs --- lang/attribute/program/src/declare_program/common.rs | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/lang/attribute/program/src/declare_program/common.rs b/lang/attribute/program/src/declare_program/common.rs index f5a4485f63..9b1f71f350 100644 --- a/lang/attribute/program/src/declare_program/common.rs +++ b/lang/attribute/program/src/declare_program/common.rs @@ -38,10 +38,7 @@ pub fn gen_accounts_common(idl: &Idl, prefix: &str) -> proc_macro2::TokenStream } pub fn convert_idl_type_to_syn_type(ty: &IdlType) -> syn::Type { - match ty { - IdlType::Bytes => syn::parse_str("Vec").unwrap(), - _ => syn::parse_str(&convert_idl_type_to_str(ty)).unwrap(), - } + syn::parse_str(&convert_idl_type_to_str(ty)).unwrap() } // TODO: Impl `ToString` for `IdlType` @@ -62,7 +59,7 @@ pub fn convert_idl_type_to_str(ty: &IdlType) -> String { IdlType::I128 => "i128".into(), IdlType::U256 => "u256".into(), IdlType::I256 => "i256".into(), - IdlType::Bytes => "bytes".into(), + IdlType::Bytes => "Vec".into(), IdlType::String => "String".into(), IdlType::Pubkey => "Pubkey".into(), IdlType::Option(ty) => format!("Option<{}>", convert_idl_type_to_str(ty)), From 69bd596eec488265106930082e0f7c50a0db53a7 Mon Sep 17 00:00:00 2001 From: acheron Date: Wed, 15 May 2024 08:09:10 +0200 Subject: [PATCH 6/6] Mention being unable to use `Vec` rather than `bytes` in CHANGELOG --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 74816b002e..f5fa4ffc74 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -27,7 +27,7 @@ The minor version will be incremented upon a breaking change and the patch versi - 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)). - lang: Fix using const generics with `declare_program!` ([#2965](https://github.com/coral-xyz/anchor/pull/2965)). -- lang: Fix bytes being incorrectly converted from IDL to Rust with `declare_program!` ([#2966](https://github.com/coral-xyz/anchor/pull/2966)). +- lang: Fix using `Vec` type with `declare_program!` ([#2966](https://github.com/coral-xyz/anchor/pull/2966)). ### Breaking