fail_partial_constant.carbon 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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: file {
  49. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  50. // CHECK:STDOUT: .Core = %Core
  51. // CHECK:STDOUT: .ConditionIsNonConstant = %ConditionIsNonConstant.decl
  52. // CHECK:STDOUT: }
  53. // CHECK:STDOUT: %Core: <namespace> = namespace [template] {}
  54. // CHECK:STDOUT: %import_ref.1: %Bool.type = import_ref ir7, inst+2, loaded [template = constants.%Bool]
  55. // CHECK:STDOUT: %ConditionIsNonConstant.decl: %ConditionIsNonConstant.type = fn_decl @ConditionIsNonConstant [template = constants.%ConditionIsNonConstant] {
  56. // CHECK:STDOUT: %bool.make_type: init type = call constants.%Bool() [template = bool]
  57. // CHECK:STDOUT: %.loc4_30.1: type = value_of_initializer %bool.make_type [template = bool]
  58. // CHECK:STDOUT: %.loc4_30.2: type = converted %bool.make_type, %.loc4_30.1 [template = bool]
  59. // CHECK:STDOUT: %b.loc4_27.1: bool = param b
  60. // CHECK:STDOUT: @ConditionIsNonConstant.%b: bool = bind_name b, %b.loc4_27.1
  61. // CHECK:STDOUT: }
  62. // CHECK:STDOUT: %import_ref.2: %Int32.type = import_ref ir3, inst+3, loaded [template = constants.%Int32]
  63. // CHECK:STDOUT: %import_ref.3: %Int32.type = import_ref ir3, inst+3, loaded [template = constants.%Int32]
  64. // CHECK:STDOUT: }
  65. // CHECK:STDOUT:
  66. // CHECK:STDOUT: fn @Bool() -> type = "bool.make_type";
  67. // CHECK:STDOUT:
  68. // CHECK:STDOUT: fn @ConditionIsNonConstant(%b: bool) {
  69. // CHECK:STDOUT: !entry:
  70. // CHECK:STDOUT: %b.ref: bool = name_ref b, %b
  71. // CHECK:STDOUT: if %b.ref br !if.expr.then else br !if.expr.else
  72. // CHECK:STDOUT:
  73. // CHECK:STDOUT: !if.expr.then:
  74. // CHECK:STDOUT: %int.make_type_32.loc12_20: init type = call constants.%Int32() [template = i32]
  75. // CHECK:STDOUT: %.loc12_20.1: type = value_of_initializer %int.make_type_32.loc12_20 [template = i32]
  76. // CHECK:STDOUT: %.loc12_20.2: type = converted %int.make_type_32.loc12_20, %.loc12_20.1 [template = i32]
  77. // CHECK:STDOUT: br !if.expr.result(%.loc12_20.2)
  78. // CHECK:STDOUT:
  79. // CHECK:STDOUT: !if.expr.else:
  80. // CHECK:STDOUT: %int.make_type_32.loc12_29: init type = call constants.%Int32() [template = i32]
  81. // CHECK:STDOUT: %.loc12_24.1: type = value_of_initializer %int.make_type_32.loc12_29 [template = i32]
  82. // CHECK:STDOUT: %.loc12_24.2: type = converted %int.make_type_32.loc12_29, %.loc12_24.1 [template = i32]
  83. // CHECK:STDOUT: br !if.expr.result(%.loc12_24.2)
  84. // CHECK:STDOUT:
  85. // CHECK:STDOUT: !if.expr.result:
  86. // CHECK:STDOUT: %.loc12_10: type = block_arg !if.expr.result
  87. // CHECK:STDOUT: %v.var: ref <error> = var v
  88. // CHECK:STDOUT: %v: ref <error> = bind_name v, %v.var
  89. // CHECK:STDOUT: %.loc12_35: i32 = int_literal 1 [template = constants.%.2]
  90. // CHECK:STDOUT: assign %v.var, <error>
  91. // CHECK:STDOUT: return
  92. // CHECK:STDOUT: }
  93. // CHECK:STDOUT:
  94. // CHECK:STDOUT: fn @Int32() -> type = "int.make_type_32";
  95. // CHECK:STDOUT:
  96. // CHECK:STDOUT: --- fail_non_constant_result.carbon
  97. // CHECK:STDOUT:
  98. // CHECK:STDOUT: constants {
  99. // CHECK:STDOUT: %ChosenBranchIsNonConstant.type: type = fn_type @ChosenBranchIsNonConstant [template]
  100. // CHECK:STDOUT: %.1: type = tuple_type () [template]
  101. // CHECK:STDOUT: %ChosenBranchIsNonConstant: %ChosenBranchIsNonConstant.type = struct_value () [template]
  102. // CHECK:STDOUT: %.2: bool = bool_literal true [template]
  103. // CHECK:STDOUT: %Int32.type: type = fn_type @Int32 [template]
  104. // CHECK:STDOUT: %Int32: %Int32.type = struct_value () [template]
  105. // CHECK:STDOUT: %.3: i32 = int_literal 1 [template]
  106. // CHECK:STDOUT: %.4: bool = bool_literal false [template]
  107. // CHECK:STDOUT: }
  108. // CHECK:STDOUT:
  109. // CHECK:STDOUT: file {
  110. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  111. // CHECK:STDOUT: .Core = %Core
  112. // CHECK:STDOUT: .ChosenBranchIsNonConstant = %ChosenBranchIsNonConstant.decl
  113. // CHECK:STDOUT: }
  114. // CHECK:STDOUT: %Core: <namespace> = namespace [template] {}
  115. // CHECK:STDOUT: %ChosenBranchIsNonConstant.decl: %ChosenBranchIsNonConstant.type = fn_decl @ChosenBranchIsNonConstant [template = constants.%ChosenBranchIsNonConstant] {
  116. // CHECK:STDOUT: %t.loc4_30.1: type = param t
  117. // CHECK:STDOUT: @ChosenBranchIsNonConstant.%t: type = bind_name t, %t.loc4_30.1
  118. // CHECK:STDOUT: }
  119. // CHECK:STDOUT: %import_ref.1: %Int32.type = import_ref ir3, inst+3, loaded [template = constants.%Int32]
  120. // CHECK:STDOUT: %import_ref.2: %Int32.type = import_ref ir3, inst+3, loaded [template = constants.%Int32]
  121. // CHECK:STDOUT: }
  122. // CHECK:STDOUT:
  123. // CHECK:STDOUT: fn @ChosenBranchIsNonConstant(%t: type) {
  124. // CHECK:STDOUT: !entry:
  125. // CHECK:STDOUT: %.loc9_13: bool = bool_literal true [template = constants.%.2]
  126. // CHECK:STDOUT: if %.loc9_13 br !if.expr.then.loc9 else br !if.expr.else.loc9
  127. // CHECK:STDOUT:
  128. // CHECK:STDOUT: !if.expr.then.loc9:
  129. // CHECK:STDOUT: %t.ref.loc9: type = name_ref t, %t
  130. // CHECK:STDOUT: br !if.expr.result.loc9(%t.ref.loc9)
  131. // CHECK:STDOUT:
  132. // CHECK:STDOUT: !if.expr.else.loc9:
  133. // CHECK:STDOUT: %int.make_type_32.loc9: init type = call constants.%Int32() [template = i32]
  134. // CHECK:STDOUT: %.loc9_25.1: type = value_of_initializer %int.make_type_32.loc9 [template = i32]
  135. // CHECK:STDOUT: %.loc9_25.2: type = converted %int.make_type_32.loc9, %.loc9_25.1 [template = i32]
  136. // CHECK:STDOUT: br !if.expr.result.loc9(%.loc9_25.2)
  137. // CHECK:STDOUT:
  138. // CHECK:STDOUT: !if.expr.result.loc9:
  139. // CHECK:STDOUT: %.loc9_10: type = block_arg !if.expr.result.loc9
  140. // CHECK:STDOUT: %v.var: ref <error> = var v
  141. // CHECK:STDOUT: %v: ref <error> = bind_name v, %v.var
  142. // CHECK:STDOUT: %.loc9_36: i32 = int_literal 1 [template = constants.%.3]
  143. // CHECK:STDOUT: assign %v.var, <error>
  144. // CHECK:STDOUT: %.loc13_13: bool = bool_literal false [template = constants.%.4]
  145. // CHECK:STDOUT: if %.loc13_13 br !if.expr.then.loc13 else br !if.expr.else.loc13
  146. // CHECK:STDOUT:
  147. // CHECK:STDOUT: !if.expr.then.loc13:
  148. // CHECK:STDOUT: %int.make_type_32.loc13: init type = call constants.%Int32() [template = i32]
  149. // CHECK:STDOUT: %.loc13_24.1: type = value_of_initializer %int.make_type_32.loc13 [template = i32]
  150. // CHECK:STDOUT: %.loc13_24.2: type = converted %int.make_type_32.loc13, %.loc13_24.1 [template = i32]
  151. // CHECK:STDOUT: br !if.expr.result.loc13(%.loc13_24.2)
  152. // CHECK:STDOUT:
  153. // CHECK:STDOUT: !if.expr.else.loc13:
  154. // CHECK:STDOUT: %t.ref.loc13: type = name_ref t, %t
  155. // CHECK:STDOUT: br !if.expr.result.loc13(%t.ref.loc13)
  156. // CHECK:STDOUT:
  157. // CHECK:STDOUT: !if.expr.result.loc13:
  158. // CHECK:STDOUT: %.loc13_10: type = block_arg !if.expr.result.loc13
  159. // CHECK:STDOUT: %w.var: ref <error> = var w
  160. // CHECK:STDOUT: %w: ref <error> = bind_name w, %w.var
  161. // CHECK:STDOUT: %.loc13_37: i32 = int_literal 1 [template = constants.%.3]
  162. // CHECK:STDOUT: assign %w.var, <error>
  163. // CHECK:STDOUT: return
  164. // CHECK:STDOUT: }
  165. // CHECK:STDOUT:
  166. // CHECK:STDOUT: fn @Int32() -> type = "int.make_type_32";
  167. // CHECK:STDOUT: