fail_partial_constant.carbon 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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 [TypeExprEvaluationFailure]
  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 [TypeExprEvaluationFailure]
  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+4]]:10: error: cannot evaluate type expression [TypeExprEvaluationFailure]
  31. // CHECK:STDERR: var w: if false then i32 else t = 1;
  32. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~
  33. // CHECK:STDERR:
  34. var w: if false then i32 else t = 1;
  35. }
  36. // CHECK:STDOUT: --- fail_non_constant_condition.carbon
  37. // CHECK:STDOUT:
  38. // CHECK:STDOUT: constants {
  39. // CHECK:STDOUT: %Bool.type: type = fn_type @Bool [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: %int_32: Core.IntLiteral = int_value 32 [template]
  44. // CHECK:STDOUT: %i32: type = class_type @Int, @Int(%int_32) [template]
  45. // CHECK:STDOUT: %int_1: Core.IntLiteral = int_value 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 = %Core.Bool
  51. // CHECK:STDOUT: .Int = %Core.Int
  52. // CHECK:STDOUT: import Core//prelude
  53. // CHECK:STDOUT: import Core//prelude/...
  54. // CHECK:STDOUT: }
  55. // CHECK:STDOUT: }
  56. // CHECK:STDOUT:
  57. // CHECK:STDOUT: file {
  58. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  59. // CHECK:STDOUT: .Core = imports.%Core
  60. // CHECK:STDOUT: .ConditionIsNonConstant = %ConditionIsNonConstant.decl
  61. // CHECK:STDOUT: }
  62. // CHECK:STDOUT: %Core.import = import Core
  63. // CHECK:STDOUT: %ConditionIsNonConstant.decl: %ConditionIsNonConstant.type = fn_decl @ConditionIsNonConstant [template = constants.%ConditionIsNonConstant] {
  64. // CHECK:STDOUT: %b.patt: bool = binding_pattern b
  65. // CHECK:STDOUT: %b.param_patt: bool = value_param_pattern %b.patt, runtime_param0
  66. // CHECK:STDOUT: } {
  67. // CHECK:STDOUT: %b.param: bool = value_param runtime_param0
  68. // CHECK:STDOUT: %.loc4_30.1: type = splice_block %.loc4_30.3 [template = bool] {
  69. // CHECK:STDOUT: %bool.make_type: init type = call constants.%Bool() [template = bool]
  70. // CHECK:STDOUT: %.loc4_30.2: type = value_of_initializer %bool.make_type [template = bool]
  71. // CHECK:STDOUT: %.loc4_30.3: type = converted %bool.make_type, %.loc4_30.2 [template = bool]
  72. // CHECK:STDOUT: }
  73. // CHECK:STDOUT: %b: bool = bind_name b, %b.param
  74. // CHECK:STDOUT: }
  75. // CHECK:STDOUT: }
  76. // CHECK:STDOUT:
  77. // CHECK:STDOUT: fn @ConditionIsNonConstant(%b.param_patt: bool) {
  78. // CHECK:STDOUT: !entry:
  79. // CHECK:STDOUT: name_binding_decl {
  80. // CHECK:STDOUT: %v.patt: <error> = binding_pattern v
  81. // CHECK:STDOUT: %.loc12_3: <error> = var_pattern %v.patt
  82. // CHECK:STDOUT: }
  83. // CHECK:STDOUT: %v.var: ref <error> = var v
  84. // CHECK:STDOUT: %int_1: Core.IntLiteral = int_value 1 [template = constants.%int_1]
  85. // CHECK:STDOUT: assign %v.var, <error>
  86. // CHECK:STDOUT: br !.loc12_13
  87. // CHECK:STDOUT:
  88. // CHECK:STDOUT: !.loc12_13:
  89. // CHECK:STDOUT: %b.ref: bool = name_ref b, %b
  90. // CHECK:STDOUT: if %b.ref br !if.expr.then else br !if.expr.else
  91. // CHECK:STDOUT:
  92. // CHECK:STDOUT: !if.expr.then:
  93. // CHECK:STDOUT: %int_32.loc12_20: Core.IntLiteral = int_value 32 [template = constants.%int_32]
  94. // CHECK:STDOUT: %i32.loc12_20: type = class_type @Int, @Int(constants.%int_32) [template = constants.%i32]
  95. // CHECK:STDOUT: br !if.expr.result(%i32.loc12_20)
  96. // CHECK:STDOUT:
  97. // CHECK:STDOUT: !if.expr.else:
  98. // CHECK:STDOUT: %int_32.loc12_29: Core.IntLiteral = int_value 32 [template = constants.%int_32]
  99. // CHECK:STDOUT: %i32.loc12_29: type = class_type @Int, @Int(constants.%int_32) [template = constants.%i32]
  100. // CHECK:STDOUT: br !if.expr.result(%i32.loc12_29)
  101. // CHECK:STDOUT:
  102. // CHECK:STDOUT: !if.expr.result:
  103. // CHECK:STDOUT: %.loc12_10: type = block_arg !if.expr.result
  104. // CHECK:STDOUT: br !.loc12_7
  105. // CHECK:STDOUT:
  106. // CHECK:STDOUT: !.loc12_7:
  107. // CHECK:STDOUT: %v: <error> = bind_name v, <error>
  108. // CHECK:STDOUT: return
  109. // CHECK:STDOUT: }
  110. // CHECK:STDOUT:
  111. // CHECK:STDOUT: --- fail_non_constant_result.carbon
  112. // CHECK:STDOUT:
  113. // CHECK:STDOUT: constants {
  114. // CHECK:STDOUT: %ChosenBranchIsNonConstant.type: type = fn_type @ChosenBranchIsNonConstant [template]
  115. // CHECK:STDOUT: %ChosenBranchIsNonConstant: %ChosenBranchIsNonConstant.type = struct_value () [template]
  116. // CHECK:STDOUT: %true: bool = bool_literal true [template]
  117. // CHECK:STDOUT: %int_32: Core.IntLiteral = int_value 32 [template]
  118. // CHECK:STDOUT: %i32: type = class_type @Int, @Int(%int_32) [template]
  119. // CHECK:STDOUT: %int_1: Core.IntLiteral = int_value 1 [template]
  120. // CHECK:STDOUT: %false: bool = bool_literal false [template]
  121. // CHECK:STDOUT: }
  122. // CHECK:STDOUT:
  123. // CHECK:STDOUT: imports {
  124. // CHECK:STDOUT: %Core: <namespace> = namespace file.%Core.import, [template] {
  125. // CHECK:STDOUT: .Int = %Core.Int
  126. // CHECK:STDOUT: import Core//prelude
  127. // CHECK:STDOUT: import Core//prelude/...
  128. // CHECK:STDOUT: }
  129. // CHECK:STDOUT: }
  130. // CHECK:STDOUT:
  131. // CHECK:STDOUT: file {
  132. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  133. // CHECK:STDOUT: .Core = imports.%Core
  134. // CHECK:STDOUT: .ChosenBranchIsNonConstant = %ChosenBranchIsNonConstant.decl
  135. // CHECK:STDOUT: }
  136. // CHECK:STDOUT: %Core.import = import Core
  137. // CHECK:STDOUT: %ChosenBranchIsNonConstant.decl: %ChosenBranchIsNonConstant.type = fn_decl @ChosenBranchIsNonConstant [template = constants.%ChosenBranchIsNonConstant] {
  138. // CHECK:STDOUT: %t.patt: type = binding_pattern t
  139. // CHECK:STDOUT: %t.param_patt: type = value_param_pattern %t.patt, runtime_param0
  140. // CHECK:STDOUT: } {
  141. // CHECK:STDOUT: %t.param: type = value_param runtime_param0
  142. // CHECK:STDOUT: %t: type = bind_name t, %t.param
  143. // CHECK:STDOUT: }
  144. // CHECK:STDOUT: }
  145. // CHECK:STDOUT:
  146. // CHECK:STDOUT: fn @ChosenBranchIsNonConstant(%t.param_patt: type) {
  147. // CHECK:STDOUT: !entry:
  148. // CHECK:STDOUT: name_binding_decl {
  149. // CHECK:STDOUT: %v.patt: <error> = binding_pattern v
  150. // CHECK:STDOUT: %.loc9_3: <error> = var_pattern %v.patt
  151. // CHECK:STDOUT: }
  152. // CHECK:STDOUT: %v.var: ref <error> = var v
  153. // CHECK:STDOUT: %int_1.loc9: Core.IntLiteral = int_value 1 [template = constants.%int_1]
  154. // CHECK:STDOUT: assign %v.var, <error>
  155. // CHECK:STDOUT: br !.loc9_13
  156. // CHECK:STDOUT:
  157. // CHECK:STDOUT: !.loc9_13:
  158. // CHECK:STDOUT: %true: bool = bool_literal true [template = constants.%true]
  159. // CHECK:STDOUT: if %true 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_32.loc9: Core.IntLiteral = int_value 32 [template = constants.%int_32]
  167. // CHECK:STDOUT: %i32.loc9: type = class_type @Int, @Int(constants.%int_32) [template = constants.%i32]
  168. // CHECK:STDOUT: br !if.expr.result.loc9(%i32.loc9)
  169. // CHECK:STDOUT:
  170. // CHECK:STDOUT: !if.expr.result.loc9:
  171. // CHECK:STDOUT: %.loc9_10: type = block_arg !if.expr.result.loc9
  172. // CHECK:STDOUT: br !.loc9_7
  173. // CHECK:STDOUT:
  174. // CHECK:STDOUT: !.loc9_7:
  175. // CHECK:STDOUT: %v: <error> = bind_name v, <error>
  176. // CHECK:STDOUT: name_binding_decl {
  177. // CHECK:STDOUT: %w.patt: <error> = binding_pattern w
  178. // CHECK:STDOUT: %.loc14_3: <error> = var_pattern %w.patt
  179. // CHECK:STDOUT: }
  180. // CHECK:STDOUT: %w.var: ref <error> = var w
  181. // CHECK:STDOUT: %int_1.loc14: Core.IntLiteral = int_value 1 [template = constants.%int_1]
  182. // CHECK:STDOUT: assign %w.var, <error>
  183. // CHECK:STDOUT: br !.loc14_13
  184. // CHECK:STDOUT:
  185. // CHECK:STDOUT: !.loc14_13:
  186. // CHECK:STDOUT: %false: bool = bool_literal false [template = constants.%false]
  187. // CHECK:STDOUT: if %false br !if.expr.then.loc14 else br !if.expr.else.loc14
  188. // CHECK:STDOUT:
  189. // CHECK:STDOUT: !if.expr.then.loc14:
  190. // CHECK:STDOUT: %int_32.loc14: Core.IntLiteral = int_value 32 [template = constants.%int_32]
  191. // CHECK:STDOUT: %i32.loc14: type = class_type @Int, @Int(constants.%int_32) [template = constants.%i32]
  192. // CHECK:STDOUT: br !if.expr.result.loc14(%i32.loc14)
  193. // CHECK:STDOUT:
  194. // CHECK:STDOUT: !if.expr.else.loc14:
  195. // CHECK:STDOUT: %t.ref.loc14: type = name_ref t, %t
  196. // CHECK:STDOUT: br !if.expr.result.loc14(%t.ref.loc14)
  197. // CHECK:STDOUT:
  198. // CHECK:STDOUT: !if.expr.result.loc14:
  199. // CHECK:STDOUT: %.loc14_10: type = block_arg !if.expr.result.loc14
  200. // CHECK:STDOUT: br !.loc14_7
  201. // CHECK:STDOUT:
  202. // CHECK:STDOUT: !.loc14_7:
  203. // CHECK:STDOUT: %w: <error> = bind_name w, <error>
  204. // CHECK:STDOUT: return
  205. // CHECK:STDOUT: }
  206. // CHECK:STDOUT: