fail_assign_non_ref.carbon 13 KB

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