[clang] [clang][AST] Fix crash on labeled break/continue within switch condition statement expression (PR #228655)

Ambrose Leeb via cfe-commits cfe-commits at lists.llvm.org
Mon Oct 5 13:47:27 PDT 2026


================
@@ -1533,9 +1533,9 @@ const Stmt *LabelStmt::getInnermostLabeledStmt() const {
 }
 
 const Stmt *LoopControlStmt::getNamedLoopOrSwitch() const {
-  if (!hasLabelTarget())
-    return nullptr;
-  return getLabelDecl()->getStmt()->getInnermostLabeledStmt();
+  assert(hasLabelTarget());
+  LabelStmt *Label = getLabelDecl()->getStmt();
+  return Label ? Label->getInnermostLabeledStmt() : nullptr;
----------------
Sirraide wrote:

I think `getNamedLoopOrSwitch()` returning nullptr when `hasLabelTarget()` is true can only happen during constant evaluation, because we try and evaluate an expression that contains a labelled `break`/`continue` whose `LabelDecl` hasn’t been created yet. 

During codegen or after we’re done w/ Sema, this shouldn’t really ever happen. Perhaps renaming `hasLabelTarget()` to `isNamed()` or sth like that might make it clearer what that function actually checks for.

> `std::pair<bool, *>`? (a more general, well out of scope for this PR, option would be a result type)

I’d go with `std::optional<Stmt *>` if we want to do that, along w/ a comment that explains the three possible states are (1. not labelled, 2. labelled, but the LabelDecl doesn’t exist yet, 3. labelled and it does exist).

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


More information about the cfe-commits mailing list