fail_partial_constant.carbon 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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. // INCLUDE-FILE: toolchain/testing/testdata/min_prelude/primitives.carbon
  6. // TODO: Add ranges and switch to "--dump-sem-ir-ranges=only".
  7. // EXTRA-ARGS: --dump-sem-ir-ranges=if-present
  8. //
  9. // AUTOUPDATE
  10. // TIP: To test this file alone, run:
  11. // TIP: bazel test //toolchain/testing:file_test --test_arg=--file_tests=toolchain/check/testdata/if_expr/fail_partial_constant.carbon
  12. // TIP: To dump output, run:
  13. // TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/if_expr/fail_partial_constant.carbon
  14. // --- fail_non_constant_condition.carbon
  15. package NonConstantCondition;
  16. fn ConditionIsNonConstant(b: bool) {
  17. // We choose to not accept this even if both arms evaluate to the same
  18. // constant value, because it notionally involves evaluating a non-constant
  19. // condition.
  20. // CHECK:STDERR: fail_non_constant_condition.carbon:[[@LINE+4]]:10: error: cannot evaluate type expression [TypeExprEvaluationFailure]
  21. // CHECK:STDERR: var v: if b then i32 else i32 = 1;
  22. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~
  23. // CHECK:STDERR:
  24. var v: if b then i32 else i32 = 1;
  25. }
  26. // --- fail_non_constant_result.carbon
  27. package NonConstantResult;
  28. fn ChosenBranchIsNonConstant(t: type) {
  29. // CHECK:STDERR: fail_non_constant_result.carbon:[[@LINE+4]]:10: error: cannot evaluate type expression [TypeExprEvaluationFailure]
  30. // CHECK:STDERR: var v: if true then t else i32 = 1;
  31. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~
  32. // CHECK:STDERR:
  33. var v: if true then t else i32 = 1;
  34. // CHECK:STDERR: fail_non_constant_result.carbon:[[@LINE+4]]:10: error: cannot evaluate type expression [TypeExprEvaluationFailure]
  35. // CHECK:STDERR: var w: if false then i32 else t = 1;
  36. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~
  37. // CHECK:STDERR:
  38. var w: if false then i32 else t = 1;
  39. }
  40. // CHECK:STDOUT: --- fail_non_constant_condition.carbon
  41. // CHECK:STDOUT:
  42. // CHECK:STDOUT: constants {
  43. // CHECK:STDOUT: %Bool.type: type = fn_type @Bool [concrete]
  44. // CHECK:STDOUT: %Bool: %Bool.type = struct_value () [concrete]
  45. // CHECK:STDOUT: %pattern_type.831: type = pattern_type bool [concrete]
  46. // CHECK:STDOUT: %ConditionIsNonConstant.type: type = fn_type @ConditionIsNonConstant [concrete]
  47. // CHECK:STDOUT: %ConditionIsNonConstant: %ConditionIsNonConstant.type = struct_value () [concrete]
  48. // CHECK:STDOUT: %int_32: Core.IntLiteral = int_value 32 [concrete]
  49. // CHECK:STDOUT: %Int.type: type = generic_class_type @Int [concrete]
  50. // CHECK:STDOUT: %Int.generic: %Int.type = struct_value () [concrete]
  51. // CHECK:STDOUT: %i32: type = class_type @Int, @Int(%int_32) [concrete]
  52. // CHECK:STDOUT: %int_1: Core.IntLiteral = int_value 1 [concrete]
  53. // CHECK:STDOUT: %Destroy.type: type = facet_type <@Destroy> [concrete]
  54. // CHECK:STDOUT: }
  55. // CHECK:STDOUT:
  56. // CHECK:STDOUT: imports {
  57. // CHECK:STDOUT: %Core: <namespace> = namespace file.%Core.import, [concrete] {
  58. // CHECK:STDOUT: .Bool = %Core.Bool
  59. // CHECK:STDOUT: .Int = %Core.Int
  60. // CHECK:STDOUT: .Destroy = %Core.Destroy
  61. // CHECK:STDOUT: import Core//prelude
  62. // CHECK:STDOUT: import Core//prelude/...
  63. // CHECK:STDOUT: }
  64. // CHECK:STDOUT: %Core.Bool: %Bool.type = import_ref Core//prelude/parts/bool, Bool, loaded [concrete = constants.%Bool]
  65. // CHECK:STDOUT: %Core.Int: %Int.type = import_ref Core//prelude/parts/int, Int, loaded [concrete = constants.%Int.generic]
  66. // CHECK:STDOUT: %Core.Destroy: type = import_ref Core//prelude/parts/destroy, Destroy, loaded [concrete = constants.%Destroy.type]
  67. // CHECK:STDOUT: }
  68. // CHECK:STDOUT:
  69. // CHECK:STDOUT: file {
  70. // CHECK:STDOUT: package: <namespace> = namespace [concrete] {
  71. // CHECK:STDOUT: .Core = imports.%Core
  72. // CHECK:STDOUT: .ConditionIsNonConstant = %ConditionIsNonConstant.decl
  73. // CHECK:STDOUT: }
  74. // CHECK:STDOUT: %Core.import = import Core
  75. // CHECK:STDOUT: %ConditionIsNonConstant.decl: %ConditionIsNonConstant.type = fn_decl @ConditionIsNonConstant [concrete = constants.%ConditionIsNonConstant] {
  76. // CHECK:STDOUT: %b.patt: %pattern_type.831 = binding_pattern b [concrete]
  77. // CHECK:STDOUT: %b.param_patt: %pattern_type.831 = value_param_pattern %b.patt, call_param0 [concrete]
  78. // CHECK:STDOUT: } {
  79. // CHECK:STDOUT: %b.param: bool = value_param call_param0
  80. // CHECK:STDOUT: %.loc4_30.1: type = splice_block %.loc4_30.3 [concrete = bool] {
  81. // CHECK:STDOUT: %bool.make_type: init type = call constants.%Bool() [concrete = bool]
  82. // CHECK:STDOUT: %.loc4_30.2: type = value_of_initializer %bool.make_type [concrete = bool]
  83. // CHECK:STDOUT: %.loc4_30.3: type = converted %bool.make_type, %.loc4_30.2 [concrete = bool]
  84. // CHECK:STDOUT: }
  85. // CHECK:STDOUT: %b: bool = bind_name b, %b.param
  86. // CHECK:STDOUT: }
  87. // CHECK:STDOUT: }
  88. // CHECK:STDOUT:
  89. // CHECK:STDOUT: fn @ConditionIsNonConstant(%b.param: bool) {
  90. // CHECK:STDOUT: !entry:
  91. // CHECK:STDOUT: name_binding_decl {
  92. // CHECK:STDOUT: %v.patt: <error> = binding_pattern v [concrete]
  93. // CHECK:STDOUT: %v.var_patt: <error> = var_pattern %v.patt [concrete]
  94. // CHECK:STDOUT: }
  95. // CHECK:STDOUT: %v.var: ref <error> = var %v.var_patt [concrete = <error>]
  96. // CHECK:STDOUT: %int_1: Core.IntLiteral = int_value 1 [concrete = constants.%int_1]
  97. // CHECK:STDOUT: assign %v.var, <error>
  98. // CHECK:STDOUT: br !.loc12_13
  99. // CHECK:STDOUT:
  100. // CHECK:STDOUT: !.loc12_13:
  101. // CHECK:STDOUT: %b.ref: bool = name_ref b, %b
  102. // CHECK:STDOUT: if %b.ref br !if.expr.then else br !if.expr.else
  103. // CHECK:STDOUT:
  104. // CHECK:STDOUT: !if.expr.then:
  105. // CHECK:STDOUT: %int_32.loc12_20: Core.IntLiteral = int_value 32 [concrete = constants.%int_32]
  106. // CHECK:STDOUT: %i32.loc12_20: type = class_type @Int, @Int(constants.%int_32) [concrete = constants.%i32]
  107. // CHECK:STDOUT: br !if.expr.result(%i32.loc12_20)
  108. // CHECK:STDOUT:
  109. // CHECK:STDOUT: !if.expr.else:
  110. // CHECK:STDOUT: %int_32.loc12_29: Core.IntLiteral = int_value 32 [concrete = constants.%int_32]
  111. // CHECK:STDOUT: %i32.loc12_29: type = class_type @Int, @Int(constants.%int_32) [concrete = constants.%i32]
  112. // CHECK:STDOUT: br !if.expr.result(%i32.loc12_29)
  113. // CHECK:STDOUT:
  114. // CHECK:STDOUT: !if.expr.result:
  115. // CHECK:STDOUT: %.loc12: type = block_arg !if.expr.result
  116. // CHECK:STDOUT: br !.loc12_7
  117. // CHECK:STDOUT:
  118. // CHECK:STDOUT: !.loc12_7:
  119. // CHECK:STDOUT: %v: <error> = bind_name v, <error> [concrete = <error>]
  120. // CHECK:STDOUT: %impl.elem0: <error> = impl_witness_access <error>, element0 [concrete = <error>]
  121. // CHECK:STDOUT: return
  122. // CHECK:STDOUT: }
  123. // CHECK:STDOUT:
  124. // CHECK:STDOUT: --- fail_non_constant_result.carbon
  125. // CHECK:STDOUT:
  126. // CHECK:STDOUT: constants {
  127. // CHECK:STDOUT: %pattern_type.98f: type = pattern_type type [concrete]
  128. // CHECK:STDOUT: %ChosenBranchIsNonConstant.type: type = fn_type @ChosenBranchIsNonConstant [concrete]
  129. // CHECK:STDOUT: %ChosenBranchIsNonConstant: %ChosenBranchIsNonConstant.type = struct_value () [concrete]
  130. // CHECK:STDOUT: %true: bool = bool_literal true [concrete]
  131. // CHECK:STDOUT: %int_32: Core.IntLiteral = int_value 32 [concrete]
  132. // CHECK:STDOUT: %Int.type: type = generic_class_type @Int [concrete]
  133. // CHECK:STDOUT: %Int.generic: %Int.type = struct_value () [concrete]
  134. // CHECK:STDOUT: %i32: type = class_type @Int, @Int(%int_32) [concrete]
  135. // CHECK:STDOUT: %int_1: Core.IntLiteral = int_value 1 [concrete]
  136. // CHECK:STDOUT: %false: bool = bool_literal false [concrete]
  137. // CHECK:STDOUT: %Destroy.type: type = facet_type <@Destroy> [concrete]
  138. // CHECK:STDOUT: }
  139. // CHECK:STDOUT:
  140. // CHECK:STDOUT: imports {
  141. // CHECK:STDOUT: %Core: <namespace> = namespace file.%Core.import, [concrete] {
  142. // CHECK:STDOUT: .Int = %Core.Int
  143. // CHECK:STDOUT: .Destroy = %Core.Destroy
  144. // CHECK:STDOUT: import Core//prelude
  145. // CHECK:STDOUT: import Core//prelude/...
  146. // CHECK:STDOUT: }
  147. // CHECK:STDOUT: %Core.Int: %Int.type = import_ref Core//prelude/parts/int, Int, loaded [concrete = constants.%Int.generic]
  148. // CHECK:STDOUT: %Core.Destroy: type = import_ref Core//prelude/parts/destroy, Destroy, loaded [concrete = constants.%Destroy.type]
  149. // CHECK:STDOUT: }
  150. // CHECK:STDOUT:
  151. // CHECK:STDOUT: file {
  152. // CHECK:STDOUT: package: <namespace> = namespace [concrete] {
  153. // CHECK:STDOUT: .Core = imports.%Core
  154. // CHECK:STDOUT: .ChosenBranchIsNonConstant = %ChosenBranchIsNonConstant.decl
  155. // CHECK:STDOUT: }
  156. // CHECK:STDOUT: %Core.import = import Core
  157. // CHECK:STDOUT: %ChosenBranchIsNonConstant.decl: %ChosenBranchIsNonConstant.type = fn_decl @ChosenBranchIsNonConstant [concrete = constants.%ChosenBranchIsNonConstant] {
  158. // CHECK:STDOUT: %t.patt: %pattern_type.98f = binding_pattern t [concrete]
  159. // CHECK:STDOUT: %t.param_patt: %pattern_type.98f = value_param_pattern %t.patt, call_param0 [concrete]
  160. // CHECK:STDOUT: } {
  161. // CHECK:STDOUT: %t.param: type = value_param call_param0
  162. // CHECK:STDOUT: %t: type = bind_name t, %t.param
  163. // CHECK:STDOUT: }
  164. // CHECK:STDOUT: }
  165. // CHECK:STDOUT:
  166. // CHECK:STDOUT: fn @ChosenBranchIsNonConstant(%t.param: type) {
  167. // CHECK:STDOUT: !entry:
  168. // CHECK:STDOUT: name_binding_decl {
  169. // CHECK:STDOUT: %v.patt: <error> = binding_pattern v [concrete]
  170. // CHECK:STDOUT: %v.var_patt: <error> = var_pattern %v.patt [concrete]
  171. // CHECK:STDOUT: }
  172. // CHECK:STDOUT: %v.var: ref <error> = var %v.var_patt [concrete = <error>]
  173. // CHECK:STDOUT: %int_1.loc9: Core.IntLiteral = int_value 1 [concrete = constants.%int_1]
  174. // CHECK:STDOUT: assign %v.var, <error>
  175. // CHECK:STDOUT: br !.loc9_13
  176. // CHECK:STDOUT:
  177. // CHECK:STDOUT: !.loc9_13:
  178. // CHECK:STDOUT: %true: bool = bool_literal true [concrete = constants.%true]
  179. // CHECK:STDOUT: if %true br !if.expr.then.loc9 else br !if.expr.else.loc9
  180. // CHECK:STDOUT:
  181. // CHECK:STDOUT: !if.expr.then.loc9:
  182. // CHECK:STDOUT: %t.ref.loc9: type = name_ref t, %t
  183. // CHECK:STDOUT: br !if.expr.result.loc9(%t.ref.loc9)
  184. // CHECK:STDOUT:
  185. // CHECK:STDOUT: !if.expr.else.loc9:
  186. // CHECK:STDOUT: %int_32.loc9: Core.IntLiteral = int_value 32 [concrete = constants.%int_32]
  187. // CHECK:STDOUT: %i32.loc9: type = class_type @Int, @Int(constants.%int_32) [concrete = constants.%i32]
  188. // CHECK:STDOUT: br !if.expr.result.loc9(%i32.loc9)
  189. // CHECK:STDOUT:
  190. // CHECK:STDOUT: !if.expr.result.loc9:
  191. // CHECK:STDOUT: %.loc9: type = block_arg !if.expr.result.loc9
  192. // CHECK:STDOUT: br !.loc9_7
  193. // CHECK:STDOUT:
  194. // CHECK:STDOUT: !.loc9_7:
  195. // CHECK:STDOUT: %v: <error> = bind_name v, <error> [concrete = <error>]
  196. // CHECK:STDOUT: name_binding_decl {
  197. // CHECK:STDOUT: %w.patt: <error> = binding_pattern w [concrete]
  198. // CHECK:STDOUT: %w.var_patt: <error> = var_pattern %w.patt [concrete]
  199. // CHECK:STDOUT: }
  200. // CHECK:STDOUT: %w.var: ref <error> = var %w.var_patt [concrete = <error>]
  201. // CHECK:STDOUT: %int_1.loc14: Core.IntLiteral = int_value 1 [concrete = constants.%int_1]
  202. // CHECK:STDOUT: assign %w.var, <error>
  203. // CHECK:STDOUT: br !.loc14_13
  204. // CHECK:STDOUT:
  205. // CHECK:STDOUT: !.loc14_13:
  206. // CHECK:STDOUT: %false: bool = bool_literal false [concrete = constants.%false]
  207. // CHECK:STDOUT: if %false br !if.expr.then.loc14 else br !if.expr.else.loc14
  208. // CHECK:STDOUT:
  209. // CHECK:STDOUT: !if.expr.then.loc14:
  210. // CHECK:STDOUT: %int_32.loc14: Core.IntLiteral = int_value 32 [concrete = constants.%int_32]
  211. // CHECK:STDOUT: %i32.loc14: type = class_type @Int, @Int(constants.%int_32) [concrete = constants.%i32]
  212. // CHECK:STDOUT: br !if.expr.result.loc14(%i32.loc14)
  213. // CHECK:STDOUT:
  214. // CHECK:STDOUT: !if.expr.else.loc14:
  215. // CHECK:STDOUT: %t.ref.loc14: type = name_ref t, %t
  216. // CHECK:STDOUT: br !if.expr.result.loc14(%t.ref.loc14)
  217. // CHECK:STDOUT:
  218. // CHECK:STDOUT: !if.expr.result.loc14:
  219. // CHECK:STDOUT: %.loc14: type = block_arg !if.expr.result.loc14
  220. // CHECK:STDOUT: br !.loc14_7
  221. // CHECK:STDOUT:
  222. // CHECK:STDOUT: !.loc14_7:
  223. // CHECK:STDOUT: %w: <error> = bind_name w, <error> [concrete = <error>]
  224. // CHECK:STDOUT: %impl.elem0.loc14: <error> = impl_witness_access <error>, element0 [concrete = <error>]
  225. // CHECK:STDOUT: %impl.elem0.loc9: <error> = impl_witness_access <error>, element0 [concrete = <error>]
  226. // CHECK:STDOUT: return
  227. // CHECK:STDOUT: }
  228. // CHECK:STDOUT: