Skip to content

Add validation for the signatures of functions annotated with #[event(fetch)], #[event(start)] etc.#940

Open
jakubadamw wants to merge 3 commits intocloudflare:mainfrom
jakubadamw:validate-event-annotated-function-signatures
Open

Add validation for the signatures of functions annotated with #[event(fetch)], #[event(start)] etc.#940
jakubadamw wants to merge 3 commits intocloudflare:mainfrom
jakubadamw:validate-event-annotated-function-signatures

Conversation

@jakubadamw
Copy link
Contributor

This addresses an old TODO by having the signatures for user-defined event handlers be validated at the proc macro expansion stage to avoid having the errors crop up during type check of the expanded code in a form that wasn't exactly always very clear, like so:

error[E0061]: this function takes 1 argument but 0 arguments were supplied
 --> tests/ui/start-wrong-param-count.rs:4:4
  |
3 | #[event(start)]
  | --------------- argument #1 of type `u32` is missing
4 | fn start(a: u32) {}
  |    ^^^^^
  |

WIth this change the macro reports:

error: custom attribute panicked
 --> tests/ui/start-wrong-param-count.rs:3:1
  |
3 | #[event(start)]
  | ^^^^^^^^^^^^^^^
  |
  = help: message: the `start` handler expects 0 argument(s) but found 1

I also added a few tests using the trybuild crate.

Add compile-time validation for all four event handler types to catch
signature mismatches early with clear error messages:
- fetch: must be async, exactly 3 params (request, env, ctx)
- scheduled: must be async, exactly 3 params (event, env, ctx)
- queue: must be async, exactly 3 params (message_batch, env, ctx)
- start: must not be async, exactly 0 params
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant