fail_address_of_value.carbon 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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 [AddrOfNonRef]
  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 [AddrOfNonRef]
  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 [AddrOfNonRef]
  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 [AddrOfNonRef]
  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 [AddrOfNonRef]
  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 [AddrOfNonRef]
  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 [AddrOfNonRef]
  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 [AddrOfEphemeralRef]
  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 [AddrOfNonRef]
  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 [AddrOfNonRef]
  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 [AddrOfNonRef]
  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 [AddrOfNonRef]
  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 [AddrOfNonRef]
  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 [AddrOfNonRef]
  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: %Int32: %Int32.type = struct_value () [template]
  98. // CHECK:STDOUT: %G.type: type = fn_type @G [template]
  99. // CHECK:STDOUT: %G: %G.type = struct_value () [template]
  100. // CHECK:STDOUT: %.1: type = struct_type {.a: i32} [template]
  101. // CHECK:STDOUT: %H.type: type = fn_type @H [template]
  102. // CHECK:STDOUT: %H: %H.type = struct_value () [template]
  103. // CHECK:STDOUT: %AddressOfLiteral.type: type = fn_type @AddressOfLiteral [template]
  104. // CHECK:STDOUT: %AddressOfLiteral: %AddressOfLiteral.type = struct_value () [template]
  105. // CHECK:STDOUT: %.2: i32 = int_value 0 [template]
  106. // CHECK:STDOUT: %.3: type = ptr_type i32 [template]
  107. // CHECK:STDOUT: %.4: bool = bool_literal true [template]
  108. // CHECK:STDOUT: %.5: type = ptr_type bool [template]
  109. // CHECK:STDOUT: %.6: f64 = float_literal 1 [template]
  110. // CHECK:STDOUT: %.7: type = ptr_type f64 [template]
  111. // CHECK:STDOUT: %.8: type = ptr_type String [template]
  112. // CHECK:STDOUT: %.9: String = string_literal "Hello" [template]
  113. // CHECK:STDOUT: %.10: i32 = int_value 1 [template]
  114. // CHECK:STDOUT: %.11: i32 = int_value 2 [template]
  115. // CHECK:STDOUT: %tuple.type: type = tuple_type (i32, i32) [template]
  116. // CHECK:STDOUT: %.12: type = ptr_type %tuple.type [template]
  117. // CHECK:STDOUT: %.13: i32 = int_value 5 [template]
  118. // CHECK:STDOUT: %.14: type = ptr_type %.1 [template]
  119. // CHECK:STDOUT: %AddressOfOperator.type: type = fn_type @AddressOfOperator [template]
  120. // CHECK:STDOUT: %AddressOfOperator: %AddressOfOperator.type = struct_value () [template]
  121. // CHECK:STDOUT: %.15: bool = bool_literal false [template]
  122. // CHECK:STDOUT: %AddressOfCall.type: type = fn_type @AddressOfCall [template]
  123. // CHECK:STDOUT: %AddressOfCall: %AddressOfCall.type = struct_value () [template]
  124. // CHECK:STDOUT: %AddressOfType.type: type = fn_type @AddressOfType [template]
  125. // CHECK:STDOUT: %AddressOfType: %AddressOfType.type = struct_value () [template]
  126. // CHECK:STDOUT: %.16: type = ptr_type type [template]
  127. // CHECK:STDOUT: %.17: type = const_type i32 [template]
  128. // CHECK:STDOUT: %.18: type = ptr_type %.17 [template]
  129. // CHECK:STDOUT: %AddressOfTupleElementValue.type: type = fn_type @AddressOfTupleElementValue [template]
  130. // CHECK:STDOUT: %AddressOfTupleElementValue: %AddressOfTupleElementValue.type = struct_value () [template]
  131. // CHECK:STDOUT: %tuple: %tuple.type = tuple_value (%.10, %.11) [template]
  132. // CHECK:STDOUT: %AddressOfParam.type: type = fn_type @AddressOfParam [template]
  133. // CHECK:STDOUT: %AddressOfParam: %AddressOfParam.type = struct_value () [template]
  134. // CHECK:STDOUT: }
  135. // CHECK:STDOUT:
  136. // CHECK:STDOUT: imports {
  137. // CHECK:STDOUT: %Core: <namespace> = namespace file.%Core.import, [template] {
  138. // CHECK:STDOUT: .Int32 = %import_ref
  139. // CHECK:STDOUT: import Core//prelude
  140. // CHECK:STDOUT: import Core//prelude/...
  141. // CHECK:STDOUT: }
  142. // CHECK:STDOUT: }
  143. // CHECK:STDOUT:
  144. // CHECK:STDOUT: file {
  145. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  146. // CHECK:STDOUT: .Core = imports.%Core
  147. // CHECK:STDOUT: .G = %G.decl
  148. // CHECK:STDOUT: .H = %H.decl
  149. // CHECK:STDOUT: .AddressOfLiteral = %AddressOfLiteral.decl
  150. // CHECK:STDOUT: .AddressOfOperator = %AddressOfOperator.decl
  151. // CHECK:STDOUT: .AddressOfCall = %AddressOfCall.decl
  152. // CHECK:STDOUT: .AddressOfType = %AddressOfType.decl
  153. // CHECK:STDOUT: .AddressOfTupleElementValue = %AddressOfTupleElementValue.decl
  154. // CHECK:STDOUT: .AddressOfParam = %AddressOfParam.decl
  155. // CHECK:STDOUT: }
  156. // CHECK:STDOUT: %Core.import = import Core
  157. // CHECK:STDOUT: %G.decl: %G.type = fn_decl @G [template = constants.%G] {
  158. // CHECK:STDOUT: %return.patt: i32 = return_slot_pattern
  159. // CHECK:STDOUT: %return.param_patt: i32 = out_param_pattern %return.patt, runtime_param0
  160. // CHECK:STDOUT: } {
  161. // CHECK:STDOUT: %int.make_type_32: init type = call constants.%Int32() [template = i32]
  162. // CHECK:STDOUT: %.loc11_11.1: type = value_of_initializer %int.make_type_32 [template = i32]
  163. // CHECK:STDOUT: %.loc11_11.2: type = converted %int.make_type_32, %.loc11_11.1 [template = i32]
  164. // CHECK:STDOUT: %return.param: ref i32 = out_param runtime_param0
  165. // CHECK:STDOUT: %return: ref i32 = return_slot %return.param
  166. // CHECK:STDOUT: }
  167. // CHECK:STDOUT: %H.decl: %H.type = fn_decl @H [template = constants.%H] {
  168. // CHECK:STDOUT: %return.patt: %.1 = return_slot_pattern
  169. // CHECK:STDOUT: %return.param_patt: %.1 = out_param_pattern %return.patt, runtime_param0
  170. // CHECK:STDOUT: } {
  171. // CHECK:STDOUT: %int.make_type_32: init type = call constants.%Int32() [template = i32]
  172. // CHECK:STDOUT: %.loc13_16.1: type = value_of_initializer %int.make_type_32 [template = i32]
  173. // CHECK:STDOUT: %.loc13_16.2: type = converted %int.make_type_32, %.loc13_16.1 [template = i32]
  174. // CHECK:STDOUT: %.loc13_19: type = struct_type {.a: i32} [template = constants.%.1]
  175. // CHECK:STDOUT: %return.param: ref %.1 = out_param runtime_param0
  176. // CHECK:STDOUT: %return: ref %.1 = return_slot %return.param
  177. // CHECK:STDOUT: }
  178. // CHECK:STDOUT: %AddressOfLiteral.decl: %AddressOfLiteral.type = fn_decl @AddressOfLiteral [template = constants.%AddressOfLiteral] {} {}
  179. // CHECK:STDOUT: %AddressOfOperator.decl: %AddressOfOperator.type = fn_decl @AddressOfOperator [template = constants.%AddressOfOperator] {} {}
  180. // CHECK:STDOUT: %AddressOfCall.decl: %AddressOfCall.type = fn_decl @AddressOfCall [template = constants.%AddressOfCall] {} {}
  181. // CHECK:STDOUT: %AddressOfType.decl: %AddressOfType.type = fn_decl @AddressOfType [template = constants.%AddressOfType] {} {}
  182. // CHECK:STDOUT: %AddressOfTupleElementValue.decl: %AddressOfTupleElementValue.type = fn_decl @AddressOfTupleElementValue [template = constants.%AddressOfTupleElementValue] {} {}
  183. // CHECK:STDOUT: %AddressOfParam.decl: %AddressOfParam.type = fn_decl @AddressOfParam [template = constants.%AddressOfParam] {
  184. // CHECK:STDOUT: %param.patt: i32 = binding_pattern param
  185. // CHECK:STDOUT: %param.param_patt: i32 = value_param_pattern %param.patt, runtime_param0
  186. // CHECK:STDOUT: } {
  187. // CHECK:STDOUT: %int.make_type_32.loc95: init type = call constants.%Int32() [template = i32]
  188. // CHECK:STDOUT: %.loc95_26.1: type = value_of_initializer %int.make_type_32.loc95 [template = i32]
  189. // CHECK:STDOUT: %.loc95_26.2: type = converted %int.make_type_32.loc95, %.loc95_26.1 [template = i32]
  190. // CHECK:STDOUT: %param.param: i32 = value_param runtime_param0
  191. // CHECK:STDOUT: %param: i32 = bind_name param, %param.param
  192. // CHECK:STDOUT: }
  193. // CHECK:STDOUT: }
  194. // CHECK:STDOUT:
  195. // CHECK:STDOUT: fn @G() -> i32;
  196. // CHECK:STDOUT:
  197. // CHECK:STDOUT: fn @H() -> %.1;
  198. // CHECK:STDOUT:
  199. // CHECK:STDOUT: fn @AddressOfLiteral() {
  200. // CHECK:STDOUT: !entry:
  201. // CHECK:STDOUT: %.loc20_4: i32 = int_value 0 [template = constants.%.2]
  202. // CHECK:STDOUT: %.loc20_3: %.3 = addr_of <error> [template = <error>]
  203. // CHECK:STDOUT: %.loc25_4: bool = bool_literal true [template = constants.%.4]
  204. // CHECK:STDOUT: %.loc25_3: %.5 = addr_of <error> [template = <error>]
  205. // CHECK:STDOUT: %.loc30_4: f64 = float_literal 1 [template = constants.%.6]
  206. // CHECK:STDOUT: %.loc30_3: %.7 = addr_of <error> [template = <error>]
  207. // CHECK:STDOUT: %.loc35_4: String = string_literal "Hello" [template = constants.%.9]
  208. // CHECK:STDOUT: %.loc35_3: %.8 = addr_of <error> [template = <error>]
  209. // CHECK:STDOUT: %.loc40_5: i32 = int_value 1 [template = constants.%.10]
  210. // CHECK:STDOUT: %.loc40_8: i32 = int_value 2 [template = constants.%.11]
  211. // CHECK:STDOUT: %.loc40_9: %tuple.type = tuple_literal (%.loc40_5, %.loc40_8)
  212. // CHECK:STDOUT: %.loc40_3: %.12 = addr_of <error> [template = <error>]
  213. // CHECK:STDOUT: %.loc45_10: i32 = int_value 5 [template = constants.%.13]
  214. // CHECK:STDOUT: %.loc45_11: %.1 = struct_literal (%.loc45_10)
  215. // CHECK:STDOUT: %.loc45_3: %.14 = addr_of <error> [template = <error>]
  216. // CHECK:STDOUT: return
  217. // CHECK:STDOUT: }
  218. // CHECK:STDOUT:
  219. // CHECK:STDOUT: fn @AddressOfOperator() {
  220. // CHECK:STDOUT: !entry:
  221. // CHECK:STDOUT: %.loc53_5: bool = bool_literal true [template = constants.%.4]
  222. // CHECK:STDOUT: %.loc53_10.1: bool = bool_literal false [template = constants.%.15]
  223. // CHECK:STDOUT: if %.loc53_5 br !and.rhs else br !and.result(%.loc53_10.1)
  224. // CHECK:STDOUT:
  225. // CHECK:STDOUT: !and.rhs:
  226. // CHECK:STDOUT: %.loc53_14: bool = bool_literal false [template = constants.%.15]
  227. // CHECK:STDOUT: br !and.result(%.loc53_14)
  228. // CHECK:STDOUT:
  229. // CHECK:STDOUT: !and.result:
  230. // CHECK:STDOUT: %.loc53_10.2: bool = block_arg !and.result [template = constants.%.15]
  231. // CHECK:STDOUT: %.loc53_3: %.5 = addr_of <error> [template = <error>]
  232. // CHECK:STDOUT: %H.ref: %H.type = name_ref H, file.%H.decl [template = constants.%H]
  233. // CHECK:STDOUT: %H.call: init %.1 = call %H.ref()
  234. // CHECK:STDOUT: %.loc58_5.1: ref %.1 = temporary_storage
  235. // CHECK:STDOUT: %.loc58_5.2: ref %.1 = temporary %.loc58_5.1, %H.call
  236. // CHECK:STDOUT: %.loc58_7: ref i32 = struct_access %.loc58_5.2, element0
  237. // CHECK:STDOUT: %.loc58_3: %.3 = addr_of <error> [template = <error>]
  238. // CHECK:STDOUT: %.loc63_9: bool = bool_literal true [template = constants.%.4]
  239. // CHECK:STDOUT: %.loc63_5: bool = not %.loc63_9 [template = constants.%.15]
  240. // CHECK:STDOUT: %.loc63_3: %.5 = addr_of <error> [template = <error>]
  241. // CHECK:STDOUT: return
  242. // CHECK:STDOUT: }
  243. // CHECK:STDOUT:
  244. // CHECK:STDOUT: fn @AddressOfCall() {
  245. // CHECK:STDOUT: !entry:
  246. // CHECK:STDOUT: %G.ref: %G.type = name_ref G, file.%G.decl [template = constants.%G]
  247. // CHECK:STDOUT: %G.call: init i32 = call %G.ref()
  248. // CHECK:STDOUT: %.loc71: %.3 = addr_of <error> [template = <error>]
  249. // CHECK:STDOUT: return
  250. // CHECK:STDOUT: }
  251. // CHECK:STDOUT:
  252. // CHECK:STDOUT: fn @AddressOfType() {
  253. // CHECK:STDOUT: !entry:
  254. // CHECK:STDOUT: %int.make_type_32.loc79: init type = call constants.%Int32() [template = i32]
  255. // CHECK:STDOUT: %.loc79: %.16 = addr_of <error> [template = <error>]
  256. // CHECK:STDOUT: %int.make_type_32.loc84: init type = call constants.%Int32() [template = i32]
  257. // CHECK:STDOUT: %.loc84_5.1: type = value_of_initializer %int.make_type_32.loc84 [template = i32]
  258. // CHECK:STDOUT: %.loc84_5.2: type = converted %int.make_type_32.loc84, %.loc84_5.1 [template = i32]
  259. // CHECK:STDOUT: %.loc84_5.3: type = const_type i32 [template = constants.%.17]
  260. // CHECK:STDOUT: %.loc84_14: type = ptr_type %.17 [template = constants.%.18]
  261. // CHECK:STDOUT: %.loc84_3: %.16 = addr_of <error> [template = <error>]
  262. // CHECK:STDOUT: return
  263. // CHECK:STDOUT: }
  264. // CHECK:STDOUT:
  265. // CHECK:STDOUT: fn @AddressOfTupleElementValue() {
  266. // CHECK:STDOUT: !entry:
  267. // CHECK:STDOUT: %.loc92_6: i32 = int_value 1 [template = constants.%.10]
  268. // CHECK:STDOUT: %.loc92_9: i32 = int_value 2 [template = constants.%.11]
  269. // CHECK:STDOUT: %.loc92_10.1: %tuple.type = tuple_literal (%.loc92_6, %.loc92_9)
  270. // CHECK:STDOUT: %.loc92_12: i32 = int_value 0 [template = constants.%.2]
  271. // CHECK:STDOUT: %tuple: %tuple.type = tuple_value (%.loc92_6, %.loc92_9) [template = constants.%tuple]
  272. // CHECK:STDOUT: %.loc92_10.2: %tuple.type = converted %.loc92_10.1, %tuple [template = constants.%tuple]
  273. // CHECK:STDOUT: %.loc92_11: i32 = tuple_access %.loc92_10.2, element0 [template = constants.%.10]
  274. // CHECK:STDOUT: %.loc92_3: %.3 = addr_of <error> [template = <error>]
  275. // CHECK:STDOUT: return
  276. // CHECK:STDOUT: }
  277. // CHECK:STDOUT:
  278. // CHECK:STDOUT: fn @AddressOfParam(%param.param_patt: i32) {
  279. // CHECK:STDOUT: !entry:
  280. // CHECK:STDOUT: %int.make_type_32.loc99: init type = call constants.%Int32() [template = i32]
  281. // CHECK:STDOUT: %.loc99_22.1: type = value_of_initializer %int.make_type_32.loc99 [template = i32]
  282. // CHECK:STDOUT: %.loc99_22.2: type = converted %int.make_type_32.loc99, %.loc99_22.1 [template = i32]
  283. // CHECK:STDOUT: %.loc99_22.3: type = ptr_type i32 [template = constants.%.3]
  284. // CHECK:STDOUT: %param_addr.var: ref %.3 = var param_addr
  285. // CHECK:STDOUT: %param_addr: ref %.3 = bind_name param_addr, %param_addr.var
  286. // CHECK:STDOUT: %param.ref: i32 = name_ref param, %param
  287. // CHECK:STDOUT: %.loc99_26: %.3 = addr_of <error> [template = <error>]
  288. // CHECK:STDOUT: assign %param_addr.var, %.loc99_26
  289. // CHECK:STDOUT: return
  290. // CHECK:STDOUT: }
  291. // CHECK:STDOUT: