[clang] [clang] Deduce _BitInt(N) template parameter as size_t (PR #195534)

via cfe-commits cfe-commits at lists.llvm.org
Sun May 3 08:24:02 PDT 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang

Author: Jan Schultke (eisenwave)

<details>
<summary>Changes</summary>

Fixes #<!-- -->195033

Update template argument deduction to deduce the `N` in `_BitInt(N)` as `size_t` rather than `int`. This increases consistency with deduction of array sizes, and matches the behavior proposed in P3666.

### Testing
```sh
llvm-lit -sv clang/test/SemaTemplate/deduction.cpp
```

The whole test is guarded by a feature test for C++17. It wouldn't compile for me otherwise.

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


2 Files Affected:

- (modified) clang/lib/Sema/SemaTemplateDeduction.cpp (+5-2) 
- (modified) clang/test/SemaTemplate/deduction.cpp (+27) 


``````````diff
diff --git a/clang/lib/Sema/SemaTemplateDeduction.cpp b/clang/lib/Sema/SemaTemplateDeduction.cpp
index a287319cc4f88..3528fa001c201 100644
--- a/clang/lib/Sema/SemaTemplateDeduction.cpp
+++ b/clang/lib/Sema/SemaTemplateDeduction.cpp
@@ -2521,11 +2521,14 @@ static TemplateDeductionResult DeduceTemplateArgumentsByTypeMatch(
         if (!NTTP)
           return TemplateDeductionResult::Success;
 
-        llvm::APSInt ArgSize(S.Context.getTypeSize(S.Context.IntTy), false);
+        // P3666 suggested wording for [temp.deduct.type]:
+        //   The type of N in the type _BitInt(N) is std::size_t.
+        QualType T = S.Context.getSizeType();
+        llvm::APSInt ArgSize(S.Context.getTypeSize(T), false);
         ArgSize = IA->getNumBits();
 
         return DeduceNonTypeTemplateArgument(
-            S, TemplateParams, NTTP, ArgSize, S.Context.IntTy, true, Info,
+            S, TemplateParams, NTTP, ArgSize, T, true, Info,
             POK != PartialOrderingKind::None, Deduced, HasDeducedAnyParam);
       }
 
diff --git a/clang/test/SemaTemplate/deduction.cpp b/clang/test/SemaTemplate/deduction.cpp
index a209615c36479..e1ac6a9e5d267 100644
--- a/clang/test/SemaTemplate/deduction.cpp
+++ b/clang/test/SemaTemplate/deduction.cpp
@@ -633,6 +633,33 @@ namespace dependent_list_deduction {
   }
 }
 
+namespace bitint_deduction {
+#if __cplusplus >= 201703L
+  template<auto X> void f(unsigned _BitInt(X)) {
+    static_assert(is_same<decltype(X), decltype(sizeof(0))>::value, "");
+    static_assert(X == 31, "");
+  }
+  template<auto X> void g(_BitInt(X)) {
+    static_assert(is_same<decltype(X), decltype(sizeof(0))>::value, "");
+    static_assert(X == 32, "");
+  }
+  template<typename T, T V> void i(unsigned _BitInt(V)) {
+    static_assert(is_same<T, decltype(sizeof(0))>::value, "");
+    static_assert(V == 33, "");
+  }
+  template<typename T, T V> void j(_BitInt(V)) {
+    static_assert(is_same<T, decltype(sizeof(0))>::value, "");
+    static_assert(V == 34, "");
+  }
+  void h() {
+    f(static_cast<unsigned _BitInt(31)>(0));
+    g(static_cast<_BitInt(32)>(0));
+    i(static_cast<unsigned _BitInt(33)>(0));
+    j(static_cast<_BitInt(34)>(0));
+  }
+#endif
+}
+
 namespace designators {
   template<typename T, int N> constexpr int f(T (&&)[N]) { return N; } // expected-note 2{{couldn't infer template argument 'T'}}
   static_assert(f({1, 2, [20] = 3}) == 3, ""); // expected-error {{no matching function}} expected-warning 2{{C99}} expected-note {{}}

``````````

</details>


https://github.com/llvm/llvm-project/pull/195534


More information about the cfe-commits mailing list