fail_address_of_value.carbon 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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+3]]:3: ERROR: Cannot take the address of non-reference expression.
  10. // CHECK:STDERR: &0;
  11. // CHECK:STDERR: ^
  12. &0;
  13. // CHECK:STDERR: fail_address_of_value.carbon:[[@LINE+3]]:3: ERROR: Cannot take the address of non-reference expression.
  14. // CHECK:STDERR: &true;
  15. // CHECK:STDERR: ^
  16. &true;
  17. // CHECK:STDERR: fail_address_of_value.carbon:[[@LINE+3]]:3: ERROR: Cannot take the address of non-reference expression.
  18. // CHECK:STDERR: &1.0;
  19. // CHECK:STDERR: ^
  20. &1.0;
  21. // CHECK:STDERR: fail_address_of_value.carbon:[[@LINE+3]]:3: ERROR: Cannot take the address of non-reference expression.
  22. // CHECK:STDERR: &"Hello";
  23. // CHECK:STDERR: ^
  24. &"Hello";
  25. // CHECK:STDERR: fail_address_of_value.carbon:[[@LINE+3]]:3: ERROR: Cannot take the address of non-reference expression.
  26. // CHECK:STDERR: &(1, 2);
  27. // CHECK:STDERR: ^
  28. &(1, 2);
  29. // CHECK:STDERR: fail_address_of_value.carbon:[[@LINE+3]]:3: ERROR: Cannot take the address of non-reference expression.
  30. // CHECK:STDERR: &{.a = 5};
  31. // CHECK:STDERR: ^
  32. &{.a = 5};
  33. }
  34. fn AddressOfOperator() {
  35. // CHECK:STDERR: fail_address_of_value.carbon:[[@LINE+3]]:3: ERROR: Cannot take the address of non-reference expression.
  36. // CHECK:STDERR: &(true and false);
  37. // CHECK:STDERR: ^
  38. &(true and false);
  39. // CHECK:STDERR: fail_address_of_value.carbon:[[@LINE+3]]:3: ERROR: Cannot take the address of a temporary object.
  40. // CHECK:STDERR: &H().a;
  41. // CHECK:STDERR: ^
  42. &H().a;
  43. // CHECK:STDERR: fail_address_of_value.carbon:[[@LINE+3]]:3: ERROR: Cannot take the address of non-reference expression.
  44. // CHECK:STDERR: &(not true);
  45. // CHECK:STDERR: ^
  46. &(not true);
  47. }
  48. fn AddressOfCall() {
  49. // CHECK:STDERR: fail_address_of_value.carbon:[[@LINE+3]]:3: ERROR: Cannot take the address of non-reference expression.
  50. // CHECK:STDERR: &G();
  51. // CHECK:STDERR: ^
  52. &G();
  53. }
  54. fn AddressOfType() {
  55. // CHECK:STDERR: fail_address_of_value.carbon:[[@LINE+3]]:3: ERROR: Cannot take the address of non-reference expression.
  56. // CHECK:STDERR: &i32;
  57. // CHECK:STDERR: ^
  58. &i32;
  59. // CHECK:STDERR: fail_address_of_value.carbon:[[@LINE+3]]:3: ERROR: Cannot take the address of non-reference expression.
  60. // CHECK:STDERR: &(const i32*);
  61. // CHECK:STDERR: ^
  62. &(const i32*);
  63. }
  64. fn AddressOfTupleElementValue() {
  65. // CHECK:STDERR: fail_address_of_value.carbon:[[@LINE+3]]:3: ERROR: Cannot take the address of non-reference expression.
  66. // CHECK:STDERR: &((1, 2)[0]);
  67. // CHECK:STDERR: ^
  68. &((1, 2)[0]);
  69. }
  70. fn AddressOfParam(param: i32) {
  71. // CHECK:STDERR: fail_address_of_value.carbon:[[@LINE+3]]:26: ERROR: Cannot take the address of non-reference expression.
  72. // CHECK:STDERR: var param_addr: i32* = &param;
  73. // CHECK:STDERR: ^
  74. var param_addr: i32* = &param;
  75. }
  76. // CHECK:STDOUT: --- fail_address_of_value.carbon
  77. // CHECK:STDOUT:
  78. // CHECK:STDOUT: constants {
  79. // CHECK:STDOUT: %.1: type = struct_type {.a: i32} [template]
  80. // CHECK:STDOUT: %.2: i32 = int_literal 0 [template]
  81. // CHECK:STDOUT: %.3: type = ptr_type i32 [template]
  82. // CHECK:STDOUT: %.4: bool = bool_literal true [template]
  83. // CHECK:STDOUT: %.5: type = ptr_type bool [template]
  84. // CHECK:STDOUT: %.6: f64 = real_literal 10e-1 [template]
  85. // CHECK:STDOUT: %.7: type = ptr_type f64 [template]
  86. // CHECK:STDOUT: %.8: type = ptr_type String [template]
  87. // CHECK:STDOUT: %.9: String = string_literal "Hello" [template]
  88. // CHECK:STDOUT: %.10: i32 = int_literal 1 [template]
  89. // CHECK:STDOUT: %.11: i32 = int_literal 2 [template]
  90. // CHECK:STDOUT: %.12: type = tuple_type (i32, i32) [template]
  91. // CHECK:STDOUT: %.13: type = ptr_type (i32, i32) [template]
  92. // CHECK:STDOUT: %.14: i32 = int_literal 5 [template]
  93. // CHECK:STDOUT: %.15: type = ptr_type {.a: i32} [template]
  94. // CHECK:STDOUT: %.16: bool = bool_literal false [template]
  95. // CHECK:STDOUT: %.17: type = ptr_type type [template]
  96. // CHECK:STDOUT: %.18: type = const_type i32 [template]
  97. // CHECK:STDOUT: %.19: type = ptr_type const i32 [template]
  98. // CHECK:STDOUT: %.20: (i32, i32) = tuple_value (%.10, %.11) [template]
  99. // CHECK:STDOUT: }
  100. // CHECK:STDOUT:
  101. // CHECK:STDOUT: file {
  102. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  103. // CHECK:STDOUT: .G = %G
  104. // CHECK:STDOUT: .H = %H
  105. // CHECK:STDOUT: .AddressOfLiteral = %AddressOfLiteral
  106. // CHECK:STDOUT: .AddressOfOperator = %AddressOfOperator
  107. // CHECK:STDOUT: .AddressOfCall = %AddressOfCall
  108. // CHECK:STDOUT: .AddressOfType = %AddressOfType
  109. // CHECK:STDOUT: .AddressOfTupleElementValue = %AddressOfTupleElementValue
  110. // CHECK:STDOUT: .AddressOfParam = %AddressOfParam
  111. // CHECK:STDOUT: }
  112. // CHECK:STDOUT: %G: <function> = fn_decl @G [template] {
  113. // CHECK:STDOUT: %return.var.loc7: ref i32 = var <return slot>
  114. // CHECK:STDOUT: }
  115. // CHECK:STDOUT: %H: <function> = fn_decl @H [template] {
  116. // CHECK:STDOUT: %.loc9: type = struct_type {.a: i32} [template = constants.%.1]
  117. // CHECK:STDOUT: %return.var.loc9: ref {.a: i32} = var <return slot>
  118. // CHECK:STDOUT: }
  119. // CHECK:STDOUT: %AddressOfLiteral: <function> = fn_decl @AddressOfLiteral [template] {}
  120. // CHECK:STDOUT: %AddressOfOperator: <function> = fn_decl @AddressOfOperator [template] {}
  121. // CHECK:STDOUT: %AddressOfCall: <function> = fn_decl @AddressOfCall [template] {}
  122. // CHECK:STDOUT: %AddressOfType: <function> = fn_decl @AddressOfType [template] {}
  123. // CHECK:STDOUT: %AddressOfTupleElementValue: <function> = fn_decl @AddressOfTupleElementValue [template] {}
  124. // CHECK:STDOUT: %AddressOfParam: <function> = fn_decl @AddressOfParam [template] {
  125. // CHECK:STDOUT: %param.loc78_19.1: i32 = param param
  126. // CHECK:STDOUT: @AddressOfParam.%param: i32 = bind_name param, %param.loc78_19.1
  127. // CHECK:STDOUT: }
  128. // CHECK:STDOUT: }
  129. // CHECK:STDOUT:
  130. // CHECK:STDOUT: fn @G() -> i32;
  131. // CHECK:STDOUT:
  132. // CHECK:STDOUT: fn @H() -> {.a: i32};
  133. // CHECK:STDOUT:
  134. // CHECK:STDOUT: fn @AddressOfLiteral() {
  135. // CHECK:STDOUT: !entry:
  136. // CHECK:STDOUT: %.loc15_4: i32 = int_literal 0 [template = constants.%.2]
  137. // CHECK:STDOUT: %.loc15_3: i32* = addr_of <error> [template = <error>]
  138. // CHECK:STDOUT: %.loc19_4: bool = bool_literal true [template = constants.%.4]
  139. // CHECK:STDOUT: %.loc19_3: bool* = addr_of <error> [template = <error>]
  140. // CHECK:STDOUT: %.loc23_4: f64 = real_literal 10e-1 [template = constants.%.6]
  141. // CHECK:STDOUT: %.loc23_3: f64* = addr_of <error> [template = <error>]
  142. // CHECK:STDOUT: %.loc27_4: String = string_literal "Hello" [template = constants.%.9]
  143. // CHECK:STDOUT: %.loc27_3: String* = addr_of <error> [template = <error>]
  144. // CHECK:STDOUT: %.loc31_5: i32 = int_literal 1 [template = constants.%.10]
  145. // CHECK:STDOUT: %.loc31_8: i32 = int_literal 2 [template = constants.%.11]
  146. // CHECK:STDOUT: %.loc31_9: (i32, i32) = tuple_literal (%.loc31_5, %.loc31_8)
  147. // CHECK:STDOUT: %.loc31_3: (i32, i32)* = addr_of <error> [template = <error>]
  148. // CHECK:STDOUT: %.loc35_10: i32 = int_literal 5 [template = constants.%.14]
  149. // CHECK:STDOUT: %.loc35_11: {.a: i32} = struct_literal (%.loc35_10)
  150. // CHECK:STDOUT: %.loc35_3: {.a: i32}* = addr_of <error> [template = <error>]
  151. // CHECK:STDOUT: return
  152. // CHECK:STDOUT: }
  153. // CHECK:STDOUT:
  154. // CHECK:STDOUT: fn @AddressOfOperator() {
  155. // CHECK:STDOUT: !entry:
  156. // CHECK:STDOUT: %.loc42_5: bool = bool_literal true [template = constants.%.4]
  157. // CHECK:STDOUT: %.loc42_10.1: bool = bool_literal false [template = constants.%.16]
  158. // CHECK:STDOUT: if %.loc42_5 br !and.rhs else br !and.result(%.loc42_10.1)
  159. // CHECK:STDOUT:
  160. // CHECK:STDOUT: !and.rhs:
  161. // CHECK:STDOUT: %.loc42_14: bool = bool_literal false [template = constants.%.16]
  162. // CHECK:STDOUT: br !and.result(%.loc42_14)
  163. // CHECK:STDOUT:
  164. // CHECK:STDOUT: !and.result:
  165. // CHECK:STDOUT: %.loc42_10.2: bool = block_arg !and.result
  166. // CHECK:STDOUT: %.loc42_3: bool* = addr_of <error> [template = <error>]
  167. // CHECK:STDOUT: %H.ref: <function> = name_ref H, file.%H [template = file.%H]
  168. // CHECK:STDOUT: %.loc46_5.1: init {.a: i32} = call %H.ref()
  169. // CHECK:STDOUT: %.loc46_5.2: ref {.a: i32} = temporary_storage
  170. // CHECK:STDOUT: %.loc46_5.3: ref {.a: i32} = temporary %.loc46_5.2, %.loc46_5.1
  171. // CHECK:STDOUT: %.loc46_7: ref i32 = struct_access %.loc46_5.3, element0
  172. // CHECK:STDOUT: %.loc46_3: i32* = addr_of <error> [template = <error>]
  173. // CHECK:STDOUT: %.loc50_9: bool = bool_literal true [template = constants.%.4]
  174. // CHECK:STDOUT: %.loc50_5: bool = not %.loc50_9 [template = constants.%.16]
  175. // CHECK:STDOUT: %.loc50_3: bool* = addr_of <error> [template = <error>]
  176. // CHECK:STDOUT: return
  177. // CHECK:STDOUT: }
  178. // CHECK:STDOUT:
  179. // CHECK:STDOUT: fn @AddressOfCall() {
  180. // CHECK:STDOUT: !entry:
  181. // CHECK:STDOUT: %G.ref: <function> = name_ref G, file.%G [template = file.%G]
  182. // CHECK:STDOUT: %.loc57_5: init i32 = call %G.ref()
  183. // CHECK:STDOUT: %.loc57_3: i32* = addr_of <error> [template = <error>]
  184. // CHECK:STDOUT: return
  185. // CHECK:STDOUT: }
  186. // CHECK:STDOUT:
  187. // CHECK:STDOUT: fn @AddressOfType() {
  188. // CHECK:STDOUT: !entry:
  189. // CHECK:STDOUT: %.loc64: type* = addr_of <error> [template = <error>]
  190. // CHECK:STDOUT: %.loc68_5: type = const_type i32 [template = constants.%.18]
  191. // CHECK:STDOUT: %.loc68_14: type = ptr_type const i32 [template = constants.%.19]
  192. // CHECK:STDOUT: %.loc68_3: type* = addr_of <error> [template = <error>]
  193. // CHECK:STDOUT: return
  194. // CHECK:STDOUT: }
  195. // CHECK:STDOUT:
  196. // CHECK:STDOUT: fn @AddressOfTupleElementValue() {
  197. // CHECK:STDOUT: !entry:
  198. // CHECK:STDOUT: %.loc75_6: i32 = int_literal 1 [template = constants.%.10]
  199. // CHECK:STDOUT: %.loc75_9: i32 = int_literal 2 [template = constants.%.11]
  200. // CHECK:STDOUT: %.loc75_10.1: (i32, i32) = tuple_literal (%.loc75_6, %.loc75_9)
  201. // CHECK:STDOUT: %.loc75_12: i32 = int_literal 0 [template = constants.%.2]
  202. // CHECK:STDOUT: %.loc75_10.2: (i32, i32) = tuple_value (%.loc75_6, %.loc75_9) [template = constants.%.20]
  203. // CHECK:STDOUT: %.loc75_10.3: (i32, i32) = converted %.loc75_10.1, %.loc75_10.2 [template = constants.%.20]
  204. // CHECK:STDOUT: %.loc75_13: i32 = tuple_index %.loc75_10.3, %.loc75_12 [template = constants.%.10]
  205. // CHECK:STDOUT: %.loc75_3: i32* = addr_of <error> [template = <error>]
  206. // CHECK:STDOUT: return
  207. // CHECK:STDOUT: }
  208. // CHECK:STDOUT:
  209. // CHECK:STDOUT: fn @AddressOfParam(%param: i32) {
  210. // CHECK:STDOUT: !entry:
  211. // CHECK:STDOUT: %.loc82_22: type = ptr_type i32 [template = constants.%.3]
  212. // CHECK:STDOUT: %param_addr.var: ref i32* = var param_addr
  213. // CHECK:STDOUT: %param_addr: ref i32* = bind_name param_addr, %param_addr.var
  214. // CHECK:STDOUT: %param.ref: i32 = name_ref param, %param
  215. // CHECK:STDOUT: %.loc82_26: i32* = addr_of <error> [template = <error>]
  216. // CHECK:STDOUT: assign %param_addr.var, %.loc82_26
  217. // CHECK:STDOUT: return
  218. // CHECK:STDOUT: }
  219. // CHECK:STDOUT: