fail_address_of_value.carbon 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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: %.loc15_4: i32 = int_literal 0, const
  80. // CHECK:STDOUT: %.loc15_3.1: type = ptr_type i32, const
  81. // CHECK:STDOUT: %.loc15_3.2: i32* = addr_of %.loc15_4, const
  82. // CHECK:STDOUT: %.loc19_4: bool = bool_literal true, const
  83. // CHECK:STDOUT: %.loc19_3.1: type = ptr_type bool, const
  84. // CHECK:STDOUT: %.loc19_3.2: bool* = addr_of %.loc19_4, const
  85. // CHECK:STDOUT: %.loc23_4: f64 = real_literal 10e-1, const
  86. // CHECK:STDOUT: %.loc23_3.1: type = ptr_type f64, const
  87. // CHECK:STDOUT: %.loc23_3.2: f64* = addr_of %.loc23_4, const
  88. // CHECK:STDOUT: %.1: type = ptr_type String, const
  89. // CHECK:STDOUT: %.loc27_4: String = string_literal "Hello", const
  90. // CHECK:STDOUT: %.loc27_3: String* = addr_of %.loc27_4, const
  91. // CHECK:STDOUT: %.loc31_5: i32 = int_literal 1, const
  92. // CHECK:STDOUT: %.loc31_8: i32 = int_literal 2, const
  93. // CHECK:STDOUT: %.loc31_9: type = tuple_type (i32, i32), const
  94. // CHECK:STDOUT: %.loc31_3: type = ptr_type (i32, i32), const
  95. // CHECK:STDOUT: %.loc35_10: i32 = int_literal 5, const
  96. // CHECK:STDOUT: %.loc35_3: type = ptr_type {.a: i32}, const
  97. // CHECK:STDOUT: %.loc42_5: bool = bool_literal true, const
  98. // CHECK:STDOUT: %.loc42_10: bool = bool_literal false, const
  99. // CHECK:STDOUT: %.loc42_14: bool = bool_literal false, const
  100. // CHECK:STDOUT: %.loc50_9.1: bool = bool_literal true, const
  101. // CHECK:STDOUT: %.loc50_9.2: bool = bool_literal false, const
  102. // CHECK:STDOUT: %.loc50_3: bool* = addr_of %.loc50_9.2, const
  103. // CHECK:STDOUT: %.loc64: type = ptr_type type, const
  104. // CHECK:STDOUT: %.loc68: type* = addr_of @AddressOfType.%.loc68_14, const
  105. // CHECK:STDOUT: %.loc75_6: i32 = int_literal 1, const
  106. // CHECK:STDOUT: %.loc75_9: i32 = int_literal 2, const
  107. // CHECK:STDOUT: %.loc75_12: i32 = int_literal 0, const
  108. // CHECK:STDOUT: %.loc75_10: (i32, i32) = tuple_value (%.loc75_6, %.loc75_9), const
  109. // CHECK:STDOUT: }
  110. // CHECK:STDOUT:
  111. // CHECK:STDOUT: file {
  112. // CHECK:STDOUT: package: <namespace> = namespace package, {.G = %G, .H = %H, .AddressOfLiteral = %AddressOfLiteral, .AddressOfOperator = %AddressOfOperator, .AddressOfCall = %AddressOfCall, .AddressOfType = %AddressOfType, .AddressOfTupleElementValue = %AddressOfTupleElementValue, .AddressOfParam = %AddressOfParam}
  113. // CHECK:STDOUT: %G: <function> = fn_decl @G, const
  114. // CHECK:STDOUT: %H: <function> = fn_decl @H, const
  115. // CHECK:STDOUT: %AddressOfLiteral: <function> = fn_decl @AddressOfLiteral, const
  116. // CHECK:STDOUT: %AddressOfOperator: <function> = fn_decl @AddressOfOperator, const
  117. // CHECK:STDOUT: %AddressOfCall: <function> = fn_decl @AddressOfCall, const
  118. // CHECK:STDOUT: %AddressOfType: <function> = fn_decl @AddressOfType, const
  119. // CHECK:STDOUT: %AddressOfTupleElementValue: <function> = fn_decl @AddressOfTupleElementValue, const
  120. // CHECK:STDOUT: %AddressOfParam: <function> = fn_decl @AddressOfParam, const
  121. // CHECK:STDOUT: }
  122. // CHECK:STDOUT:
  123. // CHECK:STDOUT: fn @G() -> i32;
  124. // CHECK:STDOUT:
  125. // CHECK:STDOUT: fn @H() -> {.a: i32};
  126. // CHECK:STDOUT:
  127. // CHECK:STDOUT: fn @AddressOfLiteral() {
  128. // CHECK:STDOUT: !entry:
  129. // CHECK:STDOUT: %.loc15_4: i32 = int_literal 0, const = constants.%.loc15_4
  130. // CHECK:STDOUT: %.loc15_3: i32* = addr_of %.loc15_4, const = constants.%.loc15_3.2
  131. // CHECK:STDOUT: %.loc19_4: bool = bool_literal true, const = constants.%.loc19_4
  132. // CHECK:STDOUT: %.loc19_3: bool* = addr_of %.loc19_4, const = constants.%.loc19_3.2
  133. // CHECK:STDOUT: %.loc23_4: f64 = real_literal 10e-1, const = constants.%.loc23_4
  134. // CHECK:STDOUT: %.loc23_3: f64* = addr_of %.loc23_4, const = constants.%.loc23_3.2
  135. // CHECK:STDOUT: %.loc27_4: String = string_literal "Hello", const = constants.%.loc27_4
  136. // CHECK:STDOUT: %.loc27_3: String* = addr_of %.loc27_4, const = constants.%.loc27_3
  137. // CHECK:STDOUT: %.loc31_5: i32 = int_literal 1, const = constants.%.loc31_5
  138. // CHECK:STDOUT: %.loc31_8: i32 = int_literal 2, const = constants.%.loc31_8
  139. // CHECK:STDOUT: %.loc31_9: (i32, i32) = tuple_literal (%.loc31_5, %.loc31_8)
  140. // CHECK:STDOUT: %.loc31_3: (i32, i32)* = addr_of %.loc31_9
  141. // CHECK:STDOUT: %.loc35_10: i32 = int_literal 5, const = constants.%.loc35_10
  142. // CHECK:STDOUT: %.loc35_11: {.a: i32} = struct_literal (%.loc35_10)
  143. // CHECK:STDOUT: %.loc35_3: {.a: i32}* = addr_of %.loc35_11
  144. // CHECK:STDOUT: return
  145. // CHECK:STDOUT: }
  146. // CHECK:STDOUT:
  147. // CHECK:STDOUT: fn @AddressOfOperator() {
  148. // CHECK:STDOUT: !entry:
  149. // CHECK:STDOUT: %.loc42_5: bool = bool_literal true, const = constants.%.loc42_5
  150. // CHECK:STDOUT: %.loc42_10.1: bool = bool_literal false, const = constants.%.loc42_10
  151. // CHECK:STDOUT: if %.loc42_5 br !and.rhs else br !and.result(%.loc42_10.1)
  152. // CHECK:STDOUT:
  153. // CHECK:STDOUT: !and.rhs:
  154. // CHECK:STDOUT: %.loc42_14: bool = bool_literal false, const = constants.%.loc42_14
  155. // CHECK:STDOUT: br !and.result(%.loc42_14)
  156. // CHECK:STDOUT:
  157. // CHECK:STDOUT: !and.result:
  158. // CHECK:STDOUT: %.loc42_10.2: bool = block_arg !and.result
  159. // CHECK:STDOUT: %.loc42_3: bool* = addr_of %.loc42_10.2
  160. // CHECK:STDOUT: %H.ref: <function> = name_ref H, file.%H, const = file.%H
  161. // CHECK:STDOUT: %.loc46_5.1: init {.a: i32} = call %H.ref()
  162. // CHECK:STDOUT: %.loc46_5.2: ref {.a: i32} = temporary_storage
  163. // CHECK:STDOUT: %.loc46_5.3: ref {.a: i32} = temporary %.loc46_5.2, %.loc46_5.1
  164. // CHECK:STDOUT: %.loc46_7: ref i32 = struct_access %.loc46_5.3, element0
  165. // CHECK:STDOUT: %.loc46_3: i32* = addr_of %.loc46_7
  166. // CHECK:STDOUT: %.loc50_9: bool = bool_literal true, const = constants.%.loc50_9.1
  167. // CHECK:STDOUT: %.loc50_5: bool = not %.loc50_9, const = constants.%.loc50_9.2
  168. // CHECK:STDOUT: %.loc50_3: bool* = addr_of %.loc50_5, const = constants.%.loc50_3
  169. // CHECK:STDOUT: return
  170. // CHECK:STDOUT: }
  171. // CHECK:STDOUT:
  172. // CHECK:STDOUT: fn @AddressOfCall() {
  173. // CHECK:STDOUT: !entry:
  174. // CHECK:STDOUT: %G.ref: <function> = name_ref G, file.%G, const = file.%G
  175. // CHECK:STDOUT: %.loc57_5: init i32 = call %G.ref()
  176. // CHECK:STDOUT: %.loc57_3: i32* = addr_of %.loc57_5
  177. // CHECK:STDOUT: return
  178. // CHECK:STDOUT: }
  179. // CHECK:STDOUT:
  180. // CHECK:STDOUT: fn @AddressOfType() {
  181. // CHECK:STDOUT: !entry:
  182. // CHECK:STDOUT: %.loc64: type* = addr_of i32
  183. // CHECK:STDOUT: %.loc68_5: type = const_type i32, const
  184. // CHECK:STDOUT: %.loc68_14: type = ptr_type const i32, const
  185. // CHECK:STDOUT: %.loc68_3: type* = addr_of %.loc68_14, const = constants.%.loc68
  186. // CHECK:STDOUT: return
  187. // CHECK:STDOUT: }
  188. // CHECK:STDOUT:
  189. // CHECK:STDOUT: fn @AddressOfTupleElementValue() {
  190. // CHECK:STDOUT: !entry:
  191. // CHECK:STDOUT: %.loc75_6: i32 = int_literal 1, const = constants.%.loc75_6
  192. // CHECK:STDOUT: %.loc75_9: i32 = int_literal 2, const = constants.%.loc75_9
  193. // CHECK:STDOUT: %.loc75_10.1: (i32, i32) = tuple_literal (%.loc75_6, %.loc75_9)
  194. // CHECK:STDOUT: %.loc75_12: i32 = int_literal 0, const = constants.%.loc75_12
  195. // CHECK:STDOUT: %.loc75_10.2: (i32, i32) = tuple_value (%.loc75_6, %.loc75_9), const = constants.%.loc75_10
  196. // CHECK:STDOUT: %.loc75_10.3: (i32, i32) = converted %.loc75_10.1, %.loc75_10.2, const = constants.%.loc75_10
  197. // CHECK:STDOUT: %.loc75_13: i32 = tuple_index %.loc75_10.3, %.loc75_12
  198. // CHECK:STDOUT: %.loc75_3: i32* = addr_of %.loc75_13
  199. // CHECK:STDOUT: return
  200. // CHECK:STDOUT: }
  201. // CHECK:STDOUT:
  202. // CHECK:STDOUT: fn @AddressOfParam(%param: i32) {
  203. // CHECK:STDOUT: !entry:
  204. // CHECK:STDOUT: %.loc82_22: type = ptr_type i32, const
  205. // CHECK:STDOUT: %param_addr.var: ref i32* = var param_addr
  206. // CHECK:STDOUT: %param_addr: ref i32* = bind_name param_addr, %param_addr.var
  207. // CHECK:STDOUT: %param.ref: i32 = name_ref param, %param
  208. // CHECK:STDOUT: %.loc82_26: i32* = addr_of %param.ref
  209. // CHECK:STDOUT: assign %param_addr.var, %.loc82_26
  210. // CHECK:STDOUT: return
  211. // CHECK:STDOUT: }
  212. // CHECK:STDOUT: