basics.carbon 12 KB

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