Skip to content

Commit

Permalink
Emit error with derive on unsupported item (#7128)
Browse files Browse the repository at this point in the history
  • Loading branch information
Draggu authored Jan 21, 2025
1 parent d65802d commit cdd3176
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 4 deletions.
15 changes: 14 additions & 1 deletion crates/cairo-lang-plugins/src/plugins/derive/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
};
}
})
}

Expand Down
18 changes: 15 additions & 3 deletions crates/cairo-lang-plugins/src/test_data/derive
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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]
^^^^^^^^^^

0 comments on commit cdd3176

Please sign in to comment.