fail_expr_category.carbon 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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+4]]:3: error: expression is not assignable [AssignmentToNonAssignable]
  31. // CHECK:STDERR: F()[0] = 4;
  32. // CHECK:STDERR: ^~~~~~
  33. // CHECK:STDERR:
  34. F()[0] = 4;
  35. }
  36. // CHECK:STDOUT: --- fail_expr_category.carbon
  37. // CHECK:STDOUT:
  38. // CHECK:STDOUT: constants {
  39. // CHECK:STDOUT: %int_32: Core.IntLiteral = int_value 32 [template]
  40. // CHECK:STDOUT: %i32: type = class_type @Int, @Int(%int_32) [template]
  41. // CHECK:STDOUT: %int_3: Core.IntLiteral = int_value 3 [template]
  42. // CHECK:STDOUT: %array_type: type = array_type %int_3, %i32 [template]
  43. // CHECK:STDOUT: %F.type: type = fn_type @F [template]
  44. // CHECK:STDOUT: %F: %F.type = struct_value () [template]
  45. // CHECK:STDOUT: %G.type: type = fn_type @G [template]
  46. // CHECK:STDOUT: %G: %G.type = struct_value () [template]
  47. // CHECK:STDOUT: %ptr.235: type = ptr_type %i32 [template]
  48. // CHECK:STDOUT: %int_0.5c6: Core.IntLiteral = int_value 0 [template]
  49. // CHECK:STDOUT: %ImplicitAs.type.205: type = facet_type <@ImplicitAs, @ImplicitAs(%i32)> [template]
  50. // CHECK:STDOUT: %Convert.type.1b6: type = fn_type @Convert.1, @ImplicitAs(%i32) [template]
  51. // CHECK:STDOUT: %impl_witness.d39: <witness> = impl_witness (imports.%Core.import_ref.a5b), @impl.1(%int_32) [template]
  52. // CHECK:STDOUT: %Convert.type.035: type = fn_type @Convert.2, @impl.1(%int_32) [template]
  53. // CHECK:STDOUT: %Convert.956: %Convert.type.035 = struct_value () [template]
  54. // CHECK:STDOUT: %ImplicitAs.facet: %ImplicitAs.type.205 = facet_value Core.IntLiteral, %impl_witness.d39 [template]
  55. // CHECK:STDOUT: %.a0b: type = fn_type_with_self_type %Convert.type.1b6, %ImplicitAs.facet [template]
  56. // CHECK:STDOUT: %Convert.bound.d04: <bound method> = bound_method %int_0.5c6, %Convert.956 [template]
  57. // CHECK:STDOUT: %Convert.specific_fn.d62: <specific function> = specific_function %Convert.bound.d04, @Convert.2(%int_32) [template]
  58. // CHECK:STDOUT: %int_0.6a9: %i32 = int_value 0 [template]
  59. // CHECK:STDOUT: %int_4.0c1: Core.IntLiteral = int_value 4 [template]
  60. // CHECK:STDOUT: %Convert.bound.ac3: <bound method> = bound_method %int_4.0c1, %Convert.956 [template]
  61. // CHECK:STDOUT: %Convert.specific_fn.450: <specific function> = specific_function %Convert.bound.ac3, @Convert.2(%int_32) [template]
  62. // CHECK:STDOUT: %int_4.940: %i32 = int_value 4 [template]
  63. // CHECK:STDOUT: }
  64. // CHECK:STDOUT:
  65. // CHECK:STDOUT: imports {
  66. // CHECK:STDOUT: %Core: <namespace> = namespace file.%Core.import, [template] {
  67. // CHECK:STDOUT: .Int = %Core.Int
  68. // CHECK:STDOUT: .ImplicitAs = %Core.ImplicitAs
  69. // CHECK:STDOUT: import Core//prelude
  70. // CHECK:STDOUT: import Core//prelude/...
  71. // CHECK:STDOUT: }
  72. // CHECK:STDOUT: }
  73. // CHECK:STDOUT:
  74. // CHECK:STDOUT: file {
  75. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  76. // CHECK:STDOUT: .Core = imports.%Core
  77. // CHECK:STDOUT: .F = %F.decl
  78. // CHECK:STDOUT: .G = %G.decl
  79. // CHECK:STDOUT: }
  80. // CHECK:STDOUT: %Core.import = import Core
  81. // CHECK:STDOUT: %F.decl: %F.type = fn_decl @F [template = constants.%F] {
  82. // CHECK:STDOUT: %return.patt: %array_type = return_slot_pattern
  83. // CHECK:STDOUT: %return.param_patt: %array_type = out_param_pattern %return.patt, runtime_param0
  84. // CHECK:STDOUT: } {
  85. // CHECK:STDOUT: %int_32: Core.IntLiteral = int_value 32 [template = constants.%int_32]
  86. // CHECK:STDOUT: %i32: type = class_type @Int, @Int(constants.%int_32) [template = constants.%i32]
  87. // CHECK:STDOUT: %int_3: Core.IntLiteral = int_value 3 [template = constants.%int_3]
  88. // CHECK:STDOUT: %array_type: type = array_type %int_3, %i32 [template = constants.%array_type]
  89. // CHECK:STDOUT: %return.param: ref %array_type = out_param runtime_param0
  90. // CHECK:STDOUT: %return: ref %array_type = return_slot %return.param
  91. // CHECK:STDOUT: }
  92. // CHECK:STDOUT: %G.decl: %G.type = fn_decl @G [template = constants.%G] {
  93. // CHECK:STDOUT: %b.patt: %array_type = binding_pattern b
  94. // CHECK:STDOUT: %b.param_patt: %array_type = value_param_pattern %b.patt, runtime_param0
  95. // CHECK:STDOUT: } {
  96. // CHECK:STDOUT: %b.param: %array_type = value_param runtime_param0
  97. // CHECK:STDOUT: %.loc13: type = splice_block %array_type [template = constants.%array_type] {
  98. // CHECK:STDOUT: %int_32.loc13: Core.IntLiteral = int_value 32 [template = constants.%int_32]
  99. // CHECK:STDOUT: %i32.loc13: type = class_type @Int, @Int(constants.%int_32) [template = constants.%i32]
  100. // CHECK:STDOUT: %int_3: Core.IntLiteral = int_value 3 [template = constants.%int_3]
  101. // CHECK:STDOUT: %array_type: type = array_type %int_3, %i32 [template = constants.%array_type]
  102. // CHECK:STDOUT: }
  103. // CHECK:STDOUT: %b: %array_type = bind_name b, %b.param
  104. // CHECK:STDOUT: }
  105. // CHECK:STDOUT: }
  106. // CHECK:STDOUT:
  107. // CHECK:STDOUT: fn @F() -> %array_type;
  108. // CHECK:STDOUT:
  109. // CHECK:STDOUT: fn @G(%b.param_patt: %array_type) {
  110. // CHECK:STDOUT: !entry:
  111. // CHECK:STDOUT: name_binding_decl {
  112. // CHECK:STDOUT: %pb.patt: %ptr.235 = binding_pattern pb
  113. // CHECK:STDOUT: %.loc19_3: %ptr.235 = var_pattern %pb.patt
  114. // CHECK:STDOUT: }
  115. // CHECK:STDOUT: %pb.var: ref %ptr.235 = var pb
  116. // CHECK:STDOUT: %b.ref.loc19: %array_type = name_ref b, %b
  117. // CHECK:STDOUT: %int_0.loc19: Core.IntLiteral = int_value 0 [template = constants.%int_0.5c6]
  118. // CHECK:STDOUT: %int_32.loc19_22: Core.IntLiteral = int_value 32 [template = constants.%int_32]
  119. // CHECK:STDOUT: %i32.loc19_22: type = class_type @Int, @Int(constants.%int_32) [template = constants.%i32]
  120. // CHECK:STDOUT: %impl.elem0.loc19: %.a0b = impl_witness_access constants.%impl_witness.d39, element0 [template = constants.%Convert.956]
  121. // CHECK:STDOUT: %bound_method.loc19: <bound method> = bound_method %int_0.loc19, %impl.elem0.loc19 [template = constants.%Convert.bound.d04]
  122. // CHECK:STDOUT: %specific_fn.loc19: <specific function> = specific_function %bound_method.loc19, @Convert.2(constants.%int_32) [template = constants.%Convert.specific_fn.d62]
  123. // CHECK:STDOUT: %int.convert_checked.loc19: init %i32 = call %specific_fn.loc19(%int_0.loc19) [template = constants.%int_0.6a9]
  124. // CHECK:STDOUT: %.loc19_21.1: %i32 = value_of_initializer %int.convert_checked.loc19 [template = constants.%int_0.6a9]
  125. // CHECK:STDOUT: %.loc19_21.2: %i32 = converted %int_0.loc19, %.loc19_21.1 [template = constants.%int_0.6a9]
  126. // CHECK:STDOUT: %.loc19_22.1: ref %array_type = value_as_ref %b.ref.loc19
  127. // CHECK:STDOUT: %.loc19_22.2: ref %i32 = array_index %.loc19_22.1, %.loc19_21.2
  128. // CHECK:STDOUT: %.loc19_22.3: %i32 = bind_value %.loc19_22.2
  129. // CHECK:STDOUT: %addr.loc19: %ptr.235 = addr_of <error> [template = <error>]
  130. // CHECK:STDOUT: assign %pb.var, %addr.loc19
  131. // CHECK:STDOUT: %.loc19_14: type = splice_block %ptr.loc19 [template = constants.%ptr.235] {
  132. // CHECK:STDOUT: %int_32.loc19_11: Core.IntLiteral = int_value 32 [template = constants.%int_32]
  133. // CHECK:STDOUT: %i32.loc19_11: type = class_type @Int, @Int(constants.%int_32) [template = constants.%i32]
  134. // CHECK:STDOUT: %ptr.loc19: type = ptr_type %i32 [template = constants.%ptr.235]
  135. // CHECK:STDOUT: }
  136. // CHECK:STDOUT: %pb: ref %ptr.235 = bind_name pb, %pb.var
  137. // CHECK:STDOUT: %b.ref.loc24: %array_type = name_ref b, %b
  138. // CHECK:STDOUT: %int_0.loc24: Core.IntLiteral = int_value 0 [template = constants.%int_0.5c6]
  139. // CHECK:STDOUT: %int_32.loc24: Core.IntLiteral = int_value 32 [template = constants.%int_32]
  140. // CHECK:STDOUT: %i32.loc24: type = class_type @Int, @Int(constants.%int_32) [template = constants.%i32]
  141. // CHECK:STDOUT: %impl.elem0.loc24_5: %.a0b = impl_witness_access constants.%impl_witness.d39, element0 [template = constants.%Convert.956]
  142. // CHECK:STDOUT: %bound_method.loc24_5: <bound method> = bound_method %int_0.loc24, %impl.elem0.loc24_5 [template = constants.%Convert.bound.d04]
  143. // CHECK:STDOUT: %specific_fn.loc24_5: <specific function> = specific_function %bound_method.loc24_5, @Convert.2(constants.%int_32) [template = constants.%Convert.specific_fn.d62]
  144. // CHECK:STDOUT: %int.convert_checked.loc24_5: init %i32 = call %specific_fn.loc24_5(%int_0.loc24) [template = constants.%int_0.6a9]
  145. // CHECK:STDOUT: %.loc24_5.1: %i32 = value_of_initializer %int.convert_checked.loc24_5 [template = constants.%int_0.6a9]
  146. // CHECK:STDOUT: %.loc24_5.2: %i32 = converted %int_0.loc24, %.loc24_5.1 [template = constants.%int_0.6a9]
  147. // CHECK:STDOUT: %.loc24_6.1: ref %array_type = value_as_ref %b.ref.loc24
  148. // CHECK:STDOUT: %.loc24_6.2: ref %i32 = array_index %.loc24_6.1, %.loc24_5.2
  149. // CHECK:STDOUT: %.loc24_6.3: %i32 = bind_value %.loc24_6.2
  150. // CHECK:STDOUT: %int_4.loc24: Core.IntLiteral = int_value 4 [template = constants.%int_4.0c1]
  151. // CHECK:STDOUT: %impl.elem0.loc24_8: %.a0b = impl_witness_access constants.%impl_witness.d39, element0 [template = constants.%Convert.956]
  152. // CHECK:STDOUT: %bound_method.loc24_8: <bound method> = bound_method %int_4.loc24, %impl.elem0.loc24_8 [template = constants.%Convert.bound.ac3]
  153. // CHECK:STDOUT: %specific_fn.loc24_8: <specific function> = specific_function %bound_method.loc24_8, @Convert.2(constants.%int_32) [template = constants.%Convert.specific_fn.450]
  154. // CHECK:STDOUT: %int.convert_checked.loc24_8: init %i32 = call %specific_fn.loc24_8(%int_4.loc24) [template = constants.%int_4.940]
  155. // CHECK:STDOUT: %.loc24_8: init %i32 = converted %int_4.loc24, %int.convert_checked.loc24_8 [template = constants.%int_4.940]
  156. // CHECK:STDOUT: assign %.loc24_6.3, %.loc24_8
  157. // CHECK:STDOUT: name_binding_decl {
  158. // CHECK:STDOUT: %pf.patt: %ptr.235 = binding_pattern pf
  159. // CHECK:STDOUT: %.loc32_3: %ptr.235 = var_pattern %pf.patt
  160. // CHECK:STDOUT: }
  161. // CHECK:STDOUT: %pf.var: ref %ptr.235 = var pf
  162. // CHECK:STDOUT: %F.ref.loc32: %F.type = name_ref F, file.%F.decl [template = constants.%F]
  163. // CHECK:STDOUT: %.loc32_21.1: ref %array_type = temporary_storage
  164. // CHECK:STDOUT: %F.call.loc32: init %array_type = call %F.ref.loc32() to %.loc32_21.1
  165. // CHECK:STDOUT: %int_0.loc32: Core.IntLiteral = int_value 0 [template = constants.%int_0.5c6]
  166. // CHECK:STDOUT: %.loc32_21.2: ref %array_type = temporary %.loc32_21.1, %F.call.loc32
  167. // CHECK:STDOUT: %int_32.loc32_24: Core.IntLiteral = int_value 32 [template = constants.%int_32]
  168. // CHECK:STDOUT: %i32.loc32_24: type = class_type @Int, @Int(constants.%int_32) [template = constants.%i32]
  169. // CHECK:STDOUT: %impl.elem0.loc32: %.a0b = impl_witness_access constants.%impl_witness.d39, element0 [template = constants.%Convert.956]
  170. // CHECK:STDOUT: %bound_method.loc32: <bound method> = bound_method %int_0.loc32, %impl.elem0.loc32 [template = constants.%Convert.bound.d04]
  171. // CHECK:STDOUT: %specific_fn.loc32: <specific function> = specific_function %bound_method.loc32, @Convert.2(constants.%int_32) [template = constants.%Convert.specific_fn.d62]
  172. // CHECK:STDOUT: %int.convert_checked.loc32: init %i32 = call %specific_fn.loc32(%int_0.loc32) [template = constants.%int_0.6a9]
  173. // CHECK:STDOUT: %.loc32_23.1: %i32 = value_of_initializer %int.convert_checked.loc32 [template = constants.%int_0.6a9]
  174. // CHECK:STDOUT: %.loc32_23.2: %i32 = converted %int_0.loc32, %.loc32_23.1 [template = constants.%int_0.6a9]
  175. // CHECK:STDOUT: %.loc32_24.1: ref %i32 = array_index %.loc32_21.2, %.loc32_23.2
  176. // CHECK:STDOUT: %.loc32_24.2: %i32 = bind_value %.loc32_24.1
  177. // CHECK:STDOUT: %addr.loc32: %ptr.235 = addr_of <error> [template = <error>]
  178. // CHECK:STDOUT: assign %pf.var, %addr.loc32
  179. // CHECK:STDOUT: %.loc32_14: type = splice_block %ptr.loc32 [template = constants.%ptr.235] {
  180. // CHECK:STDOUT: %int_32.loc32_11: Core.IntLiteral = int_value 32 [template = constants.%int_32]
  181. // CHECK:STDOUT: %i32.loc32_11: type = class_type @Int, @Int(constants.%int_32) [template = constants.%i32]
  182. // CHECK:STDOUT: %ptr.loc32: type = ptr_type %i32 [template = constants.%ptr.235]
  183. // CHECK:STDOUT: }
  184. // CHECK:STDOUT: %pf: ref %ptr.235 = bind_name pf, %pf.var
  185. // CHECK:STDOUT: %F.ref.loc37: %F.type = name_ref F, file.%F.decl [template = constants.%F]
  186. // CHECK:STDOUT: %.loc37_5.1: ref %array_type = temporary_storage
  187. // CHECK:STDOUT: %F.call.loc37: init %array_type = call %F.ref.loc37() to %.loc37_5.1
  188. // CHECK:STDOUT: %int_0.loc37: Core.IntLiteral = int_value 0 [template = constants.%int_0.5c6]
  189. // CHECK:STDOUT: %.loc37_5.2: ref %array_type = temporary %.loc37_5.1, %F.call.loc37
  190. // CHECK:STDOUT: %int_32.loc37: Core.IntLiteral = int_value 32 [template = constants.%int_32]
  191. // CHECK:STDOUT: %i32.loc37: type = class_type @Int, @Int(constants.%int_32) [template = constants.%i32]
  192. // CHECK:STDOUT: %impl.elem0.loc37_7: %.a0b = impl_witness_access constants.%impl_witness.d39, element0 [template = constants.%Convert.956]
  193. // CHECK:STDOUT: %bound_method.loc37_7: <bound method> = bound_method %int_0.loc37, %impl.elem0.loc37_7 [template = constants.%Convert.bound.d04]
  194. // CHECK:STDOUT: %specific_fn.loc37_7: <specific function> = specific_function %bound_method.loc37_7, @Convert.2(constants.%int_32) [template = constants.%Convert.specific_fn.d62]
  195. // CHECK:STDOUT: %int.convert_checked.loc37_7: init %i32 = call %specific_fn.loc37_7(%int_0.loc37) [template = constants.%int_0.6a9]
  196. // CHECK:STDOUT: %.loc37_7.1: %i32 = value_of_initializer %int.convert_checked.loc37_7 [template = constants.%int_0.6a9]
  197. // CHECK:STDOUT: %.loc37_7.2: %i32 = converted %int_0.loc37, %.loc37_7.1 [template = constants.%int_0.6a9]
  198. // CHECK:STDOUT: %.loc37_8.1: ref %i32 = array_index %.loc37_5.2, %.loc37_7.2
  199. // CHECK:STDOUT: %.loc37_8.2: %i32 = bind_value %.loc37_8.1
  200. // CHECK:STDOUT: %int_4.loc37: Core.IntLiteral = int_value 4 [template = constants.%int_4.0c1]
  201. // CHECK:STDOUT: %impl.elem0.loc37_10: %.a0b = impl_witness_access constants.%impl_witness.d39, element0 [template = constants.%Convert.956]
  202. // CHECK:STDOUT: %bound_method.loc37_10: <bound method> = bound_method %int_4.loc37, %impl.elem0.loc37_10 [template = constants.%Convert.bound.ac3]
  203. // CHECK:STDOUT: %specific_fn.loc37_10: <specific function> = specific_function %bound_method.loc37_10, @Convert.2(constants.%int_32) [template = constants.%Convert.specific_fn.450]
  204. // CHECK:STDOUT: %int.convert_checked.loc37_10: init %i32 = call %specific_fn.loc37_10(%int_4.loc37) [template = constants.%int_4.940]
  205. // CHECK:STDOUT: %.loc37_10: init %i32 = converted %int_4.loc37, %int.convert_checked.loc37_10 [template = constants.%int_4.940]
  206. // CHECK:STDOUT: assign %.loc37_8.2, %.loc37_10
  207. // CHECK:STDOUT: return
  208. // CHECK:STDOUT: }
  209. // CHECK:STDOUT: