llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang
Author: Akash Manna (akash-manna-sky)
<details>
<summary>Changes</summary>
Fixes #<!-- -->139511
The string-literal shortcut in `CheckArrayType` assumes the string starts the
array. It also ran when the checker re-entered `CheckArrayType` to finish an
array after a designator such as `.y[2] = 'e'`, so `"aaa"` was stored as
element 3 of `y` with its array type, and the constant evaluator (C++) or
`ConstExprEmitter` (C) asserted on it.
The shortcut now applies only at element index 0. After a designator the string
takes the ordinary element path and gets the same error that the top-level
`char a[10] = {[2] = 'e', "aaa"}` already does.
AI Assisted!
---
Full diff: https://github.com/llvm/llvm-project/pull/241933.diff
4 Files Affected:
- (modified) clang/docs/ReleaseNotes.md (+1)
- (modified) clang/lib/Sema/SemaInit.cpp (+3-2)
- (modified) clang/test/Sema/designated-initializers.c (+2)
- (modified) clang/test/SemaCXX/designated-initializers.cpp (+4)
``````````diff
diff --git a/clang/docs/ReleaseNotes.md b/clang/docs/ReleaseNotes.md
index 6d1dbbbb6eb77..78edf927e1389 100644
--- a/clang/docs/ReleaseNotes.md
+++ b/clang/docs/ReleaseNotes.md
@@ -822,6 +822,7 @@ documentation](CIR/index.md) for more information regarding
ClangIR's status and
(e.g. `({ s.b; })`) was not subject to integer promotion, unlike an ordinary
bit-field access. (#GH221542)
- No longer crashing due to follow-on diagnostics when there is an invalid
operand in a logical operator involving a vector operand. (#GH227588)
+- Fixed a crash when a string literal follows a designated initializer for an
element of a character array subobject. (#GH139511)
- Fixed assertion failures caused by stale linkage information when an extern
variable or function declaration is merged with a preceding static declaration.
(#GH204759, #GH204754)
- Fixed a crash due to typo correction mishandling custom keywords
`_virtual_inheritance` and `_multiple_inheritance` in `-fms-compatibility`
mode. (#GH228003)
- Clang no longer treats a file-scope `thread_local` declaration without an
initializer as a tentative definition in C23 mode. As specified by C23 6.9.3,
such a declaration is a definition, so declaring the same variable more that
once is now diagnosed as a redefinition. (#GH217636)
diff --git a/clang/lib/Sema/SemaInit.cpp b/clang/lib/Sema/SemaInit.cpp
index 2e3c6309fec20..d081150088180 100644
--- a/clang/lib/Sema/SemaInit.cpp
+++ b/clang/lib/Sema/SemaInit.cpp
@@ -2156,8 +2156,9 @@ void InitListChecker::CheckArrayType(const
InitializedEntity &Entity,
IList->setInit(0, Embed->getDataStringLiteral());
}
- // Check for the special-case of initializing an array with a string.
- if (Index < IList->getNumInits()) {
+ // Check for the special-case of initializing an array with a string. After a
+ // designator the string is the initializer of the next element instead.
+ if (Index < IList->getNumInits() && elementIndex.isZero()) {
if (IsStringInit(IList->getInit(Index), arrayType, SemaRef.Context) ==
SIF_None) {
// We place the string literal directly into the resulting
diff --git a/clang/test/Sema/designated-initializers.c
b/clang/test/Sema/designated-initializers.c
index 11dc3a2308dee..35582b7676bff 100644
--- a/clang/test/Sema/designated-initializers.c
+++ b/clang/test/Sema/designated-initializers.c
@@ -375,3 +375,5 @@ void gh154046(void) {
[1] = "" // expected-error {{incompatible pointer to integer conversion
initializing 'const char' with an expression of type 'char[1]'}}
}[1];
}
+
+const struct { char y[4]; } gh139511 = {.y[2] = 'e', "aaa"}; // expected-error
{{incompatible pointer to integer conversion initializing 'char' with an
expression of type 'char[4]'}} expected-error {{initializer element is not a
compile-time constant}}
diff --git a/clang/test/SemaCXX/designated-initializers.cpp
b/clang/test/SemaCXX/designated-initializers.cpp
index a897e5a4e544c..4f73b2656c5f2 100644
--- a/clang/test/SemaCXX/designated-initializers.cpp
+++ b/clang/test/SemaCXX/designated-initializers.cpp
@@ -177,3 +177,7 @@ namespace ADL {
void g() { f({}, {}); }
}
}
+
+namespace GH139511 {
+ const struct { char y[4]; } a = {.y[2] = 'e', "aaa"}; // expected-error
{{cannot initialize an array element of type 'char' with an lvalue of type
'const char[4]'}}
+}
``````````
</details>
https://github.com/llvm/llvm-project/pull/241933
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits