[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
Fri Oct 9 14:01:21 PDT 2026
================
@@ -7196,6 +7196,9 @@ bool Compiler<Emitter>::visitBreakStmt(const BreakStmt *S) {
}
}
} else {
+ const Stmt *TargetLoop = S->getNamedLoopOrSwitch();
+ assert(TargetLoop && "break target label not available");
----------------
Sirraide wrote:
So I think there’s still an issue here.
In `ExprConstant.cpp` we just treat it as unlabeled if we can’t find the label, whereas here we just assert. I think we need to fail evaluation instead in both cases if the `LabelDecl` can’t be retrieved doesn’t exist yet—because then the branch target is almost certainly not within the current evaluation. I’d imagine the error could be something like ‘cannot break/continue to label '%0' that is not inside the current evaluation context’ or however we typically phrase that.
Can you see what happens if you write:
```c++
void f() {
foo: for (;;) {
static constexpr int x = ({
for (;;) break foo;
42;
});
}
}
```
Here, after this patch, I believe `ExprConstant.cpp` will erroneously treat the `break` as unlabeled, and I don’t know what the interpreter will do.
>From what I can tell, in all of your test cases, there isn’t actually a loop inside the evaluated expression, so evaluation fails because it can’t find an enclosing loop, not because the `break` is invalid
https://github.com/llvm/llvm-project/pull/228655
More information about the cfe-commits
mailing list