fail_partial_constant.carbon 11 KB

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