fail_partial_constant.carbon 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. // --- fail_non_constant_condition.carbon
  7. package NonConstantCondition api;
  8. fn ConditionIsNonConstant(b: bool) {
  9. // We choose to not accept this even if both arms evaluate to the same
  10. // constant value, because it notionally involves evaluating a non-constant
  11. // condition.
  12. // CHECK:STDERR: fail_non_constant_condition.carbon:[[@LINE+4]]:10: ERROR: Cannot evaluate type expression.
  13. // CHECK:STDERR: var v: if b then i32 else i32 = 1;
  14. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~
  15. // CHECK:STDERR:
  16. var v: if b then i32 else i32 = 1;
  17. }
  18. // --- fail_non_constant_result.carbon
  19. package NonConstantResult api;
  20. fn ChosenBranchIsNonConstant(t: type) {
  21. // CHECK:STDERR: fail_non_constant_result.carbon:[[@LINE+4]]:10: ERROR: Cannot evaluate type expression.
  22. // CHECK:STDERR: var v: if true then t else i32 = 1;
  23. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~
  24. // CHECK:STDERR:
  25. var v: if true then t else i32 = 1;
  26. // CHECK:STDERR: fail_non_constant_result.carbon:[[@LINE+3]]:10: ERROR: Cannot evaluate type expression.
  27. // CHECK:STDERR: var w: if false then i32 else t = 1;
  28. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~
  29. var w: if false then i32 else t = 1;
  30. }
  31. // CHECK:STDOUT: --- fail_non_constant_condition.carbon
  32. // CHECK:STDOUT:
  33. // CHECK:STDOUT: constants {
  34. // CHECK:STDOUT: %.1: i32 = int_literal 1 [template]
  35. // CHECK:STDOUT: }
  36. // CHECK:STDOUT:
  37. // CHECK:STDOUT: file {
  38. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  39. // CHECK:STDOUT: .Core = %Core
  40. // CHECK:STDOUT: .ConditionIsNonConstant = %ConditionIsNonConstant
  41. // CHECK:STDOUT: }
  42. // CHECK:STDOUT: %Core: <namespace> = namespace [template] {}
  43. // CHECK:STDOUT: %ConditionIsNonConstant: <function> = fn_decl @ConditionIsNonConstant [template] {
  44. // CHECK:STDOUT: %b.loc4_27.1: bool = param b
  45. // CHECK:STDOUT: @ConditionIsNonConstant.%b: bool = bind_name b, %b.loc4_27.1
  46. // CHECK:STDOUT: }
  47. // CHECK:STDOUT: }
  48. // CHECK:STDOUT:
  49. // CHECK:STDOUT: fn @ConditionIsNonConstant(%b: bool) {
  50. // CHECK:STDOUT: !entry:
  51. // CHECK:STDOUT: %b.ref: bool = name_ref b, %b
  52. // CHECK:STDOUT: if %b.ref br !if.expr.then else br !if.expr.else
  53. // CHECK:STDOUT:
  54. // CHECK:STDOUT: !if.expr.then:
  55. // CHECK:STDOUT: br !if.expr.result(i32)
  56. // CHECK:STDOUT:
  57. // CHECK:STDOUT: !if.expr.else:
  58. // CHECK:STDOUT: br !if.expr.result(i32)
  59. // CHECK:STDOUT:
  60. // CHECK:STDOUT: !if.expr.result:
  61. // CHECK:STDOUT: %.loc12_10: type = block_arg !if.expr.result
  62. // CHECK:STDOUT: %v.var: ref <error> = var v
  63. // CHECK:STDOUT: %v: ref <error> = bind_name v, %v.var
  64. // CHECK:STDOUT: %.loc12_35: i32 = int_literal 1 [template = constants.%.1]
  65. // CHECK:STDOUT: assign %v.var, <error>
  66. // CHECK:STDOUT: return
  67. // CHECK:STDOUT: }
  68. // CHECK:STDOUT:
  69. // CHECK:STDOUT: --- fail_non_constant_result.carbon
  70. // CHECK:STDOUT:
  71. // CHECK:STDOUT: constants {
  72. // CHECK:STDOUT: %.1: bool = bool_literal true [template]
  73. // CHECK:STDOUT: %.2: i32 = int_literal 1 [template]
  74. // CHECK:STDOUT: %.3: bool = bool_literal false [template]
  75. // CHECK:STDOUT: }
  76. // CHECK:STDOUT:
  77. // CHECK:STDOUT: file {
  78. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  79. // CHECK:STDOUT: .Core = %Core
  80. // CHECK:STDOUT: .ChosenBranchIsNonConstant = %ChosenBranchIsNonConstant
  81. // CHECK:STDOUT: }
  82. // CHECK:STDOUT: %Core: <namespace> = namespace [template] {}
  83. // CHECK:STDOUT: %ChosenBranchIsNonConstant: <function> = fn_decl @ChosenBranchIsNonConstant [template] {
  84. // CHECK:STDOUT: %t.loc4_30.1: type = param t
  85. // CHECK:STDOUT: @ChosenBranchIsNonConstant.%t: type = bind_name t, %t.loc4_30.1
  86. // CHECK:STDOUT: }
  87. // CHECK:STDOUT: }
  88. // CHECK:STDOUT:
  89. // CHECK:STDOUT: fn @ChosenBranchIsNonConstant(%t: type) {
  90. // CHECK:STDOUT: !entry:
  91. // CHECK:STDOUT: %.loc9_13: bool = bool_literal true [template = constants.%.1]
  92. // CHECK:STDOUT: if %.loc9_13 br !if.expr.then.loc9 else br !if.expr.else.loc9
  93. // CHECK:STDOUT:
  94. // CHECK:STDOUT: !if.expr.then.loc9:
  95. // CHECK:STDOUT: %t.ref.loc9: type = name_ref t, %t
  96. // CHECK:STDOUT: br !if.expr.result.loc9(%t.ref.loc9)
  97. // CHECK:STDOUT:
  98. // CHECK:STDOUT: !if.expr.else.loc9:
  99. // CHECK:STDOUT: br !if.expr.result.loc9(i32)
  100. // CHECK:STDOUT:
  101. // CHECK:STDOUT: !if.expr.result.loc9:
  102. // CHECK:STDOUT: %.loc9_10: type = block_arg !if.expr.result.loc9
  103. // CHECK:STDOUT: %v.var: ref <error> = var v
  104. // CHECK:STDOUT: %v: ref <error> = bind_name v, %v.var
  105. // CHECK:STDOUT: %.loc9_36: i32 = int_literal 1 [template = constants.%.2]
  106. // CHECK:STDOUT: assign %v.var, <error>
  107. // CHECK:STDOUT: %.loc13_13: bool = bool_literal false [template = constants.%.3]
  108. // CHECK:STDOUT: if %.loc13_13 br !if.expr.then.loc13 else br !if.expr.else.loc13
  109. // CHECK:STDOUT:
  110. // CHECK:STDOUT: !if.expr.then.loc13:
  111. // CHECK:STDOUT: br !if.expr.result.loc13(i32)
  112. // CHECK:STDOUT:
  113. // CHECK:STDOUT: !if.expr.else.loc13:
  114. // CHECK:STDOUT: %t.ref.loc13: type = name_ref t, %t
  115. // CHECK:STDOUT: br !if.expr.result.loc13(%t.ref.loc13)
  116. // CHECK:STDOUT:
  117. // CHECK:STDOUT: !if.expr.result.loc13:
  118. // CHECK:STDOUT: %.loc13_10: type = block_arg !if.expr.result.loc13
  119. // CHECK:STDOUT: %w.var: ref <error> = var w
  120. // CHECK:STDOUT: %w: ref <error> = bind_name w, %w.var
  121. // CHECK:STDOUT: %.loc13_37: i32 = int_literal 1 [template = constants.%.2]
  122. // CHECK:STDOUT: assign %w.var, <error>
  123. // CHECK:STDOUT: return
  124. // CHECK:STDOUT: }
  125. // CHECK:STDOUT: