fail_address_of_value.carbon 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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: %.1: type = struct_type {.a: i32} [template]
  93. // CHECK:STDOUT: %.2: i32 = int_literal 0 [template]
  94. // CHECK:STDOUT: %.3: type = ptr_type i32 [template]
  95. // CHECK:STDOUT: %.4: bool = bool_literal true [template]
  96. // CHECK:STDOUT: %.5: type = ptr_type bool [template]
  97. // CHECK:STDOUT: %.6: f64 = real_literal 10e-1 [template]
  98. // CHECK:STDOUT: %.7: type = ptr_type f64 [template]
  99. // CHECK:STDOUT: %.8: type = ptr_type String [template]
  100. // CHECK:STDOUT: %.9: String = string_literal "Hello" [template]
  101. // CHECK:STDOUT: %.10: i32 = int_literal 1 [template]
  102. // CHECK:STDOUT: %.11: i32 = int_literal 2 [template]
  103. // CHECK:STDOUT: %.12: type = tuple_type (i32, i32) [template]
  104. // CHECK:STDOUT: %.13: type = ptr_type (i32, i32) [template]
  105. // CHECK:STDOUT: %.14: i32 = int_literal 5 [template]
  106. // CHECK:STDOUT: %.15: type = ptr_type {.a: i32} [template]
  107. // CHECK:STDOUT: %.16: bool = bool_literal false [template]
  108. // CHECK:STDOUT: %.17: type = ptr_type type [template]
  109. // CHECK:STDOUT: %.18: type = const_type i32 [template]
  110. // CHECK:STDOUT: %.19: type = ptr_type const i32 [template]
  111. // CHECK:STDOUT: %.20: (i32, i32) = tuple_value (%.10, %.11) [template]
  112. // CHECK:STDOUT: }
  113. // CHECK:STDOUT:
  114. // CHECK:STDOUT: file {
  115. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  116. // CHECK:STDOUT: .G = %G
  117. // CHECK:STDOUT: .H = %H
  118. // CHECK:STDOUT: .AddressOfLiteral = %AddressOfLiteral
  119. // CHECK:STDOUT: .AddressOfOperator = %AddressOfOperator
  120. // CHECK:STDOUT: .AddressOfCall = %AddressOfCall
  121. // CHECK:STDOUT: .AddressOfType = %AddressOfType
  122. // CHECK:STDOUT: .AddressOfTupleElementValue = %AddressOfTupleElementValue
  123. // CHECK:STDOUT: .AddressOfParam = %AddressOfParam
  124. // CHECK:STDOUT: }
  125. // CHECK:STDOUT: %G: <function> = fn_decl @G [template] {
  126. // CHECK:STDOUT: %return.var.loc7: ref i32 = var <return slot>
  127. // CHECK:STDOUT: }
  128. // CHECK:STDOUT: %H: <function> = fn_decl @H [template] {
  129. // CHECK:STDOUT: %.loc9: type = struct_type {.a: i32} [template = constants.%.1]
  130. // CHECK:STDOUT: %return.var.loc9: ref {.a: i32} = var <return slot>
  131. // CHECK:STDOUT: }
  132. // CHECK:STDOUT: %AddressOfLiteral: <function> = fn_decl @AddressOfLiteral [template] {}
  133. // CHECK:STDOUT: %AddressOfOperator: <function> = fn_decl @AddressOfOperator [template] {}
  134. // CHECK:STDOUT: %AddressOfCall: <function> = fn_decl @AddressOfCall [template] {}
  135. // CHECK:STDOUT: %AddressOfType: <function> = fn_decl @AddressOfType [template] {}
  136. // CHECK:STDOUT: %AddressOfTupleElementValue: <function> = fn_decl @AddressOfTupleElementValue [template] {}
  137. // CHECK:STDOUT: %AddressOfParam: <function> = fn_decl @AddressOfParam [template] {
  138. // CHECK:STDOUT: %param.loc91_19.1: i32 = param param
  139. // CHECK:STDOUT: @AddressOfParam.%param: i32 = bind_name param, %param.loc91_19.1
  140. // CHECK:STDOUT: }
  141. // CHECK:STDOUT: }
  142. // CHECK:STDOUT:
  143. // CHECK:STDOUT: fn @G() -> i32;
  144. // CHECK:STDOUT:
  145. // CHECK:STDOUT: fn @H() -> {.a: i32};
  146. // CHECK:STDOUT:
  147. // CHECK:STDOUT: fn @AddressOfLiteral() {
  148. // CHECK:STDOUT: !entry:
  149. // CHECK:STDOUT: %.loc16_4: i32 = int_literal 0 [template = constants.%.2]
  150. // CHECK:STDOUT: %.loc16_3: i32* = addr_of <error> [template = <error>]
  151. // CHECK:STDOUT: %.loc21_4: bool = bool_literal true [template = constants.%.4]
  152. // CHECK:STDOUT: %.loc21_3: bool* = addr_of <error> [template = <error>]
  153. // CHECK:STDOUT: %.loc26_4: f64 = real_literal 10e-1 [template = constants.%.6]
  154. // CHECK:STDOUT: %.loc26_3: f64* = addr_of <error> [template = <error>]
  155. // CHECK:STDOUT: %.loc31_4: String = string_literal "Hello" [template = constants.%.9]
  156. // CHECK:STDOUT: %.loc31_3: String* = addr_of <error> [template = <error>]
  157. // CHECK:STDOUT: %.loc36_5: i32 = int_literal 1 [template = constants.%.10]
  158. // CHECK:STDOUT: %.loc36_8: i32 = int_literal 2 [template = constants.%.11]
  159. // CHECK:STDOUT: %.loc36_9: (i32, i32) = tuple_literal (%.loc36_5, %.loc36_8)
  160. // CHECK:STDOUT: %.loc36_3: (i32, i32)* = addr_of <error> [template = <error>]
  161. // CHECK:STDOUT: %.loc41_10: i32 = int_literal 5 [template = constants.%.14]
  162. // CHECK:STDOUT: %.loc41_11: {.a: i32} = struct_literal (%.loc41_10)
  163. // CHECK:STDOUT: %.loc41_3: {.a: i32}* = addr_of <error> [template = <error>]
  164. // CHECK:STDOUT: return
  165. // CHECK:STDOUT: }
  166. // CHECK:STDOUT:
  167. // CHECK:STDOUT: fn @AddressOfOperator() {
  168. // CHECK:STDOUT: !entry:
  169. // CHECK:STDOUT: %.loc49_5: bool = bool_literal true [template = constants.%.4]
  170. // CHECK:STDOUT: %.loc49_10.1: bool = bool_literal false [template = constants.%.16]
  171. // CHECK:STDOUT: if %.loc49_5 br !and.rhs else br !and.result(%.loc49_10.1)
  172. // CHECK:STDOUT:
  173. // CHECK:STDOUT: !and.rhs:
  174. // CHECK:STDOUT: %.loc49_14: bool = bool_literal false [template = constants.%.16]
  175. // CHECK:STDOUT: br !and.result(%.loc49_14)
  176. // CHECK:STDOUT:
  177. // CHECK:STDOUT: !and.result:
  178. // CHECK:STDOUT: %.loc49_10.2: bool = block_arg !and.result
  179. // CHECK:STDOUT: %.loc49_3: bool* = addr_of <error> [template = <error>]
  180. // CHECK:STDOUT: %H.ref: <function> = name_ref H, file.%H [template = file.%H]
  181. // CHECK:STDOUT: %.loc54_5.1: init {.a: i32} = call %H.ref()
  182. // CHECK:STDOUT: %.loc54_5.2: ref {.a: i32} = temporary_storage
  183. // CHECK:STDOUT: %.loc54_5.3: ref {.a: i32} = temporary %.loc54_5.2, %.loc54_5.1
  184. // CHECK:STDOUT: %.loc54_7: ref i32 = struct_access %.loc54_5.3, element0
  185. // CHECK:STDOUT: %.loc54_3: i32* = addr_of <error> [template = <error>]
  186. // CHECK:STDOUT: %.loc59_9: bool = bool_literal true [template = constants.%.4]
  187. // CHECK:STDOUT: %.loc59_5: bool = not %.loc59_9 [template = constants.%.16]
  188. // CHECK:STDOUT: %.loc59_3: bool* = addr_of <error> [template = <error>]
  189. // CHECK:STDOUT: return
  190. // CHECK:STDOUT: }
  191. // CHECK:STDOUT:
  192. // CHECK:STDOUT: fn @AddressOfCall() {
  193. // CHECK:STDOUT: !entry:
  194. // CHECK:STDOUT: %G.ref: <function> = name_ref G, file.%G [template = file.%G]
  195. // CHECK:STDOUT: %.loc67_5: init i32 = call %G.ref()
  196. // CHECK:STDOUT: %.loc67_3: i32* = addr_of <error> [template = <error>]
  197. // CHECK:STDOUT: return
  198. // CHECK:STDOUT: }
  199. // CHECK:STDOUT:
  200. // CHECK:STDOUT: fn @AddressOfType() {
  201. // CHECK:STDOUT: !entry:
  202. // CHECK:STDOUT: %.loc75: type* = addr_of <error> [template = <error>]
  203. // CHECK:STDOUT: %.loc80_5: type = const_type i32 [template = constants.%.18]
  204. // CHECK:STDOUT: %.loc80_14: type = ptr_type const i32 [template = constants.%.19]
  205. // CHECK:STDOUT: %.loc80_3: type* = addr_of <error> [template = <error>]
  206. // CHECK:STDOUT: return
  207. // CHECK:STDOUT: }
  208. // CHECK:STDOUT:
  209. // CHECK:STDOUT: fn @AddressOfTupleElementValue() {
  210. // CHECK:STDOUT: !entry:
  211. // CHECK:STDOUT: %.loc88_6: i32 = int_literal 1 [template = constants.%.10]
  212. // CHECK:STDOUT: %.loc88_9: i32 = int_literal 2 [template = constants.%.11]
  213. // CHECK:STDOUT: %.loc88_10.1: (i32, i32) = tuple_literal (%.loc88_6, %.loc88_9)
  214. // CHECK:STDOUT: %.loc88_12: i32 = int_literal 0 [template = constants.%.2]
  215. // CHECK:STDOUT: %.loc88_10.2: (i32, i32) = tuple_value (%.loc88_6, %.loc88_9) [template = constants.%.20]
  216. // CHECK:STDOUT: %.loc88_10.3: (i32, i32) = converted %.loc88_10.1, %.loc88_10.2 [template = constants.%.20]
  217. // CHECK:STDOUT: %.loc88_13: i32 = tuple_index %.loc88_10.3, %.loc88_12 [template = constants.%.10]
  218. // CHECK:STDOUT: %.loc88_3: i32* = addr_of <error> [template = <error>]
  219. // CHECK:STDOUT: return
  220. // CHECK:STDOUT: }
  221. // CHECK:STDOUT:
  222. // CHECK:STDOUT: fn @AddressOfParam(%param: i32) {
  223. // CHECK:STDOUT: !entry:
  224. // CHECK:STDOUT: %.loc95_22: type = ptr_type i32 [template = constants.%.3]
  225. // CHECK:STDOUT: %param_addr.var: ref i32* = var param_addr
  226. // CHECK:STDOUT: %param_addr: ref i32* = bind_name param_addr, %param_addr.var
  227. // CHECK:STDOUT: %param.ref: i32 = name_ref param, %param
  228. // CHECK:STDOUT: %.loc95_26: i32* = addr_of <error> [template = <error>]
  229. // CHECK:STDOUT: assign %param_addr.var, %.loc95_26
  230. // CHECK:STDOUT: return
  231. // CHECK:STDOUT: }
  232. // CHECK:STDOUT: