while.carbon 13 KB

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