diff --git a/toolchain/check/inst_block_stack.cpp b/toolchain/check/inst_block_stack.cpp index 84e5ae8b308f..3336b90b228f 100644 --- a/toolchain/check/inst_block_stack.cpp +++ b/toolchain/check/inst_block_stack.cpp @@ -22,7 +22,7 @@ auto InstBlockStack::Push(SemIR::InstBlockId id) -> void { } auto InstBlockStack::PushGlobalInit() -> void { - Push(SemIR::InstBlockId::GlobalInit); + Push(init_block_id_); stack_[size_ - 1].content = std::move(init_block_); } @@ -58,10 +58,8 @@ auto InstBlockStack::Pop() -> SemIR::InstBlockId { } auto InstBlockStack::PopGlobalInit() -> void { - CARBON_CHECK(stack_[size_ - 1].id == SemIR::InstBlockId::GlobalInit) - << "Trying to pop Inits block from " << name_ - << " but a different block is present!"; init_block_ = std::move(stack_[size_ - 1].content); + init_block_id_ = stack_[size_ - 1].id; PopAndDiscard(); } diff --git a/toolchain/check/inst_block_stack.h b/toolchain/check/inst_block_stack.h index 0fc588dce6f3..c450f3e32005 100644 --- a/toolchain/check/inst_block_stack.h +++ b/toolchain/check/inst_block_stack.h @@ -124,6 +124,9 @@ class InstBlockStack { std::vector init_block_; + // Current global init block to push. + SemIR::InstBlockId init_block_id_ = SemIR::InstBlockId::GlobalInit; + // The actual stack. llvm::SmallVector stack_; diff --git a/toolchain/check/testdata/var/fail_todo_control_flow_init.carbon b/toolchain/check/testdata/var/fail_todo_control_flow_init.carbon new file mode 100644 index 000000000000..6200331ef698 --- /dev/null +++ b/toolchain/check/testdata/var/fail_todo_control_flow_init.carbon @@ -0,0 +1,91 @@ +// Part of the Carbon Language project, under the Apache License v2.0 with LLVM +// Exceptions. See /LICENSE for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +// AUTOUPDATE + +// CHECK:STDERR: fail_todo_control_flow_init.carbon:[[@LINE+12]]:13: ERROR: Semantics TODO: `Control flow expressions are currently only supported inside functions.`. +// CHECK:STDERR: var x: () = if true then () else (); +// CHECK:STDERR: ^~~~~~~ +// CHECK:STDERR: +// CHECK:STDERR: fail_todo_control_flow_init.carbon:[[@LINE+8]]:13: ERROR: Semantics TODO: `Control flow expressions are currently only supported inside functions.`. +// CHECK:STDERR: var x: () = if true then () else (); +// CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~ +// CHECK:STDERR: +// CHECK:STDERR: fail_todo_control_flow_init.carbon:[[@LINE+4]]:21: ERROR: Semantics TODO: `Control flow expressions are currently only supported inside functions.`. +// CHECK:STDERR: var x: () = if true then () else (); +// CHECK:STDERR: ^~~~~~~ +// CHECK:STDERR: +var x: () = if true then () else (); + +// CHECK:STDERR: fail_todo_control_flow_init.carbon:[[@LINE+12]]:14: ERROR: Semantics TODO: `Control flow expressions are currently only supported inside functions.`. +// CHECK:STDERR: var x2: () = if false then () else (); +// CHECK:STDERR: ^~~~~~~~ +// CHECK:STDERR: +// CHECK:STDERR: fail_todo_control_flow_init.carbon:[[@LINE+8]]:14: ERROR: Semantics TODO: `Control flow expressions are currently only supported inside functions.`. +// CHECK:STDERR: var x2: () = if false then () else (); +// CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~ +// CHECK:STDERR: +// CHECK:STDERR: fail_todo_control_flow_init.carbon:[[@LINE+4]]:23: ERROR: Semantics TODO: `Control flow expressions are currently only supported inside functions.`. +// CHECK:STDERR: var x2: () = if false then () else (); +// CHECK:STDERR: ^~~~~~~ +// CHECK:STDERR: +var x2: () = if false then () else (); + +// CHECK:STDERR: fail_todo_control_flow_init.carbon:[[@LINE+8]]:15: ERROR: Semantics TODO: `Control flow expressions are currently only supported inside functions.`. +// CHECK:STDERR: var y: bool = true or false; +// CHECK:STDERR: ^~~~~~~ +// CHECK:STDERR: +// CHECK:STDERR: fail_todo_control_flow_init.carbon:[[@LINE+4]]:15: ERROR: Semantics TODO: `Control flow expressions are currently only supported inside functions.`. +// CHECK:STDERR: var y: bool = true or false; +// CHECK:STDERR: ^~~~~~~~~~~~~ +// CHECK:STDERR: +var y: bool = true or false; + +// CHECK:STDERR: fail_todo_control_flow_init.carbon:[[@LINE+7]]:16: ERROR: Semantics TODO: `Control flow expressions are currently only supported inside functions.`. +// CHECK:STDERR: var y2: bool = false or true; +// CHECK:STDERR: ^~~~~~~~ +// CHECK:STDERR: +// CHECK:STDERR: fail_todo_control_flow_init.carbon:[[@LINE+3]]:16: ERROR: Semantics TODO: `Control flow expressions are currently only supported inside functions.`. +// CHECK:STDERR: var y2: bool = false or true; +// CHECK:STDERR: ^~~~~~~~~~~~~ +var y2: bool = false or true; + +// CHECK:STDOUT: --- fail_todo_control_flow_init.carbon +// CHECK:STDOUT: +// CHECK:STDOUT: constants { +// CHECK:STDOUT: %.1: type = tuple_type () [template] +// CHECK:STDOUT: %.2: bool = bool_literal true [template] +// CHECK:STDOUT: %tuple: () = tuple_value () [template] +// CHECK:STDOUT: %.3: bool = bool_literal false [template] +// CHECK:STDOUT: } +// CHECK:STDOUT: +// CHECK:STDOUT: file { +// CHECK:STDOUT: package: = namespace [template] { +// CHECK:STDOUT: .Core = %Core +// CHECK:STDOUT: .x = %x +// CHECK:STDOUT: .x2 = %x2 +// CHECK:STDOUT: .y = %y +// CHECK:STDOUT: .y2 = %y2 +// CHECK:STDOUT: } +// CHECK:STDOUT: %Core: = namespace [template] {} +// CHECK:STDOUT: %.loc19_9.1: () = tuple_literal () +// CHECK:STDOUT: %.loc19_9.2: type = converted %.loc19_9.1, constants.%.1 [template = constants.%.1] +// CHECK:STDOUT: %x.var: ref () = var x +// CHECK:STDOUT: %x: ref () = bind_name x, %x.var +// CHECK:STDOUT: %.loc33_10.1: () = tuple_literal () +// CHECK:STDOUT: %.loc33_10.2: type = converted %.loc33_10.1, constants.%.1 [template = constants.%.1] +// CHECK:STDOUT: %x2.var: ref () = var x2 +// CHECK:STDOUT: %x2: ref () = bind_name x2, %x2.var +// CHECK:STDOUT: %y.var: ref bool = var y +// CHECK:STDOUT: %y: ref bool = bind_name y, %y.var +// CHECK:STDOUT: %y2.var: ref bool = var y2 +// CHECK:STDOUT: %y2: ref bool = bind_name y2, %y2.var +// CHECK:STDOUT: } +// CHECK:STDOUT: +// CHECK:STDOUT: fn @__global_init() { +// CHECK:STDOUT: !entry: +// CHECK:STDOUT: %.loc19: bool = bool_literal true [template = constants.%.2] +// CHECK:STDOUT: if %.loc19 br !if.expr.then else br !if.expr.else +// CHECK:STDOUT: } +// CHECK:STDOUT: