Skip to content

Commit 0cbcfd9

Browse files
add trait implementation validation at compile time
1 parent c3be0b7 commit 0cbcfd9

File tree

6 files changed

+124
-34
lines changed

6 files changed

+124
-34
lines changed

tests-expanded/test_account_tests.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,23 @@ impl CustomAccountInterface for Contract {
303303
Ok(())
304304
}
305305
}
306+
const _: () = {
307+
struct TraitCheckType;
308+
impl CustomAccountInterface for TraitCheckType {
309+
type Error = Error;
310+
type Signature = ();
311+
#[allow(non_snake_case)]
312+
#[allow(unused_parameters)]
313+
fn __check_auth(
314+
_env: Env,
315+
_signature_payload: Hash<32>,
316+
_signatures: Self::Signature,
317+
_auth_contexts: Vec<Context>,
318+
) -> Result<(), Error> {
319+
::core::panicking::panic("not implemented")
320+
}
321+
}
322+
};
306323
#[doc(hidden)]
307324
#[allow(non_snake_case)]
308325
#[allow(non_snake_case)]

tests-expanded/test_account_wasm32v1-none.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,23 @@ impl CustomAccountInterface for Contract {
192192
Ok(())
193193
}
194194
}
195+
const _: () = {
196+
struct TraitCheckType;
197+
impl CustomAccountInterface for TraitCheckType {
198+
type Error = Error;
199+
type Signature = ();
200+
#[allow(non_snake_case)]
201+
#[allow(unused_parameters)]
202+
fn __check_auth(
203+
_env: Env,
204+
_signature_payload: Hash<32>,
205+
_signatures: Self::Signature,
206+
_auth_contexts: Vec<Context>,
207+
) -> Result<(), Error> {
208+
::core::panicking::panic("not implemented")
209+
}
210+
}
211+
};
195212
#[doc(hidden)]
196213
#[allow(non_snake_case)]
197214
#[allow(non_snake_case)]

tests-expanded/test_associated_types_tests.rs

