fail_assign_non_ref.carbon 15 KB

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