fail_address_of_value.carbon 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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/pointer/fail_address_of_value.carbon
  8. // TIP: To dump output, run:
  9. // TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/pointer/fail_address_of_value.carbon
  10. fn G() -> i32;
  11. fn H() -> {.a: i32};
  12. fn AddressOfLiteral() {
  13. // CHECK:STDERR: fail_address_of_value.carbon:[[@LINE+4]]:3: ERROR: Cannot take the address of non-reference expression.
  14. // CHECK:STDERR: &0;
  15. // CHECK:STDERR: ^
  16. // CHECK:STDERR:
  17. &0;
  18. // CHECK:STDERR: fail_address_of_value.carbon:[[@LINE+4]]:3: ERROR: Cannot take the address of non-reference expression.
  19. // CHECK:STDERR: &true;
  20. // CHECK:STDERR: ^
  21. // CHECK:STDERR:
  22. &true;
  23. // CHECK:STDERR: fail_address_of_value.carbon:[[@LINE+4]]:3: ERROR: Cannot take the address of non-reference expression.
  24. // CHECK:STDERR: &1.0;
  25. // CHECK:STDERR: ^
  26. // CHECK:STDERR:
  27. &1.0;
  28. // CHECK:STDERR: fail_address_of_value.carbon:[[@LINE+4]]:3: ERROR: Cannot take the address of non-reference expression.
  29. // CHECK:STDERR: &"Hello";
  30. // CHECK:STDERR: ^
  31. // CHECK:STDERR:
  32. &"Hello";
  33. // CHECK:STDERR: fail_address_of_value.carbon:[[@LINE+4]]:3: ERROR: Cannot take the address of non-reference expression.
  34. // CHECK:STDERR: &(1, 2);
  35. // CHECK:STDERR: ^
  36. // CHECK:STDERR:
  37. &(1, 2);
  38. // CHECK:STDERR: fail_address_of_value.carbon:[[@LINE+4]]:3: ERROR: Cannot take the address of non-reference expression.
  39. // CHECK:STDERR: &{.a = 5};
  40. // CHECK:STDERR: ^
  41. // CHECK:STDERR:
  42. &{.a = 5};
  43. }
  44. fn AddressOfOperator() {
  45. // CHECK:STDERR: fail_address_of_value.carbon:[[@LINE+4]]:3: ERROR: Cannot take the address of non-reference expression.
  46. // CHECK:STDERR: &(true and false);
  47. // CHECK:STDERR: ^
  48. // CHECK:STDERR:
  49. &(true and false);
  50. // CHECK:STDERR: fail_address_of_value.carbon:[[@LINE+4]]:3: ERROR: Cannot take the address of a temporary object.
  51. // CHECK:STDERR: &H().a;
  52. // CHECK:STDERR: ^
  53. // CHECK:STDERR:
  54. &H().a;
  55. // CHECK:STDERR: fail_address_of_value.carbon:[[@LINE+4]]:3: ERROR: Cannot take the address of non-reference expression.
  56. // CHECK:STDERR: &(not true);
  57. // CHECK:STDERR: ^
  58. // CHECK:STDERR:
  59. &(not true);
  60. }
  61. fn AddressOfCall() {
  62. // CHECK:STDERR: fail_address_of_value.carbon:[[@LINE+4]]:3: ERROR: Cannot take the address of non-reference expression.
  63. // CHECK:STDERR: &G();
  64. // CHECK:STDERR: ^
  65. // CHECK:STDERR:
  66. &G();
  67. }
  68. fn AddressOfType() {
  69. // CHECK:STDERR: fail_address_of_value.carbon:[[@LINE+4]]:3: ERROR: Cannot take the address of non-reference expression.
  70. // CHECK:STDERR: &i32;
  71. // CHECK:STDERR: ^
  72. // CHECK:STDERR:
  73. &i32;
  74. // CHECK:STDERR: fail_address_of_value.carbon:[[@LINE+4]]:3: ERROR: Cannot take the address of non-reference expression.
  75. // CHECK:STDERR: &(const i32*);
  76. // CHECK:STDERR: ^
  77. // CHECK:STDERR:
  78. &(const i32*);
  79. }
  80. fn AddressOfTupleElementValue() {
  81. // CHECK:STDERR: fail_address_of_value.carbon:[[@LINE+4]]:3: ERROR: Cannot take the address of non-reference expression.
  82. // CHECK:STDERR: &((1, 2)[0]);
  83. // CHECK:STDERR: ^
  84. // CHECK:STDERR:
  85. &((1, 2)[0]);
  86. }
  87. fn AddressOfParam(param: i32) {
  88. // CHECK:STDERR: fail_address_of_value.carbon:[[@LINE+3]]:26: ERROR: Cannot take the address of non-reference expression.
  89. // CHECK:STDERR: var param_addr: i32* = &param;
  90. // CHECK:STDERR: ^
  91. var param_addr: i32* = &param;
  92. }
  93. // CHECK:STDOUT: --- fail_address_of_value.carbon
  94. // CHECK:STDOUT:
  95. // CHECK:STDOUT: constants {
  96. // CHECK:STDOUT: %Int32.type: type = fn_type @Int32 [template]
  97. // CHECK:STDOUT: %.1: type = tuple_type () [template]
  98. // CHECK:STDOUT: %Int32: %Int32.type = struct_value () [template]
  99. // CHECK:STDOUT: %G.type: type = fn_type @G [template]
  100. // CHECK:STDOUT: %G: %G.type = struct_value () [template]
  101. // CHECK:STDOUT: %.2: type = struct_type {.a: i32} [template]
  102. // CHECK:STDOUT: %H.type: type = fn_type @H [template]
  103. // CHECK:STDOUT: %H: %H.type = struct_value () [template]
  104. // CHECK:STDOUT: %AddressOfLiteral.type: type = fn_type @AddressOfLiteral [template]
  105. // CHECK:STDOUT: %AddressOfLiteral: %AddressOfLiteral.type = struct_value () [template]
  106. // CHECK:STDOUT: %.3: i32 = int_literal 0 [template]
  107. // CHECK:STDOUT: %.4: type = ptr_type i32 [template]
  108. // CHECK:STDOUT: %.5: bool = bool_literal true [template]
  109. // CHECK:STDOUT: %.6: type = ptr_type bool [template]
  110. // CHECK:STDOUT: %.7: f64 = float_literal 1 [template]
  111. // CHECK:STDOUT: %.8: type = ptr_type f64 [template]
  112. // CHECK:STDOUT: %.9: type = ptr_type String [template]
  113. // CHECK:STDOUT: %.10: String = string_literal "Hello" [template]
  114. // CHECK:STDOUT: %.11: i32 = int_literal 1 [template]
  115. // CHECK:STDOUT: %.12: i32 = int_literal 2 [template]
  116. // CHECK:STDOUT: %.13: type = tuple_type (i32, i32) [template]
  117. // CHECK:STDOUT: %.14: type = ptr_type %.13 [template]
  118. // CHECK:STDOUT: %.15: i32 = int_literal 5 [template]
  119. // CHECK:STDOUT: %.16: type = ptr_type %.2 [template]
  120. // CHECK:STDOUT: %AddressOfOperator.type: type = fn_type @AddressOfOperator [template]
  121. // CHECK:STDOUT: %AddressOfOperator: %AddressOfOperator.type = struct_value () [template]
  122. // CHECK:STDOUT: %.17: bool = bool_literal false [template]
  123. // CHECK:STDOUT: %AddressOfCall.type: type = fn_type @AddressOfCall [template]
  124. // CHECK:STDOUT: %AddressOfCall: %AddressOfCall.type = struct_value () [template]
  125. // CHECK:STDOUT: %AddressOfType.type: type = fn_type @AddressOfType [template]
  126. // CHECK:STDOUT: %AddressOfType: %AddressOfType.type = struct_value () [template]
  127. // CHECK:STDOUT: %.18: type = ptr_type type [template]
  128. // CHECK:STDOUT: %.19: type = const_type i32 [template]
  129. // CHECK:STDOUT: %.20: type = ptr_type %.19 [template]
  130. // CHECK:STDOUT: %AddressOfTupleElementValue.type: type = fn_type @AddressOfTupleElementValue [template]
  131. // CHECK:STDOUT: %AddressOfTupleElementValue: %AddressOfTupleElementValue.type = struct_value () [template]
  132. // CHECK:STDOUT: %tuple: %.13 = tuple_value (%.11, %.12) [template]
  133. // CHECK:STDOUT: %AddressOfParam.type: type = fn_type @AddressOfParam [template]
  134. // CHECK:STDOUT: %AddressOfParam: %AddressOfParam.type = struct_value () [template]
  135. // CHECK:STDOUT: }
  136. // CHECK:STDOUT:
  137. // CHECK:STDOUT: imports {
  138. // CHECK:STDOUT: %Core: <namespace> = namespace file.%Core.import, [template] {
  139. // CHECK:STDOUT: .Int32 = %import_ref
  140. // CHECK:STDOUT: import Core//prelude
  141. // CHECK:STDOUT: import Core//prelude/operators
  142. // CHECK:STDOUT: import Core//prelude/types
  143. // CHECK:STDOUT: import Core//prelude/operators/arithmetic
  144. // CHECK:STDOUT: import Core//prelude/operators/bitwise
  145. // CHECK:STDOUT: import Core//prelude/operators/comparison
  146. // CHECK:STDOUT: import Core//prelude/types/bool
  147. // CHECK:STDOUT: }
  148. // CHECK:STDOUT: %import_ref: %Int32.type = import_ref Core//prelude/types, inst+4, loaded [template = constants.%Int32]
  149. // CHECK:STDOUT: }
  150. // CHECK:STDOUT:
  151. // CHECK:STDOUT: file {
  152. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  153. // CHECK:STDOUT: .Core = imports.%Core
  154. // CHECK:STDOUT: .G = %G.decl
  155. // CHECK:STDOUT: .H = %H.decl
  156. // CHECK:STDOUT: .AddressOfLiteral = %AddressOfLiteral.decl
  157. // CHECK:STDOUT: .AddressOfOperator = %AddressOfOperator.decl
  158. // CHECK:STDOUT: .AddressOfCall = %AddressOfCall.decl
  159. // CHECK:STDOUT: .AddressOfType = %AddressOfType.decl
  160. // CHECK:STDOUT: .AddressOfTupleElementValue = %AddressOfTupleElementValue.decl
  161. // CHECK:STDOUT: .AddressOfParam = %AddressOfParam.decl
  162. // CHECK:STDOUT: }
  163. // CHECK:STDOUT: %Core.import = import Core
  164. // CHECK:STDOUT: %G.decl: %G.type = fn_decl @G [template = constants.%G] {
  165. // CHECK:STDOUT: %int.make_type_32.loc11: init type = call constants.%Int32() [template = i32]
  166. // CHECK:STDOUT: %.loc11_11.1: type = value_of_initializer %int.make_type_32.loc11 [template = i32]
  167. // CHECK:STDOUT: %.loc11_11.2: type = converted %int.make_type_32.loc11, %.loc11_11.1 [template = i32]
  168. // CHECK:STDOUT: @G.%return: ref i32 = var <return slot>
  169. // CHECK:STDOUT: }
  170. // CHECK:STDOUT: %H.decl: %H.type = fn_decl @H [template = constants.%H] {
  171. // CHECK:STDOUT: %int.make_type_32.loc13: init type = call constants.%Int32() [template = i32]
  172. // CHECK:STDOUT: %.loc13_16.1: type = value_of_initializer %int.make_type_32.loc13 [template = i32]
  173. // CHECK:STDOUT: %.loc13_16.2: type = converted %int.make_type_32.loc13, %.loc13_16.1 [template = i32]
  174. // CHECK:STDOUT: %.loc13_19: type = struct_type {.a: i32} [template = constants.%.2]
  175. // CHECK:STDOUT: @H.%return: ref %.2 = var <return slot>
  176. // CHECK:STDOUT: }
  177. // CHECK:STDOUT: %AddressOfLiteral.decl: %AddressOfLiteral.type = fn_decl @AddressOfLiteral [template = constants.%AddressOfLiteral] {}
  178. // CHECK:STDOUT: %AddressOfOperator.decl: %AddressOfOperator.type = fn_decl @AddressOfOperator [template = constants.%AddressOfOperator] {}
  179. // CHECK:STDOUT: %AddressOfCall.decl: %AddressOfCall.type = fn_decl @AddressOfCall [template = constants.%AddressOfCall] {}
  180. // CHECK:STDOUT: %AddressOfType.decl: %AddressOfType.type = fn_decl @AddressOfType [template = constants.%AddressOfType] {}
  181. // CHECK:STDOUT: %AddressOfTupleElementValue.decl: %AddressOfTupleElementValue.type = fn_decl @AddressOfTupleElementValue [template = constants.%AddressOfTupleElementValue] {}
  182. // CHECK:STDOUT: %AddressOfParam.decl: %AddressOfParam.type = fn_decl @AddressOfParam [template = constants.%AddressOfParam] {
  183. // CHECK:STDOUT: %int.make_type_32.loc95: init type = call constants.%Int32() [template = i32]
  184. // CHECK:STDOUT: %.loc95_26.1: type = value_of_initializer %int.make_type_32.loc95 [template = i32]
  185. // CHECK:STDOUT: %.loc95_26.2: type = converted %int.make_type_32.loc95, %.loc95_26.1 [template = i32]
  186. // CHECK:STDOUT: %param.loc95_19.1: i32 = param param
  187. // CHECK:STDOUT: @AddressOfParam.%param: i32 = bind_name param, %param.loc95_19.1
  188. // CHECK:STDOUT: }
  189. // CHECK:STDOUT: }
  190. // CHECK:STDOUT:
  191. // CHECK:STDOUT: fn @Int32() -> type = "int.make_type_32";
  192. // CHECK:STDOUT:
  193. // CHECK:STDOUT: fn @G() -> i32;
  194. // CHECK:STDOUT:
  195. // CHECK:STDOUT: fn @H() -> %.2;
  196. // CHECK:STDOUT:
  197. // CHECK:STDOUT: fn @AddressOfLiteral() {
  198. // CHECK:STDOUT: !entry:
  199. // CHECK:STDOUT: %.loc20_4: i32 = int_literal 0 [template = constants.%.3]
  200. // CHECK:STDOUT: %.loc20_3: %.4 = addr_of <error> [template = <error>]
  201. // CHECK:STDOUT: %.loc25_4: bool = bool_literal true [template = constants.%.5]
  202. // CHECK:STDOUT: %.loc25_3: %.6 = addr_of <error> [template = <error>]
  203. // CHECK:STDOUT: %.loc30_4: f64 = float_literal 1 [template = constants.%.7]
  204. // CHECK:STDOUT: %.loc30_3: %.8 = addr_of <error> [template = <error>]
  205. // CHECK:STDOUT: %.loc35_4: String = string_literal "Hello" [template = constants.%.10]
  206. // CHECK:STDOUT: %.loc35_3: %.9 = addr_of <error> [template = <error>]
  207. // CHECK:STDOUT: %.loc40_5: i32 = int_literal 1 [template = constants.%.11]
  208. // CHECK:STDOUT: %.loc40_8: i32 = int_literal 2 [template = constants.%.12]
  209. // CHECK:STDOUT: %.loc40_9: %.13 = tuple_literal (%.loc40_5, %.loc40_8)
  210. // CHECK:STDOUT: %.loc40_3: %.14 = addr_of <error> [template = <error>]
  211. // CHECK:STDOUT: %.loc45_10: i32 = int_literal 5 [template = constants.%.15]
  212. // CHECK:STDOUT: %.loc45_11: %.2 = struct_literal (%.loc45_10)
  213. // CHECK:STDOUT: %.loc45_3: %.16 = addr_of <error> [template = <error>]
  214. // CHECK:STDOUT: return
  215. // CHECK:STDOUT: }
  216. // CHECK:STDOUT:
  217. // CHECK:STDOUT: fn @AddressOfOperator() {
  218. // CHECK:STDOUT: !entry:
  219. // CHECK:STDOUT: %.loc53_5: bool = bool_literal true [template = constants.%.5]
  220. // CHECK:STDOUT: %.loc53_10.1: bool = bool_literal false [template = constants.%.17]
  221. // CHECK:STDOUT: if %.loc53_5 br !and.rhs else br !and.result(%.loc53_10.1)
  222. // CHECK:STDOUT:
  223. // CHECK:STDOUT: !and.rhs:
  224. // CHECK:STDOUT: %.loc53_14: bool = bool_literal false [template = constants.%.17]
  225. // CHECK:STDOUT: br !and.result(%.loc53_14)
  226. // CHECK:STDOUT:
  227. // CHECK:STDOUT: !and.result:
  228. // CHECK:STDOUT: %.loc53_10.2: bool = block_arg !and.result [template = constants.%.17]
  229. // CHECK:STDOUT: %.loc53_3: %.6 = addr_of <error> [template = <error>]
  230. // CHECK:STDOUT: %H.ref: %H.type = name_ref H, file.%H.decl [template = constants.%H]
  231. // CHECK:STDOUT: %H.call: init %.2 = call %H.ref()
  232. // CHECK:STDOUT: %.loc58_5.1: ref %.2 = temporary_storage
  233. // CHECK:STDOUT: %.loc58_5.2: ref %.2 = temporary %.loc58_5.1, %H.call
  234. // CHECK:STDOUT: %.loc58_7: ref i32 = struct_access %.loc58_5.2, element0
  235. // CHECK:STDOUT: %.loc58_3: %.4 = addr_of <error> [template = <error>]
  236. // CHECK:STDOUT: %.loc63_9: bool = bool_literal true [template = constants.%.5]
  237. // CHECK:STDOUT: %.loc63_5: bool = not %.loc63_9 [template = constants.%.17]
  238. // CHECK:STDOUT: %.loc63_3: %.6 = addr_of <error> [template = <error>]
  239. // CHECK:STDOUT: return
  240. // CHECK:STDOUT: }
  241. // CHECK:STDOUT:
  242. // CHECK:STDOUT: fn @AddressOfCall() {
  243. // CHECK:STDOUT: !entry:
  244. // CHECK:STDOUT: %G.ref: %G.type = name_ref G, file.%G.decl [template = constants.%G]
  245. // CHECK:STDOUT: %G.call: init i32 = call %G.ref()
  246. // CHECK:STDOUT: %.loc71: %.4 = addr_of <error> [template = <error>]
  247. // CHECK:STDOUT: return
  248. // CHECK:STDOUT: }
  249. // CHECK:STDOUT:
  250. // CHECK:STDOUT: fn @AddressOfType() {
  251. // CHECK:STDOUT: !entry:
  252. // CHECK:STDOUT: %int.make_type_32.loc79: init type = call constants.%Int32() [template = i32]
  253. // CHECK:STDOUT: %.loc79: %.18 = addr_of <error> [template = <error>]
  254. // CHECK:STDOUT: %int.make_type_32.loc84: init type = call constants.%Int32() [template = i32]
  255. // CHECK:STDOUT: %.loc84_5.1: type = value_of_initializer %int.make_type_32.loc84 [template = i32]
  256. // CHECK:STDOUT: %.loc84_5.2: type = converted %int.make_type_32.loc84, %.loc84_5.1 [template = i32]
  257. // CHECK:STDOUT: %.loc84_5.3: type = const_type i32 [template = constants.%.19]
  258. // CHECK:STDOUT: %.loc84_14: type = ptr_type %.19 [template = constants.%.20]
  259. // CHECK:STDOUT: %.loc84_3: %.18 = addr_of <error> [template = <error>]
  260. // CHECK:STDOUT: return
  261. // CHECK:STDOUT: }
  262. // CHECK:STDOUT:
  263. // CHECK:STDOUT: fn @AddressOfTupleElementValue() {
  264. // CHECK:STDOUT: !entry:
  265. // CHECK:STDOUT: %.loc92_6: i32 = int_literal 1 [template = constants.%.11]
  266. // CHECK:STDOUT: %.loc92_9: i32 = int_literal 2 [template = constants.%.12]
  267. // CHECK:STDOUT: %.loc92_10.1: %.13 = tuple_literal (%.loc92_6, %.loc92_9)
  268. // CHECK:STDOUT: %.loc92_12: i32 = int_literal 0 [template = constants.%.3]
  269. // CHECK:STDOUT: %tuple: %.13 = tuple_value (%.loc92_6, %.loc92_9) [template = constants.%tuple]
  270. // CHECK:STDOUT: %.loc92_10.2: %.13 = converted %.loc92_10.1, %tuple [template = constants.%tuple]
  271. // CHECK:STDOUT: %.loc92_13: i32 = tuple_index %.loc92_10.2, %.loc92_12 [template = constants.%.11]
  272. // CHECK:STDOUT: %.loc92_3: %.4 = addr_of <error> [template = <error>]
  273. // CHECK:STDOUT: return
  274. // CHECK:STDOUT: }
  275. // CHECK:STDOUT:
  276. // CHECK:STDOUT: fn @AddressOfParam(%param: i32) {
  277. // CHECK:STDOUT: !entry:
  278. // CHECK:STDOUT: %int.make_type_32: init type = call constants.%Int32() [template = i32]
  279. // CHECK:STDOUT: %.loc99_22.1: type = value_of_initializer %int.make_type_32 [template = i32]
  280. // CHECK:STDOUT: %.loc99_22.2: type = converted %int.make_type_32, %.loc99_22.1 [template = i32]
  281. // CHECK:STDOUT: %.loc99_22.3: type = ptr_type i32 [template = constants.%.4]
  282. // CHECK:STDOUT: %param_addr.var: ref %.4 = var param_addr
  283. // CHECK:STDOUT: %param_addr: ref %.4 = bind_name param_addr, %param_addr.var
  284. // CHECK:STDOUT: %param.ref: i32 = name_ref param, %param
  285. // CHECK:STDOUT: %.loc99_26: %.4 = addr_of <error> [template = <error>]
  286. // CHECK:STDOUT: assign %param_addr.var, %.loc99_26
  287. // CHECK:STDOUT: return
  288. // CHECK:STDOUT: }
  289. // CHECK:STDOUT: