Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

event_created_child macro can cause downcast error if constants are not imported #623

Open
i509VCB opened this issue May 18, 2023 · 0 comments

Comments

@i509VCB
Copy link
Member

i509VCB commented May 18, 2023

The example for the macro is the following:

event_created_child!(MyState, WlFoo, [
    EVT_CREATE_BAR => (WlBar, BarUserData::new()),
]);

And expands to the following:

fn event_created_child(
    opcode: u16,
    qhandle: &QueueHandle<$selftype>
) -> std::sync::Arc<dyn ObjectData> {
    match opcode {
        EVT_CREATE_BAR => {
            qhandle.make_data::<WlBar, _>(BarUserData::new())
        },
        _ => {
            panic!("Missing event_created_child specialization for event opcode {} of {}", opcode, <$iface as $crate::Proxy>::interface().name);
        },
    }
}

However if the EVT_CREATE_BAR constant is not imported, rustc will treat the EVT_CREATE_BAR identifier as the name of the variable to pass through to the default branch.

Typically you would do that in Rust for something like this:

match value {
    0 => println!("Zero"),
    1 => println!("One"),
    x => println!("Something else: {}"),
}

This is able to be detected if the unused_variable lints (in my own code):

warning: unused variable: `EVT_TOPLEVEL_OPCODE`
  --> wm/src/foreign_toplevel.rs:65:9
   |
65 |         EVT_TOPLEVEL_OPCODE => (ExtForeignToplevelHandleV1,
   |         ^^^^^^^^^^^^^^^^^^^ help: if this is intentional, prefix it with an underscore: `_EVT_TOPLEVEL_OPCODE`
   |
   = note: `#[warn(unused_variables)]` on by default

With a single match this is a very small problem, but with multiple match statements you will get a downcast error later in wayland-backend.

I think we should try to change the macro to make this error impossible or find an alternative to event_created_child.

@i509VCB i509VCB changed the title event_created_child macro can cause error without importing constants event_created_child macro can cause downcast error if constants are not imported May 18, 2023
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

No branches or pull requests

1 participant