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

Option<T> parameters generated by a macro call are not recognized #317

Open
campbellcole opened this issue Oct 30, 2024 · 0 comments
Open

Comments

@campbellcole
Copy link

I have a use case where I want users of my bot to be able to specify the default_member_permissions field for commands considered "admin only". I didn't want to reinvent the poise::command macro, so I wrote the following macro:

macro_rules! command {
    (
        $is_admin:literal;
        $(#[$attr:meta])*
        pub async fn $name:ident($($arg:ident: $arg_ty:ty),*) -> Result<$ret_ty:ty> $body:block
    ) => {
        pub fn command(config: & $crate::config::AppConfig) -> ::poise::Command<::std::sync::Arc<$crate::Data>, ::color_eyre::eyre::Report> {
            let mut cmd = $name();

            if $is_admin {
                cmd.default_member_permissions = *config.admin_permissions;
            }

            cmd
        }

        $(#[$attr])*
        #[::poise::command(slash_command)]
        async fn $name($($arg: $arg_ty),*) -> ::color_eyre::eyre::Result<$ret_ty> $body
    };
}

However, when invoking this macro with any function arguments that are Option<T>, the poise::command macro no longer recognizes Option and considers Option<T> as a distinct type.

For example:

use poise::serenity_prelude::User;

use crate::Context;

super::command! {
    true;
    pub async fn poc(ctx: Context<'_>, example: Option<User>) -> Result<()> {
        println!("demo user = {:?}", example);

        Ok(())
    }
}

produces the following compile error:

error[E0599]: the method `create` exists for reference `&&PhantomData<Option<User>>`, but its trait bounds were not satisfied
   --> src/command/poc.rs:5:1
    |
5   | / super::command! {
6   | |     true;
7   | |     pub async fn poc(ctx: Context<'_>, example: Option<User>) -> Result<()> {
8   | |         println!("demo user = {:?}", example);
...   |
11  | |     }
12  | | }
    | |_^ method cannot be called on `&&PhantomData<Option<User>>` due to unsatisfied trait bounds
    |
   ::: /nix/store/yv81f5ak2ywr02nryl52mpngii3sc06z-rust-default-1.84.0-nightly-2024-10-28/lib/rustlib/src/rust/library/core/src/option.rs:571:1
    |
571 |   pub enum Option<T> {
    |   ------------------ doesn't satisfy `_: ArgumentConvert`, `_: FromStr` or `_: SlashArgument`
    |
   ::: /nix/store/yv81f5ak2ywr02nryl52mpngii3sc06z-rust-default-1.84.0-nightly-2024-10-28/lib/rustlib/src/rust/library/core/src/marker.rs:753:1
    |
753 |   pub struct PhantomData<T: ?Sized>;
    |   --------------------------------- doesn't satisfy `_: SlashArgumentHack<Option<User>>`
    |
    = note: the following trait bounds were not satisfied:
            `std::option::Option<poise::serenity_prelude::User>: poise::SlashArgument`
            which is required by `&std::marker::PhantomData<std::option::Option<poise::serenity_prelude::User>>: poise::SlashArgumentHack<std::option::Option<poise::serenity_prelude::User>>`
            `std::option::Option<poise::serenity_prelude::User>: poise::serenity_prelude::ArgumentConvert`
            which is required by `std::marker::PhantomData<std::option::Option<poise::serenity_prelude::User>>: poise::SlashArgumentHack<std::option::Option<poise::serenity_prelude::User>>`
            `std::option::Option<poise::serenity_prelude::User>: std::str::FromStr`
            which is required by `std::marker::PhantomData<std::option::Option<poise::serenity_prelude::User>>: poise::SlashArgumentHack<std::option::Option<poise::serenity_prelude::User>>`
    = note: this error originates in the macro `poise::create_slash_argument` which comes from the expansion of the macro `super::command` (in Nightly builds, run with -Z macro-backtrace for more info)

I poked around in the expanded code using cargo-expand, and saw that the argument type is being set to Option<T> instead of T, and required is being set to true instead of false.

I read through the code that parses the type and I can't figure out why this might be happening. I suspect this is actually a bug in Rust or syn whereby the macro_rules invocation is removing the distinction between generic and simple types but I feel it's right to post it here first and move elsewhere once it's scope has been determined.

Thank you

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