Skip to content

chore: rust 2024 #2482

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .rustfmt.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ trailing_comma = "Vertical"
match_block_trailing_comma = false
blank_lines_upper_bound = 1
blank_lines_lower_bound = 0
edition = "2021" # changed
style_edition = "2021"
edition = "2024" # changed
style_edition = "2024"
merge_derives = true
use_try_shorthand = true # changed
use_field_init_shorthand = true # changed
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ exclude = [
[workspace.package]
authors = ["Use Ink <[email protected]>"]
categories = ["no-std", "embedded"]
edition = "2021"
edition = "2024"
homepage = "https://use.ink"
keywords = ["polkavm", "ink", "riscv", "blockchain", "edsl"]
license = "Apache-2.0"
Expand Down
2 changes: 1 addition & 1 deletion crates/env/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name = "ink_env"
version.workspace = true
authors = ["Use Ink <[email protected]>"]
edition.workspace = true
rust-version = "1.81"
rust-version = "1.85"

license.workspace = true
readme = "README.md"
Expand Down
2 changes: 2 additions & 0 deletions crates/env/src/engine/off_chain/test_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,8 @@ pub fn recorded_events() -> impl Iterator<Item = EmittedEvent> {
.engine
.get_emitted_events()
.map(|evt: ink_engine::test_api::EmittedEvent| evt.into())
.collect::<Vec<_>>()
.into_iter()
})
}

Expand Down
2 changes: 1 addition & 1 deletion crates/ink/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name = "ink"
version.workspace = true
authors = ["Use Ink <[email protected]>"]
edition.workspace = true
rust-version = "1.63"
rust-version = "1.85"

license.workspace = true
readme = "README.md"
Expand Down
116 changes: 81 additions & 35 deletions crates/ink/src/option_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,35 +12,6 @@
// See the License for the specific language governing permissions and
// limitations under the License.

pub struct AsOption<'lt, T>(pub &'lt T);

impl<T> AsOption<'_, ::core::option::Option<T>> {
#[inline]
// We need to allow for dead code at this point because
// the Rust compiler thinks this function is unused even
// though it acts as the specialized case for detection.
#[allow(dead_code)]
pub fn value(&self) -> Option<&T> {
self.0.as_ref()
}
}

impl<'lt, T> AsOption<'lt, &'lt ::core::option::Option<T>> {
#[inline]
pub fn value(&self) -> Option<&T> {
self.0.as_ref()
}
}

pub trait AsOptionFallback<T> {
fn value(&self) -> Option<&T>;
}
impl<T> AsOptionFallback<T> for AsOption<'_, T> {
#[inline]
fn value(&self) -> Option<&T> {
Some(self.0)
}
}

/// Evaluates to `None` if the given expression is a `Option::None`.
///
Expand All @@ -50,10 +21,29 @@ impl<T> AsOptionFallback<T> for AsOption<'_, T> {
#[macro_export]
#[doc(hidden)]
macro_rules! as_option {
( $e:expr $(,)? ) => {{
#[allow(unused_imports)]
use $crate::option_info::AsOptionFallback as _;
$crate::option_info::AsOption(&$e).value()
(None $(,)?) => {{ None }};
(&None $(,)?) => {{ None }};
(Option::<$t:ty>::None $(,)?) => {{
None::<&$t>
}};
(&Option::<$t:ty>::None $(,)?) => {{
None::<&$t>
}};

// Special case for Some literals
(Some($val:expr) $(,)?) => {{
Some(&$val)
}};
(&Some($val:expr) $(,)?) => {{
Some(&$val)
}};

( &$local:expr $(,)? ) => {{
Some(&$local)
}};

( $local:expr $(,)? ) => {{
Some(&$local)
}};
}

Expand All @@ -64,14 +54,70 @@ mod tests {
assert_eq!(Some(&true), as_option!(true));
assert_eq!(Some(&42), as_option!(42));
assert_eq!(Some(&"Hello, World!"), as_option!("Hello, World!"));

assert_eq!(Some(&()), as_option!(Some(())));
assert_eq!(Some(&5), as_option!(Some(5)));
assert_eq!(Some(&true), as_option!(Some(true)));
assert_eq!(Some(&true), as_option!(&Some(true)));

assert_eq!(None, as_option!(Option::<u32>::None));
assert_eq!(None, as_option!(Option::<bool>::None));
assert_eq!(None, as_option!(&Option::<bool>::None));
}

#[test]
fn struct_fields_and_metadata_work() {
struct TestStruct {
field_1: u32,
field_2: u64,
}

let test = TestStruct {
field_1: 1,
field_2: 2,
};

assert_eq!(Some(&test.field_1), as_option!(test.field_1));
assert_eq!(Some(&test.field_1), as_option!(&test.field_1));
assert_eq!(Some(&test.field_2), as_option!(test.field_2));
assert_eq!(Some(&test.field_2), as_option!(&test.field_2));

// This simulates the event_metadata.rs case that was failing
#[derive(Debug)]
struct EventField {
value: u64,
}

// Test with temporary struct and field access - critical for Rust 2024
let field_ref = as_option!(EventField { value: 123 }.value);
assert_eq!(Some(&123), field_ref);
}

#[test]
fn event_stable_field_pattern_works() {
// This test simulates the exact pattern used in the event macro
// where a field is bound to a variable and then wrapped in as_option

struct EventStruct {
field_1: u32,
field_2: u64,
}

let event = EventStruct {
field_1: 42,
field_2: 100,
};

// This is how fields are processed in the event macro:
let stable_field = event.field_1;
assert_eq!(Some(&42), as_option!(stable_field));

// Test with normal field access
assert_eq!(Some(&100), as_option!(event.field_2));

// Test with temporary values
let get_value = || 123;
let stable_field_2 = get_value();
assert_eq!(Some(&123), as_option!(stable_field_2));
}
}
2 changes: 1 addition & 1 deletion crates/ink/tests/events_metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ mod contract {
#[cfg(test)]
mod tests {
fn generate_metadata() -> ink_metadata::InkProject {
extern "Rust" {
unsafe extern "Rust" {
fn __ink_generate_metadata() -> ink_metadata::InkProject;
}

Expand Down
2 changes: 1 addition & 1 deletion crates/ink/tests/return_type_metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ mod tests {
};

fn generate_metadata() -> ink_metadata::InkProject {
extern "Rust" {
unsafe extern "Rust" {
fn __ink_generate_metadata() -> ink_metadata::InkProject;
}

Expand Down
2 changes: 1 addition & 1 deletion integration-tests/public/call-runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
name = "call-runtime"
version = "6.0.0-alpha"
authors = ["Use Ink <[email protected]>"]
edition = "2021"
edition = "2024"
publish = false

[dependencies]
Expand Down
2 changes: 1 addition & 1 deletion integration-tests/public/combined-extension/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
name = "combined_extension"
version = "6.0.0-alpha"
authors = ["Use Ink <[email protected]>"]
edition = "2021"
edition = "2024"
publish = false

[dependencies]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
name = "complex_storage_structures"
version = "6.0.0-alpha"
authors = ["Use Ink <[email protected]>"]
edition = "2021"
edition = "2024"
publish = false

[dependencies]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
name = "conditional-compilation"
version = "6.0.0-alpha"
authors = ["Use Ink <[email protected]>"]
edition = "2021"
edition = "2024"

[dependencies]
ink = { path = "../../../crates/ink", default-features = false, features = ["unstable-hostfn"] }
Expand Down
2 changes: 1 addition & 1 deletion integration-tests/public/contract-invocation/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "instantiate_contract"
version = "0.1.0"
edition = "2021"
edition = "2024"
authors = ["Víctor M. González <[email protected]>"]

[lib]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "contract1"
version = "0.1.0"
edition = "2021"
edition = "2024"
authors = ["Víctor M. González <[email protected]>"]

[lib]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "contract2"
version = "0.1.0"
edition = "2021"
edition = "2024"
authors = ["Víctor M. González <[email protected]>"]

[lib]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "virtual_contract"
version = "0.1.0"
edition = "2021"
edition = "2024"
authors = ["Víctor M. González <[email protected]>"]

[lib]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "virtual_contract_ver1"
version = "0.1.0"
edition = "2021"
edition = "2024"
authors = ["Víctor M. González <[email protected]>"]

[lib]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "virtual_contract_ver2"
version = "0.1.0"
edition = "2021"
edition = "2024"
authors = ["Víctor M. González <[email protected]>"]

[lib]
Expand Down
2 changes: 1 addition & 1 deletion integration-tests/public/contract-storage/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
name = "contract-storage"
version = "6.0.0-alpha"
authors = ["Use Ink <[email protected]>"]
edition = "2021"
edition = "2024"
publish = false

[dependencies]
Expand Down
2 changes: 1 addition & 1 deletion integration-tests/public/contract-terminate/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
name = "contract_terminate"
version = "6.0.0-alpha"
authors = ["Use Ink <[email protected]>"]
edition = "2021"
edition = "2024"
publish = false

[dependencies]
Expand Down
2 changes: 1 addition & 1 deletion integration-tests/public/contract-transfer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
name = "contract_transfer"
version = "6.0.0-alpha"
authors = ["Use Ink <[email protected]>"]
edition = "2021"
edition = "2024"
publish = false

[dependencies]
Expand Down
2 changes: 1 addition & 1 deletion integration-tests/public/contract-xcm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
name = "contract-xcm"
version = "6.0.0-alpha"
authors = ["Use Ink <[email protected]>"]
edition = "2021"
edition = "2024"
publish = false

[dependencies]
Expand Down
2 changes: 1 addition & 1 deletion integration-tests/public/cross-contract-calls/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
name = "cross-contract-calls"
version = "6.0.0-alpha"
authors = ["Use Ink <[email protected]>"]
edition = "2021"
edition = "2024"
publish = false

[dependencies]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
name = "other-contract"
version = "6.0.0-alpha"
authors = ["Use Ink <[email protected]>"]
edition = "2021"
edition = "2024"
publish = false

[dependencies]
Expand Down
2 changes: 1 addition & 1 deletion integration-tests/public/custom-allocator/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
name = "custom-allocator"
version = "6.0.0-alpha"
authors = ["Use Ink <[email protected]>"]
edition = "2021"
edition = "2024"
publish = false

[dependencies]
Expand Down
2 changes: 1 addition & 1 deletion integration-tests/public/custom-environment/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
name = "custom-environment"
version = "6.0.0-alpha"
authors = ["Use Ink <[email protected]>"]
edition = "2021"
edition = "2024"
publish = false

[dependencies]
Expand Down
2 changes: 1 addition & 1 deletion integration-tests/public/debugging-strategies/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name = "debugging_strategies"
description = "Illustrates different debugging strategies"
version = "6.0.0-alpha"
authors = ["Use Ink <[email protected]>"]
edition = "2021"
edition = "2024"
publish = false

[dependencies]
Expand Down
2 changes: 1 addition & 1 deletion integration-tests/public/dns/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
name = "dns"
version = "6.0.0-alpha"
authors = ["Use Ink <[email protected]>"]
edition = "2021"
edition = "2024"
publish = false

[dependencies]
Expand Down
2 changes: 1 addition & 1 deletion integration-tests/public/e2e-call-runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
name = "e2e_call_runtime"
version = "6.0.0-alpha"
authors = ["Use Ink <[email protected]>"]
edition = "2021"
edition = "2024"
publish = false

[dependencies]
Expand Down
Loading
Loading