Lines changed: 38 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@ use soroban_sdk::{contract, contractimpl, Env, String};
88
pub struct DefaultImpl;
99
impl Trait for DefaultImpl {
1010
type Impl = Self;
11-
fn exec(env: &Env) -> String {
11+
fn exec(env: &Env, _i: u32) -> String {
1212
String::from_str(env, "default")
1313
}
1414
}
1515
pub trait Trait {
1616
type Impl: Trait;
17-
fn exec(env: &Env) -> String {
18-
Self::Impl::exec(env)
17+
fn exec(env: &Env, i: u32) -> String {
18+
Self::Impl::exec(env, i)
1919
}
2020
}
2121
pub struct Contract;
@@ -152,22 +152,32 @@ impl soroban_sdk::testutils::ContractFunctionSet for Contract {
152152
impl Trait for Contract {
153153
type Impl = DefaultImpl;
154154
}
155+
const _: () = {
156+
struct TraitCheckType;
157+
impl Trait for TraitCheckType {
158+
type Impl = DefaultImpl;
159+
#[allow(unused_parameters)]
160+
fn exec(_env: &Env, _i: u32) -> String {
161+
::core::panicking::panic("not implemented")
162+
}
163+
}
164+
};
155165
#[doc(hidden)]
156166
#[allow(non_snake_case)]
157167
pub mod __Contract__exec__spec {
158168
#[doc(hidden)]
159169
#[allow(non_snake_case)]
160170
#[allow(non_upper_case_globals)]
161-
pub static __SPEC_XDR_FN_EXEC: [u8; 28usize] = super::Contract::spec_xdr_exec();
171+
pub static __SPEC_XDR_FN_EXEC: [u8; 44usize] = super::Contract::spec_xdr_exec();
162172
}
163173
impl Contract {
164174
#[allow(non_snake_case)]
165-
pub const fn spec_xdr_exec() -> [u8; 28usize] {
166-
*b"\0\0\0\0\0\0\0\0\0\0\0\x04exec\0\0\0\0\0\0\0\x01\0\0\0\x10"
175+
pub const fn spec_xdr_exec() -> [u8; 44usize] {
176+
*b"\0\0\0\0\0\0\0\0\0\0\0\x04exec\0\0\0\x01\0\0\0\0\0\0\0\x01i\0\0\0\0\0\0\x04\0\0\0\x01\0\0\0\x10"
167177
}
168178
}
169179
impl<'a> ContractClient<'a> {
170-
pub fn exec(&self) -> String {
180+
pub fn exec(&self, _i: &u32) -> String {
171181
use core::ops::Not;
172182
let old_auth_manager = self
173183
.env
@@ -197,7 +207,7 @@ impl<'a> ContractClient<'a> {
197207
const SYMBOL: soroban_sdk::Symbol = soroban_sdk::Symbol::short("exec");
198208
SYMBOL
199209
},
200-
::soroban_sdk::Vec::new(&self.env),
210+
::soroban_sdk::Vec::from_array(&self.env, [_i.into_val(&self.env)]),
201211
);
202212
if let Some(old_auth_manager) = old_auth_manager {
203213
self.env.host().set_auth_manager(old_auth_manager).unwrap();
@@ -206,6 +216,7 @@ impl<'a> ContractClient<'a> {
206216
}
207217
pub fn try_exec(
208218
&self,
219+
_i: &u32,
209220
) -> Result<
210221
Result<
211222
String,
@@ -238,7 +249,7 @@ impl<'a> ContractClient<'a> {
238249
const SYMBOL: soroban_sdk::Symbol = soroban_sdk::Symbol::short("exec");
239250
SYMBOL
240251
},
241-
::soroban_sdk::Vec::new(&self.env),
252+
::soroban_sdk::Vec::from_array(&self.env, [_i.into_val(&self.env)]),
242253
);
243254
if let Some(old_auth_manager) = old_auth_manager {
244255
self.env.host().set_auth_manager(old_auth_manager).unwrap();
@@ -249,41 +260,49 @@ impl<'a> ContractClient<'a> {
249260
impl ContractArgs {
250261
#[inline(always)]
251262
#[allow(clippy::unused_unit)]
252-
pub fn exec<'i>() -> () {
253-
()
263+
pub fn exec<'i>(_i: &'i u32) -> (&'i u32,) {
264+
(_i,)
254265
}
255266
}
256267
#[doc(hidden)]
257268
#[allow(non_snake_case)]
258269
pub mod __Contract__exec {
259270
use super::*;
260271
#[deprecated(note = "use `ContractClient::new(&env, &contract_id).exec` instead")]
261-
pub fn invoke_raw(env: soroban_sdk::Env) -> soroban_sdk::Val {
272+
pub fn invoke_raw(env: soroban_sdk::Env, arg_0: soroban_sdk::Val) -> soroban_sdk::Val {
262273
use super::Trait;
263274
<_ as soroban_sdk::IntoVal<soroban_sdk::Env, soroban_sdk::Val>>::into_val(
264275
#[allow(deprecated)]
265-
&<super::Contract>::exec(&env),
276+
&<super::Contract>::exec(
277+
&env,
278+
<_ as soroban_sdk::unwrap::UnwrapOptimized>::unwrap_optimized(
279+
<_ as soroban_sdk::TryFromValForContractFn<
280+
soroban_sdk::Env,
281+
soroban_sdk::Val,
282+
>>::try_from_val_for_contract_fn(&env, &arg_0),
283+
),
284+
),
266285
&env,
267286
)
268287
}
269288
#[deprecated(note = "use `ContractClient::new(&env, &contract_id).exec` instead")]
270289
pub fn invoke_raw_slice(env: soroban_sdk::Env, args: &[soroban_sdk::Val]) -> soroban_sdk::Val {
271-
if args.len() != 0usize {
290+
if args.len() != 1usize {
272291
{
273292
::core::panicking::panic_fmt(format_args!(
274293
"invalid number of input arguments: {0} expected, got {1}",
275-
0usize,
294+
1usize,
276295
args.len(),
277296
));
278297
};
279298
}
280299
#[allow(deprecated)]
281-
invoke_raw(env)
300+
invoke_raw(env, args[0usize])
282301
}
283302
#[deprecated(note = "use `ContractClient::new(&env, &contract_id).exec` instead")]
284-
pub extern "C" fn invoke_raw_extern() -> soroban_sdk::Val {
303+
pub extern "C" fn invoke_raw_extern(arg_0: soroban_sdk::Val) -> soroban_sdk::Val {
285304
#[allow(deprecated)]
286-
invoke_raw(soroban_sdk::Env::default())
305+
invoke_raw(soroban_sdk::Env::default(), arg_0)
287306
}
288307
use super::*;
289308
}
@@ -347,7 +366,7 @@ mod test {
347366
let e = Env::default();
348367
let contract_id = e.register(Contract, ());
349368
let client = ContractClient::new(&e, &contract_id);
350-
let res = client.exec();
369+
let res = client.exec(&42);
351370
match (&res, &String::from_str(&e, "default")) {
352371
(left_val, right_val) => {
353372
if !(*left_val == *right_val) {

tests-expanded/test_associated_types_wasm32v1-none.rs

Lines changed: 34 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@ use soroban_sdk::{contract, contractimpl, Env, String};
88
pub struct DefaultImpl;
99
impl Trait for DefaultImpl {
1010
type Impl = Self;
11-
fn exec(env: &Env) -> String {
11+
fn exec(env: &Env, _i: u32) -> String {
1212
String::from_str(env, "default")
1313
}
1414
}
1515
pub trait Trait {
1616
type Impl: Trait;
17-
fn exec(env: &Env) -> String {
18-
Self::Impl::exec(env)
17+
fn exec(env: &Env, i: u32) -> String {
18+
Self::Impl::exec(env, i)
1919
}
2020
}
2121
pub struct Contract;
@@ -40,23 +40,33 @@ impl<'a> ContractClient<'a> {
4040
impl Trait for Contract {
4141
type Impl = DefaultImpl;
4242
}
43+
const _: () = {
44+
struct TraitCheckType;
45+
impl Trait for TraitCheckType {
46+
type Impl = DefaultImpl;
47+
#[allow(unused_parameters)]
48+
fn exec(_env: &Env, _i: u32) -> String {
49+
::core::panicking::panic("not implemented")
50+
}
51+
}
52+
};
4353
#[doc(hidden)]
4454
#[allow(non_snake_case)]
4555
pub mod __Contract__exec__spec {
4656
#[doc(hidden)]
4757
#[allow(non_snake_case)]
4858
#[allow(non_upper_case_globals)]
4959
#[link_section = "contractspecv0"]
50-
pub static __SPEC_XDR_FN_EXEC: [u8; 28usize] = super::Contract::spec_xdr_exec();
60+
pub static __SPEC_XDR_FN_EXEC: [u8; 44usize] = super::Contract::spec_xdr_exec();
5161
}
5262
impl Contract {
5363
#[allow(non_snake_case)]
54-
pub const fn spec_xdr_exec() -> [u8; 28usize] {
55-
*b"\0\0\0\0\0\0\0\0\0\0\0\x04exec\0\0\0\0\0\0\0\x01\0\0\0\x10"
64+
pub const fn spec_xdr_exec() -> [u8; 44usize] {
65+
*b"\0\0\0\0\0\0\0\0\0\0\0\x04exec\0\0\0\x01\0\0\0\0\0\0\0\x01i\0\0\0\0\0\0\x04\0\0\0\x01\0\0\0\x10"
5666
}
5767
}
5868
impl<'a> ContractClient<'a> {
59-
pub fn exec(&self) -> String {
69+
pub fn exec(&self, _i: &u32) -> String {
6070
use core::ops::Not;
6171
use soroban_sdk::{FromVal, IntoVal};
6272
let res = self.env.invoke_contract(
@@ -66,12 +76,13 @@ impl<'a> ContractClient<'a> {
6676
const SYMBOL: soroban_sdk::Symbol = soroban_sdk::Symbol::short("exec");
6777
SYMBOL
6878
},
69-
::soroban_sdk::Vec::new(&self.env),
79+
::soroban_sdk::Vec::from_array(&self.env, [_i.into_val(&self.env)]),
7080
);
7181
res
7282
}
7383
pub fn try_exec(
7484
&self,
85+
_i: &u32,
7586
) -> Result<
7687
Result<
7788
String,
@@ -87,36 +98,44 @@ impl<'a> ContractClient<'a> {
8798
const SYMBOL: soroban_sdk::Symbol = soroban_sdk::Symbol::short("exec");
8899
SYMBOL
89100
},
90-
::soroban_sdk::Vec::new(&self.env),
101+
::soroban_sdk::Vec::from_array(&self.env, [_i.into_val(&self.env)]),
91102
);
92103
res
93104
}
94105
}
95106
impl ContractArgs {
96107
#[inline(always)]
97108
#[allow(clippy::unused_unit)]
98-
pub fn exec<'i>() -> () {
99-
()
109+
pub fn exec<'i>(_i: &'i u32) -> (&'i u32,) {
110+
(_i,)
100111
}
101112
}
102113
#[doc(hidden)]
103114
#[allow(non_snake_case)]
104115
pub mod __Contract__exec {
105116
use super::*;
106117
#[deprecated(note = "use `ContractClient::new(&env, &contract_id).exec` instead")]
107-
pub fn invoke_raw(env: soroban_sdk::Env) -> soroban_sdk::Val {
118+
pub fn invoke_raw(env: soroban_sdk::Env, arg_0: soroban_sdk::Val) -> soroban_sdk::Val {
108119
use super::Trait;
109120
<_ as soroban_sdk::IntoVal<soroban_sdk::Env, soroban_sdk::Val>>::into_val(
110121
#[allow(deprecated)]
111-
&<super::Contract>::exec(&env),
122+
&<super::Contract>::exec(
123+
&env,
124+
<_ as soroban_sdk::unwrap::UnwrapOptimized>::unwrap_optimized(
125+
<_ as soroban_sdk::TryFromValForContractFn<
126+
soroban_sdk::Env,
127+
soroban_sdk::Val,
128+
>>::try_from_val_for_contract_fn(&env, &arg_0),
129+
),
130+
),
112131
&env,
113132
)
114133
}
115134
#[deprecated(note = "use `ContractClient::new(&env, &contract_id).exec` instead")]
116135
#[export_name = "exec"]
117-
pub extern "C" fn invoke_raw_extern() -> soroban_sdk::Val {
136+
pub extern "C" fn invoke_raw_extern(arg_0: soroban_sdk::Val) -> soroban_sdk::Val {
118137
#[allow(deprecated)]
119-
invoke_raw(soroban_sdk::Env::default())
138+
invoke_raw(soroban_sdk::Env::default(), arg_0)
120139
}
121140
use super::*;
122141
}

tests-expanded/test_multiimpl_tests.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -470,6 +470,15 @@ trait Trait {
470470
impl Trait for Contract {
471471
fn empty3() {}
472472
}
473+
const _: () = {
474+
struct TraitCheckType;
475+
impl Trait for TraitCheckType {
476+
#[allow(unused_parameters)]
477+
fn empty3() {
478+
::core::panicking::panic("not implemented")
479+
}
480+
}
481+
};
473482
#[doc(hidden)]
474483
#[allow(non_snake_case)]
475484
pub mod __Contract__empty3__spec {

tests-expanded/test_multiimpl_wasm32v1-none.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,15 @@ trait Trait {
188188
impl Trait for Contract {
189189
fn empty3() {}
190190
}
191+
const _: () = {
192+
struct TraitCheckType;
193+
impl Trait for TraitCheckType {
194+
#[allow(unused_parameters)]
195+
fn empty3() {
196+
::core::panicking::panic("not implemented")
197+
}
198+
}
199+
};
191200
#[doc(hidden)]
192201
#[allow(non_snake_case)]
193202
pub mod __Contract__empty3__spec {

0 commit comments

Comments
 (0)