fail_assign_non_ref.carbon 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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. // TIP: To test this file alone, run:
  7. // TIP: bazel test //toolchain/testing:file_test --test_arg=--file_tests=toolchain/check/testdata/operators/overloaded/fail_assign_non_ref.carbon
  8. // TIP: To dump output, run:
  9. // TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/operators/overloaded/fail_assign_non_ref.carbon
  10. package User;
  11. class C {};
  12. impl C as Core.Inc {
  13. fn Op[addr self: C*]();
  14. }
  15. impl C as Core.AddAssign {
  16. fn Op[addr self: C*](other: C);
  17. }
  18. fn TestIncNonRef(a: C) {
  19. // CHECK:STDERR: fail_assign_non_ref.carbon:[[@LINE+7]]:3: error: `addr self` method cannot be invoked on a value
  20. // CHECK:STDERR: ++a;
  21. // CHECK:STDERR: ^~
  22. // CHECK:STDERR: fail_assign_non_ref.carbon:[[@LINE-10]]:14: note: initializing `addr self` parameter of method declared here
  23. // CHECK:STDERR: fn Op[addr self: C*]();
  24. // CHECK:STDERR: ^~~~
  25. // CHECK:STDERR:
  26. ++a;
  27. }
  28. fn TestAddAssignNonRef(a: C, b: C) {
  29. // CHECK:STDERR: fail_assign_non_ref.carbon:[[@LINE+6]]:5: error: `addr self` method cannot be invoked on a value
  30. // CHECK:STDERR: a += b;
  31. // CHECK:STDERR: ^~
  32. // CHECK:STDERR: fail_assign_non_ref.carbon:[[@LINE-18]]:14: note: initializing `addr self` parameter of method declared here
  33. // CHECK:STDERR: fn Op[addr self: C*](other: C);
  34. // CHECK:STDERR: ^~~~
  35. a += b;
  36. }
  37. // CHECK:STDOUT: --- fail_assign_non_ref.carbon
  38. // CHECK:STDOUT:
  39. // CHECK:STDOUT: constants {
  40. // CHECK:STDOUT: %C: type = class_type @C [template]
  41. // CHECK:STDOUT: %.1: type = struct_type {} [template]
  42. // CHECK:STDOUT: %.2: <witness> = complete_type_witness %.1 [template]
  43. // CHECK:STDOUT: %.3: type = interface_type @Inc [template]
  44. // CHECK:STDOUT: %Self.1: %.3 = bind_symbolic_name Self 0 [symbolic]
  45. // CHECK:STDOUT: %.4: type = ptr_type %C [template]
  46. // CHECK:STDOUT: %Op.type.1: type = fn_type @Op.1 [template]
  47. // CHECK:STDOUT: %.5: type = tuple_type () [template]
  48. // CHECK:STDOUT: %Op.1: %Op.type.1 = struct_value () [template]
  49. // CHECK:STDOUT: %Op.type.2: type = fn_type @Op.2 [template]
  50. // CHECK:STDOUT: %Op.2: %Op.type.2 = struct_value () [template]
  51. // CHECK:STDOUT: %.6: type = ptr_type %Self.1 [symbolic]
  52. // CHECK:STDOUT: %.7: <witness> = interface_witness (%Op.1) [template]
  53. // CHECK:STDOUT: %.8: type = interface_type @AddAssign [template]
  54. // CHECK:STDOUT: %Self.2: %.8 = bind_symbolic_name Self 0 [symbolic]
  55. // CHECK:STDOUT: %Op.type.3: type = fn_type @Op.3 [template]
  56. // CHECK:STDOUT: %Op.3: %Op.type.3 = struct_value () [template]
  57. // CHECK:STDOUT: %Op.type.4: type = fn_type @Op.4 [template]
  58. // CHECK:STDOUT: %Op.4: %Op.type.4 = struct_value () [template]
  59. // CHECK:STDOUT: %.9: type = ptr_type %Self.2 [symbolic]
  60. // CHECK:STDOUT: %.10: <witness> = interface_witness (%Op.3) [template]
  61. // CHECK:STDOUT: %TestIncNonRef.type: type = fn_type @TestIncNonRef [template]
  62. // CHECK:STDOUT: %TestIncNonRef: %TestIncNonRef.type = struct_value () [template]
  63. // CHECK:STDOUT: %.11: type = ptr_type %.1 [template]
  64. // CHECK:STDOUT: %.12: type = assoc_entity_type %.3, %Op.type.2 [template]
  65. // CHECK:STDOUT: %.13: %.12 = assoc_entity element0, imports.%import_ref.9 [template]
  66. // CHECK:STDOUT: %TestAddAssignNonRef.type: type = fn_type @TestAddAssignNonRef [template]
  67. // CHECK:STDOUT: %TestAddAssignNonRef: %TestAddAssignNonRef.type = struct_value () [template]
  68. // CHECK:STDOUT: %.14: type = assoc_entity_type %.8, %Op.type.4 [template]
  69. // CHECK:STDOUT: %.15: %.14 = assoc_entity element0, imports.%import_ref.10 [template]
  70. // CHECK:STDOUT: }
  71. // CHECK:STDOUT:
  72. // CHECK:STDOUT: imports {
  73. // CHECK:STDOUT: %Core: <namespace> = namespace file.%Core.import, [template] {
  74. // CHECK:STDOUT: .Inc = %import_ref.1
  75. // CHECK:STDOUT: .AddAssign = %import_ref.5
  76. // CHECK:STDOUT: import Core//prelude
  77. // CHECK:STDOUT: import Core//prelude/operators
  78. // CHECK:STDOUT: import Core//prelude/types
  79. // CHECK:STDOUT: import Core//prelude/operators/arithmetic
  80. // CHECK:STDOUT: import Core//prelude/operators/as
  81. // CHECK:STDOUT: import Core//prelude/operators/bitwise
  82. // CHECK:STDOUT: import Core//prelude/operators/comparison
  83. // CHECK:STDOUT: import Core//prelude/types/bool
  84. // CHECK:STDOUT: }
  85. // CHECK:STDOUT: %import_ref.1: type = import_ref Core//prelude/operators/arithmetic, inst+56, loaded [template = constants.%.3]
  86. // CHECK:STDOUT: %import_ref.2 = import_ref Core//prelude/operators/arithmetic, inst+58, unloaded
  87. // CHECK:STDOUT: %import_ref.3: %.12 = import_ref Core//prelude/operators/arithmetic, inst+75, loaded [template = constants.%.13]
  88. // CHECK:STDOUT: %import_ref.4: %Op.type.2 = import_ref Core//prelude/operators/arithmetic, inst+69, loaded [template = constants.%Op.2]
  89. // CHECK:STDOUT: %import_ref.5: type = import_ref Core//prelude/operators/arithmetic, inst+29, loaded [template = constants.%.8]
  90. // CHECK:STDOUT: %import_ref.6 = import_ref Core//prelude/operators/arithmetic, inst+31, unloaded
  91. // CHECK:STDOUT: %import_ref.7: %.14 = import_ref Core//prelude/operators/arithmetic, inst+54, loaded [template = constants.%.15]
  92. // CHECK:STDOUT: %import_ref.8: %Op.type.4 = import_ref Core//prelude/operators/arithmetic, inst+48, loaded [template = constants.%Op.4]
  93. // CHECK:STDOUT: %import_ref.9 = import_ref Core//prelude/operators/arithmetic, inst+69, unloaded
  94. // CHECK:STDOUT: %import_ref.10 = import_ref Core//prelude/operators/arithmetic, inst+48, unloaded
  95. // CHECK:STDOUT: }
  96. // CHECK:STDOUT:
  97. // CHECK:STDOUT: file {
  98. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  99. // CHECK:STDOUT: .Core = imports.%Core
  100. // CHECK:STDOUT: .C = %C.decl
  101. // CHECK:STDOUT: .TestIncNonRef = %TestIncNonRef.decl
  102. // CHECK:STDOUT: .TestAddAssignNonRef = %TestAddAssignNonRef.decl
  103. // CHECK:STDOUT: }
  104. // CHECK:STDOUT: %Core.import = import Core
  105. // CHECK:STDOUT: %C.decl: type = class_decl @C [template = constants.%C] {} {}
  106. // CHECK:STDOUT: impl_decl @impl.1 {} {
  107. // CHECK:STDOUT: %C.ref: type = name_ref C, file.%C.decl [template = constants.%C]
  108. // CHECK:STDOUT: %Core.ref: <namespace> = name_ref Core, imports.%Core [template = imports.%Core]
  109. // CHECK:STDOUT: %Inc.ref: type = name_ref Inc, imports.%import_ref.1 [template = constants.%.3]
  110. // CHECK:STDOUT: }
  111. // CHECK:STDOUT: impl_decl @impl.2 {} {
  112. // CHECK:STDOUT: %C.ref: type = name_ref C, file.%C.decl [template = constants.%C]
  113. // CHECK:STDOUT: %Core.ref: <namespace> = name_ref Core, imports.%Core [template = imports.%Core]
  114. // CHECK:STDOUT: %AddAssign.ref: type = name_ref AddAssign, imports.%import_ref.5 [template = constants.%.8]
  115. // CHECK:STDOUT: }
  116. // CHECK:STDOUT: %TestIncNonRef.decl: %TestIncNonRef.type = fn_decl @TestIncNonRef [template = constants.%TestIncNonRef] {
  117. // CHECK:STDOUT: %a.patt: %C = binding_pattern a
  118. // CHECK:STDOUT: } {
  119. // CHECK:STDOUT: %C.ref: type = name_ref C, file.%C.decl [template = constants.%C]
  120. // CHECK:STDOUT: %a.param: %C = param a, runtime_param0
  121. // CHECK:STDOUT: %a: %C = bind_name a, %a.param
  122. // CHECK:STDOUT: }
  123. // CHECK:STDOUT: %TestAddAssignNonRef.decl: %TestAddAssignNonRef.type = fn_decl @TestAddAssignNonRef [template = constants.%TestAddAssignNonRef] {
  124. // CHECK:STDOUT: %a.patt: %C = binding_pattern a
  125. // CHECK:STDOUT: %b.patt: %C = binding_pattern b
  126. // CHECK:STDOUT: } {
  127. // CHECK:STDOUT: %C.ref.loc33_27: type = name_ref C, file.%C.decl [template = constants.%C]
  128. // CHECK:STDOUT: %a.param: %C = param a, runtime_param0
  129. // CHECK:STDOUT: %a: %C = bind_name a, %a.param
  130. // CHECK:STDOUT: %C.ref.loc33_33: type = name_ref C, file.%C.decl [template = constants.%C]
  131. // CHECK:STDOUT: %b.param: %C = param b, runtime_param1
  132. // CHECK:STDOUT: %b: %C = bind_name b, %b.param
  133. // CHECK:STDOUT: }
  134. // CHECK:STDOUT: }
  135. // CHECK:STDOUT:
  136. // CHECK:STDOUT: interface @Inc {
  137. // CHECK:STDOUT: !members:
  138. // CHECK:STDOUT: .Self = imports.%import_ref.2
  139. // CHECK:STDOUT: .Op = imports.%import_ref.3
  140. // CHECK:STDOUT: witness = (imports.%import_ref.4)
  141. // CHECK:STDOUT: }
  142. // CHECK:STDOUT:
  143. // CHECK:STDOUT: interface @AddAssign {
  144. // CHECK:STDOUT: !members:
  145. // CHECK:STDOUT: .Self = imports.%import_ref.6
  146. // CHECK:STDOUT: .Op = imports.%import_ref.7
  147. // CHECK:STDOUT: witness = (imports.%import_ref.8)
  148. // CHECK:STDOUT: }
  149. // CHECK:STDOUT:
  150. // CHECK:STDOUT: impl @impl.1: %C as %.3 {
  151. // CHECK:STDOUT: %Op.decl: %Op.type.1 = fn_decl @Op.1 [template = constants.%Op.1] {
  152. // CHECK:STDOUT: %self.patt: %.4 = binding_pattern self
  153. // CHECK:STDOUT: } {
  154. // CHECK:STDOUT: %C.ref: type = name_ref C, file.%C.decl [template = constants.%C]
  155. // CHECK:STDOUT: %.loc16_21: type = ptr_type %C [template = constants.%.4]
  156. // CHECK:STDOUT: %self.param: %.4 = param self, runtime_param0
  157. // CHECK:STDOUT: %self: %.4 = bind_name self, %self.param
  158. // CHECK:STDOUT: %.loc16_9: %.4 = addr_pattern %self
  159. // CHECK:STDOUT: }
  160. // CHECK:STDOUT: %.loc15: <witness> = interface_witness (%Op.decl) [template = constants.%.7]
  161. // CHECK:STDOUT:
  162. // CHECK:STDOUT: !members:
  163. // CHECK:STDOUT: .Op = %Op.decl
  164. // CHECK:STDOUT: witness = %.loc15
  165. // CHECK:STDOUT: }
  166. // CHECK:STDOUT:
  167. // CHECK:STDOUT: impl @impl.2: %C as %.8 {
  168. // CHECK:STDOUT: %Op.decl: %Op.type.3 = fn_decl @Op.3 [template = constants.%Op.3] {
  169. // CHECK:STDOUT: %self.patt: %.4 = binding_pattern self
  170. // CHECK:STDOUT: %other.patt: %C = binding_pattern other
  171. // CHECK:STDOUT: } {
  172. // CHECK:STDOUT: %C.ref.loc19_20: type = name_ref C, file.%C.decl [template = constants.%C]
  173. // CHECK:STDOUT: %.loc19_21: type = ptr_type %C [template = constants.%.4]
  174. // CHECK:STDOUT: %self.param: %.4 = param self, runtime_param0
  175. // CHECK:STDOUT: %self: %.4 = bind_name self, %self.param
  176. // CHECK:STDOUT: %.loc19_9: %.4 = addr_pattern %self
  177. // CHECK:STDOUT: %C.ref.loc19_31: type = name_ref C, file.%C.decl [template = constants.%C]
  178. // CHECK:STDOUT: %other.param: %C = param other, runtime_param1
  179. // CHECK:STDOUT: %other: %C = bind_name other, %other.param
  180. // CHECK:STDOUT: }
  181. // CHECK:STDOUT: %.loc18: <witness> = interface_witness (%Op.decl) [template = constants.%.10]
  182. // CHECK:STDOUT:
  183. // CHECK:STDOUT: !members:
  184. // CHECK:STDOUT: .Op = %Op.decl
  185. // CHECK:STDOUT: witness = %.loc18
  186. // CHECK:STDOUT: }
  187. // CHECK:STDOUT:
  188. // CHECK:STDOUT: class @C {
  189. // CHECK:STDOUT: %.loc13: <witness> = complete_type_witness %.1 [template = constants.%.2]
  190. // CHECK:STDOUT:
  191. // CHECK:STDOUT: !members:
  192. // CHECK:STDOUT: .Self = constants.%C
  193. // CHECK:STDOUT: }
  194. // CHECK:STDOUT:
  195. // CHECK:STDOUT: fn @Op.1[addr %self: %.4]();
  196. // CHECK:STDOUT:
  197. // CHECK:STDOUT: generic fn @Op.2(constants.%Self.1: %.3) {
  198. // CHECK:STDOUT: %Self: %.3 = bind_symbolic_name Self 0 [symbolic = %Self (constants.%Self.1)]
  199. // CHECK:STDOUT: %.2: type = ptr_type @Op.2.%Self (%Self.1) [symbolic = %.2 (constants.%.6)]
  200. // CHECK:STDOUT:
  201. // CHECK:STDOUT: fn[addr %self: @Op.2.%.2 (%.6)]();
  202. // CHECK:STDOUT: }
  203. // CHECK:STDOUT:
  204. // CHECK:STDOUT: fn @Op.3[addr %self: %.4](%other: %C);
  205. // CHECK:STDOUT:
  206. // CHECK:STDOUT: generic fn @Op.4(constants.%Self.2: %.8) {
  207. // CHECK:STDOUT: %Self: %.8 = bind_symbolic_name Self 0 [symbolic = %Self (constants.%Self.2)]
  208. // CHECK:STDOUT: %.2: type = ptr_type @Op.4.%Self (%Self.2) [symbolic = %.2 (constants.%.9)]
  209. // CHECK:STDOUT:
  210. // CHECK:STDOUT: fn[addr %self: @Op.4.%.2 (%.9)](%other: @Op.4.%Self (%Self.2));
  211. // CHECK:STDOUT: }
  212. // CHECK:STDOUT:
  213. // CHECK:STDOUT: fn @TestIncNonRef(%a: %C) {
  214. // CHECK:STDOUT: !entry:
  215. // CHECK:STDOUT: %a.ref: %C = name_ref a, %a
  216. // CHECK:STDOUT: %Op.ref: %.12 = name_ref Op, imports.%import_ref.3 [template = constants.%.13]
  217. // CHECK:STDOUT: %.loc30_3.1: %Op.type.2 = interface_witness_access @impl.1.%.loc15, element0 [template = constants.%Op.1]
  218. // CHECK:STDOUT: %.loc30_3.2: <bound method> = bound_method %a.ref, %.loc30_3.1
  219. // CHECK:STDOUT: %Op.call: init %.5 = call %.loc30_3.2(<invalid>) [template = <error>]
  220. // CHECK:STDOUT: return
  221. // CHECK:STDOUT: }
  222. // CHECK:STDOUT:
  223. // CHECK:STDOUT: fn @TestAddAssignNonRef(%a: %C, %b: %C) {
  224. // CHECK:STDOUT: !entry:
  225. // CHECK:STDOUT: %a.ref: %C = name_ref a, %a
  226. // CHECK:STDOUT: %b.ref: %C = name_ref b, %b
  227. // CHECK:STDOUT: %Op.ref: %.14 = name_ref Op, imports.%import_ref.7 [template = constants.%.15]
  228. // CHECK:STDOUT: %.loc40_5.1: %Op.type.4 = interface_witness_access @impl.2.%.loc18, element0 [template = constants.%Op.3]
  229. // CHECK:STDOUT: %.loc40_5.2: <bound method> = bound_method %a.ref, %.loc40_5.1
  230. // CHECK:STDOUT: %Op.call: init %.5 = call %.loc40_5.2(<invalid>) [template = <error>]
  231. // CHECK:STDOUT: return
  232. // CHECK:STDOUT: }
  233. // CHECK:STDOUT:
  234. // CHECK:STDOUT: specific @Op.2(constants.%Self.1) {
  235. // CHECK:STDOUT: %Self => constants.%Self.1
  236. // CHECK:STDOUT: %.2 => constants.%.6
  237. // CHECK:STDOUT: }
  238. // CHECK:STDOUT:
  239. // CHECK:STDOUT: specific @Op.2(constants.%C) {
  240. // CHECK:STDOUT: %Self => constants.%C
  241. // CHECK:STDOUT: %.2 => constants.%.4
  242. // CHECK:STDOUT: }
  243. // CHECK:STDOUT:
  244. // CHECK:STDOUT: specific @Op.4(constants.%Self.2) {
  245. // CHECK:STDOUT: %Self => constants.%Self.2
  246. // CHECK:STDOUT: %.2 => constants.%.9
  247. // CHECK:STDOUT: }
  248. // CHECK:STDOUT:
  249. // CHECK:STDOUT: specific @Op.4(constants.%C) {
  250. // CHECK:STDOUT: %Self => constants.%C
  251. // CHECK:STDOUT: %.2 => constants.%.4
  252. // CHECK:STDOUT: }
  253. // CHECK:STDOUT: