[clang] [clang] Fixed auto type inference failing when the rhs is a bit-field expression (PR #226784)
Abdulmalek Almkainzi via cfe-commits
cfe-commits at lists.llvm.org
Sun Sep 27 06:25:06 PDT 2026
https://github.com/aalmkainzi created https://github.com/llvm/llvm-project/pull/226784
e.g.
```C
void f()
{
struct S { unsigned i : 2; };
struct S s = {};
auto x = s.i; // previously failed. now accepted and x is an `unsigned int`
}
```
Fixes #226280
>From 7c81e84a437e3219045e04acd6643fb080fd7dab Mon Sep 17 00:00:00 2001
From: aalmkainzi <aa.almkainzi at wakeb.tech>
Date: Sun, 27 Sep 2026 13:38:10 +0300
Subject: [PATCH 1/2] only error out if __auto_type
---
clang/lib/Sema/SemaTemplateDeduction.cpp | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)
diff --git a/clang/lib/Sema/SemaTemplateDeduction.cpp b/clang/lib/Sema/SemaTemplateDeduction.cpp
index 653240092e64a..5a08eb3196b90 100644
--- a/clang/lib/Sema/SemaTemplateDeduction.cpp
+++ b/clang/lib/Sema/SemaTemplateDeduction.cpp
@@ -5505,7 +5505,8 @@ Sema::DeduceAutoType(TypeLoc Type, Expr *Init, QualType &Result,
DeducedFromInitRange = Init->getSourceRange();
}
} else {
- if (!getLangOpts().CPlusPlus && Init->refersToBitField()) {
+ if (!getLangOpts().CPlusPlus && Init->refersToBitField() &&
+ AT->getKeyword() == AutoTypeKeyword::GNUAutoType) {
Diag(Loc, diag::err_auto_bitfield);
return TemplateDeductionResult::AlreadyDiagnosed;
}
>From 767dadc57fddb0b98705630e812484bfcd934c98 Mon Sep 17 00:00:00 2001
From: aalmkainzi <aa.almkainzi at wakeb.tech>
Date: Sun, 27 Sep 2026 16:14:47 +0300
Subject: [PATCH 2/2] added a test verifying that a bit-field is converted to
its underlying type when type inference
---
clang/test/Sema/c23-auto-bitfield.c | 10 ++++++++++
1 file changed, 10 insertions(+)
create mode 100644 clang/test/Sema/c23-auto-bitfield.c
diff --git a/clang/test/Sema/c23-auto-bitfield.c b/clang/test/Sema/c23-auto-bitfield.c
new file mode 100644
index 0000000000000..0c93897f1ee61
--- /dev/null
+++ b/clang/test/Sema/c23-auto-bitfield.c
@@ -0,0 +1,10 @@
+// RUN: %clang_cc1 -std=c23 -fsyntax-only -verify %s
+// expected-no-diagnostics
+
+void f()
+{
+ struct S { unsigned i : 1; };
+ struct S s;
+ auto si = s.i;
+ static_assert(_Generic(si, unsigned int : 1, default : 0), "the underlying type of the bit-field is 'unsigned int'");
+}
More information about the cfe-commits
mailing list