From cdd317675179bf6849a526479dc91e41608d684b Mon Sep 17 00:00:00 2001 From: Piotr Figiela <77412592+Draggu@users.noreply.github.com> Date: Tue, 21 Jan 2025 15:33:01 +0100 Subject: [PATCH] Emit error with derive on unsupported item (#7128) --- .../src/plugins/derive/mod.rs | 15 ++++++++++++++- crates/cairo-lang-plugins/src/test_data/derive | 18 +++++++++++++++--- 2 files changed, 29 insertions(+), 4 deletions(-) diff --git a/crates/cairo-lang-plugins/src/plugins/derive/mod.rs b/crates/cairo-lang-plugins/src/plugins/derive/mod.rs index bc4bf8ee14d..5a2f288d077 100644 --- a/crates/cairo-lang-plugins/src/plugins/derive/mod.rs +++ b/crates/cairo-lang-plugins/src/plugins/derive/mod.rs @@ -58,7 +58,20 @@ impl MacroPlugin for DerivePlugin { extern_type_ast.generic_params(db), TypeVariantInfo::Extern, ), - _ => return PluginResult::default(), + _ => { + let maybe_error = item_ast.find_attr(db, DERIVE_ATTR).map(|derive_attr| { + vec![PluginDiagnostic::error( + derive_attr.as_syntax_node().stable_ptr(), + "`derive` may only be applied to `struct`s, `enum`s and `extern type`s" + .to_string(), + )] + }); + + return PluginResult { + diagnostics: maybe_error.unwrap_or_default(), + ..PluginResult::default() + }; + } }) } diff --git a/crates/cairo-lang-plugins/src/test_data/derive b/crates/cairo-lang-plugins/src/test_data/derive index c3e1a36cd83..16f10f7ea91 100644 --- a/crates/cairo-lang-plugins/src/test_data/derive +++ b/crates/cairo-lang-plugins/src/test_data/derive @@ -411,6 +411,9 @@ struct UnsupportedTrait {} #[derive(long::path)] struct NonSimplePath {} +#[derive(Copy, Debug, Destruct, PanicDestruct, Default, Hash)] +fn unsupportedItem() {} + #[derive(Clone)] extern type NotCloneable; @@ -441,6 +444,9 @@ struct UnsupportedTrait {} #[derive(long::path)] struct NonSimplePath {} +#[derive(Copy, Debug, Destruct, PanicDestruct, Default, Hash)] +fn unsupportedItem() {} + #[derive(Clone)] extern type NotCloneable; @@ -488,19 +494,25 @@ error: Unknown derive `long::path` - a plugin might be missing. ^^^^^^^^^^ +error: `derive` may only be applied to `struct`s, `enum`s and `extern type`s + --> test_src/lib.cairo:13:1 +#[derive(Copy, Debug, Destruct, PanicDestruct, Default, Hash)] +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + + error: Unsupported trait for derive for extern types. - --> test_src/lib.cairo:13:10 + --> test_src/lib.cairo:16:10 #[derive(Clone)] ^^^^^ error: derive `Default` for enum only supported with a default variant. - --> test_src/lib.cairo:16:10 + --> test_src/lib.cairo:19:10 #[derive(Default)] ^^^^^^^ error: Multiple variants annotated with `#[default]` - --> test_src/lib.cairo:26:5 + --> test_src/lib.cairo:29:5 #[default] ^^^^^^^^^^