From 16cdd17c191ec3789c14d2ee2d20fa6f75a5f598 Mon Sep 17 00:00:00 2001 From: Leigh McCulloch <351529+leighmcculloch@users.noreply.github.com> Date: Fri, 12 Jul 2024 08:18:43 +1000 Subject: [PATCH] Add nice compiler error when Copy is missing on error enums and enum ints (#1293) ### What Add nice compiler error when Copy is missing on error enums and enum ints. ### Why The existing compiler error is not very intuitive. The existing error will still be outputted but a more direct error describing the problem and the fix will be output first. #### Before ``` error[E0507]: cannot move out of `*val` which is behind a shared reference --> tests/errors/src/lib.rs:15:1 | 15 | #[contracterror] | ^^^^^^^^^^^^^^^^ move occurs because `*val` has type `Error`, which does not implement the `Copy` trait | = note: this error originates in the attribute macro `contracterror` (in Nightly builds, run with -Z macro-backtrace for more info) help: consider cloning the value if the performance cost is acceptable | 15 | #[contracterror].clone() | ++++++++ ``` #### After ``` error[E0277]: the trait bound `Error: core::marker::Copy` is not satisfied --> tests/errors/src/lib.rs:15:1 | 15 | #[contracterror] | ^^^^^^^^^^^^^^^^ the trait `core::marker::Copy` is not implemented for `Error` | help: consider annotating `Error` with `#[derive(Copy)]` | 17 + #[derive(Copy)] 18 | pub enum Error { | error[E0507]: cannot move out of `*val` which is behind a shared reference --> tests/errors/src/lib.rs:15:1 | 15 | #[contracterror] | ^^^^^^^^^^^^^^^^ move occurs because `*val` has type `Error`, which does not implement the `Copy` trait | = note: this error originates in the attribute macro `contracterror` (in Nightly builds, run with -Z macro-backtrace for more info) help: consider cloning the value if the performance cost is acceptable | 15 | #[contracterror].clone() | ++++++++ ``` Close #630 --- soroban-sdk-macros/src/derive_enum_int.rs | 1 + soroban-sdk-macros/src/derive_error_enum_int.rs | 1 + 2 files changed, 2 insertions(+) diff --git a/soroban-sdk-macros/src/derive_enum_int.rs b/soroban-sdk-macros/src/derive_enum_int.rs index d38baa5b6..6eaf4ec27 100644 --- a/soroban-sdk-macros/src/derive_enum_int.rs +++ b/soroban-sdk-macros/src/derive_enum_int.rs @@ -96,6 +96,7 @@ pub fn derive_type_enum_int( // Output. quote! { #spec_gen + const _: () = { fn assert_copy(v: #enum_ident) -> impl Copy { v } }; impl #path::TryFromVal<#path::Env, #path::Val> for #enum_ident { type Error = #path::ConversionError; diff --git a/soroban-sdk-macros/src/derive_error_enum_int.rs b/soroban-sdk-macros/src/derive_error_enum_int.rs index 1e65088b0..bfa45f301 100644 --- a/soroban-sdk-macros/src/derive_error_enum_int.rs +++ b/soroban-sdk-macros/src/derive_error_enum_int.rs @@ -92,6 +92,7 @@ pub fn derive_type_error_enum_int( // Output. quote! { #spec_gen + const _: () = { fn assert_copy(v: #enum_ident) -> impl Copy { v } }; impl TryFrom<#path::Error> for #enum_ident { type Error = #path::Error;