fail_partial_constant.carbon 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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_expr/fail_partial_constant.carbon
  8. // TIP: To dump output, run:
  9. // TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/if_expr/fail_partial_constant.carbon
  10. // --- fail_non_constant_condition.carbon
  11. package NonConstantCondition;
  12. fn ConditionIsNonConstant(b: bool) {
  13. // We choose to not accept this even if both arms evaluate to the same
  14. // constant value, because it notionally involves evaluating a non-constant
  15. // condition.
  16. // CHECK:STDERR: fail_non_constant_condition.carbon:[[@LINE+4]]:10: error: cannot evaluate type expression
  17. // CHECK:STDERR: var v: if b then i32 else i32 = 1;
  18. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~
  19. // CHECK:STDERR:
  20. var v: if b then i32 else i32 = 1;
  21. }
  22. // --- fail_non_constant_result.carbon
  23. package NonConstantResult;
  24. fn ChosenBranchIsNonConstant(t: type) {
  25. // CHECK:STDERR: fail_non_constant_result.carbon:[[@LINE+4]]:10: error: cannot evaluate type expression
  26. // CHECK:STDERR: var v: if true then t else i32 = 1;
  27. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~
  28. // CHECK:STDERR:
  29. var v: if true then t else i32 = 1;
  30. // CHECK:STDERR: fail_non_constant_result.carbon:[[@LINE+3]]:10: error: cannot evaluate type expression
  31. // CHECK:STDERR: var w: if false then i32 else t = 1;
  32. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~
  33. var w: if false then i32 else t = 1;
  34. }
  35. // CHECK:STDOUT: --- fail_non_constant_condition.carbon
  36. // CHECK:STDOUT:
  37. // CHECK:STDOUT: constants {
  38. // CHECK:STDOUT: %Bool.type: type = fn_type @Bool [template]
  39. // CHECK:STDOUT: %.1: type = tuple_type () [template]
  40. // CHECK:STDOUT: %Bool: %Bool.type = struct_value () [template]
  41. // CHECK:STDOUT: %ConditionIsNonConstant.type: type = fn_type @ConditionIsNonConstant [template]
  42. // CHECK:STDOUT: %ConditionIsNonConstant: %ConditionIsNonConstant.type = struct_value () [template]
  43. // CHECK:STDOUT: %Int32.type: type = fn_type @Int32 [template]
  44. // CHECK:STDOUT: %Int32: %Int32.type = struct_value () [template]
  45. // CHECK:STDOUT: %.2: i32 = int_literal 1 [template]
  46. // CHECK:STDOUT: }
  47. // CHECK:STDOUT:
  48. // CHECK:STDOUT: imports {
  49. // CHECK:STDOUT: %Core: <namespace> = namespace file.%Core.import, [template] {
  50. // CHECK:STDOUT: .Bool = %import_ref.1
  51. // CHECK:STDOUT: .Int32 = %import_ref.2
  52. // CHECK:STDOUT: import Core//prelude
  53. // CHECK:STDOUT: import Core//prelude/operators
  54. // CHECK:STDOUT: import Core//prelude/types
  55. // CHECK:STDOUT: import Core//prelude/operators/arithmetic
  56. // CHECK:STDOUT: import Core//prelude/operators/as
  57. // CHECK:STDOUT: import Core//prelude/operators/bitwise
  58. // CHECK:STDOUT: import Core//prelude/operators/comparison
  59. // CHECK:STDOUT: import Core//prelude/types/bool
  60. // CHECK:STDOUT: }
  61. // CHECK:STDOUT: %import_ref.1: %Bool.type = import_ref Core//prelude/types/bool, inst+2, loaded [template = constants.%Bool]
  62. // CHECK:STDOUT: %import_ref.2: %Int32.type = import_ref Core//prelude/types, inst+9, loaded [template = constants.%Int32]
  63. // CHECK:STDOUT: }
  64. // CHECK:STDOUT:
  65. // CHECK:STDOUT: file {
  66. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  67. // CHECK:STDOUT: .Core = imports.%Core
  68. // CHECK:STDOUT: .ConditionIsNonConstant = %ConditionIsNonConstant.decl
  69. // CHECK:STDOUT: }
  70. // CHECK:STDOUT: %Core.import = import Core
  71. // CHECK:STDOUT: %ConditionIsNonConstant.decl: %ConditionIsNonConstant.type = fn_decl @ConditionIsNonConstant [template = constants.%ConditionIsNonConstant] {
  72. // CHECK:STDOUT: %b.patt: bool = binding_pattern b
  73. // CHECK:STDOUT: %b.param_patt: bool = param_pattern %b.patt, runtime_param0
  74. // CHECK:STDOUT: } {
  75. // CHECK:STDOUT: %bool.make_type: init type = call constants.%Bool() [template = bool]
  76. // CHECK:STDOUT: %.loc4_30.1: type = value_of_initializer %bool.make_type [template = bool]
  77. // CHECK:STDOUT: %.loc4_30.2: type = converted %bool.make_type, %.loc4_30.1 [template = bool]
  78. // CHECK:STDOUT: %param: bool = param runtime_param0
  79. // CHECK:STDOUT: %b: bool = bind_name b, %param
  80. // CHECK:STDOUT: }
  81. // CHECK:STDOUT: }
  82. // CHECK:STDOUT:
  83. // CHECK:STDOUT: fn @Bool() -> type = "bool.make_type";
  84. // CHECK:STDOUT:
  85. // CHECK:STDOUT: fn @ConditionIsNonConstant(%b.param_patt: bool) {
  86. // CHECK:STDOUT: !entry:
  87. // CHECK:STDOUT: %b.ref: bool = name_ref b, %b
  88. // CHECK:STDOUT: if %b.ref br !if.expr.then else br !if.expr.else
  89. // CHECK:STDOUT:
  90. // CHECK:STDOUT: !if.expr.then:
  91. // CHECK:STDOUT: %int.make_type_32.loc12_20: init type = call constants.%Int32() [template = i32]
  92. // CHECK:STDOUT: %.loc12_20.1: type = value_of_initializer %int.make_type_32.loc12_20 [template = i32]
  93. // CHECK:STDOUT: %.loc12_20.2: type = converted %int.make_type_32.loc12_20, %.loc12_20.1 [template = i32]
  94. // CHECK:STDOUT: br !if.expr.result(%.loc12_20.2)
  95. // CHECK:STDOUT:
  96. // CHECK:STDOUT: !if.expr.else:
  97. // CHECK:STDOUT: %int.make_type_32.loc12_29: init type = call constants.%Int32() [template = i32]
  98. // CHECK:STDOUT: %.loc12_24.1: type = value_of_initializer %int.make_type_32.loc12_29 [template = i32]
  99. // CHECK:STDOUT: %.loc12_24.2: type = converted %int.make_type_32.loc12_29, %.loc12_24.1 [template = i32]
  100. // CHECK:STDOUT: br !if.expr.result(%.loc12_24.2)
  101. // CHECK:STDOUT:
  102. // CHECK:STDOUT: !if.expr.result:
  103. // CHECK:STDOUT: %.loc12_10: type = block_arg !if.expr.result
  104. // CHECK:STDOUT: %v.var: ref <error> = var v
  105. // CHECK:STDOUT: %v: ref <error> = bind_name v, %v.var
  106. // CHECK:STDOUT: %.loc12_35: i32 = int_literal 1 [template = constants.%.2]
  107. // CHECK:STDOUT: assign %v.var, <error>
  108. // CHECK:STDOUT: return
  109. // CHECK:STDOUT: }
  110. // CHECK:STDOUT:
  111. // CHECK:STDOUT: fn @Int32() -> type = "int.make_type_32";
  112. // CHECK:STDOUT:
  113. // CHECK:STDOUT: --- fail_non_constant_result.carbon
  114. // CHECK:STDOUT:
  115. // CHECK:STDOUT: constants {
  116. // CHECK:STDOUT: %ChosenBranchIsNonConstant.type: type = fn_type @ChosenBranchIsNonConstant [template]
  117. // CHECK:STDOUT: %.1: type = tuple_type () [template]
  118. // CHECK:STDOUT: %ChosenBranchIsNonConstant: %ChosenBranchIsNonConstant.type = struct_value () [template]
  119. // CHECK:STDOUT: %.2: bool = bool_literal true [template]
  120. // CHECK:STDOUT: %Int32.type: type = fn_type @Int32 [template]
  121. // CHECK:STDOUT: %Int32: %Int32.type = struct_value () [template]
  122. // CHECK:STDOUT: %.3: i32 = int_literal 1 [template]
  123. // CHECK:STDOUT: %.4: bool = bool_literal false [template]
  124. // CHECK:STDOUT: }
  125. // CHECK:STDOUT:
  126. // CHECK:STDOUT: imports {
  127. // CHECK:STDOUT: %Core: <namespace> = namespace file.%Core.import, [template] {
  128. // CHECK:STDOUT: .Int32 = %import_ref
  129. // CHECK:STDOUT: import Core//prelude
  130. // CHECK:STDOUT: import Core//prelude/operators
  131. // CHECK:STDOUT: import Core//prelude/types
  132. // CHECK:STDOUT: import Core//prelude/operators/arithmetic
  133. // CHECK:STDOUT: import Core//prelude/operators/as
  134. // CHECK:STDOUT: import Core//prelude/operators/bitwise
  135. // CHECK:STDOUT: import Core//prelude/operators/comparison
  136. // CHECK:STDOUT: import Core//prelude/types/bool
  137. // CHECK:STDOUT: }
  138. // CHECK:STDOUT: %import_ref: %Int32.type = import_ref Core//prelude/types, inst+9, loaded [template = constants.%Int32]
  139. // CHECK:STDOUT: }
  140. // CHECK:STDOUT:
  141. // CHECK:STDOUT: file {
  142. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  143. // CHECK:STDOUT: .Core = imports.%Core
  144. // CHECK:STDOUT: .ChosenBranchIsNonConstant = %ChosenBranchIsNonConstant.decl
  145. // CHECK:STDOUT: }
  146. // CHECK:STDOUT: %Core.import = import Core
  147. // CHECK:STDOUT: %ChosenBranchIsNonConstant.decl: %ChosenBranchIsNonConstant.type = fn_decl @ChosenBranchIsNonConstant [template = constants.%ChosenBranchIsNonConstant] {
  148. // CHECK:STDOUT: %t.patt: type = binding_pattern t
  149. // CHECK:STDOUT: %t.param_patt: type = param_pattern %t.patt, runtime_param0
  150. // CHECK:STDOUT: } {
  151. // CHECK:STDOUT: %param: type = param runtime_param0
  152. // CHECK:STDOUT: %t: type = bind_name t, %param
  153. // CHECK:STDOUT: }
  154. // CHECK:STDOUT: }
  155. // CHECK:STDOUT:
  156. // CHECK:STDOUT: fn @ChosenBranchIsNonConstant(%t.param_patt: type) {
  157. // CHECK:STDOUT: !entry:
  158. // CHECK:STDOUT: %.loc9_13: bool = bool_literal true [template = constants.%.2]
  159. // CHECK:STDOUT: if %.loc9_13 br !if.expr.then.loc9 else br !if.expr.else.loc9
  160. // CHECK:STDOUT:
  161. // CHECK:STDOUT: !if.expr.then.loc9:
  162. // CHECK:STDOUT: %t.ref.loc9: type = name_ref t, %t
  163. // CHECK:STDOUT: br !if.expr.result.loc9(%t.ref.loc9)
  164. // CHECK:STDOUT:
  165. // CHECK:STDOUT: !if.expr.else.loc9:
  166. // CHECK:STDOUT: %int.make_type_32.loc9: init type = call constants.%Int32() [template = i32]
  167. // CHECK:STDOUT: %.loc9_25.1: type = value_of_initializer %int.make_type_32.loc9 [template = i32]
  168. // CHECK:STDOUT: %.loc9_25.2: type = converted %int.make_type_32.loc9, %.loc9_25.1 [template = i32]
  169. // CHECK:STDOUT: br !if.expr.result.loc9(%.loc9_25.2)
  170. // CHECK:STDOUT:
  171. // CHECK:STDOUT: !if.expr.result.loc9:
  172. // CHECK:STDOUT: %.loc9_10: type = block_arg !if.expr.result.loc9
  173. // CHECK:STDOUT: %v.var: ref <error> = var v
  174. // CHECK:STDOUT: %v: ref <error> = bind_name v, %v.var
  175. // CHECK:STDOUT: %.loc9_36: i32 = int_literal 1 [template = constants.%.3]
  176. // CHECK:STDOUT: assign %v.var, <error>
  177. // CHECK:STDOUT: %.loc13_13: bool = bool_literal false [template = constants.%.4]
  178. // CHECK:STDOUT: if %.loc13_13 br !if.expr.then.loc13 else br !if.expr.else.loc13
  179. // CHECK:STDOUT:
  180. // CHECK:STDOUT: !if.expr.then.loc13:
  181. // CHECK:STDOUT: %int.make_type_32.loc13: init type = call constants.%Int32() [template = i32]
  182. // CHECK:STDOUT: %.loc13_24.1: type = value_of_initializer %int.make_type_32.loc13 [template = i32]
  183. // CHECK:STDOUT: %.loc13_24.2: type = converted %int.make_type_32.loc13, %.loc13_24.1 [template = i32]
  184. // CHECK:STDOUT: br !if.expr.result.loc13(%.loc13_24.2)
  185. // CHECK:STDOUT:
  186. // CHECK:STDOUT: !if.expr.else.loc13:
  187. // CHECK:STDOUT: %t.ref.loc13: type = name_ref t, %t
  188. // CHECK:STDOUT: br !if.expr.result.loc13(%t.ref.loc13)
  189. // CHECK:STDOUT:
  190. // CHECK:STDOUT: !if.expr.result.loc13:
  191. // CHECK:STDOUT: %.loc13_10: type = block_arg !if.expr.result.loc13
  192. // CHECK:STDOUT: %w.var: ref <error> = var w
  193. // CHECK:STDOUT: %w: ref <error> = bind_name w, %w.var
  194. // CHECK:STDOUT: %.loc13_37: i32 = int_literal 1 [template = constants.%.3]
  195. // CHECK:STDOUT: assign %w.var, <error>
  196. // CHECK:STDOUT: return
  197. // CHECK:STDOUT: }
  198. // CHECK:STDOUT:
  199. // CHECK:STDOUT: fn @Int32() -> type = "int.make_type_32";
  200. // CHECK:STDOUT: