fail_partial_constant.carbon 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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+3]]:10: error: cannot evaluate type expression [TypeExprEvaluationFailure]
  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: %Bool: %Bool.type = struct_value () [template]
  40. // CHECK:STDOUT: %ConditionIsNonConstant.type: type = fn_type @ConditionIsNonConstant [template]
  41. // CHECK:STDOUT: %ConditionIsNonConstant: %ConditionIsNonConstant.type = struct_value () [template]
  42. // CHECK:STDOUT: %int_1: Core.IntLiteral = int_value 1 [template]
  43. // CHECK:STDOUT: }
  44. // CHECK:STDOUT:
  45. // CHECK:STDOUT: imports {
  46. // CHECK:STDOUT: %Core: <namespace> = namespace file.%Core.import, [template] {
  47. // CHECK:STDOUT: .Bool = %import_ref.f7c
  48. // CHECK:STDOUT: .Int = %import_ref.187
  49. // CHECK:STDOUT: import Core//prelude
  50. // CHECK:STDOUT: import Core//prelude/...
  51. // CHECK:STDOUT: }
  52. // CHECK:STDOUT: }
  53. // CHECK:STDOUT:
  54. // CHECK:STDOUT: file {
  55. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  56. // CHECK:STDOUT: .Core = imports.%Core
  57. // CHECK:STDOUT: .ConditionIsNonConstant = %ConditionIsNonConstant.decl
  58. // CHECK:STDOUT: }
  59. // CHECK:STDOUT: %Core.import = import Core
  60. // CHECK:STDOUT: %ConditionIsNonConstant.decl: %ConditionIsNonConstant.type = fn_decl @ConditionIsNonConstant [template = constants.%ConditionIsNonConstant] {
  61. // CHECK:STDOUT: %b.patt: bool = binding_pattern b
  62. // CHECK:STDOUT: %b.param_patt: bool = value_param_pattern %b.patt, runtime_param0
  63. // CHECK:STDOUT: } {
  64. // CHECK:STDOUT: %b.param: bool = value_param runtime_param0
  65. // CHECK:STDOUT: %.loc4_30.1: type = splice_block %.loc4_30.3 [template = bool] {
  66. // CHECK:STDOUT: %bool.make_type: init type = call constants.%Bool() [template = bool]
  67. // CHECK:STDOUT: %.loc4_30.2: type = value_of_initializer %bool.make_type [template = bool]
  68. // CHECK:STDOUT: %.loc4_30.3: type = converted %bool.make_type, %.loc4_30.2 [template = bool]
  69. // CHECK:STDOUT: }
  70. // CHECK:STDOUT: %b: bool = bind_name b, %b.param
  71. // CHECK:STDOUT: }
  72. // CHECK:STDOUT: }
  73. // CHECK:STDOUT:
  74. // CHECK:STDOUT: fn @ConditionIsNonConstant(%b.param_patt: bool) {
  75. // CHECK:STDOUT: !entry:
  76. // CHECK:STDOUT: %v.var: ref <error> = var v
  77. // CHECK:STDOUT: %v: ref <error> = bind_name v, %v.var
  78. // CHECK:STDOUT: %int_1: Core.IntLiteral = int_value 1 [template = constants.%int_1]
  79. // CHECK:STDOUT: assign %v.var, <error>
  80. // CHECK:STDOUT: return
  81. // CHECK:STDOUT: }
  82. // CHECK:STDOUT:
  83. // CHECK:STDOUT: --- fail_non_constant_result.carbon
  84. // CHECK:STDOUT:
  85. // CHECK:STDOUT: constants {
  86. // CHECK:STDOUT: %ChosenBranchIsNonConstant.type: type = fn_type @ChosenBranchIsNonConstant [template]
  87. // CHECK:STDOUT: %ChosenBranchIsNonConstant: %ChosenBranchIsNonConstant.type = struct_value () [template]
  88. // CHECK:STDOUT: %int_1: Core.IntLiteral = int_value 1 [template]
  89. // CHECK:STDOUT: }
  90. // CHECK:STDOUT:
  91. // CHECK:STDOUT: imports {
  92. // CHECK:STDOUT: %Core: <namespace> = namespace file.%Core.import, [template] {
  93. // CHECK:STDOUT: .Int = %import_ref.187
  94. // CHECK:STDOUT: import Core//prelude
  95. // CHECK:STDOUT: import Core//prelude/...
  96. // CHECK:STDOUT: }
  97. // CHECK:STDOUT: }
  98. // CHECK:STDOUT:
  99. // CHECK:STDOUT: file {
  100. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  101. // CHECK:STDOUT: .Core = imports.%Core
  102. // CHECK:STDOUT: .ChosenBranchIsNonConstant = %ChosenBranchIsNonConstant.decl
  103. // CHECK:STDOUT: }
  104. // CHECK:STDOUT: %Core.import = import Core
  105. // CHECK:STDOUT: %ChosenBranchIsNonConstant.decl: %ChosenBranchIsNonConstant.type = fn_decl @ChosenBranchIsNonConstant [template = constants.%ChosenBranchIsNonConstant] {
  106. // CHECK:STDOUT: %t.patt: type = binding_pattern t
  107. // CHECK:STDOUT: %t.param_patt: type = value_param_pattern %t.patt, runtime_param0
  108. // CHECK:STDOUT: } {
  109. // CHECK:STDOUT: %t.param: type = value_param runtime_param0
  110. // CHECK:STDOUT: %t: type = bind_name t, %t.param
  111. // CHECK:STDOUT: }
  112. // CHECK:STDOUT: }
  113. // CHECK:STDOUT:
  114. // CHECK:STDOUT: fn @ChosenBranchIsNonConstant(%t.param_patt: type) {
  115. // CHECK:STDOUT: !entry:
  116. // CHECK:STDOUT: %v.var: ref <error> = var v
  117. // CHECK:STDOUT: %v: ref <error> = bind_name v, %v.var
  118. // CHECK:STDOUT: %int_1.loc9: Core.IntLiteral = int_value 1 [template = constants.%int_1]
  119. // CHECK:STDOUT: assign %v.var, <error>
  120. // CHECK:STDOUT: %w.var: ref <error> = var w
  121. // CHECK:STDOUT: %w: ref <error> = bind_name w, %w.var
  122. // CHECK:STDOUT: %int_1.loc13: Core.IntLiteral = int_value 1 [template = constants.%int_1]
  123. // CHECK:STDOUT: assign %w.var, <error>
  124. // CHECK:STDOUT: return
  125. // CHECK:STDOUT: }
  126. // CHECK:STDOUT: