Skip to content

[libc] fix -Wshift-count-overflow in UInt.h #74649

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
Dec 7, 2023

Conversation

nickdesaulniers
Copy link
Member

Not that I'm very good at SFINAE, but it seems that conversion operators are
perhaps difficult to compose with SFINAE. I saw an example that used one layer
of indirection to have an explicit return type that could then be used with
enable_if_t.

Link: https://stackoverflow.com/a/7604580
Fixes: #74623

Not that I'm very good at SFINAE, but it seems that conversion operators are
perhaps difficult to compose with SFINAE.  I saw an example that used one layer
of indirection to have an explicit return type that could then be used with
enable_if_t.

Link: https://stackoverflow.com/a/7604580
Fixes: llvm#74623
@llvmbot llvmbot added the libc label Dec 6, 2023
@llvmbot
Copy link
Member

llvmbot commented Dec 6, 2023

@llvm/pr-subscribers-libc

Author: Nick Desaulniers (nickdesaulniers)

Changes

Not that I'm very good at SFINAE, but it seems that conversion operators are
perhaps difficult to compose with SFINAE. I saw an example that used one layer
of indirection to have an explicit return type that could then be used with
enable_if_t.

Link: https://stackoverflow.com/a/7604580
Fixes: #74623


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

1 Files Affected:

  • (modified) libc/src/__support/UInt.h (+13-7)
diff --git a/libc/src/__support/UInt.h b/libc/src/__support/UInt.h
index 3bec2e3a47130..f72b995f8788d 100644
--- a/libc/src/__support/UInt.h
+++ b/libc/src/__support/UInt.h
@@ -103,13 +103,20 @@ template <size_t Bits, bool Signed> struct BigInt {
       val[i] = words[i];
   }
 
-  template <typename T, typename = cpp::enable_if_t<cpp::is_integral_v<T> &&
-                                                    sizeof(T) <= 16 &&
-                                                    !cpp::is_same_v<T, bool>>>
-  LIBC_INLINE constexpr explicit operator T() const {
-    if constexpr (sizeof(T) <= 8)
-      return static_cast<T>(val[0]);
+  template <typename T> LIBC_INLINE constexpr explicit operator T() const {
+    return to<T>();
+  }
 
+  template <typename T>
+  LIBC_INLINE constexpr cpp::enable_if_t<
+      cpp::is_integral_v<T> && sizeof(T) <= 8 && !cpp::is_same_v<T, bool>, T>
+  to() const {
+    return static_cast<T>(val[0]);
+  }
+  template <typename T>
+  LIBC_INLINE constexpr cpp::enable_if_t<
+      cpp::is_integral_v<T> && sizeof(T) == 16, T>
+  to() const {
     // T is 128-bit.
     T lo = static_cast<T>(val[0]);
 
@@ -121,7 +128,6 @@ template <size_t Bits, bool Signed> struct BigInt {
         return lo;
       }
     } else {
-      // TODO: silence shift warning
       return static_cast<T>((static_cast<T>(val[1]) << 64) + lo);
     }
   }

LIBC_INLINE constexpr explicit operator T() const {
if constexpr (sizeof(T) <= 8)
return static_cast<T>(val[0]);
template <typename T> LIBC_INLINE constexpr explicit operator T() const {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does this template need any sort of limit (e.g. enable_if_t<is_integral_v<T>>)?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't need extra guard here, since it is already guarded with to<T>() templates.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, if it doesn't fail here it will fail there. I don't feel strongly either way, so merging for now. If you feel strongly otherwise I'll send another patch to add that.

@nickdesaulniers nickdesaulniers merged commit 1ee6a1e into llvm:main Dec 7, 2023
@nickdesaulniers nickdesaulniers deleted the Wshift-count-overflow branch December 7, 2023 16:20
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.

[libc] -Wshift-count-overflow in libc/src/__support/UInt.h
4 participants