fail_address_of_value.carbon 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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. fn G() -> i32;
  7. fn H() -> {.a: i32};
  8. fn AddressOfLiteral() {
  9. // CHECK:STDERR: fail_address_of_value.carbon:[[@LINE+4]]:3: ERROR: Cannot take the address of non-reference expression.
  10. // CHECK:STDERR: &0;
  11. // CHECK:STDERR: ^
  12. // CHECK:STDERR:
  13. &0;
  14. // CHECK:STDERR: fail_address_of_value.carbon:[[@LINE+4]]:3: ERROR: Cannot take the address of non-reference expression.
  15. // CHECK:STDERR: &true;
  16. // CHECK:STDERR: ^
  17. // CHECK:STDERR:
  18. &true;
  19. // CHECK:STDERR: fail_address_of_value.carbon:[[@LINE+4]]:3: ERROR: Cannot take the address of non-reference expression.
  20. // CHECK:STDERR: &1.0;
  21. // CHECK:STDERR: ^
  22. // CHECK:STDERR:
  23. &1.0;
  24. // CHECK:STDERR: fail_address_of_value.carbon:[[@LINE+4]]:3: ERROR: Cannot take the address of non-reference expression.
  25. // CHECK:STDERR: &"Hello";
  26. // CHECK:STDERR: ^
  27. // CHECK:STDERR:
  28. &"Hello";
  29. // CHECK:STDERR: fail_address_of_value.carbon:[[@LINE+4]]:3: ERROR: Cannot take the address of non-reference expression.
  30. // CHECK:STDERR: &(1, 2);
  31. // CHECK:STDERR: ^
  32. // CHECK:STDERR:
  33. &(1, 2);
  34. // CHECK:STDERR: fail_address_of_value.carbon:[[@LINE+4]]:3: ERROR: Cannot take the address of non-reference expression.
  35. // CHECK:STDERR: &{.a = 5};
  36. // CHECK:STDERR: ^
  37. // CHECK:STDERR:
  38. &{.a = 5};
  39. }
  40. fn AddressOfOperator() {
  41. // CHECK:STDERR: fail_address_of_value.carbon:[[@LINE+4]]:3: ERROR: Cannot take the address of non-reference expression.
  42. // CHECK:STDERR: &(true and false);
  43. // CHECK:STDERR: ^
  44. // CHECK:STDERR:
  45. &(true and false);
  46. // CHECK:STDERR: fail_address_of_value.carbon:[[@LINE+4]]:3: ERROR: Cannot take the address of a temporary object.
  47. // CHECK:STDERR: &H().a;
  48. // CHECK:STDERR: ^
  49. // CHECK:STDERR:
  50. &H().a;
  51. // CHECK:STDERR: fail_address_of_value.carbon:[[@LINE+4]]:3: ERROR: Cannot take the address of non-reference expression.
  52. // CHECK:STDERR: &(not true);
  53. // CHECK:STDERR: ^
  54. // CHECK:STDERR:
  55. &(not true);
  56. }
  57. fn AddressOfCall() {
  58. // CHECK:STDERR: fail_address_of_value.carbon:[[@LINE+4]]:3: ERROR: Cannot take the address of non-reference expression.
  59. // CHECK:STDERR: &G();
  60. // CHECK:STDERR: ^
  61. // CHECK:STDERR:
  62. &G();
  63. }
  64. fn AddressOfType() {
  65. // CHECK:STDERR: fail_address_of_value.carbon:[[@LINE+4]]:3: ERROR: Cannot take the address of non-reference expression.
  66. // CHECK:STDERR: &i32;
  67. // CHECK:STDERR: ^
  68. // CHECK:STDERR:
  69. &i32;
  70. // CHECK:STDERR: fail_address_of_value.carbon:[[@LINE+4]]:3: ERROR: Cannot take the address of non-reference expression.
  71. // CHECK:STDERR: &(const i32*);
  72. // CHECK:STDERR: ^
  73. // CHECK:STDERR:
  74. &(const i32*);
  75. }
  76. fn AddressOfTupleElementValue() {
  77. // CHECK:STDERR: fail_address_of_value.carbon:[[@LINE+4]]:3: ERROR: Cannot take the address of non-reference expression.
  78. // CHECK:STDERR: &((1, 2)[0]);
  79. // CHECK:STDERR: ^
  80. // CHECK:STDERR:
  81. &((1, 2)[0]);
  82. }
  83. fn AddressOfParam(param: i32) {
  84. // CHECK:STDERR: fail_address_of_value.carbon:[[@LINE+3]]:26: ERROR: Cannot take the address of non-reference expression.
  85. // CHECK:STDERR: var param_addr: i32* = &param;
  86. // CHECK:STDERR: ^
  87. var param_addr: i32* = &param;
  88. }
  89. // CHECK:STDOUT: --- fail_address_of_value.carbon
  90. // CHECK:STDOUT:
  91. // CHECK:STDOUT: constants {
  92. // CHECK:STDOUT: %G: type = fn_type @G [template]
  93. // CHECK:STDOUT: %.1: type = tuple_type () [template]
  94. // CHECK:STDOUT: %struct.1: G = struct_value () [template]
  95. // CHECK:STDOUT: %.2: type = struct_type {.a: i32} [template]
  96. // CHECK:STDOUT: %H: type = fn_type @H [template]
  97. // CHECK:STDOUT: %struct.2: H = struct_value () [template]
  98. // CHECK:STDOUT: %AddressOfLiteral: type = fn_type @AddressOfLiteral [template]
  99. // CHECK:STDOUT: %struct.3: AddressOfLiteral = struct_value () [template]
  100. // CHECK:STDOUT: %.3: i32 = int_literal 0 [template]
  101. // CHECK:STDOUT: %.4: type = ptr_type i32 [template]
  102. // CHECK:STDOUT: %.5: bool = bool_literal true [template]
  103. // CHECK:STDOUT: %.6: type = ptr_type bool [template]
  104. // CHECK:STDOUT: %.7: f64 = float_literal 1 [template]
  105. // CHECK:STDOUT: %.8: type = ptr_type f64 [template]
  106. // CHECK:STDOUT: %.9: type = ptr_type String [template]
  107. // CHECK:STDOUT: %.10: String = string_literal "Hello" [template]
  108. // CHECK:STDOUT: %.11: i32 = int_literal 1 [template]
  109. // CHECK:STDOUT: %.12: i32 = int_literal 2 [template]
  110. // CHECK:STDOUT: %.13: type = tuple_type (i32, i32) [template]
  111. // CHECK:STDOUT: %.14: type = ptr_type (i32, i32) [template]
  112. // CHECK:STDOUT: %.15: i32 = int_literal 5 [template]
  113. // CHECK:STDOUT: %.16: type = ptr_type {.a: i32} [template]
  114. // CHECK:STDOUT: %AddressOfOperator: type = fn_type @AddressOfOperator [template]
  115. // CHECK:STDOUT: %struct.4: AddressOfOperator = struct_value () [template]
  116. // CHECK:STDOUT: %.17: bool = bool_literal false [template]
  117. // CHECK:STDOUT: %AddressOfCall: type = fn_type @AddressOfCall [template]
  118. // CHECK:STDOUT: %struct.5: AddressOfCall = struct_value () [template]
  119. // CHECK:STDOUT: %AddressOfType: type = fn_type @AddressOfType [template]
  120. // CHECK:STDOUT: %struct.6: AddressOfType = struct_value () [template]
  121. // CHECK:STDOUT: %.18: type = ptr_type type [template]
  122. // CHECK:STDOUT: %.19: type = const_type i32 [template]
  123. // CHECK:STDOUT: %.20: type = ptr_type const i32 [template]
  124. // CHECK:STDOUT: %AddressOfTupleElementValue: type = fn_type @AddressOfTupleElementValue [template]
  125. // CHECK:STDOUT: %struct.7: AddressOfTupleElementValue = struct_value () [template]
  126. // CHECK:STDOUT: %tuple: (i32, i32) = tuple_value (%.11, %.12) [template]
  127. // CHECK:STDOUT: %AddressOfParam: type = fn_type @AddressOfParam [template]
  128. // CHECK:STDOUT: %struct.8: AddressOfParam = struct_value () [template]
  129. // CHECK:STDOUT: }
  130. // CHECK:STDOUT:
  131. // CHECK:STDOUT: file {
  132. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  133. // CHECK:STDOUT: .Core = %Core
  134. // CHECK:STDOUT: .G = %G.decl
  135. // CHECK:STDOUT: .H = %H.decl
  136. // CHECK:STDOUT: .AddressOfLiteral = %AddressOfLiteral.decl
  137. // CHECK:STDOUT: .AddressOfOperator = %AddressOfOperator.decl
  138. // CHECK:STDOUT: .AddressOfCall = %AddressOfCall.decl
  139. // CHECK:STDOUT: .AddressOfType = %AddressOfType.decl
  140. // CHECK:STDOUT: .AddressOfTupleElementValue = %AddressOfTupleElementValue.decl
  141. // CHECK:STDOUT: .AddressOfParam = %AddressOfParam.decl
  142. // CHECK:STDOUT: }
  143. // CHECK:STDOUT: %Core: <namespace> = namespace [template] {}
  144. // CHECK:STDOUT: %G.decl: G = fn_decl @G [template = constants.%struct.1] {
  145. // CHECK:STDOUT: @G.%return: ref i32 = var <return slot>
  146. // CHECK:STDOUT: }
  147. // CHECK:STDOUT: %H.decl: H = fn_decl @H [template = constants.%struct.2] {
  148. // CHECK:STDOUT: %.loc9: type = struct_type {.a: i32} [template = constants.%.2]
  149. // CHECK:STDOUT: @H.%return: ref {.a: i32} = var <return slot>
  150. // CHECK:STDOUT: }
  151. // CHECK:STDOUT: %AddressOfLiteral.decl: AddressOfLiteral = fn_decl @AddressOfLiteral [template = constants.%struct.3] {}
  152. // CHECK:STDOUT: %AddressOfOperator.decl: AddressOfOperator = fn_decl @AddressOfOperator [template = constants.%struct.4] {}
  153. // CHECK:STDOUT: %AddressOfCall.decl: AddressOfCall = fn_decl @AddressOfCall [template = constants.%struct.5] {}
  154. // CHECK:STDOUT: %AddressOfType.decl: AddressOfType = fn_decl @AddressOfType [template = constants.%struct.6] {}
  155. // CHECK:STDOUT: %AddressOfTupleElementValue.decl: AddressOfTupleElementValue = fn_decl @AddressOfTupleElementValue [template = constants.%struct.7] {}
  156. // CHECK:STDOUT: %AddressOfParam.decl: AddressOfParam = fn_decl @AddressOfParam [template = constants.%struct.8] {
  157. // CHECK:STDOUT: %param.loc91_19.1: i32 = param param
  158. // CHECK:STDOUT: @AddressOfParam.%param: i32 = bind_name param, %param.loc91_19.1
  159. // CHECK:STDOUT: }
  160. // CHECK:STDOUT: }
  161. // CHECK:STDOUT:
  162. // CHECK:STDOUT: fn @G() -> i32;
  163. // CHECK:STDOUT:
  164. // CHECK:STDOUT: fn @H() -> {.a: i32};
  165. // CHECK:STDOUT:
  166. // CHECK:STDOUT: fn @AddressOfLiteral() {
  167. // CHECK:STDOUT: !entry:
  168. // CHECK:STDOUT: %.loc16_4: i32 = int_literal 0 [template = constants.%.3]
  169. // CHECK:STDOUT: %.loc16_3: i32* = addr_of <error> [template = <error>]
  170. // CHECK:STDOUT: %.loc21_4: bool = bool_literal true [template = constants.%.5]
  171. // CHECK:STDOUT: %.loc21_3: bool* = addr_of <error> [template = <error>]
  172. // CHECK:STDOUT: %.loc26_4: f64 = float_literal 1 [template = constants.%.7]
  173. // CHECK:STDOUT: %.loc26_3: f64* = addr_of <error> [template = <error>]
  174. // CHECK:STDOUT: %.loc31_4: String = string_literal "Hello" [template = constants.%.10]
  175. // CHECK:STDOUT: %.loc31_3: String* = addr_of <error> [template = <error>]
  176. // CHECK:STDOUT: %.loc36_5: i32 = int_literal 1 [template = constants.%.11]
  177. // CHECK:STDOUT: %.loc36_8: i32 = int_literal 2 [template = constants.%.12]
  178. // CHECK:STDOUT: %.loc36_9: (i32, i32) = tuple_literal (%.loc36_5, %.loc36_8)
  179. // CHECK:STDOUT: %.loc36_3: (i32, i32)* = addr_of <error> [template = <error>]
  180. // CHECK:STDOUT: %.loc41_10: i32 = int_literal 5 [template = constants.%.15]
  181. // CHECK:STDOUT: %.loc41_11: {.a: i32} = struct_literal (%.loc41_10)
  182. // CHECK:STDOUT: %.loc41_3: {.a: i32}* = addr_of <error> [template = <error>]
  183. // CHECK:STDOUT: return
  184. // CHECK:STDOUT: }
  185. // CHECK:STDOUT:
  186. // CHECK:STDOUT: fn @AddressOfOperator() {
  187. // CHECK:STDOUT: !entry:
  188. // CHECK:STDOUT: %.loc49_5: bool = bool_literal true [template = constants.%.5]
  189. // CHECK:STDOUT: %.loc49_10.1: bool = bool_literal false [template = constants.%.17]
  190. // CHECK:STDOUT: if %.loc49_5 br !and.rhs else br !and.result(%.loc49_10.1)
  191. // CHECK:STDOUT:
  192. // CHECK:STDOUT: !and.rhs:
  193. // CHECK:STDOUT: %.loc49_14: bool = bool_literal false [template = constants.%.17]
  194. // CHECK:STDOUT: br !and.result(%.loc49_14)
  195. // CHECK:STDOUT:
  196. // CHECK:STDOUT: !and.result:
  197. // CHECK:STDOUT: %.loc49_10.2: bool = block_arg !and.result [template = constants.%.17]
  198. // CHECK:STDOUT: %.loc49_3: bool* = addr_of <error> [template = <error>]
  199. // CHECK:STDOUT: %H.ref: H = name_ref H, file.%H.decl [template = constants.%struct.2]
  200. // CHECK:STDOUT: %H.call: init {.a: i32} = call %H.ref()
  201. // CHECK:STDOUT: %.loc54_5.1: ref {.a: i32} = temporary_storage
  202. // CHECK:STDOUT: %.loc54_5.2: ref {.a: i32} = temporary %.loc54_5.1, %H.call
  203. // CHECK:STDOUT: %.loc54_7: ref i32 = struct_access %.loc54_5.2, element0
  204. // CHECK:STDOUT: %.loc54_3: i32* = addr_of <error> [template = <error>]
  205. // CHECK:STDOUT: %.loc59_9: bool = bool_literal true [template = constants.%.5]
  206. // CHECK:STDOUT: %.loc59_5: bool = not %.loc59_9 [template = constants.%.17]
  207. // CHECK:STDOUT: %.loc59_3: bool* = addr_of <error> [template = <error>]
  208. // CHECK:STDOUT: return
  209. // CHECK:STDOUT: }
  210. // CHECK:STDOUT:
  211. // CHECK:STDOUT: fn @AddressOfCall() {
  212. // CHECK:STDOUT: !entry:
  213. // CHECK:STDOUT: %G.ref: G = name_ref G, file.%G.decl [template = constants.%struct.1]
  214. // CHECK:STDOUT: %G.call: init i32 = call %G.ref()
  215. // CHECK:STDOUT: %.loc67: i32* = addr_of <error> [template = <error>]
  216. // CHECK:STDOUT: return
  217. // CHECK:STDOUT: }
  218. // CHECK:STDOUT:
  219. // CHECK:STDOUT: fn @AddressOfType() {
  220. // CHECK:STDOUT: !entry:
  221. // CHECK:STDOUT: %.loc75: type* = addr_of <error> [template = <error>]
  222. // CHECK:STDOUT: %.loc80_5: type = const_type i32 [template = constants.%.19]
  223. // CHECK:STDOUT: %.loc80_14: type = ptr_type const i32 [template = constants.%.20]
  224. // CHECK:STDOUT: %.loc80_3: type* = addr_of <error> [template = <error>]
  225. // CHECK:STDOUT: return
  226. // CHECK:STDOUT: }
  227. // CHECK:STDOUT:
  228. // CHECK:STDOUT: fn @AddressOfTupleElementValue() {
  229. // CHECK:STDOUT: !entry:
  230. // CHECK:STDOUT: %.loc88_6: i32 = int_literal 1 [template = constants.%.11]
  231. // CHECK:STDOUT: %.loc88_9: i32 = int_literal 2 [template = constants.%.12]
  232. // CHECK:STDOUT: %.loc88_10.1: (i32, i32) = tuple_literal (%.loc88_6, %.loc88_9)
  233. // CHECK:STDOUT: %.loc88_12: i32 = int_literal 0 [template = constants.%.3]
  234. // CHECK:STDOUT: %tuple: (i32, i32) = tuple_value (%.loc88_6, %.loc88_9) [template = constants.%tuple]
  235. // CHECK:STDOUT: %.loc88_10.2: (i32, i32) = converted %.loc88_10.1, %tuple [template = constants.%tuple]
  236. // CHECK:STDOUT: %.loc88_13: i32 = tuple_index %.loc88_10.2, %.loc88_12 [template = constants.%.11]
  237. // CHECK:STDOUT: %.loc88_3: i32* = addr_of <error> [template = <error>]
  238. // CHECK:STDOUT: return
  239. // CHECK:STDOUT: }
  240. // CHECK:STDOUT:
  241. // CHECK:STDOUT: fn @AddressOfParam(%param: i32) {
  242. // CHECK:STDOUT: !entry:
  243. // CHECK:STDOUT: %.loc95_22: type = ptr_type i32 [template = constants.%.4]
  244. // CHECK:STDOUT: %param_addr.var: ref i32* = var param_addr
  245. // CHECK:STDOUT: %param_addr: ref i32* = bind_name param_addr, %param_addr.var
  246. // CHECK:STDOUT: %param.ref: i32 = name_ref param, %param
  247. // CHECK:STDOUT: %.loc95_26: i32* = addr_of <error> [template = <error>]
  248. // CHECK:STDOUT: assign %param_addr.var, %.loc95_26
  249. // CHECK:STDOUT: return
  250. // CHECK:STDOUT: }
  251. // CHECK:STDOUT: