Fix crash when if expressions aren't in a function. (#3373)

Another issue found while trying to make `package` work, lurking in
fuzzer inputs. This leaves TODOs because we probably do want to support
this, it's just non-trivial to fix.
This commit is contained in:
Jon Ross-Perkins
2023-11-07 22:14:15 +00:00
committed by GitHub
parent 50a614aaf3
commit 84bc8cc4bf
4 changed files with 71 additions and 6 deletions
+10 -2
View File
@@ -379,9 +379,17 @@ auto Context::AddConvergenceBlockWithArgAndPush(
}
// Add the current code block to the enclosing function.
auto Context::AddCurrentCodeBlockToFunction() -> void {
auto Context::AddCurrentCodeBlockToFunction(Parse::Node parse_node) -> void {
CARBON_CHECK(!inst_block_stack().empty()) << "no current code block";
CARBON_CHECK(!return_scope_stack().empty()) << "no current function";
if (return_scope_stack().empty()) {
CARBON_CHECK(parse_node.is_valid())
<< "No current function, but parse_node not provided";
TODO(parse_node,
"Control flow expressions are currently only supported inside "
"functions.");
return;
}
if (!inst_block_stack().is_current_block_reachable()) {
// Don't include unreachable blocks in the function.