Add event registration via emits attribute#18
Merged
Conversation
…tection Add #[contract(emits = [...])] for manual event registration on trait methods with default implementations. Add compile error for public &mut self methods that emit no events, suppressible with #[contract(no_event)]. Detect variable identifiers used as abi::emit() topics (warning pending proc_macro_diagnostic stabilisation).
Add #[contract(emits = [...])] to Ownable trait methods and #[contract(no_event)] to init. Add schema test verifying ownership events appear via manual registration.
22c2162 to
dca0d73
Compare
a0e87ce to
736bcc8
Compare
HDauven
approved these changes
Apr 13, 2026
Member
HDauven
left a comment
There was a problem hiding this comment.
LGTM
One follow-up we should still do is the helper/core delegation case. We already have that in DuskEVM dispute-game hubs: resolve() in
https://github.com/dusk-network/duskevm-contracts/blob/main/contracts/permissioned-dispute-game-hub/src/lib.rs#L780-L781
just delegates into core, while the actual abi::emit(...) calls happen in
https://github.com/dusk-network/duskevm-contracts/blob/main/core/src/dispute_game_hub/engine.rs#L478-L482
and
https://github.com/dusk-network/duskevm-contracts/blob/main/core/src/dispute_game_hub/engine.rs#L765-L769
Forge cannot infer/register helper events like this for the contract data-driver at this point.
Previously `#[contract(emits = [...])]` was collected and validated only on trait-impl methods; on inherent methods it was silently ignored and the strict mutating-method check hardcoded `has_manual_events` to false. This left no way to declare events for inherent methods that delegate to a helper outside the impl block (the body scanner cannot see the helper's `abi::emit` calls), so such methods broke the strict check unless the author fell back to `no_event` — which drops the events from the schema. Treat `emits` as a property of "this method declares additional events" regardless of where the method is defined. Both the trait and inherent collection paths share an `impl_method_emits` helper that differs only in the inclusion predicate.
Test scaffolding for the inherent-method `emits` path: a free function that calls `abi::emit` from outside any contract impl block, exercising the "emit invisible to the macro's body scanner" property.
Add `bump_tally`, an inherent `&mut self` method that delegates the event emit to `helpers::emit_tally_bumped` and declares the event via `#[contract(emits = [...])]`. Verify both the schema entry and the runtime event emission.
`#[allow(dead_code)]` was masking two genuine issues in the shared `test_session.rs` module: - `assert_contract_panic` is unused in every test binary; remove it. - `direct_call` and `feeder_call` are only used by the contract test binary, but were declared on the shared `TestSession` and warned in the schema binary. Move them into a binary-local impl block in contract.rs so each binary only sees what it actually exercises.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Add manual event registration for the
#[contract]macro via three related features:#[contract(emits = [...])]— method-level attribute that declares events emitted by trait default implementations (invisible to the macro's body scanner). Events listed inemitsare included in the contract schema alongside those discovered fromabi::emit()calls.&mut selfmethods must now emit events or declare them viaemits. Use#[contract(no_event)]to suppress the check for methods likeinitthat intentionally skip emission.abi::emit()topics (likely variables whose runtime value the macro can't capture). Warning is a no-op on stable Rust pendingproc_macro_diagnosticstabilisation.Stacks on #17.