basics.carbon 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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. // INCLUDE-FILE: toolchain/testing/testdata/min_prelude/bool.carbon
  5. //
  6. // AUTOUPDATE
  7. // TIP: To test this file alone, run:
  8. // TIP: bazel test //toolchain/testing:file_test --test_arg=--file_tests=toolchain/check/testdata/if/basics.carbon
  9. // TIP: To dump output, run:
  10. // TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/if/basics.carbon
  11. // --- else.carbon
  12. library "[[@TEST_NAME]]";
  13. fn F() {}
  14. fn G() {}
  15. fn H() {}
  16. //@dump-sem-ir-begin
  17. fn If(b: bool) {
  18. if (b) {
  19. F();
  20. } else {
  21. G();
  22. }
  23. H();
  24. }
  25. //@dump-sem-ir-end
  26. // --- no_else.carbon
  27. library "[[@TEST_NAME]]";
  28. fn F() {}
  29. fn G() {}
  30. //@dump-sem-ir-begin
  31. fn If(b: bool) {
  32. if (b) {
  33. F();
  34. }
  35. G();
  36. }
  37. //@dump-sem-ir-end
  38. // --- unreachable_fallthrough.carbon
  39. library "[[@TEST_NAME]]";
  40. //@dump-sem-ir-begin
  41. fn If(b: bool) -> bool {
  42. if (b) {
  43. return false;
  44. } else {
  45. return true;
  46. }
  47. // Missing return here is OK.
  48. }
  49. //@dump-sem-ir-end
  50. // --- fail_reachable_fallthrough.carbon
  51. library "[[@TEST_NAME]]";
  52. fn If1(b: bool) -> bool {
  53. if (b) {
  54. return false;
  55. } else {
  56. }
  57. // CHECK:STDERR: fail_reachable_fallthrough.carbon:[[@LINE+4]]:1: error: missing `return` at end of function with declared return type [MissingReturnStatement]
  58. // CHECK:STDERR: }
  59. // CHECK:STDERR: ^
  60. // CHECK:STDERR:
  61. }
  62. fn If2(b: bool) -> bool {
  63. if (b) {
  64. } else {
  65. return true;
  66. }
  67. // CHECK:STDERR: fail_reachable_fallthrough.carbon:[[@LINE+4]]:1: error: missing `return` at end of function with declared return type [MissingReturnStatement]
  68. // CHECK:STDERR: }
  69. // CHECK:STDERR: ^
  70. // CHECK:STDERR:
  71. }
  72. fn If3(b: bool) -> bool {
  73. if (b) {
  74. return false;
  75. }
  76. // CHECK:STDERR: fail_reachable_fallthrough.carbon:[[@LINE+4]]:1: error: missing `return` at end of function with declared return type [MissingReturnStatement]
  77. // CHECK:STDERR: }
  78. // CHECK:STDERR: ^
  79. // CHECK:STDERR:
  80. }
  81. // --- fail_scope.carbon
  82. library "[[@TEST_NAME]]";
  83. fn VarScope(b: bool) -> bool {
  84. if (b) {
  85. var n: bool = true;
  86. return n;
  87. }
  88. // CHECK:STDERR: fail_scope.carbon:[[@LINE+4]]:10: error: name `n` not found [NameNotFound]
  89. // CHECK:STDERR: return n;
  90. // CHECK:STDERR: ^
  91. // CHECK:STDERR:
  92. return n;
  93. }
  94. // CHECK:STDOUT: --- else.carbon
  95. // CHECK:STDOUT:
  96. // CHECK:STDOUT: constants {
  97. // CHECK:STDOUT: %F.type: type = fn_type @F [concrete]
  98. // CHECK:STDOUT: %empty_tuple.type: type = tuple_type () [concrete]
  99. // CHECK:STDOUT: %F: %F.type = struct_value () [concrete]
  100. // CHECK:STDOUT: %G.type: type = fn_type @G [concrete]
  101. // CHECK:STDOUT: %G: %G.type = struct_value () [concrete]
  102. // CHECK:STDOUT: %H.type: type = fn_type @H [concrete]
  103. // CHECK:STDOUT: %H: %H.type = struct_value () [concrete]
  104. // CHECK:STDOUT: %Bool.type: type = fn_type @Bool [concrete]
  105. // CHECK:STDOUT: %Bool: %Bool.type = struct_value () [concrete]
  106. // CHECK:STDOUT: %pattern_type.831: type = pattern_type bool [concrete]
  107. // CHECK:STDOUT: %If.type: type = fn_type @If [concrete]
  108. // CHECK:STDOUT: %If: %If.type = struct_value () [concrete]
  109. // CHECK:STDOUT: }
  110. // CHECK:STDOUT:
  111. // CHECK:STDOUT: imports {
  112. // CHECK:STDOUT: }
  113. // CHECK:STDOUT:
  114. // CHECK:STDOUT: file {
  115. // CHECK:STDOUT: %If.decl: %If.type = fn_decl @If [concrete = constants.%If] {
  116. // CHECK:STDOUT: %b.patt: %pattern_type.831 = binding_pattern b [concrete]
  117. // CHECK:STDOUT: %b.param_patt: %pattern_type.831 = value_param_pattern %b.patt, call_param0 [concrete]
  118. // CHECK:STDOUT: } {
  119. // CHECK:STDOUT: %b.param: bool = value_param call_param0
  120. // CHECK:STDOUT: %.loc8_10.1: type = splice_block %.loc8_10.3 [concrete = bool] {
  121. // CHECK:STDOUT: %bool.make_type: init type = call constants.%Bool() [concrete = bool]
  122. // CHECK:STDOUT: %.loc8_10.2: type = value_of_initializer %bool.make_type [concrete = bool]
  123. // CHECK:STDOUT: %.loc8_10.3: type = converted %bool.make_type, %.loc8_10.2 [concrete = bool]
  124. // CHECK:STDOUT: }
  125. // CHECK:STDOUT: %b: bool = bind_name b, %b.param
  126. // CHECK:STDOUT: }
  127. // CHECK:STDOUT: }
  128. // CHECK:STDOUT:
  129. // CHECK:STDOUT: fn @If(%b.param: bool) {
  130. // CHECK:STDOUT: !entry:
  131. // CHECK:STDOUT: %b.ref: bool = name_ref b, %b
  132. // CHECK:STDOUT: if %b.ref br !if.then else br !if.else
  133. // CHECK:STDOUT:
  134. // CHECK:STDOUT: !if.then:
  135. // CHECK:STDOUT: %F.ref: %F.type = name_ref F, file.%F.decl [concrete = constants.%F]
  136. // CHECK:STDOUT: %F.call: init %empty_tuple.type = call %F.ref()
  137. // CHECK:STDOUT: br !if.done
  138. // CHECK:STDOUT:
  139. // CHECK:STDOUT: !if.else:
  140. // CHECK:STDOUT: %G.ref: %G.type = name_ref G, file.%G.decl [concrete = constants.%G]
  141. // CHECK:STDOUT: %G.call: init %empty_tuple.type = call %G.ref()
  142. // CHECK:STDOUT: br !if.done
  143. // CHECK:STDOUT:
  144. // CHECK:STDOUT: !if.done:
  145. // CHECK:STDOUT: %H.ref: %H.type = name_ref H, file.%H.decl [concrete = constants.%H]
  146. // CHECK:STDOUT: %H.call: init %empty_tuple.type = call %H.ref()
  147. // CHECK:STDOUT: return
  148. // CHECK:STDOUT: }
  149. // CHECK:STDOUT:
  150. // CHECK:STDOUT: --- no_else.carbon
  151. // CHECK:STDOUT:
  152. // CHECK:STDOUT: constants {
  153. // CHECK:STDOUT: %F.type: type = fn_type @F [concrete]
  154. // CHECK:STDOUT: %empty_tuple.type: type = tuple_type () [concrete]
  155. // CHECK:STDOUT: %F: %F.type = struct_value () [concrete]
  156. // CHECK:STDOUT: %G.type: type = fn_type @G [concrete]
  157. // CHECK:STDOUT: %G: %G.type = struct_value () [concrete]
  158. // CHECK:STDOUT: %Bool.type: type = fn_type @Bool [concrete]
  159. // CHECK:STDOUT: %Bool: %Bool.type = struct_value () [concrete]
  160. // CHECK:STDOUT: %pattern_type.831: type = pattern_type bool [concrete]
  161. // CHECK:STDOUT: %If.type: type = fn_type @If [concrete]
  162. // CHECK:STDOUT: %If: %If.type = struct_value () [concrete]
  163. // CHECK:STDOUT: }
  164. // CHECK:STDOUT:
  165. // CHECK:STDOUT: imports {
  166. // CHECK:STDOUT: }
  167. // CHECK:STDOUT:
  168. // CHECK:STDOUT: file {
  169. // CHECK:STDOUT: %If.decl: %If.type = fn_decl @If [concrete = constants.%If] {
  170. // CHECK:STDOUT: %b.patt: %pattern_type.831 = binding_pattern b [concrete]
  171. // CHECK:STDOUT: %b.param_patt: %pattern_type.831 = value_param_pattern %b.patt, call_param0 [concrete]
  172. // CHECK:STDOUT: } {
  173. // CHECK:STDOUT: %b.param: bool = value_param call_param0
  174. // CHECK:STDOUT: %.loc7_10.1: type = splice_block %.loc7_10.3 [concrete = bool] {
  175. // CHECK:STDOUT: %bool.make_type: init type = call constants.%Bool() [concrete = bool]
  176. // CHECK:STDOUT: %.loc7_10.2: type = value_of_initializer %bool.make_type [concrete = bool]
  177. // CHECK:STDOUT: %.loc7_10.3: type = converted %bool.make_type, %.loc7_10.2 [concrete = bool]
  178. // CHECK:STDOUT: }
  179. // CHECK:STDOUT: %b: bool = bind_name b, %b.param
  180. // CHECK:STDOUT: }
  181. // CHECK:STDOUT: }
  182. // CHECK:STDOUT:
  183. // CHECK:STDOUT: fn @If(%b.param: bool) {
  184. // CHECK:STDOUT: !entry:
  185. // CHECK:STDOUT: %b.ref: bool = name_ref b, %b
  186. // CHECK:STDOUT: if %b.ref br !if.then else br !if.else
  187. // CHECK:STDOUT:
  188. // CHECK:STDOUT: !if.then:
  189. // CHECK:STDOUT: %F.ref: %F.type = name_ref F, file.%F.decl [concrete = constants.%F]
  190. // CHECK:STDOUT: %F.call: init %empty_tuple.type = call %F.ref()
  191. // CHECK:STDOUT: br !if.else
  192. // CHECK:STDOUT:
  193. // CHECK:STDOUT: !if.else:
  194. // CHECK:STDOUT: %G.ref: %G.type = name_ref G, file.%G.decl [concrete = constants.%G]
  195. // CHECK:STDOUT: %G.call: init %empty_tuple.type = call %G.ref()
  196. // CHECK:STDOUT: return
  197. // CHECK:STDOUT: }
  198. // CHECK:STDOUT:
  199. // CHECK:STDOUT: --- unreachable_fallthrough.carbon
  200. // CHECK:STDOUT:
  201. // CHECK:STDOUT: constants {
  202. // CHECK:STDOUT: %Bool.type: type = fn_type @Bool [concrete]
  203. // CHECK:STDOUT: %Bool: %Bool.type = struct_value () [concrete]
  204. // CHECK:STDOUT: %pattern_type.831: type = pattern_type bool [concrete]
  205. // CHECK:STDOUT: %If.type: type = fn_type @If [concrete]
  206. // CHECK:STDOUT: %If: %If.type = struct_value () [concrete]
  207. // CHECK:STDOUT: %false: bool = bool_literal false [concrete]
  208. // CHECK:STDOUT: %true: bool = bool_literal true [concrete]
  209. // CHECK:STDOUT: }
  210. // CHECK:STDOUT:
  211. // CHECK:STDOUT: imports {
  212. // CHECK:STDOUT: }
  213. // CHECK:STDOUT:
  214. // CHECK:STDOUT: file {
  215. // CHECK:STDOUT: %If.decl: %If.type = fn_decl @If [concrete = constants.%If] {
  216. // CHECK:STDOUT: %b.patt: %pattern_type.831 = binding_pattern b [concrete]
  217. // CHECK:STDOUT: %b.param_patt: %pattern_type.831 = value_param_pattern %b.patt, call_param0 [concrete]
  218. // CHECK:STDOUT: %return.patt: %pattern_type.831 = return_slot_pattern [concrete]
  219. // CHECK:STDOUT: %return.param_patt: %pattern_type.831 = out_param_pattern %return.patt, call_param1 [concrete]
  220. // CHECK:STDOUT: } {
  221. // CHECK:STDOUT: %bool.make_type.loc4_19: init type = call constants.%Bool() [concrete = bool]
  222. // CHECK:STDOUT: %.loc4_19.1: type = value_of_initializer %bool.make_type.loc4_19 [concrete = bool]
  223. // CHECK:STDOUT: %.loc4_19.2: type = converted %bool.make_type.loc4_19, %.loc4_19.1 [concrete = bool]
  224. // CHECK:STDOUT: %b.param: bool = value_param call_param0
  225. // CHECK:STDOUT: %.loc4_10.1: type = splice_block %.loc4_10.3 [concrete = bool] {
  226. // CHECK:STDOUT: %bool.make_type.loc4_10: init type = call constants.%Bool() [concrete = bool]
  227. // CHECK:STDOUT: %.loc4_10.2: type = value_of_initializer %bool.make_type.loc4_10 [concrete = bool]
  228. // CHECK:STDOUT: %.loc4_10.3: type = converted %bool.make_type.loc4_10, %.loc4_10.2 [concrete = bool]
  229. // CHECK:STDOUT: }
  230. // CHECK:STDOUT: %b: bool = bind_name b, %b.param
  231. // CHECK:STDOUT: %return.param: ref bool = out_param call_param1
  232. // CHECK:STDOUT: %return: ref bool = return_slot %return.param
  233. // CHECK:STDOUT: }
  234. // CHECK:STDOUT: }
  235. // CHECK:STDOUT:
  236. // CHECK:STDOUT: fn @If(%b.param: bool) -> bool {
  237. // CHECK:STDOUT: !entry:
  238. // CHECK:STDOUT: %b.ref: bool = name_ref b, %b
  239. // CHECK:STDOUT: if %b.ref br !if.then else br !if.else
  240. // CHECK:STDOUT:
  241. // CHECK:STDOUT: !if.then:
  242. // CHECK:STDOUT: %false: bool = bool_literal false [concrete = constants.%false]
  243. // CHECK:STDOUT: return %false
  244. // CHECK:STDOUT:
  245. // CHECK:STDOUT: !if.else:
  246. // CHECK:STDOUT: %true: bool = bool_literal true [concrete = constants.%true]
  247. // CHECK:STDOUT: return %true
  248. // CHECK:STDOUT: }
  249. // CHECK:STDOUT: