pub(restricted) bitflags #72

Closed
opened 2017-03-07 16:22:20 -06:00 by dtolnay · 2 comments
dtolnay commented 2017-03-07 16:22:20 -06:00 (Migrated from github.com)

cc https://github.com/rust-lang/rust/issues/32409 - maybe no action is warranted until this feature is stable.

bitflags! {
    pub(super) flags Flags: u8 {
        const A = 1
    }
}
cc https://github.com/rust-lang/rust/issues/32409 - maybe no action is warranted until this feature is stable. ```rust bitflags! { pub(super) flags Flags: u8 { const A = 1 } } ```
kennytm commented 2017-06-13 03:07:11 -05:00 (Migrated from github.com)

pub(restricted) is now stable. But I think it is still blocked by :vis matcher (rust-lang/rust#41022) to allow a proper fix.


Currently the straightforward fix using :vis causes an error in example_generated.rs

error: local ambiguity: multiple parsing options: built-in NTs vis ('vis') or 1 other option.
  --> src/example_generated.rs:5:5
   |
4  | / bitflags! {
5  | |     /// This is the same `Flags` struct defined in the [crate level example](../index.html#example).
   | |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
6  | |     /// Note that this struct is just for documentation purposes only, it must not be used outside
7  | |     /// this crate.
...  |
15 | |     }
16 | | }
   | |_- in this macro invocation

error: Could not compile `bitflags`.
(diff)
diff --git a/src/lib.rs b/src/lib.rs
index 1694837..356fba1 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -8,6 +8,8 @@
 // option. This file may not be copied, modified, or distributed
 // except according to those terms.
 
+#![feature(macro_vis_matcher)]
+
 //! A typesafe bitmask flag generator useful for sets of C-style bitmask flags.
 //! It can be used for creating typesafe wrappers around C APIs.
 //!
@@ -305,16 +307,16 @@ pub extern crate core as _core;
 /// ```
 #[macro_export]
 macro_rules! bitflags {
-    ($(#[$attr:meta])* pub struct $BitFlags:ident: $T:ty {
+    ($(#[$attr:meta])* $vis:vis struct $BitFlags:ident: $T:ty {
         $($(#[$Flag_attr:meta])* const $Flag:ident = $value:expr;)+
     }) => {
         #[derive(Copy, PartialEq, Eq, Clone, PartialOrd, Ord, Hash)]
         $(#[$attr])*
-        pub struct $BitFlags {
+        $vis struct $BitFlags {
             bits: $T,
         }
 
-        $($(#[$Flag_attr])* pub const $Flag: $BitFlags = $BitFlags { bits: $value };)+
+        $($(#[$Flag_attr])* $vis const $Flag: $BitFlags = $BitFlags { bits: $value };)+
 
         __impl_bitflags! {
             struct $BitFlags: $T {
@@ -322,24 +324,6 @@ macro_rules! bitflags {
             }
         }
     };
-    ($(#[$attr:meta])* struct $BitFlags:ident: $T:ty {
-        $($(#[$Flag_attr:meta])* const $Flag:ident = $value:expr;)+
-    }) => {
-        #[derive(Copy, PartialEq, Eq, Clone, PartialOrd, Ord, Hash)]
-        $(#[$attr])*
-        struct $BitFlags {
-            bits: $T,
-        }
-
-        $($(#[$Flag_attr])* const $Flag: $BitFlags = $BitFlags { bits: $value };)+
-
-        __impl_bitflags! {
-            struct $BitFlags: $T {
-                $($(#[$Flag_attr])* const $Flag = $value;)+
-            }
-        }
-
-    };
 }
pub(restricted) is now stable. But I think it is still blocked by `:vis` matcher (rust-lang/rust#41022) to allow a proper fix. ---- Currently the straightforward fix using `:vis` causes an error in `example_generated.rs` ```rust error: local ambiguity: multiple parsing options: built-in NTs vis ('vis') or 1 other option. --> src/example_generated.rs:5:5 | 4 | / bitflags! { 5 | | /// This is the same `Flags` struct defined in the [crate level example](../index.html#example). | | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 6 | | /// Note that this struct is just for documentation purposes only, it must not be used outside 7 | | /// this crate. ... | 15 | | } 16 | | } | |_- in this macro invocation error: Could not compile `bitflags`. ``` <details><summary>(diff)</summary> ```diff diff --git a/src/lib.rs b/src/lib.rs index 1694837..356fba1 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -8,6 +8,8 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. +#![feature(macro_vis_matcher)] + //! A typesafe bitmask flag generator useful for sets of C-style bitmask flags. //! It can be used for creating typesafe wrappers around C APIs. //! @@ -305,16 +307,16 @@ pub extern crate core as _core; /// ``` #[macro_export] macro_rules! bitflags { - ($(#[$attr:meta])* pub struct $BitFlags:ident: $T:ty { + ($(#[$attr:meta])* $vis:vis struct $BitFlags:ident: $T:ty { $($(#[$Flag_attr:meta])* const $Flag:ident = $value:expr;)+ }) => { #[derive(Copy, PartialEq, Eq, Clone, PartialOrd, Ord, Hash)] $(#[$attr])* - pub struct $BitFlags { + $vis struct $BitFlags { bits: $T, } - $($(#[$Flag_attr])* pub const $Flag: $BitFlags = $BitFlags { bits: $value };)+ + $($(#[$Flag_attr])* $vis const $Flag: $BitFlags = $BitFlags { bits: $value };)+ __impl_bitflags! { struct $BitFlags: $T { @@ -322,24 +324,6 @@ macro_rules! bitflags { } } }; - ($(#[$attr:meta])* struct $BitFlags:ident: $T:ty { - $($(#[$Flag_attr:meta])* const $Flag:ident = $value:expr;)+ - }) => { - #[derive(Copy, PartialEq, Eq, Clone, PartialOrd, Ord, Hash)] - $(#[$attr])* - struct $BitFlags { - bits: $T, - } - - $($(#[$Flag_attr])* const $Flag: $BitFlags = $BitFlags { bits: $value };)+ - - __impl_bitflags! { - struct $BitFlags: $T { - $($(#[$Flag_attr])* const $Flag = $value;)+ - } - } - - }; } ``` </details>
KodrAus commented 2017-10-10 15:11:12 -05:00 (Migrated from github.com)

It looks like using :vis here is unblocked now, right?

It looks like using `:vis` here is unblocked now, right?
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#72
No description provided.