Skip to content

Commit bf7db0b

Browse files
Merge pull request #1644 from multiversx/diagnostics-on-unimplemented
Unified syntax - better compile-time errors
2 parents 08ac2fa + 099e888 commit bf7db0b

File tree

16 files changed

+121
-7
lines changed

16 files changed

+121
-7
lines changed

.github/workflows/actions-nightly.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ jobs:
1616
name: Contracts (nightly)
1717
uses: multiversx/mx-sc-actions/.github/workflows/contracts.yml@v3.2.0
1818
with:
19-
rust-toolchain: nightly-2023-12-11
19+
rust-toolchain: nightly-2024-05-22
2020
path-to-sc-meta: framework/meta
2121
enable-contracts-size-report: false
2222
mx-scenario-go-version: v2.1.0-alpha

contracts/feature-tests/abi-tester/src/abi_test_type.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,5 +39,6 @@ pub struct AbiManagedVecItem {
3939

4040
#[type_abi]
4141
pub struct OnlyShowsUpInEsdtAttr {
42+
#[allow(dead_code)]
4243
pub field: OnlyShowsUpAsNested10,
4344
}

contracts/feature-tests/abi-tester/tests/abi_tester_abi_test.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ fn abi_tester_esdt_attr_abi_generated_ok() {
7070

7171
#[test]
7272
fn check_multi_contract_config() {
73-
let mut blockchain = ScenarioWorld::new();
73+
let blockchain = ScenarioWorld::new();
7474

7575
let multi_contract_config = multiversx_sc_meta::multi_contract_config::<abi_tester::AbiProvider>(
7676
blockchain.current_dir().as_path(),

contracts/feature-tests/composability/interact/src/comp_interact_controller.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ pub struct ComposabilityInteract {
99
pub wallet_address: Address,
1010
pub forw_queue_code: BytesValue,
1111
pub vault_code: BytesValue,
12+
#[allow(dead_code)]
1213
pub state: State,
1314
}
1415

framework/base/src/types/interaction/tx_data/tx_code_source.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@ where
1414
{
1515
}
1616

17+
#[diagnostic::on_unimplemented(
18+
message = "Type `{Self}` cannot be used as code (does not implement `TxCodeValue<{Env}>`)",
19+
label = "not a valid smart contract byte code",
20+
note = "there are multiple ways to specify SC byte code, but `{Self}` is not one of them"
21+
)]
1722
pub trait TxCodeValue<Env>: AnnotatedValue<Env, ManagedBuffer<Env::Api>>
1823
where
1924
Env: TxEnv,
@@ -39,6 +44,11 @@ where
3944
{
4045
}
4146

47+
#[diagnostic::on_unimplemented(
48+
message = "Type `{Self}` cannot be used as code source value (does not implement `TxFromSourceValue<{Env}>`)",
49+
label = "not an address from where to copy the code",
50+
note = "there are multiple ways to specify a code source address, but `{Self}` is not one of them"
51+
)]
4252
pub trait TxFromSourceValue<Env>: AnnotatedValue<Env, ManagedAddress<Env::Api>>
4353
where
4454
Env: TxEnv,

framework/base/src/types/interaction/tx_exec/tx_exec_async_promises.rs

Lines changed: 74 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::{
22
api::{const_handles, CallTypeApi},
3-
contract_base::SendRawWrapper,
3+
contract_base::{ErrorHelper, SendRawWrapper},
44
types::{
55
interaction::callback_closure::CallbackClosureWithGas, CallbackClosure, ExplicitGas,
66
FunctionCall, ManagedBuffer, ManagedType, OriginalResultMarker, Tx, TxGas, TxGasValue,
@@ -143,6 +143,20 @@ where
143143
GasValue: TxGasValue<TxScEnv<Api>>,
144144
Callback: TxPromisesCallback<Api>,
145145
{
146+
/// Launches a transaction as an asynchronous promise (async v2 mechanism).
147+
///
148+
/// Several such transactions can be launched from a single transaction.
149+
///
150+
/// Must set:
151+
/// - to
152+
/// - gas
153+
/// - a function call, ideally via a proxy.
154+
///
155+
/// Value-only promises are not supported.
156+
///
157+
/// Optionally, can add:
158+
/// - any payment
159+
/// - a promise callback, which also needs explicit gas for callback.
146160
pub fn register_promise(self) {
147161
let callback_name = self.result_handler.callback_name();
148162
let mut cb_closure_args_serialized =
@@ -174,6 +188,65 @@ where
174188
}
175189
}
176190

191+
impl<Api, To, Payment, Callback> Tx<TxScEnv<Api>, (), To, Payment, (), FunctionCall<Api>, Callback>
192+
where
193+
Api: CallTypeApi,
194+
To: TxToSpecified<TxScEnv<Api>>,
195+
Payment: TxPayment<TxScEnv<Api>>,
196+
Callback: TxPromisesCallback<Api>,
197+
{
198+
/// ## Incorrect call
199+
///
200+
/// Must set **gas** in order to call `register_promise`.
201+
///
202+
/// ## Safety
203+
///
204+
/// This version of the method must never be called. It is only here to provide a more readable error.
205+
pub unsafe fn register_promise(self) {
206+
ErrorHelper::<Api>::signal_error_with_message("register_promise requires explicit gas");
207+
}
208+
}
209+
210+
impl<Api, To, Payment, Callback> Tx<TxScEnv<Api>, (), To, Payment, (), (), Callback>
211+
where
212+
Api: CallTypeApi,
213+
To: TxToSpecified<TxScEnv<Api>>,
214+
Payment: TxPayment<TxScEnv<Api>>,
215+
Callback: TxPromisesCallback<Api>,
216+
{
217+
/// ## Incorrect call
218+
///
219+
/// Must set **gas** and **function call** in order to call `register_promise`.
220+
///
221+
/// ## Safety
222+
///
223+
/// This version of the method must never be called. It is only here to provide a more readable error.
224+
pub unsafe fn register_promise(self) {
225+
ErrorHelper::<Api>::signal_error_with_message("register_promise requires explicit gas and function call");
226+
}
227+
}
228+
229+
impl<Api, To, Payment, GasValue, Callback>
230+
Tx<TxScEnv<Api>, (), To, Payment, ExplicitGas<GasValue>, (), Callback>
231+
where
232+
Api: CallTypeApi,
233+
To: TxToSpecified<TxScEnv<Api>>,
234+
Payment: TxPayment<TxScEnv<Api>>,
235+
GasValue: TxGasValue<TxScEnv<Api>>,
236+
Callback: TxPromisesCallback<Api>,
237+
{
238+
/// ## Incorrect call
239+
///
240+
/// Must set **function call** in order to call `register_promise`.
241+
///
242+
/// ## Safety
243+
///
244+
/// This version of the method must never be called. It is only here to provide a more readable error.
245+
pub unsafe fn register_promise(self) {
246+
ErrorHelper::<Api>::signal_error_with_message("register_promise requires function call");
247+
}
248+
}
249+
177250
impl<Api, To, Payment, Gas, Callback>
178251
Tx<TxScEnv<Api>, (), To, Payment, Gas, FunctionCall<Api>, Callback>
179252
where

framework/base/src/types/interaction/tx_from.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@ where
1313
/// Marks the non-empty sender of a transaction.
1414
///
1515
/// Enforces the reciipent to be explicitly specified.
16+
#[diagnostic::on_unimplemented(
17+
message = "Type `{Self}` cannot be used as a sender value (does not implement `TxFromSpecified<{Env}>`)",
18+
label = "sender needs to be explicit",
19+
note = "there are multiple ways to specify the sender value for a transaction, but `{Self}` is not one of them"
20+
)]
1621
pub trait TxFromSpecified<Env>:
1722
TxFrom<Env> + AnnotatedValue<Env, ManagedAddress<Env::Api>>
1823
where

framework/base/src/types/interaction/tx_gas.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use crate::{
44
types::ManagedBuffer,
55
};
66

7+
/// All typed that populate the gas field of a transaction need to implement this trait.
78
pub trait TxGas<Env>
89
where
910
Env: TxEnv,
@@ -32,6 +33,11 @@ where
3233
}
3334
}
3435

36+
#[diagnostic::on_unimplemented(
37+
message = "Type `{Self}` cannot be used as gas value (does not implement `TxGasValue<{Env}>`)",
38+
label = "not a valid value for gas",
39+
note = "there are multiple ways to specify the gas value for a transaction, but `{Self}` is not one of them"
40+
)]
3541
pub trait TxGasValue<Env>: AnnotatedValue<Env, u64>
3642
where
3743
Env: TxEnv,

framework/base/src/types/interaction/tx_payment.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@ use crate::{
2424
use super::{AnnotatedValue, FunctionCall, TxEnv, TxFrom, TxToSpecified};
2525

2626
/// Describes a payment that is part of a transaction.
27+
#[diagnostic::on_unimplemented(
28+
message = "Type `{Self}` cannot be used as payment (does not implement `TxPayment<{Env}>`)",
29+
label = "not a valid payment type",
30+
note = "there are multiple ways to specify the transaction payment, but `{Self}` is not one of them"
31+
)]
2732
pub trait TxPayment<Env>
2833
where
2934
Env: TxEnv,

framework/base/src/types/interaction/tx_result_handler_list/tx_result_handler_list_item.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@ use crate::types::TxEnv;
33
/// Result handler list item.
44
///
55
/// It acts as a result handler that produces a single result.
6+
#[diagnostic::on_unimplemented(
7+
message = "Type `{Self}` cannot be used as a decoder result handler (does not implement `RHListItem<{Env}>`)",
8+
label = "not a valid decoder result handler",
9+
note = "there are multiple ways to specify the result handling, but `{Self}` is not one of them"
10+
)]
611
pub trait RHListItem<Env, Original>
712
where
813
Env: TxEnv,

0 commit comments

Comments
 (0)