Cannot use #[doc(alias)] #308

Closed
opened 2023-03-13 10:29:57 -05:00 by pie-flavor · 3 comments
pie-flavor commented 2023-03-13 10:29:57 -05:00 (Migrated from github.com)

The following code:

bitflags::bitflags! {
  #[doc(alias = "SYMBOLIC_LINK_FLAGS")]
  pub struct SymbolicLinkFlags:u32 {
    #[doc(alias = "SYMBOLIC_LINK_FLAG_DIRECTORY")]
    const DIRECTORY = 0x1;
    #[doc(alias = "SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE")]
    const ALLOW_UNPRIVILEGED_CREATE = 0x2;
  }
}

Produces the error:

error: `#[doc(alias = "...")]` isn't allowed on expression
  --> src\fs.rs:67:15
   |
67 |         #[doc(alias = "SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE")]
   |               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
The following code: ```rs bitflags::bitflags! { #[doc(alias = "SYMBOLIC_LINK_FLAGS")] pub struct SymbolicLinkFlags:u32 { #[doc(alias = "SYMBOLIC_LINK_FLAG_DIRECTORY")] const DIRECTORY = 0x1; #[doc(alias = "SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE")] const ALLOW_UNPRIVILEGED_CREATE = 0x2; } } ``` Produces the error: ``` error: `#[doc(alias = "...")]` isn't allowed on expression --> src\fs.rs:67:15 | 67 | #[doc(alias = "SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE")] | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ```
KodrAus commented 2023-03-13 18:00:08 -05:00 (Migrated from github.com)

This is a general problem for attributes that can't be applied to expressions now. In methods like from_name we now generate code like this:

#[inline]
pub fn from_name(name: &str) -> ::bitflags::__private::core::option::Option<Self> {
    match name {
        #[doc(alias = "SYMBOLIC_LINK_FLAG_DIRECTORY")]
        "DIRECTORY" => ::bitflags::__private::core::option::Option::Some(Self {
            bits: SymbolicLinkFlags::DIRECTORY.bits(),
        }),
        #[doc(alias = "SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE")]
        "ALLOW_UNPRIVILEGED_CREATE" => {
            ::bitflags::__private::core::option::Option::Some(Self {
                bits: SymbolicLinkFlags::ALLOW_UNPRIVILEGED_CREATE.bits(),
            })
        }
        _ => ::bitflags::__private::core::option::Option::None,
    }
}

I think the quickest fix would be to introduce a helper macro that filtered out some attributes like #[doc(..)] for these match arms. Any more of these that come up in the future could be added to that macro.

This is a general problem for attributes that can't be applied to expressions now. In methods like `from_name` we now generate code like this: ```rust #[inline] pub fn from_name(name: &str) -> ::bitflags::__private::core::option::Option<Self> { match name { #[doc(alias = "SYMBOLIC_LINK_FLAG_DIRECTORY")] "DIRECTORY" => ::bitflags::__private::core::option::Option::Some(Self { bits: SymbolicLinkFlags::DIRECTORY.bits(), }), #[doc(alias = "SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE")] "ALLOW_UNPRIVILEGED_CREATE" => { ::bitflags::__private::core::option::Option::Some(Self { bits: SymbolicLinkFlags::ALLOW_UNPRIVILEGED_CREATE.bits(), }) } _ => ::bitflags::__private::core::option::Option::None, } } ``` I think the quickest fix would be to introduce a helper macro that filtered out some attributes like `#[doc(..)]` for these match arms. Any more of these that come up in the future could be added to that macro.
pie-flavor commented 2023-03-13 22:08:11 -05:00 (Migrated from github.com)

What kinds of attributes would be useful to apply there other than cfg?

What kinds of attributes would be useful to apply there other than `cfg`?
KodrAus commented 2023-04-02 20:44:02 -05:00 (Migrated from github.com)

I can't really think of any besides maybe #[allow], but we handle those on the item itself. I think it would be fair to flip this into an allow-list so only cfg attributes get propagated.

I can't really think of any besides maybe `#[allow]`, but we handle those on the item itself. I think it would be fair to flip this into an allow-list so only `cfg` attributes get propagated.
Sign in to join this conversation.
No labels
bug
good first issue
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
gh-bitflags/bitflags#308
No description provided.