Skip to content

Commit cbc9644

Browse files
deprecate lib arg on contracttype/error
1 parent 28142cd commit cbc9644

4 files changed

Lines changed: 60 additions & 0 deletions

File tree

soroban-sdk-macros/src/lib.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,24 @@ pub(crate) fn export_arg_v2_deprecation(export: &Option<bool>, ident: &syn::Iden
8181
}
8282
}
8383

84+
/// Emit a deprecation warning when `lib` is set on a contract type or error.
85+
/// The argument is a vestige of an earlier design that was never used and will
86+
/// be removed in a future release.
87+
pub(crate) fn lib_arg_deprecation(lib: &Option<String>, ident: &syn::Ident) -> TokenStream2 {
88+
if lib.is_some() {
89+
let marker = format_ident!("__SOROBAN_LIB_ARG_DEPRECATED_FOR_{}", ident);
90+
quote! {
91+
#[doc(hidden)]
92+
#[allow(non_upper_case_globals)]
93+
#[deprecated = "`lib` is deprecated and will be removed in a future release"]
94+
const #marker: () = ();
95+
const _: () = #marker;
96+
}
97+
} else {
98+
TokenStream2::new()
99+
}
100+
}
101+
84102
#[proc_macro]
85103
pub fn internal_symbol_short(input: TokenStream) -> TokenStream {
86104
let input = parse_macro_input!(input as LitStr);
@@ -456,6 +474,7 @@ pub fn contracttype(metadata: TokenStream, input: TokenStream) -> TokenStream {
456474
Err(e) => return e.to_compile_error().into(),
457475
}
458476
let export_deprecation = export_arg_v2_deprecation(&args.export, ident);
477+
let lib_deprecation = lib_arg_deprecation(&args.lib, ident);
459478
// Under `experimental_spec_shaking_v2` the spec is always emitted and
460479
// reachability determines what is retained, so the `export` argument is
461480
// ignored (a deprecation warning is emitted above). Otherwise, honor an
@@ -512,6 +531,7 @@ pub fn contracttype(metadata: TokenStream, input: TokenStream) -> TokenStream {
512531
quote! {
513532
#input
514533
#export_deprecation
534+
#lib_deprecation
515535
#derived
516536
}
517537
.into()
@@ -533,6 +553,7 @@ pub fn contracterror(metadata: TokenStream, input: TokenStream) -> TokenStream {
533553
let ident = &input.ident;
534554
let attrs = &input.attrs;
535555
let export_deprecation = export_arg_v2_deprecation(&args.export, ident);
556+
let lib_deprecation = lib_arg_deprecation(&args.lib, ident);
536557
// Under `experimental_spec_shaking_v2` the spec is always emitted and
537558
// reachability determines what is retained, so the `export` argument is
538559
// ignored (a deprecation warning is emitted above). Otherwise, honor an
@@ -567,6 +588,7 @@ pub fn contracterror(metadata: TokenStream, input: TokenStream) -> TokenStream {
567588
quote! {
568589
#input
569590
#export_deprecation
591+
#lib_deprecation
570592
#derived
571593
}
572594
.into()

soroban-sdk/tests/compile_fails.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@
22
fn compile_fails() {
33
let t = trybuild::TestCases::new();
44
t.compile_fail("tests/compile_fails/contracttrait_cfg_errors.rs");
5+
t.compile_fail("tests/compile_fails/contracttype_lib_deprecated.rs");
56
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Setting `lib` on `contracttype` or `contracterror` is deprecated. With the
2+
// `deprecated` lint denied, using it must fail to compile, proving the
3+
// deprecation warning is emitted.
4+
#![deny(deprecated)]
5+
6+
use soroban_sdk::{contracterror, contracttype};
7+
8+
#[contracttype(lib = "libname")]
9+
pub struct S {
10+
pub a: u32,
11+
}
12+
13+
#[contracterror(lib = "libname")]
14+
#[derive(Copy, Clone)]
15+
#[repr(u32)]
16+
pub enum E {
17+
A = 1,
18+
}
19+
20+
fn main() {}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
error: use of deprecated constant `__SOROBAN_LIB_ARG_DEPRECATED_FOR_S`: `lib` is deprecated and will be removed in a future release
2+
--> tests/compile_fails/contracttype_lib_deprecated.rs:9:12
3+
|
4+
9 | pub struct S {
5+
| ^
6+
|
7+
note: the lint level is defined here
8+
--> tests/compile_fails/contracttype_lib_deprecated.rs:4:9
9+
|
10+
4 | #![deny(deprecated)]
11+
| ^^^^^^^^^^
12+
13+
error: use of deprecated constant `__SOROBAN_LIB_ARG_DEPRECATED_FOR_E`: `lib` is deprecated and will be removed in a future release
14+
--> tests/compile_fails/contracttype_lib_deprecated.rs:16:10
15+
|
16+
16 | pub enum E {
17+
| ^

0 commit comments

Comments
 (0)