Explorar el Código

Fix CHECK failure when there is control flow in a global variable initializer (#3925)

Problem found by fuzzing.

---------

Co-authored-by: Josh L <josh11b@users.noreply.github.com>
josh11b hace 2 años
padre
commit
4f2bd0b095

+ 2 - 4
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();
 }
 

+ 3 - 0
toolchain/check/inst_block_stack.h

@@ -124,6 +124,9 @@ class InstBlockStack {
 
   std::vector<SemIR::InstId> init_block_;
 
+  // Current global init block to push.
+  SemIR::InstBlockId init_block_id_ = SemIR::InstBlockId::GlobalInit;
+
   // The actual stack.
   llvm::SmallVector<StackEntry> stack_;
 

+ 91 - 0
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> = 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> = 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: