From a33be41b38bc8ef7cc8fb7f81a1effc4803f2043 Mon Sep 17 00:00:00 2001 From: Tomasz Kurcz <360248+uint@users.noreply.github.com> Date: Tue, 29 Aug 2023 00:34:27 +0200 Subject: [PATCH 1/2] fix: Un-deprecate private init functions (#1074) Co-authored-by: Tomasz Kurcz --- .../src/core_impl/info_extractor/visitor.rs | 19 ++-------------- near-sdk/compilation_tests/all.rs | 1 + .../compilation_tests/private_init_method.rs | 22 +++++++++++++++++++ 3 files changed, 25 insertions(+), 17 deletions(-) create mode 100644 near-sdk/compilation_tests/private_init_method.rs diff --git a/near-sdk-macros/src/core_impl/info_extractor/visitor.rs b/near-sdk-macros/src/core_impl/info_extractor/visitor.rs index 1f94c30bc..c32f4fbf3 100644 --- a/near-sdk-macros/src/core_impl/info_extractor/visitor.rs +++ b/near-sdk-macros/src/core_impl/info_extractor/visitor.rs @@ -87,23 +87,8 @@ impl Visitor { } pub fn visit_private_attr(&mut self, _attr: &Attribute) -> syn::Result<()> { - use VisitorKind::*; - - match self.kind { - Call | View => { - self.parsed_data.is_private = true; - Ok(()) - } - Init => { - // TODO: return an error instead in 5.0 - // see https://github.com/near/near-sdk-rs/issues/1040 - println!("near_bindgen: private init functions will be disallowed in 5.0"); - Ok(()) - - // let message = format!("{} function can't be private.", self.kind); - // Err(Error::new(attr.span(), message)) - } - } + self.parsed_data.is_private = true; + Ok(()) } pub fn visit_result_serializer_attr( diff --git a/near-sdk/compilation_tests/all.rs b/near-sdk/compilation_tests/all.rs index 09f9d73c7..8acd10db3 100644 --- a/near-sdk/compilation_tests/all.rs +++ b/near-sdk/compilation_tests/all.rs @@ -30,4 +30,5 @@ fn compilation_tests() { // // t.compile_fail("compilation_tests/self_forbidden_in_non_init_fn_return.rs"); // t.compile_fail("compilation_tests/self_forbidden_in_non_init_fn_arg.rs"); + t.pass("compilation_tests/private_init_method.rs"); } diff --git a/near-sdk/compilation_tests/private_init_method.rs b/near-sdk/compilation_tests/private_init_method.rs new file mode 100644 index 000000000..3060c1713 --- /dev/null +++ b/near-sdk/compilation_tests/private_init_method.rs @@ -0,0 +1,22 @@ +//! Even though it might feel unintuitive, a method can be both private and init. +//! See: https://github.com/near/near-sdk-rs/issues/1040#issuecomment-1687126452 + +use borsh::{BorshDeserialize, BorshSerialize}; +use near_sdk::near_bindgen; + +#[near_bindgen] +#[derive(BorshDeserialize, BorshSerialize)] +struct Incrementer { + value: u32, +} + +#[near_bindgen] +impl Incrementer { + #[private] + #[init] + pub fn new(starting_value: u32) -> Self { + Self { value: starting_value } + } +} + +fn main() {} From db299121d572279ba59f9a558f09b4e99c1b0877 Mon Sep 17 00:00:00 2001 From: Vlad Frolov Date: Tue, 12 Sep 2023 09:56:27 +0200 Subject: [PATCH 2/2] fix: Fixed compilation-tests after stable Rust release 1.72 (#1081) --- Cargo.toml | 1 + near-sdk/compilation_tests/all.rs | 6 ++- .../schema_derive_invalids.stderr | 38 ++++++------- near-sdk/src/collections/tree_map.rs | 53 +++++++++---------- near-sdk/src/store/tree_map/mod.rs | 53 +++++++++---------- 5 files changed, 77 insertions(+), 74 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 86a74b66e..32956114e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,4 +1,5 @@ [workspace] +resolver = "2" members = [ "near-sdk", "near-sdk-macros", diff --git a/near-sdk/compilation_tests/all.rs b/near-sdk/compilation_tests/all.rs index 8acd10db3..4675c1b74 100644 --- a/near-sdk/compilation_tests/all.rs +++ b/near-sdk/compilation_tests/all.rs @@ -21,7 +21,11 @@ fn compilation_tests() { t.pass("compilation_tests/function_error.rs"); t.pass("compilation_tests/enum_near_bindgen.rs"); t.pass("compilation_tests/schema_derive.rs"); - t.compile_fail("compilation_tests/schema_derive_invalids.rs"); + if rustversion::cfg!(since(1.72)) { + // The compilation error output has slightly changed in 1.72, so we + // snapshoted this new version + t.compile_fail("compilation_tests/schema_derive_invalids.rs"); + } t.compile_fail("compilation_tests/generic_function.rs"); t.compile_fail("compilation_tests/generic_const_function.rs"); t.pass("compilation_tests/self_support.rs"); diff --git a/near-sdk/compilation_tests/schema_derive_invalids.stderr b/near-sdk/compilation_tests/schema_derive_invalids.stderr index 62635e119..8a842a534 100644 --- a/near-sdk/compilation_tests/schema_derive_invalids.stderr +++ b/near-sdk/compilation_tests/schema_derive_invalids.stderr @@ -68,23 +68,23 @@ error: `NearSchema` does not support derive for unions | |_^ error[E0277]: the trait bound `Inner: JsonSchema` is not satisfied - --> compilation_tests/schema_derive_invalids.rs:6:14 - | -6 | struct Outer(Inner); - | ^^^^^ the trait `JsonSchema` is not implemented for `Inner` - | - = help: the following other types implement trait `JsonSchema`: - &'a T - &'a mut T - () - (T0, T1) - (T0, T1, T2) - (T0, T1, T2, T3) - (T0, T1, T2, T3, T4) - (T0, T1, T2, T3, T4, T5) - and 165 others + --> compilation_tests/schema_derive_invalids.rs:6:14 + | +6 | struct Outer(Inner); + | ^^^^^ the trait `JsonSchema` is not implemented for `Inner` + | + = help: the following other types implement trait `JsonSchema`: + bool + char + isize + i8 + i16 + i32 + i64 + i128 + and $N others note: required by a bound in `SchemaGenerator::subschema_for` - --> $CARGO/schemars-0.8.12/src/gen.rs - | - | pub fn subschema_for(&mut self) -> Schema { - | ^^^^^^^^^^ required by this bound in `SchemaGenerator::subschema_for` + --> $CARGO/schemars-0.8.13/src/gen.rs + | + | pub fn subschema_for(&mut self) -> Schema { + | ^^^^^^^^^^ required by this bound in `SchemaGenerator::subschema_for` diff --git a/near-sdk/src/collections/tree_map.rs b/near-sdk/src/collections/tree_map.rs index 13a79b976..6f6d950be 100644 --- a/near-sdk/src/collections/tree_map.rs +++ b/near-sdk/src/collections/tree_map.rs @@ -1134,7 +1134,7 @@ mod tests { #[test] fn test_lower() { let mut map: TreeMap = TreeMap::new(next_trie_id()); - let vec: Vec = vec![10, 20, 30, 40, 50]; + let vec = [10, 20, 30, 40, 50]; for x in vec.iter() { map.insert(x, &1); @@ -1154,7 +1154,7 @@ mod tests { #[test] fn test_higher() { let mut map: TreeMap = TreeMap::new(next_trie_id()); - let vec: Vec = vec![10, 20, 30, 40, 50]; + let vec = [10, 20, 30, 40, 50]; for x in vec.iter() { map.insert(x, &1); @@ -1174,7 +1174,7 @@ mod tests { #[test] fn test_floor_key() { let mut map: TreeMap = TreeMap::new(next_trie_id()); - let vec: Vec = vec![10, 20, 30, 40, 50]; + let vec = [10, 20, 30, 40, 50]; for x in vec.iter() { map.insert(x, &1); @@ -1194,7 +1194,7 @@ mod tests { #[test] fn test_ceil_key() { let mut map: TreeMap = TreeMap::new(next_trie_id()); - let vec: Vec = vec![10, 20, 30, 40, 50]; + let vec = [10, 20, 30, 40, 50]; for x in vec.iter() { map.insert(x, &1); @@ -1231,7 +1231,7 @@ mod tests { #[test] fn test_remove_3_desc() { - let vec: Vec = vec![3, 2, 1]; + let vec = [3, 2, 1]; let mut map: TreeMap = TreeMap::new(next_trie_id()); for x in &vec { @@ -1250,7 +1250,7 @@ mod tests { #[test] fn test_remove_3_asc() { - let vec: Vec = vec![1, 2, 3]; + let vec = [1, 2, 3]; let mut map: TreeMap = TreeMap::new(next_trie_id()); for x in &vec { @@ -1269,8 +1269,8 @@ mod tests { #[test] fn test_remove_7_regression_1() { - let vec: Vec = - vec![2104297040, 552624607, 4269683389, 3382615941, 155419892, 4102023417, 1795725075]; + let vec = + [2104297040, 552624607, 4269683389, 3382615941, 155419892, 4102023417, 1795725075]; let mut map: TreeMap = TreeMap::new(next_trie_id()); for x in &vec { @@ -1289,8 +1289,7 @@ mod tests { #[test] fn test_remove_7_regression_2() { - let vec: Vec = - vec![700623085, 87488544, 1500140781, 1111706290, 3187278102, 4042663151, 3731533080]; + let vec = [700623085, 87488544, 1500140781, 1111706290, 3187278102, 4042663151, 3731533080]; let mut map: TreeMap = TreeMap::new(next_trie_id()); for x in &vec { @@ -1309,7 +1308,7 @@ mod tests { #[test] fn test_remove_9_regression() { - let vec: Vec = vec![ + let vec = [ 1186903464, 506371929, 1738679820, 1883936615, 1815331350, 1512669683, 3581743264, 1396738166, 1902061760, ]; @@ -1331,7 +1330,7 @@ mod tests { #[test] fn test_remove_20_regression_1() { - let vec: Vec = vec![ + let vec = [ 552517392, 3638992158, 1015727752, 2500937532, 638716734, 586360620, 2476692174, 1425948996, 3608478547, 757735878, 2709959928, 2092169539, 3620770200, 783020918, 1986928932, 200210441, 1972255302, 533239929, 497054557, 2137924638, @@ -1354,7 +1353,7 @@ mod tests { #[test] fn test_remove_7_regression() { - let vec: Vec = vec![280, 606, 163, 857, 436, 508, 44, 801]; + let vec = [280, 606, 163, 857, 436, 508, 44, 801]; let mut map: TreeMap = TreeMap::new(next_trie_id()); @@ -1377,8 +1376,8 @@ mod tests { #[test] fn test_insert_8_remove_4_regression() { - let insert = vec![882, 398, 161, 76]; - let remove = vec![242, 687, 860, 811]; + let insert = [882, 398, 161, 76]; + let remove = [242, 687, 860, 811]; let mut map: TreeMap = TreeMap::new(next_trie_id()); @@ -1443,8 +1442,8 @@ mod tests { #[test] fn test_insert_2_remove_2_regression() { - let ins: Vec = vec![11760225, 611327897]; - let rem: Vec = vec![2982517385, 1833990072]; + let ins = [11760225, 611327897]; + let rem = [2982517385, 1833990072]; let mut map: TreeMap = TreeMap::new(next_trie_id()); map.insert(&ins[0], &1); @@ -1581,8 +1580,8 @@ mod tests { fn test_iter_from() { let mut map: TreeMap = TreeMap::new(next_trie_id()); - let one: Vec = vec![10, 20, 30, 40, 50]; - let two: Vec = vec![45, 35, 25, 15, 5]; + let one = [10, 20, 30, 40, 50]; + let two = [45, 35, 25, 15, 5]; for x in &one { map.insert(x, &42); @@ -1625,8 +1624,8 @@ mod tests { fn test_iter_rev_from() { let mut map: TreeMap = TreeMap::new(next_trie_id()); - let one: Vec = vec![10, 20, 30, 40, 50]; - let two: Vec = vec![45, 35, 25, 15, 5]; + let one = [10, 20, 30, 40, 50]; + let two = [45, 35, 25, 15, 5]; for x in &one { map.insert(x, &42); @@ -1663,8 +1662,8 @@ mod tests { fn test_range() { let mut map: TreeMap = TreeMap::new(next_trie_id()); - let one: Vec = vec![10, 20, 30, 40, 50]; - let two: Vec = vec![45, 35, 25, 15, 5]; + let one = [10, 20, 30, 40, 50]; + let two = [45, 35, 25, 15, 5]; for x in &one { map.insert(x, &42); @@ -1760,8 +1759,8 @@ mod tests { #[test] fn test_balance_regression_1() { - let insert = vec![(2, 0), (3, 0), (4, 0)]; - let remove = vec![0, 0, 0, 1]; + let insert = [(2, 0), (3, 0), (4, 0)]; + let remove = [0, 0, 0, 1]; let map = avl(&insert, &remove); assert!(is_balanced(&map, map.root)); @@ -1769,8 +1768,8 @@ mod tests { #[test] fn test_balance_regression_2() { - let insert = vec![(1, 0), (2, 0), (0, 0), (3, 0), (5, 0), (6, 0)]; - let remove = vec![0, 0, 0, 3, 5, 6, 7, 4]; + let insert = [(1, 0), (2, 0), (0, 0), (3, 0), (5, 0), (6, 0)]; + let remove = [0, 0, 0, 3, 5, 6, 7, 4]; let map = avl(&insert, &remove); assert!(is_balanced(&map, map.root)); diff --git a/near-sdk/src/store/tree_map/mod.rs b/near-sdk/src/store/tree_map/mod.rs index 38c3d8b82..80be8eb6c 100644 --- a/near-sdk/src/store/tree_map/mod.rs +++ b/near-sdk/src/store/tree_map/mod.rs @@ -1225,7 +1225,7 @@ mod tests { #[test] fn test_lower() { let mut map: TreeMap = TreeMap::new(next_trie_id()); - let vec: Vec = vec![10, 20, 30, 40, 50]; + let vec = [10, 20, 30, 40, 50]; for x in vec.into_iter() { map.insert(x, 1); @@ -1245,7 +1245,7 @@ mod tests { #[test] fn test_higher() { let mut map: TreeMap = TreeMap::new(next_trie_id()); - let vec: Vec = vec![10, 20, 30, 40, 50]; + let vec = [10, 20, 30, 40, 50]; for x in vec.into_iter() { map.insert(x, 1); @@ -1265,7 +1265,7 @@ mod tests { #[test] fn test_floor_key() { let mut map: TreeMap = TreeMap::new(next_trie_id()); - let vec: Vec = vec![10, 20, 30, 40, 50]; + let vec = [10, 20, 30, 40, 50]; for x in vec.into_iter() { map.insert(x, 1); @@ -1285,7 +1285,7 @@ mod tests { #[test] fn test_ceil_key() { let mut map: TreeMap = TreeMap::new(next_trie_id()); - let vec: Vec = vec![10, 20, 30, 40, 50]; + let vec = [10, 20, 30, 40, 50]; for x in vec.into_iter() { map.insert(x, 1); @@ -1322,7 +1322,7 @@ mod tests { #[test] fn test_remove_3_desc() { - let vec: Vec = vec![3, 2, 1]; + let vec = [3, 2, 1]; let mut map: TreeMap = TreeMap::new(next_trie_id()); for x in &vec { @@ -1341,7 +1341,7 @@ mod tests { #[test] fn test_remove_3_asc() { - let vec: Vec = vec![1, 2, 3]; + let vec = [1, 2, 3]; let mut map: TreeMap = TreeMap::new(next_trie_id()); for x in &vec { @@ -1361,8 +1361,8 @@ mod tests { #[test] fn test_remove_7_regression_1() { - let vec: Vec = - vec![2104297040, 552624607, 4269683389, 3382615941, 155419892, 4102023417, 1795725075]; + let vec = + [2104297040, 552624607, 4269683389, 3382615941, 155419892, 4102023417, 1795725075]; let mut map: TreeMap = TreeMap::new(next_trie_id()); for x in &vec { @@ -1383,8 +1383,7 @@ mod tests { #[test] fn test_remove_7_regression_2() { - let vec: Vec = - vec![700623085, 87488544, 1500140781, 1111706290, 3187278102, 4042663151, 3731533080]; + let vec = [700623085, 87488544, 1500140781, 1111706290, 3187278102, 4042663151, 3731533080]; let mut map: TreeMap = TreeMap::new(next_trie_id()); for x in &vec { @@ -1403,7 +1402,7 @@ mod tests { #[test] fn test_remove_9_regression() { - let vec: Vec = vec![ + let vec = [ 1186903464, 506371929, 1738679820, 1883936615, 1815331350, 1512669683, 3581743264, 1396738166, 1902061760, ]; @@ -1425,7 +1424,7 @@ mod tests { #[test] fn test_remove_20_regression_1() { - let vec: Vec = vec![ + let vec = [ 552517392, 3638992158, 1015727752, 2500937532, 638716734, 586360620, 2476692174, 1425948996, 3608478547, 757735878, 2709959928, 2092169539, 3620770200, 783020918, 1986928932, 200210441, 1972255302, 533239929, 497054557, 2137924638, @@ -1448,7 +1447,7 @@ mod tests { #[test] fn test_remove_7_regression() { - let vec: Vec = vec![280, 606, 163, 857, 436, 508, 44, 801]; + let vec = [280, 606, 163, 857, 436, 508, 44, 801]; let mut map: TreeMap = TreeMap::new(next_trie_id()); @@ -1471,8 +1470,8 @@ mod tests { #[test] fn test_insert_8_remove_4_regression() { - let insert = vec![882, 398, 161, 76]; - let remove = vec![242, 687, 860, 811]; + let insert = [882, 398, 161, 76]; + let remove = [242, 687, 860, 811]; let mut map: TreeMap = TreeMap::new(next_trie_id()); @@ -1537,8 +1536,8 @@ mod tests { #[test] fn test_insert_2_remove_2_regression() { - let ins: Vec = vec![11760225, 611327897]; - let rem: Vec = vec![2982517385, 1833990072]; + let ins = [11760225, 611327897]; + let rem = [2982517385, 1833990072]; let mut map: TreeMap = TreeMap::new(next_trie_id()); map.insert(ins[0], 1); @@ -1661,8 +1660,8 @@ mod tests { fn test_iter_from() { let mut map: TreeMap = TreeMap::new(next_trie_id()); - let one: Vec = vec![10, 20, 30, 40, 50]; - let two: Vec = vec![45, 35, 25, 15, 5]; + let one = [10, 20, 30, 40, 50]; + let two = [45, 35, 25, 15, 5]; for x in &one { map.insert(*x, 42); @@ -1700,8 +1699,8 @@ mod tests { fn test_iter_rev_from() { let mut map: TreeMap = TreeMap::new(next_trie_id()); - let one: Vec = vec![10, 20, 30, 40, 50]; - let two: Vec = vec![45, 35, 25, 15, 5]; + let one = [10, 20, 30, 40, 50]; + let two = [45, 35, 25, 15, 5]; for x in &one { map.insert(*x, 42); @@ -1738,8 +1737,8 @@ mod tests { fn test_range() { let mut map: TreeMap = TreeMap::new(next_trie_id()); - let one: Vec = vec![10, 20, 30, 40, 50]; - let two: Vec = vec![45, 35, 25, 15, 5]; + let one = [10, 20, 30, 40, 50]; + let two = [45, 35, 25, 15, 5]; for x in &one { map.insert(*x, 42); @@ -1814,8 +1813,8 @@ mod tests { #[test] fn test_balance_regression_1() { - let insert = vec![(2, 0), (3, 0), (4, 0)]; - let remove = vec![0, 0, 0, 1]; + let insert = [(2, 0), (3, 0), (4, 0)]; + let remove = [0, 0, 0, 1]; let map = avl(&insert, &remove); assert!(is_balanced(&map, map.tree.root.unwrap())); @@ -1823,8 +1822,8 @@ mod tests { #[test] fn test_balance_regression_2() { - let insert = vec![(1, 0), (2, 0), (0, 0), (3, 0), (5, 0), (6, 0)]; - let remove = vec![0, 0, 0, 3, 5, 6, 7, 4]; + let insert = [(1, 0), (2, 0), (0, 0), (3, 0), (5, 0), (6, 0)]; + let remove = [0, 0, 0, 3, 5, 6, 7, 4]; let map = avl(&insert, &remove); assert!(is_balanced(&map, map.tree.root.unwrap()));