fail_partial_constant.carbon 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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: %ConditionIsNonConstant: type = fn_type @ConditionIsNonConstant [template]
  35. // CHECK:STDOUT: %.1: type = tuple_type () [template]
  36. // CHECK:STDOUT: %struct: ConditionIsNonConstant = struct_value () [template]
  37. // CHECK:STDOUT: %.2: i32 = int_literal 1 [template]
  38. // CHECK:STDOUT: }
  39. // CHECK:STDOUT:
  40. // CHECK:STDOUT: file {
  41. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  42. // CHECK:STDOUT: .Core = %Core
  43. // CHECK:STDOUT: .ConditionIsNonConstant = %ConditionIsNonConstant.decl
  44. // CHECK:STDOUT: }
  45. // CHECK:STDOUT: %Core: <namespace> = namespace [template] {}
  46. // CHECK:STDOUT: %ConditionIsNonConstant.decl: ConditionIsNonConstant = fn_decl @ConditionIsNonConstant [template = constants.%struct] {
  47. // CHECK:STDOUT: %b.loc4_27.1: bool = param b
  48. // CHECK:STDOUT: @ConditionIsNonConstant.%b: bool = bind_name b, %b.loc4_27.1
  49. // CHECK:STDOUT: }
  50. // CHECK:STDOUT: }
  51. // CHECK:STDOUT:
  52. // CHECK:STDOUT: fn @ConditionIsNonConstant(%b: bool) {
  53. // CHECK:STDOUT: !entry:
  54. // CHECK:STDOUT: %b.ref: bool = name_ref b, %b
  55. // CHECK:STDOUT: if %b.ref br !if.expr.then else br !if.expr.else
  56. // CHECK:STDOUT:
  57. // CHECK:STDOUT: !if.expr.then:
  58. // CHECK:STDOUT: br !if.expr.result(i32)
  59. // CHECK:STDOUT:
  60. // CHECK:STDOUT: !if.expr.else:
  61. // CHECK:STDOUT: br !if.expr.result(i32)
  62. // CHECK:STDOUT:
  63. // CHECK:STDOUT: !if.expr.result:
  64. // CHECK:STDOUT: %.loc12_10: type = block_arg !if.expr.result
  65. // CHECK:STDOUT: %v.var: ref <error> = var v
  66. // CHECK:STDOUT: %v: ref <error> = bind_name v, %v.var
  67. // CHECK:STDOUT: %.loc12_35: i32 = int_literal 1 [template = constants.%.2]
  68. // CHECK:STDOUT: assign %v.var, <error>
  69. // CHECK:STDOUT: return
  70. // CHECK:STDOUT: }
  71. // CHECK:STDOUT:
  72. // CHECK:STDOUT: --- fail_non_constant_result.carbon
  73. // CHECK:STDOUT:
  74. // CHECK:STDOUT: constants {
  75. // CHECK:STDOUT: %ChosenBranchIsNonConstant: type = fn_type @ChosenBranchIsNonConstant [template]
  76. // CHECK:STDOUT: %.1: type = tuple_type () [template]
  77. // CHECK:STDOUT: %struct: ChosenBranchIsNonConstant = struct_value () [template]
  78. // CHECK:STDOUT: %.2: bool = bool_literal true [template]
  79. // CHECK:STDOUT: %.3: i32 = int_literal 1 [template]
  80. // CHECK:STDOUT: %.4: bool = bool_literal false [template]
  81. // CHECK:STDOUT: }
  82. // CHECK:STDOUT:
  83. // CHECK:STDOUT: file {
  84. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  85. // CHECK:STDOUT: .Core = %Core
  86. // CHECK:STDOUT: .ChosenBranchIsNonConstant = %ChosenBranchIsNonConstant.decl
  87. // CHECK:STDOUT: }
  88. // CHECK:STDOUT: %Core: <namespace> = namespace [template] {}
  89. // CHECK:STDOUT: %ChosenBranchIsNonConstant.decl: ChosenBranchIsNonConstant = fn_decl @ChosenBranchIsNonConstant [template = constants.%struct] {
  90. // CHECK:STDOUT: %t.loc4_30.1: type = param t
  91. // CHECK:STDOUT: @ChosenBranchIsNonConstant.%t: type = bind_name t, %t.loc4_30.1
  92. // CHECK:STDOUT: }
  93. // CHECK:STDOUT: }
  94. // CHECK:STDOUT:
  95. // CHECK:STDOUT: fn @ChosenBranchIsNonConstant(%t: type) {
  96. // CHECK:STDOUT: !entry:
  97. // CHECK:STDOUT: %.loc9_13: bool = bool_literal true [template = constants.%.2]
  98. // CHECK:STDOUT: if %.loc9_13 br !if.expr.then.loc9 else br !if.expr.else.loc9
  99. // CHECK:STDOUT:
  100. // CHECK:STDOUT: !if.expr.then.loc9:
  101. // CHECK:STDOUT: %t.ref.loc9: type = name_ref t, %t
  102. // CHECK:STDOUT: br !if.expr.result.loc9(%t.ref.loc9)
  103. // CHECK:STDOUT:
  104. // CHECK:STDOUT: !if.expr.else.loc9:
  105. // CHECK:STDOUT: br !if.expr.result.loc9(i32)
  106. // CHECK:STDOUT:
  107. // CHECK:STDOUT: !if.expr.result.loc9:
  108. // CHECK:STDOUT: %.loc9_10: type = block_arg !if.expr.result.loc9
  109. // CHECK:STDOUT: %v.var: ref <error> = var v
  110. // CHECK:STDOUT: %v: ref <error> = bind_name v, %v.var
  111. // CHECK:STDOUT: %.loc9_36: i32 = int_literal 1 [template = constants.%.3]
  112. // CHECK:STDOUT: assign %v.var, <error>
  113. // CHECK:STDOUT: %.loc13_13: bool = bool_literal false [template = constants.%.4]
  114. // CHECK:STDOUT: if %.loc13_13 br !if.expr.then.loc13 else br !if.expr.else.loc13
  115. // CHECK:STDOUT:
  116. // CHECK:STDOUT: !if.expr.then.loc13:
  117. // CHECK:STDOUT: br !if.expr.result.loc13(i32)
  118. // CHECK:STDOUT:
  119. // CHECK:STDOUT: !if.expr.else.loc13:
  120. // CHECK:STDOUT: %t.ref.loc13: type = name_ref t, %t
  121. // CHECK:STDOUT: br !if.expr.result.loc13(%t.ref.loc13)
  122. // CHECK:STDOUT:
  123. // CHECK:STDOUT: !if.expr.result.loc13:
  124. // CHECK:STDOUT: %.loc13_10: type = block_arg !if.expr.result.loc13
  125. // CHECK:STDOUT: %w.var: ref <error> = var w
  126. // CHECK:STDOUT: %w: ref <error> = bind_name w, %w.var
  127. // CHECK:STDOUT: %.loc13_37: i32 = int_literal 1 [template = constants.%.3]
  128. // CHECK:STDOUT: assign %w.var, <error>
  129. // CHECK:STDOUT: return
  130. // CHECK:STDOUT: }
  131. // CHECK:STDOUT: