Skip to content

Conversation

AaronBallman
Copy link
Collaborator

The original wording can be squinted at to pretend this was always allowed. GCC squints at it that way, so we're doing the same and no longer issuing an extension diagnostic for use of static_assert in the condition-1 of a for loop in C.

Fixes #149633

The original wording can be squinted at to pretend this was always
allowed. GCC squints at it that way, so we're doing the same and no
longer issuing an extension diagnostic for use of static_assert in the
condition-1 of a for loop in C.

Fixes llvm#149633
@AaronBallman AaronBallman added this to the LLVM 21.x Release milestone Aug 4, 2025
@AaronBallman AaronBallman added c clang:diagnostics New/improved warning or error message in Clang, but not in clang-tidy or static analyzer labels Aug 4, 2025
@AaronBallman AaronBallman added the diverges-from:gcc Does the clang frontend diverge from gcc on this issue label Aug 4, 2025
@github-project-automation github-project-automation bot moved this to Needs Triage in LLVM Release Status Aug 4, 2025
@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" labels Aug 4, 2025
@AaronBallman
Copy link
Collaborator Author

Adding this to the 21.x milestone so we remember to backport it because the original changes introducing this diagnostic were added for 21.x

@llvmbot
Copy link
Member

llvmbot commented Aug 4, 2025

@llvm/pr-subscribers-clang

Author: Aaron Ballman (AaronBallman)

Changes

The original wording can be squinted at to pretend this was always allowed. GCC squints at it that way, so we're doing the same and no longer issuing an extension diagnostic for use of static_assert in the condition-1 of a for loop in C.

Fixes #149633


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

2 Files Affected:

  • (modified) clang/lib/Sema/SemaStmt.cpp (+5-1)
  • (modified) clang/test/Sema/for.c (+1-2)
diff --git a/clang/lib/Sema/SemaStmt.cpp b/clang/lib/Sema/SemaStmt.cpp
index 3f89843a9081a..a5f92020f49f8 100644
--- a/clang/lib/Sema/SemaStmt.cpp
+++ b/clang/lib/Sema/SemaStmt.cpp
@@ -2284,7 +2284,11 @@ StmtResult Sema::ActOnForStmt(SourceLocation ForLoc, SourceLocation LParenLoc,
           // we can diagnose if we don't see any variable declarations. This
           // covers a case like declaring a typedef, function, or structure
           // type rather than a variable.
-          NonVarSeen = DI;
+          //
+          // Note, _Static_assert is acceptable because it does not declare an
+          // identifier at all, so "for object having" does not apply.
+          if (!isa<StaticAssertDecl>(DI))
+            NonVarSeen = DI;
         }
       }
       // Diagnose if we saw a non-variable declaration but no variable
diff --git a/clang/test/Sema/for.c b/clang/test/Sema/for.c
index e16169aac0c4c..35c4720ef3305 100644
--- a/clang/test/Sema/for.c
+++ b/clang/test/Sema/for.c
@@ -26,6 +26,5 @@ void b11 (void) { for (static _Thread_local struct { int i; } s;s.i;); } /* c11-
 #endif
 
 void b12(void) {
-  for(_Static_assert(1, "");;) {} /* c11-warning {{non-variable declaration in 'for' loop is a C23 extension}}
-                                     c23-warning {{non-variable declaration in 'for' loop is incompatible with C standards before C23}} */
+  for(_Static_assert(1, "");;) {} /* okay, _Static_assert declares *no* identifiers */
 }

Copy link
Contributor

@cor3ntin cor3ntin left a comment

Choose a reason for hiding this comment

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

Thanks, I hate it.

LGTM

@github-project-automation github-project-automation bot moved this from Needs Triage to Needs Merge in LLVM Release Status Aug 4, 2025
@AaronBallman AaronBallman merged commit cb50d78 into llvm:main Aug 4, 2025
15 checks passed
@github-project-automation github-project-automation bot moved this from Needs Merge to Done in LLVM Release Status Aug 4, 2025
@AaronBallman AaronBallman deleted the aballman-gh149633 branch August 4, 2025 16:46
@AaronBallman
Copy link
Collaborator Author

/cherry-pick cb50d78

@llvmbot
Copy link
Member

llvmbot commented Aug 4, 2025

/pull-request #151999

tru pushed a commit to llvmbot/llvm-project that referenced this pull request Aug 5, 2025
The original wording can be squinted at to pretend this was always
allowed. GCC squints at it that way, so we're doing the same and no
longer issuing an extension diagnostic for use of static_assert in the
condition-1 of a for loop in C.

Fixes llvm#149633

(cherry picked from commit cb50d78)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
c clang:diagnostics New/improved warning or error message in Clang, but not in clang-tidy or static analyzer clang:frontend Language frontend issues, e.g. anything involving "Sema" clang Clang issues not falling into any other category diverges-from:gcc Does the clang frontend diverge from gcc on this issue
Projects
Development

Successfully merging this pull request may close these issues.

Pre-C23 check in for declaration is incorrect
3 participants