Skip to content

[libc][bit_test] fix -Wimplicit-int-conversion #126317

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

Merged
merged 1 commit into from
Feb 12, 2025

Conversation

nickdesaulniers
Copy link
Member

Fixes:

llvm-project/libc/src/__support/CPP/bit.h:235:28: error: implicit
conversion loses integer precision: 'int' to
'cpp::enable_if_t<cpp::is_unsigned_v<unsigned short>, unsigned short>' (aka
'unsigned short') [-Werror,-Wimplicit-int-conversion]
  235 |   return (value << rotate) | (value >> (N - rotate));
      |   ~~~~~~ ~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
llvm-project/libc/src/__support/CPP/bit.h:247:28: error: implicit
conversion loses integer precision: 'int' to
'cpp::enable_if_t<cpp::is_unsigned_v<unsigned short>, unsigned short>' (aka
'unsigned short') [-Werror,-Wimplicit-int-conversion]
  247 |   return (value >> rotate) | (value << (N - rotate));
      |   ~~~~~~ ~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
llvm-project/libc/test/src/__support/CPP/bit_test.cpp:45:36: error:
implicit conversion loses integer precision: 'int' to 'unsigned char'
[-Werror,-Wimplicit-int-conversion]
   45 |     EXPECT_FALSE(has_single_bit<T>(two_bits_value));
      |                  ~~~~~~~~~~~~~~    ^~~~~~~~~~~~~~

Via the libc-cpp-utils-tests ninja target.

Fixes:

    llvm-project/libc/src/__support/CPP/bit.h:235:28: error: implicit
    conversion loses integer precision: 'int' to
    'cpp::enable_if_t<cpp::is_unsigned_v<unsigned short>, unsigned short>' (aka
    'unsigned short') [-Werror,-Wimplicit-int-conversion]
      235 |   return (value << rotate) | (value >> (N - rotate));
          |   ~~~~~~ ~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
    llvm-project/libc/src/__support/CPP/bit.h:247:28: error: implicit
    conversion loses integer precision: 'int' to
    'cpp::enable_if_t<cpp::is_unsigned_v<unsigned short>, unsigned short>' (aka
    'unsigned short') [-Werror,-Wimplicit-int-conversion]
      247 |   return (value >> rotate) | (value << (N - rotate));
          |   ~~~~~~ ~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
    llvm-project/libc/test/src/__support/CPP/bit_test.cpp:45:36: error:
    implicit conversion loses integer precision: 'int' to 'unsigned char'
    [-Werror,-Wimplicit-int-conversion]
       45 |     EXPECT_FALSE(has_single_bit<T>(two_bits_value));
          |                  ~~~~~~~~~~~~~~    ^~~~~~~~~~~~~~

Via the libc-cpp-utils-tests ninja target.
@llvmbot
Copy link
Member

llvmbot commented Feb 7, 2025

@llvm/pr-subscribers-libc

Author: Nick Desaulniers (nickdesaulniers)

Changes

Fixes:

llvm-project/libc/src/__support/CPP/bit.h:235:28: error: implicit
conversion loses integer precision: 'int' to
'cpp::enable_if_t&lt;cpp::is_unsigned_v&lt;unsigned short&gt;, unsigned short&gt;' (aka
'unsigned short') [-Werror,-Wimplicit-int-conversion]
  235 |   return (value &lt;&lt; rotate) | (value &gt;&gt; (N - rotate));
      |   ~~~~~~ ~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
llvm-project/libc/src/__support/CPP/bit.h:247:28: error: implicit
conversion loses integer precision: 'int' to
'cpp::enable_if_t&lt;cpp::is_unsigned_v&lt;unsigned short&gt;, unsigned short&gt;' (aka
'unsigned short') [-Werror,-Wimplicit-int-conversion]
  247 |   return (value &gt;&gt; rotate) | (value &lt;&lt; (N - rotate));
      |   ~~~~~~ ~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
llvm-project/libc/test/src/__support/CPP/bit_test.cpp:45:36: error:
implicit conversion loses integer precision: 'int' to 'unsigned char'
[-Werror,-Wimplicit-int-conversion]
   45 |     EXPECT_FALSE(has_single_bit&lt;T&gt;(two_bits_value));
      |                  ~~~~~~~~~~~~~~    ^~~~~~~~~~~~~~

Via the libc-cpp-utils-tests ninja target.


Full diff: https://github.com/llvm/llvm-project/pull/126317.diff

2 Files Affected:

  • (modified) libc/src/__support/CPP/bit.h (+2-2)
  • (modified) libc/test/src/__support/CPP/bit_test.cpp (+2-1)
diff --git a/libc/src/__support/CPP/bit.h b/libc/src/__support/CPP/bit.h
index adcd0472747d01a..82b9eb51282625a 100644
--- a/libc/src/__support/CPP/bit.h
+++ b/libc/src/__support/CPP/bit.h
@@ -232,7 +232,7 @@ rotl(T value, int rotate) {
     return value;
   if (rotate < 0)
     return cpp::rotr<T>(value, -rotate);
-  return (value << rotate) | (value >> (N - rotate));
+  return static_cast<T>((value << rotate) | (value >> (N - rotate)));
 }
 
 template <typename T>
@@ -244,7 +244,7 @@ rotr(T value, int rotate) {
     return value;
   if (rotate < 0)
     return cpp::rotl<T>(value, -rotate);
-  return (value >> rotate) | (value << (N - rotate));
+  return static_cast<T>((value >> rotate) | (value << (N - rotate)));
 }
 
 // TODO: Do we need this function at all? How is it different from
diff --git a/libc/test/src/__support/CPP/bit_test.cpp b/libc/test/src/__support/CPP/bit_test.cpp
index 9429b66ad1f9815..1f2315281bc1dab 100644
--- a/libc/test/src/__support/CPP/bit_test.cpp
+++ b/libc/test/src/__support/CPP/bit_test.cpp
@@ -41,7 +41,8 @@ TYPED_TEST(LlvmLibcBitTest, HasSingleBit, UnsignedTypes) {
   constexpr auto LSB = T(1);
   constexpr auto MSB = T(~(ALL_ONES >> 1));
   for (T value = 1; value; value <<= 1) {
-    auto two_bits_value = value | ((value <= MIDPOINT) ? MSB : LSB);
+    T two_bits_value =
+        static_cast<T>(value | ((value <= MIDPOINT) ? MSB : LSB));
     EXPECT_FALSE(has_single_bit<T>(two_bits_value));
   }
 }

@nickdesaulniers nickdesaulniers merged commit a3e2075 into llvm:main Feb 12, 2025
16 checks passed
@nickdesaulniers nickdesaulniers deleted the implicit_conversion branch February 12, 2025 16:59
flovent pushed a commit to flovent/llvm-project that referenced this pull request Feb 13, 2025
Fixes:

    llvm-project/libc/src/__support/CPP/bit.h:235:28: error: implicit
    conversion loses integer precision: 'int' to
'cpp::enable_if_t<cpp::is_unsigned_v<unsigned short>, unsigned short>'
(aka
    'unsigned short') [-Werror,-Wimplicit-int-conversion]
      235 |   return (value << rotate) | (value >> (N - rotate));
          |   ~~~~~~ ~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
    llvm-project/libc/src/__support/CPP/bit.h:247:28: error: implicit
    conversion loses integer precision: 'int' to
'cpp::enable_if_t<cpp::is_unsigned_v<unsigned short>, unsigned short>'
(aka
    'unsigned short') [-Werror,-Wimplicit-int-conversion]
      247 |   return (value >> rotate) | (value << (N - rotate));
          |   ~~~~~~ ~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
    llvm-project/libc/test/src/__support/CPP/bit_test.cpp:45:36: error:
implicit conversion loses integer precision: 'int' to 'unsigned char'
    [-Werror,-Wimplicit-int-conversion]
       45 |     EXPECT_FALSE(has_single_bit<T>(two_bits_value));
          |                  ~~~~~~~~~~~~~~    ^~~~~~~~~~~~~~

Via the libc-cpp-utils-tests ninja target.
joaosaffran pushed a commit to joaosaffran/llvm-project that referenced this pull request Feb 14, 2025
Fixes:

    llvm-project/libc/src/__support/CPP/bit.h:235:28: error: implicit
    conversion loses integer precision: 'int' to
'cpp::enable_if_t<cpp::is_unsigned_v<unsigned short>, unsigned short>'
(aka
    'unsigned short') [-Werror,-Wimplicit-int-conversion]
      235 |   return (value << rotate) | (value >> (N - rotate));
          |   ~~~~~~ ~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
    llvm-project/libc/src/__support/CPP/bit.h:247:28: error: implicit
    conversion loses integer precision: 'int' to
'cpp::enable_if_t<cpp::is_unsigned_v<unsigned short>, unsigned short>'
(aka
    'unsigned short') [-Werror,-Wimplicit-int-conversion]
      247 |   return (value >> rotate) | (value << (N - rotate));
          |   ~~~~~~ ~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
    llvm-project/libc/test/src/__support/CPP/bit_test.cpp:45:36: error:
implicit conversion loses integer precision: 'int' to 'unsigned char'
    [-Werror,-Wimplicit-int-conversion]
       45 |     EXPECT_FALSE(has_single_bit<T>(two_bits_value));
          |                  ~~~~~~~~~~~~~~    ^~~~~~~~~~~~~~

Via the libc-cpp-utils-tests ninja target.
sivan-shani pushed a commit to sivan-shani/llvm-project that referenced this pull request Feb 24, 2025
Fixes:

    llvm-project/libc/src/__support/CPP/bit.h:235:28: error: implicit
    conversion loses integer precision: 'int' to
'cpp::enable_if_t<cpp::is_unsigned_v<unsigned short>, unsigned short>'
(aka
    'unsigned short') [-Werror,-Wimplicit-int-conversion]
      235 |   return (value << rotate) | (value >> (N - rotate));
          |   ~~~~~~ ~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
    llvm-project/libc/src/__support/CPP/bit.h:247:28: error: implicit
    conversion loses integer precision: 'int' to
'cpp::enable_if_t<cpp::is_unsigned_v<unsigned short>, unsigned short>'
(aka
    'unsigned short') [-Werror,-Wimplicit-int-conversion]
      247 |   return (value >> rotate) | (value << (N - rotate));
          |   ~~~~~~ ~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
    llvm-project/libc/test/src/__support/CPP/bit_test.cpp:45:36: error:
implicit conversion loses integer precision: 'int' to 'unsigned char'
    [-Werror,-Wimplicit-int-conversion]
       45 |     EXPECT_FALSE(has_single_bit<T>(two_bits_value));
          |                  ~~~~~~~~~~~~~~    ^~~~~~~~~~~~~~

Via the libc-cpp-utils-tests ninja target.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants