fail_expr_category.carbon 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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/index/fail_expr_category.carbon
  8. // TIP: To dump output, run:
  9. // TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/index/fail_expr_category.carbon
  10. fn F() -> [i32; 3];
  11. fn G(b: [i32; 3]) {
  12. // Indexing an array value gives a value.
  13. // CHECK:STDERR: fail_expr_category.carbon:[[@LINE+4]]:18: error: cannot take the address of non-reference expression [AddrOfNonRef]
  14. // CHECK:STDERR: var pb: i32* = &b[0];
  15. // CHECK:STDERR: ^
  16. // CHECK:STDERR:
  17. var pb: i32* = &b[0];
  18. // CHECK:STDERR: fail_expr_category.carbon:[[@LINE+4]]:3: error: expression is not assignable [AssignmentToNonAssignable]
  19. // CHECK:STDERR: b[0] = 4;
  20. // CHECK:STDERR: ^~~~
  21. // CHECK:STDERR:
  22. b[0] = 4;
  23. // Indexing an ephemeral reference (materialized from an initializing
  24. // expression) gives a value.
  25. // CHECK:STDERR: fail_expr_category.carbon:[[@LINE+4]]:18: error: cannot take the address of non-reference expression [AddrOfNonRef]
  26. // CHECK:STDERR: var pf: i32* = &F()[0];
  27. // CHECK:STDERR: ^
  28. // CHECK:STDERR:
  29. var pf: i32* = &F()[0];
  30. // CHECK:STDERR: fail_expr_category.carbon:[[@LINE+3]]:3: error: expression is not assignable [AssignmentToNonAssignable]
  31. // CHECK:STDERR: F()[0] = 4;
  32. // CHECK:STDERR: ^~~~~~
  33. F()[0] = 4;
  34. }
  35. // CHECK:STDOUT: --- fail_expr_category.carbon
  36. // CHECK:STDOUT:
  37. // CHECK:STDOUT: constants {
  38. // CHECK:STDOUT: %int_32: Core.IntLiteral = int_value 32 [template]
  39. // CHECK:STDOUT: %i32: type = class_type @Int, @Int(%int_32) [template]
  40. // CHECK:STDOUT: %int_3: Core.IntLiteral = int_value 3 [template]
  41. // CHECK:STDOUT: %array_type: type = array_type %int_3, %i32 [template]
  42. // CHECK:STDOUT: %F.type: type = fn_type @F [template]
  43. // CHECK:STDOUT: %F: %F.type = struct_value () [template]
  44. // CHECK:STDOUT: %G.type: type = fn_type @G [template]
  45. // CHECK:STDOUT: %G: %G.type = struct_value () [template]
  46. // CHECK:STDOUT: %ptr.2: type = ptr_type %i32 [template]
  47. // CHECK:STDOUT: %int_0.1: Core.IntLiteral = int_value 0 [template]
  48. // CHECK:STDOUT: %Convert.type.2: type = fn_type @Convert.1, @ImplicitAs(%i32) [template]
  49. // CHECK:STDOUT: %Convert.type.10: type = fn_type @Convert.2, @impl.1(%int_32) [template]
  50. // CHECK:STDOUT: %Convert.10: %Convert.type.10 = struct_value () [template]
  51. // CHECK:STDOUT: %interface.5: <witness> = interface_witness (%Convert.10) [template]
  52. // CHECK:STDOUT: %Convert.bound.1: <bound method> = bound_method %int_0.1, %Convert.10 [template]
  53. // CHECK:STDOUT: %Convert.specific_fn.1: <specific function> = specific_function %Convert.bound.1, @Convert.2(%int_32) [template]
  54. // CHECK:STDOUT: %int_0.2: %i32 = int_value 0 [template]
  55. // CHECK:STDOUT: %int_4.1: Core.IntLiteral = int_value 4 [template]
  56. // CHECK:STDOUT: %Convert.bound.2: <bound method> = bound_method %int_4.1, %Convert.10 [template]
  57. // CHECK:STDOUT: %Convert.specific_fn.2: <specific function> = specific_function %Convert.bound.2, @Convert.2(%int_32) [template]
  58. // CHECK:STDOUT: %int_4.2: %i32 = int_value 4 [template]
  59. // CHECK:STDOUT: }
  60. // CHECK:STDOUT:
  61. // CHECK:STDOUT: imports {
  62. // CHECK:STDOUT: %Core: <namespace> = namespace file.%Core.import, [template] {
  63. // CHECK:STDOUT: .Int = %import_ref.1
  64. // CHECK:STDOUT: .ImplicitAs = %import_ref.5
  65. // CHECK:STDOUT: import Core//prelude
  66. // CHECK:STDOUT: import Core//prelude/...
  67. // CHECK:STDOUT: }
  68. // CHECK:STDOUT: }
  69. // CHECK:STDOUT:
  70. // CHECK:STDOUT: file {
  71. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  72. // CHECK:STDOUT: .Core = imports.%Core
  73. // CHECK:STDOUT: .F = %F.decl
  74. // CHECK:STDOUT: .G = %G.decl
  75. // CHECK:STDOUT: }
  76. // CHECK:STDOUT: %Core.import = import Core
  77. // CHECK:STDOUT: %F.decl: %F.type = fn_decl @F [template = constants.%F] {
  78. // CHECK:STDOUT: %return.patt: %array_type = return_slot_pattern
  79. // CHECK:STDOUT: %return.param_patt: %array_type = out_param_pattern %return.patt, runtime_param0
  80. // CHECK:STDOUT: } {
  81. // CHECK:STDOUT: %int_32: Core.IntLiteral = int_value 32 [template = constants.%int_32]
  82. // CHECK:STDOUT: %i32: type = class_type @Int, @Int(constants.%int_32) [template = constants.%i32]
  83. // CHECK:STDOUT: %int_3: Core.IntLiteral = int_value 3 [template = constants.%int_3]
  84. // CHECK:STDOUT: %array_type: type = array_type %int_3, %i32 [template = constants.%array_type]
  85. // CHECK:STDOUT: %return.param: ref %array_type = out_param runtime_param0
  86. // CHECK:STDOUT: %return: ref %array_type = return_slot %return.param
  87. // CHECK:STDOUT: }
  88. // CHECK:STDOUT: %G.decl: %G.type = fn_decl @G [template = constants.%G] {
  89. // CHECK:STDOUT: %b.patt: %array_type = binding_pattern b
  90. // CHECK:STDOUT: %b.param_patt: %array_type = value_param_pattern %b.patt, runtime_param0
  91. // CHECK:STDOUT: } {
  92. // CHECK:STDOUT: %b.param: %array_type = value_param runtime_param0
  93. // CHECK:STDOUT: %.loc13: type = splice_block %array_type [template = constants.%array_type] {
  94. // CHECK:STDOUT: %int_32.loc13: Core.IntLiteral = int_value 32 [template = constants.%int_32]
  95. // CHECK:STDOUT: %i32.loc13: type = class_type @Int, @Int(constants.%int_32) [template = constants.%i32]
  96. // CHECK:STDOUT: %int_3: Core.IntLiteral = int_value 3 [template = constants.%int_3]
  97. // CHECK:STDOUT: %array_type: type = array_type %int_3, %i32 [template = constants.%array_type]
  98. // CHECK:STDOUT: }
  99. // CHECK:STDOUT: %b: %array_type = bind_name b, %b.param
  100. // CHECK:STDOUT: }
  101. // CHECK:STDOUT: }
  102. // CHECK:STDOUT:
  103. // CHECK:STDOUT: fn @F() -> %array_type;
  104. // CHECK:STDOUT:
  105. // CHECK:STDOUT: fn @G(%b.param_patt: %array_type) {
  106. // CHECK:STDOUT: !entry:
  107. // CHECK:STDOUT: %pb.var: ref %ptr.2 = var pb
  108. // CHECK:STDOUT: %pb: ref %ptr.2 = bind_name pb, %pb.var
  109. // CHECK:STDOUT: %b.ref.loc19: %array_type = name_ref b, %b
  110. // CHECK:STDOUT: %int_0.loc19: Core.IntLiteral = int_value 0 [template = constants.%int_0.1]
  111. // CHECK:STDOUT: %int_32.loc19: Core.IntLiteral = int_value 32 [template = constants.%int_32]
  112. // CHECK:STDOUT: %i32.loc19: type = class_type @Int, @Int(constants.%int_32) [template = constants.%i32]
  113. // CHECK:STDOUT: %impl.elem0.loc19: %Convert.type.2 = interface_witness_access constants.%interface.5, element0 [template = constants.%Convert.10]
  114. // CHECK:STDOUT: %Convert.bound.loc19: <bound method> = bound_method %int_0.loc19, %impl.elem0.loc19 [template = constants.%Convert.bound.1]
  115. // CHECK:STDOUT: %Convert.specific_fn.loc19: <specific function> = specific_function %Convert.bound.loc19, @Convert.2(constants.%int_32) [template = constants.%Convert.specific_fn.1]
  116. // CHECK:STDOUT: %int.convert_checked.loc19: init %i32 = call %Convert.specific_fn.loc19(%int_0.loc19) [template = constants.%int_0.2]
  117. // CHECK:STDOUT: %.loc19_21.1: %i32 = value_of_initializer %int.convert_checked.loc19 [template = constants.%int_0.2]
  118. // CHECK:STDOUT: %.loc19_21.2: %i32 = converted %int_0.loc19, %.loc19_21.1 [template = constants.%int_0.2]
  119. // CHECK:STDOUT: %.loc19_22.1: ref %array_type = value_as_ref %b.ref.loc19
  120. // CHECK:STDOUT: %.loc19_22.2: ref %i32 = array_index %.loc19_22.1, %.loc19_21.2
  121. // CHECK:STDOUT: %.loc19_22.3: %i32 = bind_value %.loc19_22.2
  122. // CHECK:STDOUT: %addr.loc19: %ptr.2 = addr_of <error> [template = <error>]
  123. // CHECK:STDOUT: assign %pb.var, %addr.loc19
  124. // CHECK:STDOUT: %b.ref.loc24: %array_type = name_ref b, %b
  125. // CHECK:STDOUT: %int_0.loc24: Core.IntLiteral = int_value 0 [template = constants.%int_0.1]
  126. // CHECK:STDOUT: %int_32.loc24: Core.IntLiteral = int_value 32 [template = constants.%int_32]
  127. // CHECK:STDOUT: %i32.loc24: type = class_type @Int, @Int(constants.%int_32) [template = constants.%i32]
  128. // CHECK:STDOUT: %impl.elem0.loc24_5: %Convert.type.2 = interface_witness_access constants.%interface.5, element0 [template = constants.%Convert.10]
  129. // CHECK:STDOUT: %Convert.bound.loc24_5: <bound method> = bound_method %int_0.loc24, %impl.elem0.loc24_5 [template = constants.%Convert.bound.1]
  130. // CHECK:STDOUT: %Convert.specific_fn.loc24_5: <specific function> = specific_function %Convert.bound.loc24_5, @Convert.2(constants.%int_32) [template = constants.%Convert.specific_fn.1]
  131. // CHECK:STDOUT: %int.convert_checked.loc24_5: init %i32 = call %Convert.specific_fn.loc24_5(%int_0.loc24) [template = constants.%int_0.2]
  132. // CHECK:STDOUT: %.loc24_5.1: %i32 = value_of_initializer %int.convert_checked.loc24_5 [template = constants.%int_0.2]
  133. // CHECK:STDOUT: %.loc24_5.2: %i32 = converted %int_0.loc24, %.loc24_5.1 [template = constants.%int_0.2]
  134. // CHECK:STDOUT: %.loc24_6.1: ref %array_type = value_as_ref %b.ref.loc24
  135. // CHECK:STDOUT: %.loc24_6.2: ref %i32 = array_index %.loc24_6.1, %.loc24_5.2
  136. // CHECK:STDOUT: %.loc24_6.3: %i32 = bind_value %.loc24_6.2
  137. // CHECK:STDOUT: %int_4.loc24: Core.IntLiteral = int_value 4 [template = constants.%int_4.1]
  138. // CHECK:STDOUT: %impl.elem0.loc24_8: %Convert.type.2 = interface_witness_access constants.%interface.5, element0 [template = constants.%Convert.10]
  139. // CHECK:STDOUT: %Convert.bound.loc24_8: <bound method> = bound_method %int_4.loc24, %impl.elem0.loc24_8 [template = constants.%Convert.bound.2]
  140. // CHECK:STDOUT: %Convert.specific_fn.loc24_8: <specific function> = specific_function %Convert.bound.loc24_8, @Convert.2(constants.%int_32) [template = constants.%Convert.specific_fn.2]
  141. // CHECK:STDOUT: %int.convert_checked.loc24_8: init %i32 = call %Convert.specific_fn.loc24_8(%int_4.loc24) [template = constants.%int_4.2]
  142. // CHECK:STDOUT: %.loc24_8: init %i32 = converted %int_4.loc24, %int.convert_checked.loc24_8 [template = constants.%int_4.2]
  143. // CHECK:STDOUT: assign %.loc24_6.3, %.loc24_8
  144. // CHECK:STDOUT: %pf.var: ref %ptr.2 = var pf
  145. // CHECK:STDOUT: %pf: ref %ptr.2 = bind_name pf, %pf.var
  146. // CHECK:STDOUT: %F.ref.loc32: %F.type = name_ref F, file.%F.decl [template = constants.%F]
  147. // CHECK:STDOUT: %.loc32_21.1: ref %array_type = temporary_storage
  148. // CHECK:STDOUT: %F.call.loc32: init %array_type = call %F.ref.loc32() to %.loc32_21.1
  149. // CHECK:STDOUT: %int_0.loc32: Core.IntLiteral = int_value 0 [template = constants.%int_0.1]
  150. // CHECK:STDOUT: %.loc32_21.2: ref %array_type = temporary %.loc32_21.1, %F.call.loc32
  151. // CHECK:STDOUT: %int_32.loc32: Core.IntLiteral = int_value 32 [template = constants.%int_32]
  152. // CHECK:STDOUT: %i32.loc32: type = class_type @Int, @Int(constants.%int_32) [template = constants.%i32]
  153. // CHECK:STDOUT: %impl.elem0.loc32: %Convert.type.2 = interface_witness_access constants.%interface.5, element0 [template = constants.%Convert.10]
  154. // CHECK:STDOUT: %Convert.bound.loc32: <bound method> = bound_method %int_0.loc32, %impl.elem0.loc32 [template = constants.%Convert.bound.1]
  155. // CHECK:STDOUT: %Convert.specific_fn.loc32: <specific function> = specific_function %Convert.bound.loc32, @Convert.2(constants.%int_32) [template = constants.%Convert.specific_fn.1]
  156. // CHECK:STDOUT: %int.convert_checked.loc32: init %i32 = call %Convert.specific_fn.loc32(%int_0.loc32) [template = constants.%int_0.2]
  157. // CHECK:STDOUT: %.loc32_23.1: %i32 = value_of_initializer %int.convert_checked.loc32 [template = constants.%int_0.2]
  158. // CHECK:STDOUT: %.loc32_23.2: %i32 = converted %int_0.loc32, %.loc32_23.1 [template = constants.%int_0.2]
  159. // CHECK:STDOUT: %.loc32_24.1: ref %i32 = array_index %.loc32_21.2, %.loc32_23.2
  160. // CHECK:STDOUT: %.loc32_24.2: %i32 = bind_value %.loc32_24.1
  161. // CHECK:STDOUT: %addr.loc32: %ptr.2 = addr_of <error> [template = <error>]
  162. // CHECK:STDOUT: assign %pf.var, %addr.loc32
  163. // CHECK:STDOUT: %F.ref.loc36: %F.type = name_ref F, file.%F.decl [template = constants.%F]
  164. // CHECK:STDOUT: %.loc36_5.1: ref %array_type = temporary_storage
  165. // CHECK:STDOUT: %F.call.loc36: init %array_type = call %F.ref.loc36() to %.loc36_5.1
  166. // CHECK:STDOUT: %int_0.loc36: Core.IntLiteral = int_value 0 [template = constants.%int_0.1]
  167. // CHECK:STDOUT: %.loc36_5.2: ref %array_type = temporary %.loc36_5.1, %F.call.loc36
  168. // CHECK:STDOUT: %int_32.loc36: Core.IntLiteral = int_value 32 [template = constants.%int_32]
  169. // CHECK:STDOUT: %i32.loc36: type = class_type @Int, @Int(constants.%int_32) [template = constants.%i32]
  170. // CHECK:STDOUT: %impl.elem0.loc36_7: %Convert.type.2 = interface_witness_access constants.%interface.5, element0 [template = constants.%Convert.10]
  171. // CHECK:STDOUT: %Convert.bound.loc36_7: <bound method> = bound_method %int_0.loc36, %impl.elem0.loc36_7 [template = constants.%Convert.bound.1]
  172. // CHECK:STDOUT: %Convert.specific_fn.loc36_7: <specific function> = specific_function %Convert.bound.loc36_7, @Convert.2(constants.%int_32) [template = constants.%Convert.specific_fn.1]
  173. // CHECK:STDOUT: %int.convert_checked.loc36_7: init %i32 = call %Convert.specific_fn.loc36_7(%int_0.loc36) [template = constants.%int_0.2]
  174. // CHECK:STDOUT: %.loc36_7.1: %i32 = value_of_initializer %int.convert_checked.loc36_7 [template = constants.%int_0.2]
  175. // CHECK:STDOUT: %.loc36_7.2: %i32 = converted %int_0.loc36, %.loc36_7.1 [template = constants.%int_0.2]
  176. // CHECK:STDOUT: %.loc36_8.1: ref %i32 = array_index %.loc36_5.2, %.loc36_7.2
  177. // CHECK:STDOUT: %.loc36_8.2: %i32 = bind_value %.loc36_8.1
  178. // CHECK:STDOUT: %int_4.loc36: Core.IntLiteral = int_value 4 [template = constants.%int_4.1]
  179. // CHECK:STDOUT: %impl.elem0.loc36_10: %Convert.type.2 = interface_witness_access constants.%interface.5, element0 [template = constants.%Convert.10]
  180. // CHECK:STDOUT: %Convert.bound.loc36_10: <bound method> = bound_method %int_4.loc36, %impl.elem0.loc36_10 [template = constants.%Convert.bound.2]
  181. // CHECK:STDOUT: %Convert.specific_fn.loc36_10: <specific function> = specific_function %Convert.bound.loc36_10, @Convert.2(constants.%int_32) [template = constants.%Convert.specific_fn.2]
  182. // CHECK:STDOUT: %int.convert_checked.loc36_10: init %i32 = call %Convert.specific_fn.loc36_10(%int_4.loc36) [template = constants.%int_4.2]
  183. // CHECK:STDOUT: %.loc36_10: init %i32 = converted %int_4.loc36, %int.convert_checked.loc36_10 [template = constants.%int_4.2]
  184. // CHECK:STDOUT: assign %.loc36_8.2, %.loc36_10
  185. // CHECK:STDOUT: return
  186. // CHECK:STDOUT: }
  187. // CHECK:STDOUT: