[clang] [clang] [SemaCXX] Implement CWG2627 Bit-fields and narrowing conversions (PR #78112)
Mital Ashok via cfe-commits
cfe-commits at lists.llvm.org
Thu May 30 10:54:30 PDT 2024
================
@@ -24,6 +44,103 @@ using enum E;
#endif
}
+namespace cwg2627 { // cwg2627: 19
+#if __cplusplus >= 202002L
+struct C {
+ long long i : 8;
+ friend auto operator<=>(C, C) = default;
+};
+
+void f() {
+ C x{1}, y{2};
+ static_cast<void>(x <=> y);
+ static_cast<void>(x.i <=> y.i);
+}
+
+template<typename T>
+struct CDependent {
+ T i : 8;
+ friend auto operator<=>(CDependent, CDependent) = default;
+};
+
+template<typename T>
+concept three_way_comparable = requires(T t) { { t <=> t }; };
+template<typename T>
+concept bf_three_way_comparable = requires(T t) { { t.i <=> t.i }; };
+static_assert(three_way_comparable<CDependent<long long>>);
+static_assert(bf_three_way_comparable<CDependent<long long>>);
+#endif
+
+#if __cplusplus >= 201103L
+template<int W>
+struct D {
+ __int128 i : W;
----------------
MitalAshok wrote:
Narrowing conversions don't consider the rank of integral types, only the range of values of the source and destination types. For `struct { long long i : 32; } x;` `int y{ x.i }`, clang uses an integral promotion (not an integral conversion) to initialize `y`, and it considers this to be non-narrowing even though it's from `long long` to `int`.
https://github.com/llvm/llvm-project/blob/ded04bf5d32a4fd5e0919053a598443f9d773549/clang/lib/Sema/SemaOverload.cpp#L549-L551
This leads to the situation where with `struct { __int128 i : 32; } x;`, `int{ x.i }` is not considered narrowing in clang, but `int64_t{ x.i }` *is*.
https://github.com/llvm/llvm-project/pull/78112
More information about the cfe-commits
mailing list