while.carbon 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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/while/while.carbon
  10. // TIP: To dump output, run:
  11. // TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/while/while.carbon
  12. // --- while.carbon
  13. library "[[@TEST_NAME]]";
  14. fn Cond() -> bool;
  15. fn F();
  16. fn G();
  17. fn H();
  18. //@dump-sem-ir-begin
  19. fn While() {
  20. F();
  21. while (Cond()) {
  22. G();
  23. }
  24. H();
  25. }
  26. //@dump-sem-ir-end
  27. // --- unreachable_end.carbon
  28. library "[[@TEST_NAME]]";
  29. fn Cond() -> bool;
  30. fn F();
  31. fn G();
  32. fn H();
  33. //@dump-sem-ir-begin
  34. fn While() {
  35. F();
  36. while (Cond()) {
  37. G();
  38. return;
  39. }
  40. H();
  41. }
  42. //@dump-sem-ir-end
  43. // --- break_continue.carbon
  44. library "[[@TEST_NAME]]";
  45. fn A() -> bool;
  46. fn B() -> bool;
  47. fn C() -> bool;
  48. fn D() -> bool;
  49. fn E() -> bool;
  50. fn F() -> bool;
  51. fn G() -> bool;
  52. fn H() -> bool;
  53. //@dump-sem-ir-begin
  54. fn While() {
  55. while (A()) {
  56. if (B()) { continue; }
  57. if (C()) { break; }
  58. while (D()) {
  59. if (E()) { continue; }
  60. if (F()) { break; }
  61. }
  62. if (G()) { continue; }
  63. if (H()) { break; }
  64. }
  65. }
  66. //@dump-sem-ir-end
  67. // --- fail_bad_condition.carbon
  68. library "[[@TEST_NAME]]";
  69. fn While() {
  70. // CHECK:STDERR: fail_bad_condition.carbon:[[@LINE+7]]:9: error: cannot implicitly convert expression of type `{}` to `bool` [ConversionFailure]
  71. // CHECK:STDERR: while ({}) {}
  72. // CHECK:STDERR: ^~~~
  73. // CHECK:STDERR: fail_bad_condition.carbon:[[@LINE+4]]:9: note: type `{}` does not implement interface `Core.ImplicitAs(bool)` [MissingImplInMemberAccessNote]
  74. // CHECK:STDERR: while ({}) {}
  75. // CHECK:STDERR: ^~~~
  76. // CHECK:STDERR:
  77. while ({}) {}
  78. }
  79. // --- fail_bad_break_continue.carbon
  80. library "[[@TEST_NAME]]";
  81. fn While() {
  82. // CHECK:STDERR: fail_bad_break_continue.carbon:[[@LINE+4]]:3: error: `continue` can only be used in a loop [ContinueOutsideLoop]
  83. // CHECK:STDERR: continue;
  84. // CHECK:STDERR: ^~~~~~~~
  85. // CHECK:STDERR:
  86. continue;
  87. // CHECK:STDERR: fail_bad_break_continue.carbon:[[@LINE+4]]:3: error: `break` can only be used in a loop [BreakOutsideLoop]
  88. // CHECK:STDERR: break;
  89. // CHECK:STDERR: ^~~~~
  90. // CHECK:STDERR:
  91. break;
  92. while (false) {
  93. continue;
  94. break;
  95. }
  96. // CHECK:STDERR: fail_bad_break_continue.carbon:[[@LINE+4]]:3: error: `continue` can only be used in a loop [ContinueOutsideLoop]
  97. // CHECK:STDERR: continue;
  98. // CHECK:STDERR: ^~~~~~~~
  99. // CHECK:STDERR:
  100. continue;
  101. // CHECK:STDERR: fail_bad_break_continue.carbon:[[@LINE+4]]:3: error: `break` can only be used in a loop [BreakOutsideLoop]
  102. // CHECK:STDERR: break;
  103. // CHECK:STDERR: ^~~~~
  104. // CHECK:STDERR:
  105. break;
  106. }
  107. // CHECK:STDOUT: --- while.carbon
  108. // CHECK:STDOUT:
  109. // CHECK:STDOUT: constants {
  110. // CHECK:STDOUT: %Cond.type: type = fn_type @Cond [concrete]
  111. // CHECK:STDOUT: %empty_tuple.type: type = tuple_type () [concrete]
  112. // CHECK:STDOUT: %Cond: %Cond.type = struct_value () [concrete]
  113. // CHECK:STDOUT: %F.type: type = fn_type @F [concrete]
  114. // CHECK:STDOUT: %F: %F.type = struct_value () [concrete]
  115. // CHECK:STDOUT: %G.type: type = fn_type @G [concrete]
  116. // CHECK:STDOUT: %G: %G.type = struct_value () [concrete]
  117. // CHECK:STDOUT: %H.type: type = fn_type @H [concrete]
  118. // CHECK:STDOUT: %H: %H.type = struct_value () [concrete]
  119. // CHECK:STDOUT: %While.type: type = fn_type @While [concrete]
  120. // CHECK:STDOUT: %While: %While.type = struct_value () [concrete]
  121. // CHECK:STDOUT: }
  122. // CHECK:STDOUT:
  123. // CHECK:STDOUT: imports {
  124. // CHECK:STDOUT: }
  125. // CHECK:STDOUT:
  126. // CHECK:STDOUT: file {
  127. // CHECK:STDOUT: %While.decl: %While.type = fn_decl @While [concrete = constants.%While] {} {}
  128. // CHECK:STDOUT: }
  129. // CHECK:STDOUT:
  130. // CHECK:STDOUT: fn @While() {
  131. // CHECK:STDOUT: !entry:
  132. // CHECK:STDOUT: %F.ref: %F.type = name_ref F, file.%F.decl [concrete = constants.%F]
  133. // CHECK:STDOUT: %F.call: init %empty_tuple.type = call %F.ref()
  134. // CHECK:STDOUT: br !while.cond
  135. // CHECK:STDOUT:
  136. // CHECK:STDOUT: !while.cond:
  137. // CHECK:STDOUT: %Cond.ref: %Cond.type = name_ref Cond, file.%Cond.decl [concrete = constants.%Cond]
  138. // CHECK:STDOUT: %Cond.call: init bool = call %Cond.ref()
  139. // CHECK:STDOUT: %.loc12_16.1: bool = value_of_initializer %Cond.call
  140. // CHECK:STDOUT: %.loc12_16.2: bool = converted %Cond.call, %.loc12_16.1
  141. // CHECK:STDOUT: if %.loc12_16.2 br !while.body else br !while.done
  142. // CHECK:STDOUT:
  143. // CHECK:STDOUT: !while.body:
  144. // CHECK:STDOUT: %G.ref: %G.type = name_ref G, file.%G.decl [concrete = constants.%G]
  145. // CHECK:STDOUT: %G.call: init %empty_tuple.type = call %G.ref()
  146. // CHECK:STDOUT: br !while.cond
  147. // CHECK:STDOUT:
  148. // CHECK:STDOUT: !while.done:
  149. // CHECK:STDOUT: %H.ref: %H.type = name_ref H, file.%H.decl [concrete = constants.%H]
  150. // CHECK:STDOUT: %H.call: init %empty_tuple.type = call %H.ref()
  151. // CHECK:STDOUT: return
  152. // CHECK:STDOUT: }
  153. // CHECK:STDOUT:
  154. // CHECK:STDOUT: --- unreachable_end.carbon
  155. // CHECK:STDOUT:
  156. // CHECK:STDOUT: constants {
  157. // CHECK:STDOUT: %Cond.type: type = fn_type @Cond [concrete]
  158. // CHECK:STDOUT: %empty_tuple.type: type = tuple_type () [concrete]
  159. // CHECK:STDOUT: %Cond: %Cond.type = struct_value () [concrete]
  160. // CHECK:STDOUT: %F.type: type = fn_type @F [concrete]
  161. // CHECK:STDOUT: %F: %F.type = struct_value () [concrete]
  162. // CHECK:STDOUT: %G.type: type = fn_type @G [concrete]
  163. // CHECK:STDOUT: %G: %G.type = struct_value () [concrete]
  164. // CHECK:STDOUT: %H.type: type = fn_type @H [concrete]
  165. // CHECK:STDOUT: %H: %H.type = struct_value () [concrete]
  166. // CHECK:STDOUT: %While.type: type = fn_type @While [concrete]
  167. // CHECK:STDOUT: %While: %While.type = struct_value () [concrete]
  168. // CHECK:STDOUT: }
  169. // CHECK:STDOUT:
  170. // CHECK:STDOUT: imports {
  171. // CHECK:STDOUT: }
  172. // CHECK:STDOUT:
  173. // CHECK:STDOUT: file {
  174. // CHECK:STDOUT: %While.decl: %While.type = fn_decl @While [concrete = constants.%While] {} {}
  175. // CHECK:STDOUT: }
  176. // CHECK:STDOUT:
  177. // CHECK:STDOUT: fn @While() {
  178. // CHECK:STDOUT: !entry:
  179. // CHECK:STDOUT: %F.ref: %F.type = name_ref F, file.%F.decl [concrete = constants.%F]
  180. // CHECK:STDOUT: %F.call: init %empty_tuple.type = call %F.ref()
  181. // CHECK:STDOUT: br !while.cond
  182. // CHECK:STDOUT:
  183. // CHECK:STDOUT: !while.cond:
  184. // CHECK:STDOUT: %Cond.ref: %Cond.type = name_ref Cond, file.%Cond.decl [concrete = constants.%Cond]
  185. // CHECK:STDOUT: %Cond.call: init bool = call %Cond.ref()
  186. // CHECK:STDOUT: %.loc12_16.1: bool = value_of_initializer %Cond.call
  187. // CHECK:STDOUT: %.loc12_16.2: bool = converted %Cond.call, %.loc12_16.1
  188. // CHECK:STDOUT: if %.loc12_16.2 br !while.body else br !while.done
  189. // CHECK:STDOUT:
  190. // CHECK:STDOUT: !while.body:
  191. // CHECK:STDOUT: %G.ref: %G.type = name_ref G, file.%G.decl [concrete = constants.%G]
  192. // CHECK:STDOUT: %G.call: init %empty_tuple.type = call %G.ref()
  193. // CHECK:STDOUT: return
  194. // CHECK:STDOUT:
  195. // CHECK:STDOUT: !while.done:
  196. // CHECK:STDOUT: %H.ref: %H.type = name_ref H, file.%H.decl [concrete = constants.%H]
  197. // CHECK:STDOUT: %H.call: init %empty_tuple.type = call %H.ref()
  198. // CHECK:STDOUT: return
  199. // CHECK:STDOUT: }
  200. // CHECK:STDOUT:
  201. // CHECK:STDOUT: --- break_continue.carbon
  202. // CHECK:STDOUT:
  203. // CHECK:STDOUT: constants {
  204. // CHECK:STDOUT: %A.type: type = fn_type @A [concrete]
  205. // CHECK:STDOUT: %A: %A.type = struct_value () [concrete]
  206. // CHECK:STDOUT: %B.type: type = fn_type @B [concrete]
  207. // CHECK:STDOUT: %B: %B.type = struct_value () [concrete]
  208. // CHECK:STDOUT: %C.type: type = fn_type @C [concrete]
  209. // CHECK:STDOUT: %C: %C.type = struct_value () [concrete]
  210. // CHECK:STDOUT: %D.type: type = fn_type @D [concrete]
  211. // CHECK:STDOUT: %D: %D.type = struct_value () [concrete]
  212. // CHECK:STDOUT: %E.type: type = fn_type @E [concrete]
  213. // CHECK:STDOUT: %E: %E.type = struct_value () [concrete]
  214. // CHECK:STDOUT: %F.type: type = fn_type @F [concrete]
  215. // CHECK:STDOUT: %F: %F.type = struct_value () [concrete]
  216. // CHECK:STDOUT: %G.type: type = fn_type @G [concrete]
  217. // CHECK:STDOUT: %G: %G.type = struct_value () [concrete]
  218. // CHECK:STDOUT: %H.type: type = fn_type @H [concrete]
  219. // CHECK:STDOUT: %H: %H.type = struct_value () [concrete]
  220. // CHECK:STDOUT: %While.type: type = fn_type @While [concrete]
  221. // CHECK:STDOUT: %While: %While.type = struct_value () [concrete]
  222. // CHECK:STDOUT: }
  223. // CHECK:STDOUT:
  224. // CHECK:STDOUT: imports {
  225. // CHECK:STDOUT: }
  226. // CHECK:STDOUT:
  227. // CHECK:STDOUT: file {
  228. // CHECK:STDOUT: %While.decl: %While.type = fn_decl @While [concrete = constants.%While] {} {}
  229. // CHECK:STDOUT: }
  230. // CHECK:STDOUT:
  231. // CHECK:STDOUT: fn @While() {
  232. // CHECK:STDOUT: !entry:
  233. // CHECK:STDOUT: br !while.cond.loc14
  234. // CHECK:STDOUT:
  235. // CHECK:STDOUT: !while.cond.loc14:
  236. // CHECK:STDOUT: %A.ref: %A.type = name_ref A, file.%A.decl [concrete = constants.%A]
  237. // CHECK:STDOUT: %A.call: init bool = call %A.ref()
  238. // CHECK:STDOUT: %.loc14_13.1: bool = value_of_initializer %A.call
  239. // CHECK:STDOUT: %.loc14_13.2: bool = converted %A.call, %.loc14_13.1
  240. // CHECK:STDOUT: if %.loc14_13.2 br !while.body.loc14 else br !while.done.loc14
  241. // CHECK:STDOUT:
  242. // CHECK:STDOUT: !while.body.loc14:
  243. // CHECK:STDOUT: %B.ref: %B.type = name_ref B, file.%B.decl [concrete = constants.%B]
  244. // CHECK:STDOUT: %B.call: init bool = call %B.ref()
  245. // CHECK:STDOUT: %.loc15_12.1: bool = value_of_initializer %B.call
  246. // CHECK:STDOUT: %.loc15_12.2: bool = converted %B.call, %.loc15_12.1
  247. // CHECK:STDOUT: if %.loc15_12.2 br !if.then.loc15 else br !if.else.loc15
  248. // CHECK:STDOUT:
  249. // CHECK:STDOUT: !if.then.loc15:
  250. // CHECK:STDOUT: br !while.cond.loc14
  251. // CHECK:STDOUT:
  252. // CHECK:STDOUT: !if.else.loc15:
  253. // CHECK:STDOUT: %C.ref: %C.type = name_ref C, file.%C.decl [concrete = constants.%C]
  254. // CHECK:STDOUT: %C.call: init bool = call %C.ref()
  255. // CHECK:STDOUT: %.loc16_12.1: bool = value_of_initializer %C.call
  256. // CHECK:STDOUT: %.loc16_12.2: bool = converted %C.call, %.loc16_12.1
  257. // CHECK:STDOUT: if %.loc16_12.2 br !if.then.loc16 else br !if.else.loc16
  258. // CHECK:STDOUT:
  259. // CHECK:STDOUT: !if.then.loc16:
  260. // CHECK:STDOUT: br !while.done.loc14
  261. // CHECK:STDOUT:
  262. // CHECK:STDOUT: !if.else.loc16:
  263. // CHECK:STDOUT: br !while.cond.loc17
  264. // CHECK:STDOUT:
  265. // CHECK:STDOUT: !while.cond.loc17:
  266. // CHECK:STDOUT: %D.ref: %D.type = name_ref D, file.%D.decl [concrete = constants.%D]
  267. // CHECK:STDOUT: %D.call: init bool = call %D.ref()
  268. // CHECK:STDOUT: %.loc17_15.1: bool = value_of_initializer %D.call
  269. // CHECK:STDOUT: %.loc17_15.2: bool = converted %D.call, %.loc17_15.1
  270. // CHECK:STDOUT: if %.loc17_15.2 br !while.body.loc17 else br !while.done.loc17
  271. // CHECK:STDOUT:
  272. // CHECK:STDOUT: !while.body.loc17:
  273. // CHECK:STDOUT: %E.ref: %E.type = name_ref E, file.%E.decl [concrete = constants.%E]
  274. // CHECK:STDOUT: %E.call: init bool = call %E.ref()
  275. // CHECK:STDOUT: %.loc18_14.1: bool = value_of_initializer %E.call
  276. // CHECK:STDOUT: %.loc18_14.2: bool = converted %E.call, %.loc18_14.1
  277. // CHECK:STDOUT: if %.loc18_14.2 br !if.then.loc18 else br !if.else.loc18
  278. // CHECK:STDOUT:
  279. // CHECK:STDOUT: !if.then.loc18:
  280. // CHECK:STDOUT: br !while.cond.loc17
  281. // CHECK:STDOUT:
  282. // CHECK:STDOUT: !if.else.loc18:
  283. // CHECK:STDOUT: %F.ref: %F.type = name_ref F, file.%F.decl [concrete = constants.%F]
  284. // CHECK:STDOUT: %F.call: init bool = call %F.ref()
  285. // CHECK:STDOUT: %.loc19_14.1: bool = value_of_initializer %F.call
  286. // CHECK:STDOUT: %.loc19_14.2: bool = converted %F.call, %.loc19_14.1
  287. // CHECK:STDOUT: if %.loc19_14.2 br !if.then.loc19 else br !if.else.loc19
  288. // CHECK:STDOUT:
  289. // CHECK:STDOUT: !if.then.loc19:
  290. // CHECK:STDOUT: br !while.done.loc17
  291. // CHECK:STDOUT:
  292. // CHECK:STDOUT: !if.else.loc19:
  293. // CHECK:STDOUT: br !while.cond.loc17
  294. // CHECK:STDOUT:
  295. // CHECK:STDOUT: !while.done.loc17:
  296. // CHECK:STDOUT: %G.ref: %G.type = name_ref G, file.%G.decl [concrete = constants.%G]
  297. // CHECK:STDOUT: %G.call: init bool = call %G.ref()
  298. // CHECK:STDOUT: %.loc21_12.1: bool = value_of_initializer %G.call
  299. // CHECK:STDOUT: %.loc21_12.2: bool = converted %G.call, %.loc21_12.1
  300. // CHECK:STDOUT: if %.loc21_12.2 br !if.then.loc21 else br !if.else.loc21
  301. // CHECK:STDOUT:
  302. // CHECK:STDOUT: !if.then.loc21:
  303. // CHECK:STDOUT: br !while.cond.loc14
  304. // CHECK:STDOUT:
  305. // CHECK:STDOUT: !if.else.loc21:
  306. // CHECK:STDOUT: %H.ref: %H.type = name_ref H, file.%H.decl [concrete = constants.%H]
  307. // CHECK:STDOUT: %H.call: init bool = call %H.ref()
  308. // CHECK:STDOUT: %.loc22_12.1: bool = value_of_initializer %H.call
  309. // CHECK:STDOUT: %.loc22_12.2: bool = converted %H.call, %.loc22_12.1
  310. // CHECK:STDOUT: if %.loc22_12.2 br !if.then.loc22 else br !if.else.loc22
  311. // CHECK:STDOUT:
  312. // CHECK:STDOUT: !if.then.loc22:
  313. // CHECK:STDOUT: br !while.done.loc14
  314. // CHECK:STDOUT:
  315. // CHECK:STDOUT: !if.else.loc22:
  316. // CHECK:STDOUT: br !while.cond.loc14
  317. // CHECK:STDOUT:
  318. // CHECK:STDOUT: !while.done.loc14:
  319. // CHECK:STDOUT: return
  320. // CHECK:STDOUT: }
  321. // CHECK:STDOUT: