diff --git a/lang/syn/src/idl/build.rs b/lang/syn/src/idl/build.rs index c82e02a2ed..db627594d1 100644 --- a/lang/syn/src/idl/build.rs +++ b/lang/syn/src/idl/build.rs @@ -208,6 +208,13 @@ pub fn idl_type_ts_from_syn_type( } } } + syn::Type::Reference(reference) => match reference.elem.as_ref() { + syn::Type::Slice(slice) if matches!(&*slice.elem, syn::Type::Path(path) if the_only_segment_is(path, "u8")) => + { + return Ok((quote! {#idl::IdlType::Bytes}, vec![])); + } + _ => panic!("Reference types other than byte slice(`&[u8]`) are not allowed"), + }, _ => Err(()), } } @@ -846,7 +853,7 @@ pub fn gen_idl_print_function_for_constant(item: &syn::ItemConst) -> TokenStream let impl_ts = match idl_type_ts_from_syn_type(&item.ty, &vec![]) { Ok((ty, _)) => quote! { - let value = format!("{}", #expr); + let value = format!("{:?}", #expr); let idl = #idl::IdlConst { name: #name.into(), diff --git a/tests/idl/programs/idl/src/lib.rs b/tests/idl/programs/idl/src/lib.rs index 6f917987ef..9f3e5a1707 100644 --- a/tests/idl/programs/idl/src/lib.rs +++ b/tests/idl/programs/idl/src/lib.rs @@ -4,16 +4,16 @@ use std::str::FromStr; declare_id!("id11111111111111111111111111111111111111111"); #[constant] -pub const FOO_CONST: u128 = 1_000_000; +pub const U8: u8 = 6; #[constant] -pub const BAR_CONST: u8 = 6; +pub const I128: i128 = 1_000_000; #[constant] -pub const BYTES_STR: &[u8] = b"test"; +pub const BYTE_STR: u8 = b't'; #[constant] -pub const BYTE_STR: u8 = b't'; +pub const BYTES_STR: &[u8] = b"test"; pub const NO_IDL: u16 = 55; diff --git a/tests/idl/tests/idl.ts b/tests/idl/tests/idl.ts index 5b053006d9..4aa7e4ddc2 100644 --- a/tests/idl/tests/idl.ts +++ b/tests/idl/tests/idl.ts @@ -8,24 +8,29 @@ describe("IDL", () => { anchor.setProvider(anchor.AnchorProvider.env()); const program = anchor.workspace.idl as Program; - it("Should include `FOO_CONST`", () => { - assert.isDefined( - program.idl.constants.find( - (c) => - c.name === "FOO_CONST" && c.type === "u128" && c.value === "1000000" - ) - ); - }); + it("Includes constants that use `#[constant]` macro", () => { + const checkDefined = ( + cb: (constant: typeof program["idl"]["constants"][number]) => boolean + ) => { + program.idl.constants.find((c) => cb(c)); + }; - it("Should include `BAR_CONST`", () => { - assert.isDefined( - program.idl.constants.find( - (c) => c.name === "BAR_CONST" && c.type === "u8" && c.value === "6" - ) + checkDefined((c) => c.name === "U8" && c.type === "u8" && c.value === "6"); + checkDefined( + (c) => c.name === "I128" && c.type === "i128" && c.value === "1000000" + ); + checkDefined( + (c) => c.name === "BYTE_STR" && c.type === "u8" && c.value === "116" + ); + checkDefined( + (c) => + c.name === "BYTES_STR" && + c.type === "bytes" && + c.value === "[116, 101, 115, 116]" ); }); - it("Should not include `NO_IDL` const", () => { + it("Does not include constants that does not use `#[constant]` macro ", () => { // @ts-expect-error assert.isUndefined(program.idl.constants.find((c) => c.name === "NO_IDL")); });