fail_reachable_fallthrough.carbon 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. // Part of the Carbon Language project, under the Apache License v2.0 with LLVM
  2. // Exceptions. See /LICENSE for license information.
  3. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. //
  5. // AUTOUPDATE
  6. // TIP: To test this file alone, run:
  7. // TIP: bazel test //toolchain/testing:file_test --test_arg=--file_tests=toolchain/check/testdata/if/fail_reachable_fallthrough.carbon
  8. // TIP: To dump output, run:
  9. // TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/if/fail_reachable_fallthrough.carbon
  10. fn If1(b: bool) -> i32 {
  11. if (b) {
  12. return 1;
  13. } else {
  14. }
  15. // CHECK:STDERR: fail_reachable_fallthrough.carbon:[[@LINE+4]]:1: error: missing `return` at end of function with declared return type [MissingReturnStatement]
  16. // CHECK:STDERR: }
  17. // CHECK:STDERR: ^
  18. // CHECK:STDERR:
  19. }
  20. fn If2(b: bool) -> i32 {
  21. if (b) {
  22. } else {
  23. return 2;
  24. }
  25. // CHECK:STDERR: fail_reachable_fallthrough.carbon:[[@LINE+4]]:1: error: missing `return` at end of function with declared return type [MissingReturnStatement]
  26. // CHECK:STDERR: }
  27. // CHECK:STDERR: ^
  28. // CHECK:STDERR:
  29. }
  30. fn If3(b: bool) -> i32 {
  31. if (b) {
  32. return 1;
  33. }
  34. // CHECK:STDERR: fail_reachable_fallthrough.carbon:[[@LINE+3]]:1: error: missing `return` at end of function with declared return type [MissingReturnStatement]
  35. // CHECK:STDERR: }
  36. // CHECK:STDERR: ^
  37. }
  38. // CHECK:STDOUT: --- fail_reachable_fallthrough.carbon
  39. // CHECK:STDOUT:
  40. // CHECK:STDOUT: constants {
  41. // CHECK:STDOUT: %Bool.type: type = fn_type @Bool [template]
  42. // CHECK:STDOUT: %Bool: %Bool.type = struct_value () [template]
  43. // CHECK:STDOUT: %int_32: Core.IntLiteral = int_value 32 [template]
  44. // CHECK:STDOUT: %i32: type = class_type @Int, @Int(%int_32) [template]
  45. // CHECK:STDOUT: %If1.type: type = fn_type @If1 [template]
  46. // CHECK:STDOUT: %If1: %If1.type = struct_value () [template]
  47. // CHECK:STDOUT: %int_1.1: Core.IntLiteral = int_value 1 [template]
  48. // CHECK:STDOUT: %Convert.type.2: type = fn_type @Convert.1, @ImplicitAs(%i32) [template]
  49. // CHECK:STDOUT: %Convert.type.10: type = fn_type @Convert.2, @impl.1(%int_32) [template]
  50. // CHECK:STDOUT: %Convert.10: %Convert.type.10 = struct_value () [template]
  51. // CHECK:STDOUT: %interface.5: <witness> = interface_witness (%Convert.10) [template]
  52. // CHECK:STDOUT: %Convert.bound.1: <bound method> = bound_method %int_1.1, %Convert.10 [template]
  53. // CHECK:STDOUT: %Convert.specific_fn.1: <specific function> = specific_function %Convert.bound.1, @Convert.2(%int_32) [template]
  54. // CHECK:STDOUT: %int_1.2: %i32 = int_value 1 [template]
  55. // CHECK:STDOUT: %If2.type: type = fn_type @If2 [template]
  56. // CHECK:STDOUT: %If2: %If2.type = struct_value () [template]
  57. // CHECK:STDOUT: %int_2.1: Core.IntLiteral = int_value 2 [template]
  58. // CHECK:STDOUT: %Convert.bound.2: <bound method> = bound_method %int_2.1, %Convert.10 [template]
  59. // CHECK:STDOUT: %Convert.specific_fn.2: <specific function> = specific_function %Convert.bound.2, @Convert.2(%int_32) [template]
  60. // CHECK:STDOUT: %int_2.2: %i32 = int_value 2 [template]
  61. // CHECK:STDOUT: %If3.type: type = fn_type @If3 [template]
  62. // CHECK:STDOUT: %If3: %If3.type = struct_value () [template]
  63. // CHECK:STDOUT: }
  64. // CHECK:STDOUT:
  65. // CHECK:STDOUT: imports {
  66. // CHECK:STDOUT: %Core: <namespace> = namespace file.%Core.import, [template] {
  67. // CHECK:STDOUT: .Bool = %import_ref.1
  68. // CHECK:STDOUT: .Int = %import_ref.2
  69. // CHECK:STDOUT: .ImplicitAs = %import_ref.6
  70. // CHECK:STDOUT: import Core//prelude
  71. // CHECK:STDOUT: import Core//prelude/...
  72. // CHECK:STDOUT: }
  73. // CHECK:STDOUT: }
  74. // CHECK:STDOUT:
  75. // CHECK:STDOUT: file {
  76. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  77. // CHECK:STDOUT: .Core = imports.%Core
  78. // CHECK:STDOUT: .If1 = %If1.decl
  79. // CHECK:STDOUT: .If2 = %If2.decl
  80. // CHECK:STDOUT: .If3 = %If3.decl
  81. // CHECK:STDOUT: }
  82. // CHECK:STDOUT: %Core.import = import Core
  83. // CHECK:STDOUT: %If1.decl: %If1.type = fn_decl @If1 [template = constants.%If1] {
  84. // CHECK:STDOUT: %b.patt: bool = binding_pattern b
  85. // CHECK:STDOUT: %b.param_patt: bool = value_param_pattern %b.patt, runtime_param0
  86. // CHECK:STDOUT: %return.patt: %i32 = return_slot_pattern
  87. // CHECK:STDOUT: %return.param_patt: %i32 = out_param_pattern %return.patt, runtime_param1
  88. // CHECK:STDOUT: } {
  89. // CHECK:STDOUT: %int_32: Core.IntLiteral = int_value 32 [template = constants.%int_32]
  90. // CHECK:STDOUT: %i32: type = class_type @Int, @Int(constants.%int_32) [template = constants.%i32]
  91. // CHECK:STDOUT: %b.param: bool = value_param runtime_param0
  92. // CHECK:STDOUT: %.loc11_11.3: type = splice_block %.loc11_11.2 [template = bool] {
  93. // CHECK:STDOUT: %bool.make_type: init type = call constants.%Bool() [template = bool]
  94. // CHECK:STDOUT: %.loc11_11.1: type = value_of_initializer %bool.make_type [template = bool]
  95. // CHECK:STDOUT: %.loc11_11.2: type = converted %bool.make_type, %.loc11_11.1 [template = bool]
  96. // CHECK:STDOUT: }
  97. // CHECK:STDOUT: %b: bool = bind_name b, %b.param
  98. // CHECK:STDOUT: %return.param: ref %i32 = out_param runtime_param1
  99. // CHECK:STDOUT: %return: ref %i32 = return_slot %return.param
  100. // CHECK:STDOUT: }
  101. // CHECK:STDOUT: %If2.decl: %If2.type = fn_decl @If2 [template = constants.%If2] {
  102. // CHECK:STDOUT: %b.patt: bool = binding_pattern b
  103. // CHECK:STDOUT: %b.param_patt: bool = value_param_pattern %b.patt, runtime_param0
  104. // CHECK:STDOUT: %return.patt: %i32 = return_slot_pattern
  105. // CHECK:STDOUT: %return.param_patt: %i32 = out_param_pattern %return.patt, runtime_param1
  106. // CHECK:STDOUT: } {
  107. // CHECK:STDOUT: %int_32: Core.IntLiteral = int_value 32 [template = constants.%int_32]
  108. // CHECK:STDOUT: %i32: type = class_type @Int, @Int(constants.%int_32) [template = constants.%i32]
  109. // CHECK:STDOUT: %b.param: bool = value_param runtime_param0
  110. // CHECK:STDOUT: %.loc22_11.3: type = splice_block %.loc22_11.2 [template = bool] {
  111. // CHECK:STDOUT: %bool.make_type: init type = call constants.%Bool() [template = bool]
  112. // CHECK:STDOUT: %.loc22_11.1: type = value_of_initializer %bool.make_type [template = bool]
  113. // CHECK:STDOUT: %.loc22_11.2: type = converted %bool.make_type, %.loc22_11.1 [template = bool]
  114. // CHECK:STDOUT: }
  115. // CHECK:STDOUT: %b: bool = bind_name b, %b.param
  116. // CHECK:STDOUT: %return.param: ref %i32 = out_param runtime_param1
  117. // CHECK:STDOUT: %return: ref %i32 = return_slot %return.param
  118. // CHECK:STDOUT: }
  119. // CHECK:STDOUT: %If3.decl: %If3.type = fn_decl @If3 [template = constants.%If3] {
  120. // CHECK:STDOUT: %b.patt: bool = binding_pattern b
  121. // CHECK:STDOUT: %b.param_patt: bool = value_param_pattern %b.patt, runtime_param0
  122. // CHECK:STDOUT: %return.patt: %i32 = return_slot_pattern
  123. // CHECK:STDOUT: %return.param_patt: %i32 = out_param_pattern %return.patt, runtime_param1
  124. // CHECK:STDOUT: } {
  125. // CHECK:STDOUT: %int_32: Core.IntLiteral = int_value 32 [template = constants.%int_32]
  126. // CHECK:STDOUT: %i32: type = class_type @Int, @Int(constants.%int_32) [template = constants.%i32]
  127. // CHECK:STDOUT: %b.param: bool = value_param runtime_param0
  128. // CHECK:STDOUT: %.loc33_11.3: type = splice_block %.loc33_11.2 [template = bool] {
  129. // CHECK:STDOUT: %bool.make_type: init type = call constants.%Bool() [template = bool]
  130. // CHECK:STDOUT: %.loc33_11.1: type = value_of_initializer %bool.make_type [template = bool]
  131. // CHECK:STDOUT: %.loc33_11.2: type = converted %bool.make_type, %.loc33_11.1 [template = bool]
  132. // CHECK:STDOUT: }
  133. // CHECK:STDOUT: %b: bool = bind_name b, %b.param
  134. // CHECK:STDOUT: %return.param: ref %i32 = out_param runtime_param1
  135. // CHECK:STDOUT: %return: ref %i32 = return_slot %return.param
  136. // CHECK:STDOUT: }
  137. // CHECK:STDOUT: }
  138. // CHECK:STDOUT:
  139. // CHECK:STDOUT: fn @If1(%b.param_patt: bool) -> %i32 {
  140. // CHECK:STDOUT: !entry:
  141. // CHECK:STDOUT: %b.ref: bool = name_ref b, %b
  142. // CHECK:STDOUT: if %b.ref br !if.then else br !if.else
  143. // CHECK:STDOUT:
  144. // CHECK:STDOUT: !if.then:
  145. // CHECK:STDOUT: %int_1: Core.IntLiteral = int_value 1 [template = constants.%int_1.1]
  146. // CHECK:STDOUT: %impl.elem0: %Convert.type.2 = interface_witness_access constants.%interface.5, element0 [template = constants.%Convert.10]
  147. // CHECK:STDOUT: %Convert.bound: <bound method> = bound_method %int_1, %impl.elem0 [template = constants.%Convert.bound.1]
  148. // CHECK:STDOUT: %Convert.specific_fn: <specific function> = specific_function %Convert.bound, @Convert.2(constants.%int_32) [template = constants.%Convert.specific_fn.1]
  149. // CHECK:STDOUT: %int.convert_checked: init %i32 = call %Convert.specific_fn(%int_1) [template = constants.%int_1.2]
  150. // CHECK:STDOUT: %.loc13_13.1: %i32 = value_of_initializer %int.convert_checked [template = constants.%int_1.2]
  151. // CHECK:STDOUT: %.loc13_13.2: %i32 = converted %int_1, %.loc13_13.1 [template = constants.%int_1.2]
  152. // CHECK:STDOUT: return %.loc13_13.2
  153. // CHECK:STDOUT:
  154. // CHECK:STDOUT: !if.else:
  155. // CHECK:STDOUT: br !if.done
  156. // CHECK:STDOUT:
  157. // CHECK:STDOUT: !if.done:
  158. // CHECK:STDOUT: }
  159. // CHECK:STDOUT:
  160. // CHECK:STDOUT: fn @If2(%b.param_patt: bool) -> %i32 {
  161. // CHECK:STDOUT: !entry:
  162. // CHECK:STDOUT: %b.ref: bool = name_ref b, %b
  163. // CHECK:STDOUT: if %b.ref br !if.then else br !if.else
  164. // CHECK:STDOUT:
  165. // CHECK:STDOUT: !if.then:
  166. // CHECK:STDOUT: br !if.done
  167. // CHECK:STDOUT:
  168. // CHECK:STDOUT: !if.else:
  169. // CHECK:STDOUT: %int_2: Core.IntLiteral = int_value 2 [template = constants.%int_2.1]
  170. // CHECK:STDOUT: %impl.elem0: %Convert.type.2 = interface_witness_access constants.%interface.5, element0 [template = constants.%Convert.10]
  171. // CHECK:STDOUT: %Convert.bound: <bound method> = bound_method %int_2, %impl.elem0 [template = constants.%Convert.bound.2]
  172. // CHECK:STDOUT: %Convert.specific_fn: <specific function> = specific_function %Convert.bound, @Convert.2(constants.%int_32) [template = constants.%Convert.specific_fn.2]
  173. // CHECK:STDOUT: %int.convert_checked: init %i32 = call %Convert.specific_fn(%int_2) [template = constants.%int_2.2]
  174. // CHECK:STDOUT: %.loc25_13.1: %i32 = value_of_initializer %int.convert_checked [template = constants.%int_2.2]
  175. // CHECK:STDOUT: %.loc25_13.2: %i32 = converted %int_2, %.loc25_13.1 [template = constants.%int_2.2]
  176. // CHECK:STDOUT: return %.loc25_13.2
  177. // CHECK:STDOUT:
  178. // CHECK:STDOUT: !if.done:
  179. // CHECK:STDOUT: }
  180. // CHECK:STDOUT:
  181. // CHECK:STDOUT: fn @If3(%b.param_patt: bool) -> %i32 {
  182. // CHECK:STDOUT: !entry:
  183. // CHECK:STDOUT: %b.ref: bool = name_ref b, %b
  184. // CHECK:STDOUT: if %b.ref br !if.then else br !if.else
  185. // CHECK:STDOUT:
  186. // CHECK:STDOUT: !if.then:
  187. // CHECK:STDOUT: %int_1: Core.IntLiteral = int_value 1 [template = constants.%int_1.1]
  188. // CHECK:STDOUT: %impl.elem0: %Convert.type.2 = interface_witness_access constants.%interface.5, element0 [template = constants.%Convert.10]
  189. // CHECK:STDOUT: %Convert.bound: <bound method> = bound_method %int_1, %impl.elem0 [template = constants.%Convert.bound.1]
  190. // CHECK:STDOUT: %Convert.specific_fn: <specific function> = specific_function %Convert.bound, @Convert.2(constants.%int_32) [template = constants.%Convert.specific_fn.1]
  191. // CHECK:STDOUT: %int.convert_checked: init %i32 = call %Convert.specific_fn(%int_1) [template = constants.%int_1.2]
  192. // CHECK:STDOUT: %.loc35_13.1: %i32 = value_of_initializer %int.convert_checked [template = constants.%int_1.2]
  193. // CHECK:STDOUT: %.loc35_13.2: %i32 = converted %int_1, %.loc35_13.1 [template = constants.%int_1.2]
  194. // CHECK:STDOUT: return %.loc35_13.2
  195. // CHECK:STDOUT:
  196. // CHECK:STDOUT: !if.else:
  197. // CHECK:STDOUT: }
  198. // CHECK:STDOUT: