Skip to content

Commit

Permalink
Fix regression introduced in bitflags#246
Browse files Browse the repository at this point in the history
Add test case
  • Loading branch information
konsumlamm committed Jul 19, 2021
1 parent efab7f3 commit 75f7192
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 32 deletions.
65 changes: 34 additions & 31 deletions src/lib.rs
Expand Up @@ -246,6 +246,9 @@
#![cfg_attr(not(test), no_std)]
#![doc(html_root_url = "https://docs.rs/bitflags/1.2.1")]

#[doc(hidden)]
pub extern crate core as _core;

/// The macro used to generate the flag structures.
///
/// See the [crate level docs](../bitflags/index.html) for complete documentation.
Expand Down Expand Up @@ -394,8 +397,8 @@ macro_rules! __impl_bitflags {
)*
}
) => {
impl core::fmt::Debug for $BitFlags {
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
impl $crate::_core::fmt::Debug for $BitFlags {
fn fmt(&self, f: &mut $crate::_core::fmt::Formatter) -> $crate::_core::fmt::Result {
// This convoluted approach is to handle #[cfg]-based flag
// omission correctly. For example it needs to support:
//
Expand Down Expand Up @@ -448,32 +451,32 @@ macro_rules! __impl_bitflags {
}
first = false;
f.write_str("0x")?;
core::fmt::LowerHex::fmt(&extra_bits, f)?;
$crate::_core::fmt::LowerHex::fmt(&extra_bits, f)?;
}
if first {
f.write_str("(empty)")?;
}
Ok(())
}
}
impl core::fmt::Binary for $BitFlags {
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
core::fmt::Binary::fmt(&self.bits, f)
impl $crate::_core::fmt::Binary for $BitFlags {
fn fmt(&self, f: &mut $crate::_core::fmt::Formatter) -> $crate::_core::fmt::Result {
$crate::_core::fmt::Binary::fmt(&self.bits, f)
}
}
impl core::fmt::Octal for $BitFlags {
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
core::fmt::Octal::fmt(&self.bits, f)
impl $crate::_core::fmt::Octal for $BitFlags {
fn fmt(&self, f: &mut $crate::_core::fmt::Formatter) -> $crate::_core::fmt::Result {
$crate::_core::fmt::Octal::fmt(&self.bits, f)
}
}
impl core::fmt::LowerHex for $BitFlags {
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
core::fmt::LowerHex::fmt(&self.bits, f)
impl $crate::_core::fmt::LowerHex for $BitFlags {
fn fmt(&self, f: &mut $crate::_core::fmt::Formatter) -> $crate::_core::fmt::Result {
$crate::_core::fmt::LowerHex::fmt(&self.bits, f)
}
}
impl core::fmt::UpperHex for $BitFlags {
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
core::fmt::UpperHex::fmt(&self.bits, f)
impl $crate::_core::fmt::UpperHex for $BitFlags {
fn fmt(&self, f: &mut $crate::_core::fmt::Formatter) -> $crate::_core::fmt::Result {
$crate::_core::fmt::UpperHex::fmt(&self.bits, f)
}
}

Expand Down Expand Up @@ -512,11 +515,11 @@ macro_rules! __impl_bitflags {
/// Convert from underlying bit representation, unless that
/// representation contains bits that do not correspond to a flag.
#[inline]
pub const fn from_bits(bits: $T) -> core::option::Option<$BitFlags> {
pub const fn from_bits(bits: $T) -> $crate::_core::option::Option<$BitFlags> {
if (bits & !$BitFlags::all().bits()) == 0 {
core::option::Option::Some($BitFlags { bits })
$crate::_core::option::Option::Some($BitFlags { bits })
} else {
core::option::Option::None
$crate::_core::option::Option::None
}
}

Expand Down Expand Up @@ -596,7 +599,7 @@ macro_rules! __impl_bitflags {
}
}

impl core::ops::BitOr for $BitFlags {
impl $crate::_core::ops::BitOr for $BitFlags {
type Output = $BitFlags;

/// Returns the union of the two sets of flags.
Expand All @@ -606,7 +609,7 @@ macro_rules! __impl_bitflags {
}
}

impl core::ops::BitOrAssign for $BitFlags {
impl $crate::_core::ops::BitOrAssign for $BitFlags {

/// Adds the set of flags.
#[inline]
Expand All @@ -615,7 +618,7 @@ macro_rules! __impl_bitflags {
}
}

impl core::ops::BitXor for $BitFlags {
impl $crate::_core::ops::BitXor for $BitFlags {
type Output = $BitFlags;

/// Returns the left flags, but with all the right flags toggled.
Expand All @@ -625,7 +628,7 @@ macro_rules! __impl_bitflags {
}
}

impl core::ops::BitXorAssign for $BitFlags {
impl $crate::_core::ops::BitXorAssign for $BitFlags {

/// Toggles the set of flags.
#[inline]
Expand All @@ -634,7 +637,7 @@ macro_rules! __impl_bitflags {
}
}

impl core::ops::BitAnd for $BitFlags {
impl $crate::_core::ops::BitAnd for $BitFlags {
type Output = $BitFlags;

/// Returns the intersection between the two sets of flags.
Expand All @@ -644,7 +647,7 @@ macro_rules! __impl_bitflags {
}
}

impl core::ops::BitAndAssign for $BitFlags {
impl $crate::_core::ops::BitAndAssign for $BitFlags {

/// Disables all flags disabled in the set.
#[inline]
Expand All @@ -653,7 +656,7 @@ macro_rules! __impl_bitflags {
}
}

impl core::ops::Sub for $BitFlags {
impl $crate::_core::ops::Sub for $BitFlags {
type Output = $BitFlags;

/// Returns the set difference of the two sets of flags.
Expand All @@ -663,7 +666,7 @@ macro_rules! __impl_bitflags {
}
}

impl core::ops::SubAssign for $BitFlags {
impl $crate::_core::ops::SubAssign for $BitFlags {

/// Disables all flags enabled in the set.
#[inline]
Expand All @@ -672,7 +675,7 @@ macro_rules! __impl_bitflags {
}
}

impl core::ops::Not for $BitFlags {
impl $crate::_core::ops::Not for $BitFlags {
type Output = $BitFlags;

/// Returns the complement of this set of flags.
Expand All @@ -682,16 +685,16 @@ macro_rules! __impl_bitflags {
}
}

impl core::iter::Extend<$BitFlags> for $BitFlags {
fn extend<T: core::iter::IntoIterator<Item=$BitFlags>>(&mut self, iterator: T) {
impl $crate::_core::iter::Extend<$BitFlags> for $BitFlags {
fn extend<T: $crate::_core::iter::IntoIterator<Item=$BitFlags>>(&mut self, iterator: T) {
for item in iterator {
self.insert(item)
}
}
}

impl core::iter::FromIterator<$BitFlags> for $BitFlags {
fn from_iter<T: core::iter::IntoIterator<Item=$BitFlags>>(iterator: T) -> $BitFlags {
impl $crate::_core::iter::FromIterator<$BitFlags> for $BitFlags {
fn from_iter<T: $crate::_core::iter::IntoIterator<Item=$BitFlags>>(iterator: T) -> $BitFlags {
let mut result = Self::empty();
result.extend(iterator);
result
Expand Down
2 changes: 1 addition & 1 deletion test_suite/tests/compile-fail/private_flags.rs
@@ -1,5 +1,5 @@
mod example {
use bitflags::bitflags
use bitflags::bitflags;

bitflags! {
pub struct Flags1: u32 {
Expand Down
9 changes: 9 additions & 0 deletions test_suite/tests/redefine_core.rs
@@ -0,0 +1,9 @@
use bitflags::bitflags;

// ensure that no naming conflicts happen
mod core {}
mod _core {}

bitflags! {
struct Test: u8 {}
}

0 comments on commit 75f7192

Please sign in to comment.