fail_assign_non_ref.carbon 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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. // --- prelude.carbon
  7. package Core api;
  8. interface Inc {
  9. fn Op[addr self: Self*]();
  10. }
  11. interface AddAssign {
  12. fn Op[addr self: Self*](other: Self);
  13. }
  14. // --- fail_assign_non_ref.carbon
  15. package User api;
  16. import Core;
  17. class C {};
  18. impl C as Core.Inc {
  19. fn Op[addr self: C*]();
  20. }
  21. impl C as Core.AddAssign {
  22. fn Op[addr self: C*](other: C);
  23. }
  24. fn TestIncNonRef(a: C) {
  25. // CHECK:STDERR: fail_assign_non_ref.carbon:[[@LINE+6]]:3: ERROR: `addr self` method cannot be invoked on a value.
  26. // CHECK:STDERR: ++a;
  27. // CHECK:STDERR: ^~
  28. // CHECK:STDERR: fail_assign_non_ref.carbon:[[@LINE-10]]:14: Initializing `addr self` parameter of method declared here.
  29. // CHECK:STDERR: fn Op[addr self: C*]();
  30. // CHECK:STDERR: ^~~~
  31. ++a;
  32. }
  33. fn TestAddAssignNonRef(a: C, b: C) {
  34. // CHECK:STDERR: fail_assign_non_ref.carbon:[[@LINE+6]]:5: ERROR: `addr self` method cannot be invoked on a value.
  35. // CHECK:STDERR: a += b;
  36. // CHECK:STDERR: ^~
  37. // CHECK:STDERR: fail_assign_non_ref.carbon:[[@LINE-17]]:14: Initializing `addr self` parameter of method declared here.
  38. // CHECK:STDERR: fn Op[addr self: C*](other: C);
  39. // CHECK:STDERR: ^~~~
  40. a += b;
  41. }
  42. // CHECK:STDOUT: --- prelude.carbon
  43. // CHECK:STDOUT:
  44. // CHECK:STDOUT: constants {
  45. // CHECK:STDOUT: %.1: type = interface_type @Inc [template]
  46. // CHECK:STDOUT: %.2: type = ptr_type Self [symbolic]
  47. // CHECK:STDOUT: %.3: type = assoc_entity_type @Inc, <function> [template]
  48. // CHECK:STDOUT: %.4: <associated <function> in Inc> = assoc_entity element0, @Inc.%Op [template]
  49. // CHECK:STDOUT: %.5: type = interface_type @AddAssign [template]
  50. // CHECK:STDOUT: %.6: type = ptr_type Self [symbolic]
  51. // CHECK:STDOUT: %.7: type = assoc_entity_type @AddAssign, <function> [template]
  52. // CHECK:STDOUT: %.8: <associated <function> in AddAssign> = assoc_entity element0, @AddAssign.%Op [template]
  53. // CHECK:STDOUT: }
  54. // CHECK:STDOUT:
  55. // CHECK:STDOUT: file {
  56. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  57. // CHECK:STDOUT: .Inc = %Inc.decl
  58. // CHECK:STDOUT: .AddAssign = %AddAssign.decl
  59. // CHECK:STDOUT: }
  60. // CHECK:STDOUT: %Inc.decl: type = interface_decl @Inc [template = constants.%.1] {}
  61. // CHECK:STDOUT: %AddAssign.decl: type = interface_decl @AddAssign [template = constants.%.5] {}
  62. // CHECK:STDOUT: }
  63. // CHECK:STDOUT:
  64. // CHECK:STDOUT: interface @Inc {
  65. // CHECK:STDOUT: %Self: Inc = bind_symbolic_name Self [symbolic]
  66. // CHECK:STDOUT: %Op: <function> = fn_decl @Op.1 [template] {
  67. // CHECK:STDOUT: %Self.ref: Inc = name_ref Self, %Self [symbolic = %Self]
  68. // CHECK:STDOUT: %.loc5_24.1: type = facet_type_access %Self.ref [symbolic = %Self]
  69. // CHECK:STDOUT: %.loc5_20: type = converted %Self.ref, %.loc5_24.1 [symbolic = %Self]
  70. // CHECK:STDOUT: %.loc5_24.2: type = ptr_type Self [symbolic = constants.%.2]
  71. // CHECK:STDOUT: %self.loc5_14.1: Self* = param self
  72. // CHECK:STDOUT: %self.loc5_14.3: Self* = bind_name self, %self.loc5_14.1
  73. // CHECK:STDOUT: %.loc5_9: Self* = addr_pattern %self.loc5_14.3
  74. // CHECK:STDOUT: }
  75. // CHECK:STDOUT: %.loc5_28: <associated <function> in Inc> = assoc_entity element0, %Op [template = constants.%.4]
  76. // CHECK:STDOUT:
  77. // CHECK:STDOUT: !members:
  78. // CHECK:STDOUT: .Self = %Self
  79. // CHECK:STDOUT: .Op = %.loc5_28
  80. // CHECK:STDOUT: witness = (%Op)
  81. // CHECK:STDOUT: }
  82. // CHECK:STDOUT:
  83. // CHECK:STDOUT: interface @AddAssign {
  84. // CHECK:STDOUT: %Self: AddAssign = bind_symbolic_name Self [symbolic]
  85. // CHECK:STDOUT: %Op: <function> = fn_decl @Op.2 [template] {
  86. // CHECK:STDOUT: %Self.ref.loc8_20: AddAssign = name_ref Self, %Self [symbolic = %Self]
  87. // CHECK:STDOUT: %.loc8_24.1: type = facet_type_access %Self.ref.loc8_20 [symbolic = %Self]
  88. // CHECK:STDOUT: %.loc8_20: type = converted %Self.ref.loc8_20, %.loc8_24.1 [symbolic = %Self]
  89. // CHECK:STDOUT: %.loc8_24.2: type = ptr_type Self [symbolic = constants.%.6]
  90. // CHECK:STDOUT: %self.loc8_14.1: Self* = param self
  91. // CHECK:STDOUT: %self.loc8_14.3: Self* = bind_name self, %self.loc8_14.1
  92. // CHECK:STDOUT: %.loc8_9: Self* = addr_pattern %self.loc8_14.3
  93. // CHECK:STDOUT: %Self.ref.loc8_34: AddAssign = name_ref Self, %Self [symbolic = %Self]
  94. // CHECK:STDOUT: %.loc8_34.1: type = facet_type_access %Self.ref.loc8_34 [symbolic = %Self]
  95. // CHECK:STDOUT: %.loc8_34.2: type = converted %Self.ref.loc8_34, %.loc8_34.1 [symbolic = %Self]
  96. // CHECK:STDOUT: %other.loc8_27.1: Self = param other
  97. // CHECK:STDOUT: %other.loc8_27.2: Self = bind_name other, %other.loc8_27.1
  98. // CHECK:STDOUT: }
  99. // CHECK:STDOUT: %.loc8_39: <associated <function> in AddAssign> = assoc_entity element0, %Op [template = constants.%.8]
  100. // CHECK:STDOUT:
  101. // CHECK:STDOUT: !members:
  102. // CHECK:STDOUT: .Self = %Self
  103. // CHECK:STDOUT: .Op = %.loc8_39
  104. // CHECK:STDOUT: witness = (%Op)
  105. // CHECK:STDOUT: }
  106. // CHECK:STDOUT:
  107. // CHECK:STDOUT: fn @Op.1[addr @Inc.%self.loc5_14.3: Self*]();
  108. // CHECK:STDOUT:
  109. // CHECK:STDOUT: fn @Op.2[addr @AddAssign.%self.loc8_14.3: Self*](@AddAssign.%other.loc8_27.2: Self);
  110. // CHECK:STDOUT:
  111. // CHECK:STDOUT: --- fail_assign_non_ref.carbon
  112. // CHECK:STDOUT:
  113. // CHECK:STDOUT: constants {
  114. // CHECK:STDOUT: %C: type = class_type @C [template]
  115. // CHECK:STDOUT: %.1: type = struct_type {} [template]
  116. // CHECK:STDOUT: %.2: type = interface_type @Inc [template]
  117. // CHECK:STDOUT: %.3: type = ptr_type C [template]
  118. // CHECK:STDOUT: %.4: type = ptr_type Self [symbolic]
  119. // CHECK:STDOUT: %.5: <witness> = interface_witness (@impl.1.%Op) [template]
  120. // CHECK:STDOUT: %.6: type = interface_type @AddAssign [template]
  121. // CHECK:STDOUT: %.7: type = ptr_type Self [symbolic]
  122. // CHECK:STDOUT: %.8: <witness> = interface_witness (@impl.2.%Op) [template]
  123. // CHECK:STDOUT: %.9: type = tuple_type () [template]
  124. // CHECK:STDOUT: %.10: type = ptr_type {} [template]
  125. // CHECK:STDOUT: %.11: type = assoc_entity_type @Inc, <function> [template]
  126. // CHECK:STDOUT: %.12: <associated <function> in Inc> = assoc_entity element0, file.%import_ref.10 [template]
  127. // CHECK:STDOUT: %.13: type = assoc_entity_type @AddAssign, <function> [template]
  128. // CHECK:STDOUT: %.14: <associated <function> in AddAssign> = assoc_entity element0, file.%import_ref.12 [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: .C = %C.decl
  135. // CHECK:STDOUT: .TestIncNonRef = %TestIncNonRef
  136. // CHECK:STDOUT: .TestAddAssignNonRef = %TestAddAssignNonRef
  137. // CHECK:STDOUT: }
  138. // CHECK:STDOUT: %Core: <namespace> = namespace [template] {}
  139. // CHECK:STDOUT: %C.decl: type = class_decl @C [template = constants.%C] {}
  140. // CHECK:STDOUT: %import_ref.1: type = import_ref ir1, inst+1, used [template = constants.%.2]
  141. // CHECK:STDOUT: %import_ref.2: <associated <function> in Inc> = import_ref ir1, inst+14, used [template = constants.%.12]
  142. // CHECK:STDOUT: %import_ref.3 = import_ref ir1, inst+3, unused
  143. // CHECK:STDOUT: %import_ref.4: <function> = import_ref ir1, inst+12, used [template = imports.%Op.1]
  144. // CHECK:STDOUT: impl_decl @impl.1 {
  145. // CHECK:STDOUT: %C.ref.loc8: type = name_ref C, %C.decl [template = constants.%C]
  146. // CHECK:STDOUT: %Core.ref.loc8: <namespace> = name_ref Core, %Core [template = %Core]
  147. // CHECK:STDOUT: %Inc.decl: invalid = interface_decl @Inc [template = constants.%.2] {}
  148. // CHECK:STDOUT: %Inc.ref: type = name_ref Inc, %import_ref.1 [template = constants.%.2]
  149. // CHECK:STDOUT: }
  150. // CHECK:STDOUT: %import_ref.5: type = import_ref ir1, inst+16, used [template = constants.%.6]
  151. // CHECK:STDOUT: %import_ref.6: <associated <function> in AddAssign> = import_ref ir1, inst+34, used [template = constants.%.14]
  152. // CHECK:STDOUT: %import_ref.7 = import_ref ir1, inst+18, unused
  153. // CHECK:STDOUT: %import_ref.8: <function> = import_ref ir1, inst+32, used [template = imports.%Op.2]
  154. // CHECK:STDOUT: impl_decl @impl.2 {
  155. // CHECK:STDOUT: %C.ref.loc11: type = name_ref C, %C.decl [template = constants.%C]
  156. // CHECK:STDOUT: %Core.ref.loc11: <namespace> = name_ref Core, %Core [template = %Core]
  157. // CHECK:STDOUT: %AddAssign.decl: invalid = interface_decl @AddAssign [template = constants.%.6] {}
  158. // CHECK:STDOUT: %AddAssign.ref: type = name_ref AddAssign, %import_ref.5 [template = constants.%.6]
  159. // CHECK:STDOUT: }
  160. // CHECK:STDOUT: %TestIncNonRef: <function> = fn_decl @TestIncNonRef [template] {
  161. // CHECK:STDOUT: %C.ref.loc15: type = name_ref C, %C.decl [template = constants.%C]
  162. // CHECK:STDOUT: %a.loc15_18.1: C = param a
  163. // CHECK:STDOUT: @TestIncNonRef.%a: C = bind_name a, %a.loc15_18.1
  164. // CHECK:STDOUT: }
  165. // CHECK:STDOUT: %import_ref.9: type = import_ref ir1, inst+1, used [template = constants.%.2]
  166. // CHECK:STDOUT: %import_ref.10 = import_ref ir1, inst+12, unused
  167. // CHECK:STDOUT: %TestAddAssignNonRef: <function> = fn_decl @TestAddAssignNonRef [template] {
  168. // CHECK:STDOUT: %C.ref.loc25_27: type = name_ref C, %C.decl [template = constants.%C]
  169. // CHECK:STDOUT: %a.loc25_24.1: C = param a
  170. // CHECK:STDOUT: @TestAddAssignNonRef.%a: C = bind_name a, %a.loc25_24.1
  171. // CHECK:STDOUT: %C.ref.loc25_33: type = name_ref C, %C.decl [template = constants.%C]
  172. // CHECK:STDOUT: %b.loc25_30.1: C = param b
  173. // CHECK:STDOUT: @TestAddAssignNonRef.%b: C = bind_name b, %b.loc25_30.1
  174. // CHECK:STDOUT: }
  175. // CHECK:STDOUT: %import_ref.11: type = import_ref ir1, inst+16, used [template = constants.%.6]
  176. // CHECK:STDOUT: %import_ref.12 = import_ref ir1, inst+32, unused
  177. // CHECK:STDOUT: }
  178. // CHECK:STDOUT:
  179. // CHECK:STDOUT: interface @Inc {
  180. // CHECK:STDOUT: !members:
  181. // CHECK:STDOUT: .Op = file.%import_ref.2
  182. // CHECK:STDOUT: .Self = file.%import_ref.3
  183. // CHECK:STDOUT: witness = (file.%import_ref.4)
  184. // CHECK:STDOUT: }
  185. // CHECK:STDOUT:
  186. // CHECK:STDOUT: interface @AddAssign {
  187. // CHECK:STDOUT: !members:
  188. // CHECK:STDOUT: .Op = file.%import_ref.6
  189. // CHECK:STDOUT: .Self = file.%import_ref.7
  190. // CHECK:STDOUT: witness = (file.%import_ref.8)
  191. // CHECK:STDOUT: }
  192. // CHECK:STDOUT:
  193. // CHECK:STDOUT: impl @impl.1: C as Inc {
  194. // CHECK:STDOUT: %Op: <function> = fn_decl @Op.1 [template] {
  195. // CHECK:STDOUT: %C.ref: type = name_ref C, file.%C.decl [template = constants.%C]
  196. // CHECK:STDOUT: %.loc9_21: type = ptr_type C [template = constants.%.3]
  197. // CHECK:STDOUT: %self.loc9_14.1: C* = param self
  198. // CHECK:STDOUT: %self.loc9_14.3: C* = bind_name self, %self.loc9_14.1
  199. // CHECK:STDOUT: %.loc9_9: C* = addr_pattern %self.loc9_14.3
  200. // CHECK:STDOUT: }
  201. // CHECK:STDOUT: %.1: <witness> = interface_witness (%Op) [template = constants.%.5]
  202. // CHECK:STDOUT:
  203. // CHECK:STDOUT: !members:
  204. // CHECK:STDOUT: .Op = %Op
  205. // CHECK:STDOUT: witness = %.1
  206. // CHECK:STDOUT: }
  207. // CHECK:STDOUT:
  208. // CHECK:STDOUT: impl @impl.2: C as AddAssign {
  209. // CHECK:STDOUT: %Op: <function> = fn_decl @Op.3 [template] {
  210. // CHECK:STDOUT: %C.ref.loc12_20: type = name_ref C, file.%C.decl [template = constants.%C]
  211. // CHECK:STDOUT: %.loc12_21: type = ptr_type C [template = constants.%.3]
  212. // CHECK:STDOUT: %self.loc12_14.1: C* = param self
  213. // CHECK:STDOUT: %self.loc12_14.3: C* = bind_name self, %self.loc12_14.1
  214. // CHECK:STDOUT: %.loc12_9: C* = addr_pattern %self.loc12_14.3
  215. // CHECK:STDOUT: %C.ref.loc12_31: type = name_ref C, file.%C.decl [template = constants.%C]
  216. // CHECK:STDOUT: %other.loc12_24.1: C = param other
  217. // CHECK:STDOUT: %other.loc12_24.2: C = bind_name other, %other.loc12_24.1
  218. // CHECK:STDOUT: }
  219. // CHECK:STDOUT: %.1: <witness> = interface_witness (%Op) [template = constants.%.8]
  220. // CHECK:STDOUT:
  221. // CHECK:STDOUT: !members:
  222. // CHECK:STDOUT: .Op = %Op
  223. // CHECK:STDOUT: witness = %.1
  224. // CHECK:STDOUT: }
  225. // CHECK:STDOUT:
  226. // CHECK:STDOUT: class @C {
  227. // CHECK:STDOUT: !members:
  228. // CHECK:STDOUT: .Self = constants.%C
  229. // CHECK:STDOUT: }
  230. // CHECK:STDOUT:
  231. // CHECK:STDOUT: fn @Op.1[addr @impl.1.%self.loc9_14.3: C*]();
  232. // CHECK:STDOUT:
  233. // CHECK:STDOUT: fn @Op.2[addr %self: Self*]();
  234. // CHECK:STDOUT:
  235. // CHECK:STDOUT: fn @Op.3[addr @impl.2.%self.loc12_14.3: C*](@impl.2.%other.loc12_24.2: C);
  236. // CHECK:STDOUT:
  237. // CHECK:STDOUT: fn @Op.4[addr %self: Self*](%other: Self);
  238. // CHECK:STDOUT:
  239. // CHECK:STDOUT: fn @TestIncNonRef(%a: C) {
  240. // CHECK:STDOUT: !entry:
  241. // CHECK:STDOUT: %a.ref: C = name_ref a, %a
  242. // CHECK:STDOUT: %.1: <function> = interface_witness_access @impl.1.%.1, element0 [template = @impl.1.%Op]
  243. // CHECK:STDOUT: %.loc22_3.1: <bound method> = bound_method %a.ref, %.1
  244. // CHECK:STDOUT: %.loc22_3.2: init () = call %.loc22_3.1(<invalid>) [template = <error>]
  245. // CHECK:STDOUT: return
  246. // CHECK:STDOUT: }
  247. // CHECK:STDOUT:
  248. // CHECK:STDOUT: fn @TestAddAssignNonRef(%a: C, %b: C) {
  249. // CHECK:STDOUT: !entry:
  250. // CHECK:STDOUT: %a.ref: C = name_ref a, %a
  251. // CHECK:STDOUT: %b.ref: C = name_ref b, %b
  252. // CHECK:STDOUT: %.1: <function> = interface_witness_access @impl.2.%.1, element0 [template = @impl.2.%Op]
  253. // CHECK:STDOUT: %.loc32_5.1: <bound method> = bound_method %a.ref, %.1
  254. // CHECK:STDOUT: %.loc32_5.2: init () = call %.loc32_5.1(<invalid>) [template = <error>]
  255. // CHECK:STDOUT: return
  256. // CHECK:STDOUT: }
  257. // CHECK:STDOUT: