fail_reachable_fallthrough.carbon 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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
  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
  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
  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: %.1: type = tuple_type () [template]
  43. // CHECK:STDOUT: %Bool: %Bool.type = struct_value () [template]
  44. // CHECK:STDOUT: %Int32.type: type = fn_type @Int32 [template]
  45. // CHECK:STDOUT: %Int32: %Int32.type = struct_value () [template]
  46. // CHECK:STDOUT: %If1.type: type = fn_type @If1 [template]
  47. // CHECK:STDOUT: %If1: %If1.type = struct_value () [template]
  48. // CHECK:STDOUT: %.2: i32 = int_literal 1 [template]
  49. // CHECK:STDOUT: %If2.type: type = fn_type @If2 [template]
  50. // CHECK:STDOUT: %If2: %If2.type = struct_value () [template]
  51. // CHECK:STDOUT: %.3: i32 = int_literal 2 [template]
  52. // CHECK:STDOUT: %If3.type: type = fn_type @If3 [template]
  53. // CHECK:STDOUT: %If3: %If3.type = struct_value () [template]
  54. // CHECK:STDOUT: }
  55. // CHECK:STDOUT:
  56. // CHECK:STDOUT: imports {
  57. // CHECK:STDOUT: %Core: <namespace> = namespace file.%Core.import, [template] {
  58. // CHECK:STDOUT: .Bool = %import_ref.1
  59. // CHECK:STDOUT: .Int32 = %import_ref.2
  60. // CHECK:STDOUT: import Core//prelude
  61. // CHECK:STDOUT: import Core//prelude/operators
  62. // CHECK:STDOUT: import Core//prelude/types
  63. // CHECK:STDOUT: import Core//prelude/operators/arithmetic
  64. // CHECK:STDOUT: import Core//prelude/operators/as
  65. // CHECK:STDOUT: import Core//prelude/operators/bitwise
  66. // CHECK:STDOUT: import Core//prelude/operators/comparison
  67. // CHECK:STDOUT: import Core//prelude/types/bool
  68. // CHECK:STDOUT: }
  69. // CHECK:STDOUT: %import_ref.1: %Bool.type = import_ref Core//prelude/types/bool, inst+2, loaded [template = constants.%Bool]
  70. // CHECK:STDOUT: %import_ref.2: %Int32.type = import_ref Core//prelude/types, inst+4, loaded [template = constants.%Int32]
  71. // CHECK:STDOUT: }
  72. // CHECK:STDOUT:
  73. // CHECK:STDOUT: file {
  74. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  75. // CHECK:STDOUT: .Core = imports.%Core
  76. // CHECK:STDOUT: .If1 = %If1.decl
  77. // CHECK:STDOUT: .If2 = %If2.decl
  78. // CHECK:STDOUT: .If3 = %If3.decl
  79. // CHECK:STDOUT: }
  80. // CHECK:STDOUT: %Core.import = import Core
  81. // CHECK:STDOUT: %If1.decl: %If1.type = fn_decl @If1 [template = constants.%If1] {
  82. // CHECK:STDOUT: %b.patt: bool = binding_pattern b
  83. // CHECK:STDOUT: } {
  84. // CHECK:STDOUT: %bool.make_type: init type = call constants.%Bool() [template = bool]
  85. // CHECK:STDOUT: %.loc11_11.1: type = value_of_initializer %bool.make_type [template = bool]
  86. // CHECK:STDOUT: %.loc11_11.2: type = converted %bool.make_type, %.loc11_11.1 [template = bool]
  87. // CHECK:STDOUT: %b.param: bool = param b, runtime_param0
  88. // CHECK:STDOUT: %b: bool = bind_name b, %b.param
  89. // CHECK:STDOUT: %int.make_type_32: init type = call constants.%Int32() [template = i32]
  90. // CHECK:STDOUT: %.loc11_20.1: type = value_of_initializer %int.make_type_32 [template = i32]
  91. // CHECK:STDOUT: %.loc11_20.2: type = converted %int.make_type_32, %.loc11_20.1 [template = i32]
  92. // CHECK:STDOUT: %return: ref i32 = var <return slot>
  93. // CHECK:STDOUT: }
  94. // CHECK:STDOUT: %If2.decl: %If2.type = fn_decl @If2 [template = constants.%If2] {
  95. // CHECK:STDOUT: %b.patt: bool = binding_pattern b
  96. // CHECK:STDOUT: } {
  97. // CHECK:STDOUT: %bool.make_type: init type = call constants.%Bool() [template = bool]
  98. // CHECK:STDOUT: %.loc22_11.1: type = value_of_initializer %bool.make_type [template = bool]
  99. // CHECK:STDOUT: %.loc22_11.2: type = converted %bool.make_type, %.loc22_11.1 [template = bool]
  100. // CHECK:STDOUT: %b.param: bool = param b, runtime_param0
  101. // CHECK:STDOUT: %b: bool = bind_name b, %b.param
  102. // CHECK:STDOUT: %int.make_type_32: init type = call constants.%Int32() [template = i32]
  103. // CHECK:STDOUT: %.loc22_20.1: type = value_of_initializer %int.make_type_32 [template = i32]
  104. // CHECK:STDOUT: %.loc22_20.2: type = converted %int.make_type_32, %.loc22_20.1 [template = i32]
  105. // CHECK:STDOUT: %return: ref i32 = var <return slot>
  106. // CHECK:STDOUT: }
  107. // CHECK:STDOUT: %If3.decl: %If3.type = fn_decl @If3 [template = constants.%If3] {
  108. // CHECK:STDOUT: %b.patt: bool = binding_pattern b
  109. // CHECK:STDOUT: } {
  110. // CHECK:STDOUT: %bool.make_type: init type = call constants.%Bool() [template = bool]
  111. // CHECK:STDOUT: %.loc33_11.1: type = value_of_initializer %bool.make_type [template = bool]
  112. // CHECK:STDOUT: %.loc33_11.2: type = converted %bool.make_type, %.loc33_11.1 [template = bool]
  113. // CHECK:STDOUT: %b.param: bool = param b, runtime_param0
  114. // CHECK:STDOUT: %b: bool = bind_name b, %b.param
  115. // CHECK:STDOUT: %int.make_type_32: init type = call constants.%Int32() [template = i32]
  116. // CHECK:STDOUT: %.loc33_20.1: type = value_of_initializer %int.make_type_32 [template = i32]
  117. // CHECK:STDOUT: %.loc33_20.2: type = converted %int.make_type_32, %.loc33_20.1 [template = i32]
  118. // CHECK:STDOUT: %return: ref i32 = var <return slot>
  119. // CHECK:STDOUT: }
  120. // CHECK:STDOUT: }
  121. // CHECK:STDOUT:
  122. // CHECK:STDOUT: fn @Bool() -> type = "bool.make_type";
  123. // CHECK:STDOUT:
  124. // CHECK:STDOUT: fn @Int32() -> type = "int.make_type_32";
  125. // CHECK:STDOUT:
  126. // CHECK:STDOUT: fn @If1(%b: bool) -> i32 {
  127. // CHECK:STDOUT: !entry:
  128. // CHECK:STDOUT: %b.ref: bool = name_ref b, %b
  129. // CHECK:STDOUT: if %b.ref br !if.then else br !if.else
  130. // CHECK:STDOUT:
  131. // CHECK:STDOUT: !if.then:
  132. // CHECK:STDOUT: %.loc13: i32 = int_literal 1 [template = constants.%.2]
  133. // CHECK:STDOUT: return %.loc13
  134. // CHECK:STDOUT:
  135. // CHECK:STDOUT: !if.else:
  136. // CHECK:STDOUT: br !if.done
  137. // CHECK:STDOUT:
  138. // CHECK:STDOUT: !if.done:
  139. // CHECK:STDOUT: }
  140. // CHECK:STDOUT:
  141. // CHECK:STDOUT: fn @If2(%b: bool) -> i32 {
  142. // CHECK:STDOUT: !entry:
  143. // CHECK:STDOUT: %b.ref: bool = name_ref b, %b
  144. // CHECK:STDOUT: if %b.ref br !if.then else br !if.else
  145. // CHECK:STDOUT:
  146. // CHECK:STDOUT: !if.then:
  147. // CHECK:STDOUT: br !if.done
  148. // CHECK:STDOUT:
  149. // CHECK:STDOUT: !if.else:
  150. // CHECK:STDOUT: %.loc25: i32 = int_literal 2 [template = constants.%.3]
  151. // CHECK:STDOUT: return %.loc25
  152. // CHECK:STDOUT:
  153. // CHECK:STDOUT: !if.done:
  154. // CHECK:STDOUT: }
  155. // CHECK:STDOUT:
  156. // CHECK:STDOUT: fn @If3(%b: bool) -> i32 {
  157. // CHECK:STDOUT: !entry:
  158. // CHECK:STDOUT: %b.ref: bool = name_ref b, %b
  159. // CHECK:STDOUT: if %b.ref br !if.then else br !if.else
  160. // CHECK:STDOUT:
  161. // CHECK:STDOUT: !if.then:
  162. // CHECK:STDOUT: %.loc35: i32 = int_literal 1 [template = constants.%.2]
  163. // CHECK:STDOUT: return %.loc35
  164. // CHECK:STDOUT:
  165. // CHECK:STDOUT: !if.else:
  166. // CHECK:STDOUT: }
  167. // CHECK:STDOUT: