Skip to content

Commit

Permalink
Allow same named types and functions (#839)
Browse files Browse the repository at this point in the history
### What
Change the names of the static variables that contain the spec XDR to
include a type or function qualifier for the kind of entry the spec
contains.

### Why
To allow same named types and functions in contracts. When a type and
function have the same name today they result in two static variables
that have the same names. Adding a qualifier to what they are to those
variables names will stop them from colliding.

Close #828
  • Loading branch information
leighmcculloch authored Jan 26, 2023
1 parent 84d8162 commit db018aa
Show file tree
Hide file tree
Showing 11 changed files with 37 additions and 9 deletions.
2 changes: 1 addition & 1 deletion soroban-sdk-macros/src/derive_enum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ pub fn derive_type_enum(
let spec_xdr = spec_entry.to_xdr().unwrap();
let spec_xdr_lit = proc_macro2::Literal::byte_string(spec_xdr.as_slice());
let spec_xdr_len = spec_xdr.len();
let spec_ident = format_ident!("__SPEC_XDR_{}", enum_ident.to_string().to_uppercase());
let spec_ident = format_ident!("__SPEC_XDR_TYPE_{}", enum_ident.to_string().to_uppercase());
Some(quote! {
#[cfg_attr(target_family = "wasm", link_section = "contractspecv0")]
pub static #spec_ident: [u8; #spec_xdr_len] = #enum_ident::spec_xdr();
Expand Down
2 changes: 1 addition & 1 deletion soroban-sdk-macros/src/derive_enum_int.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ pub fn derive_type_enum_int(
let spec_xdr = spec_entry.to_xdr().unwrap();
let spec_xdr_lit = proc_macro2::Literal::byte_string(spec_xdr.as_slice());
let spec_xdr_len = spec_xdr.len();
let spec_ident = format_ident!("__SPEC_XDR_{}", enum_ident.to_string().to_uppercase());
let spec_ident = format_ident!("__SPEC_XDR_TYPE_{}", enum_ident.to_string().to_uppercase());
Some(quote! {
#[cfg_attr(target_family = "wasm", link_section = "contractspecv0")]
pub static #spec_ident: [u8; #spec_xdr_len] = #enum_ident::spec_xdr();
Expand Down
2 changes: 1 addition & 1 deletion soroban-sdk-macros/src/derive_error_enum_int.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ pub fn derive_type_error_enum_int(
let spec_xdr = spec_entry.to_xdr().unwrap();
let spec_xdr_lit = proc_macro2::Literal::byte_string(spec_xdr.as_slice());
let spec_xdr_len = spec_xdr.len();
let spec_ident = format_ident!("__SPEC_XDR_{}", enum_ident.to_string().to_uppercase());
let spec_ident = format_ident!("__SPEC_XDR_TYPE_{}", enum_ident.to_string().to_uppercase());
Some(quote! {
#[cfg_attr(target_family = "wasm", link_section = "contractspecv0")]
pub static #spec_ident: [u8; #spec_xdr_len] = #enum_ident::spec_xdr();
Expand Down
2 changes: 1 addition & 1 deletion soroban-sdk-macros/src/derive_fn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ pub fn derive_fn(
let spec_xdr = spec_entry.to_xdr().unwrap();
let spec_xdr_lit = proc_macro2::Literal::byte_string(spec_xdr.as_slice());
let spec_xdr_len = spec_xdr.len();
let spec_ident = format_ident!("__SPEC_XDR_{}", ident.to_string().to_uppercase());
let spec_ident = format_ident!("__SPEC_XDR_FN_{}", ident.to_string().to_uppercase());
let spec_fn_ident = format_ident!("spec_xdr_{}", ident.to_string());

// If errors have occurred, render them instead.
Expand Down
2 changes: 1 addition & 1 deletion soroban-sdk-macros/src/derive_struct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ pub fn derive_type_struct(
let spec_xdr = spec_entry.to_xdr().unwrap();
let spec_xdr_lit = proc_macro2::Literal::byte_string(spec_xdr.as_slice());
let spec_xdr_len = spec_xdr.len();
let spec_ident = format_ident!("__SPEC_XDR_{}", ident.to_string().to_uppercase());
let spec_ident = format_ident!("__SPEC_XDR_TYPE_{}", ident.to_string().to_uppercase());
Some(quote! {
#[cfg_attr(target_family = "wasm", link_section = "contractspecv0")]
pub static #spec_ident: [u8; #spec_xdr_len] = #ident::spec_xdr();
Expand Down
2 changes: 1 addition & 1 deletion soroban-sdk-macros/src/derive_struct_tuple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ pub fn derive_type_struct_tuple(
let spec_xdr = spec_entry.to_xdr().unwrap();
let spec_xdr_lit = proc_macro2::Literal::byte_string(spec_xdr.as_slice());
let spec_xdr_len = spec_xdr.len();
let spec_ident = format_ident!("__SPEC_XDR_{}", ident.to_string().to_uppercase());
let spec_ident = format_ident!("__SPEC_XDR_TYPE_{}", ident.to_string().to_uppercase());
Some(quote! {
#[cfg_attr(target_family = "wasm", link_section = "contractspecv0")]
pub static #spec_ident: [u8; #spec_xdr_len] = #ident::spec_xdr();
Expand Down
1 change: 1 addition & 0 deletions soroban-sdk/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ mod contract_call_stack;
mod contract_invoke;
mod contract_invoker_account;
mod contract_invoker_client;
mod contract_overlapping_type_fn_names;
mod contract_snapshot;
mod contract_store;
mod contract_udt_enum;
Expand Down
2 changes: 1 addition & 1 deletion soroban-sdk/src/tests/contract_add_i32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ fn test_functional() {

#[test]
fn test_spec() {
let entries = ScSpecEntry::from_xdr(__SPEC_XDR_ADD).unwrap();
let entries = ScSpecEntry::from_xdr(__SPEC_XDR_FN_ADD).unwrap();
let expect = ScSpecEntry::FunctionV0(ScSpecFunctionV0 {
name: "add".try_into().unwrap(),
inputs: vec![
Expand Down
27 changes: 27 additions & 0 deletions soroban-sdk/src/tests/contract_overlapping_type_fn_names.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
use crate as soroban_sdk;
use soroban_sdk::{contractimpl, contracttype, Env};

#[derive(Copy, Clone, Debug, Eq, PartialEq)]
#[contracttype]
pub struct State {
pub a: i32,
}

pub struct Contract;

#[contractimpl]
impl Contract {
pub fn state() -> State {
State { a: 1 }
}
}

#[test]
fn test_functional() {
let env = Env::default();
let contract_id = env.register_contract(None, Contract);

let client = ContractClient::new(&env, &contract_id);
let s = client.state();
assert_eq!(s, State { a: 1 });
}
2 changes: 1 addition & 1 deletion soroban-sdk/src/tests/contract_udt_struct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ fn test_error_on_partial_decode() {

#[test]
fn test_spec() {
let entries = ScSpecEntry::from_xdr(__SPEC_XDR_ADD).unwrap();
let entries = ScSpecEntry::from_xdr(__SPEC_XDR_FN_ADD).unwrap();
let expect = ScSpecEntry::FunctionV0(ScSpecFunctionV0 {
name: "add".try_into().unwrap(),
inputs: vec![
Expand Down
2 changes: 1 addition & 1 deletion soroban-sdk/src/tests/contract_udt_struct_tuple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ fn test_error_on_partial_decode() {

#[test]
fn test_spec() {
let entries = ScSpecEntry::from_xdr(__SPEC_XDR_ADD).unwrap();
let entries = ScSpecEntry::from_xdr(__SPEC_XDR_FN_ADD).unwrap();
let expect = ScSpecEntry::FunctionV0(ScSpecFunctionV0 {
name: "add".try_into().unwrap(),
inputs: std::vec![
Expand Down

0 comments on commit db018aa

Please sign in to comment.