fail_assign_non_ref.carbon 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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: 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: 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: type = interface_type @Inc [template]
  43. // CHECK:STDOUT: %Self.1: %.2 = bind_symbolic_name Self 0 [symbolic]
  44. // CHECK:STDOUT: %.3: type = ptr_type %C [template]
  45. // CHECK:STDOUT: %Op.type.1: type = fn_type @Op.1 [template]
  46. // CHECK:STDOUT: %.4: type = tuple_type () [template]
  47. // CHECK:STDOUT: %Op.1: %Op.type.1 = struct_value () [template]
  48. // CHECK:STDOUT: %.5: type = ptr_type %Self.1 [symbolic]
  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: <witness> = interface_witness (%Op.1) [template]
  52. // CHECK:STDOUT: %.7: type = interface_type @AddAssign [template]
  53. // CHECK:STDOUT: %Self.2: %.7 = bind_symbolic_name Self 0 [symbolic]
  54. // CHECK:STDOUT: %Op.type.3: type = fn_type @Op.3 [template]
  55. // CHECK:STDOUT: %Op.3: %Op.type.3 = struct_value () [template]
  56. // CHECK:STDOUT: %.8: type = ptr_type %Self.2 [symbolic]
  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: <witness> = interface_witness (%Op.3) [template]
  60. // CHECK:STDOUT: %TestIncNonRef.type: type = fn_type @TestIncNonRef [template]
  61. // CHECK:STDOUT: %TestIncNonRef: %TestIncNonRef.type = struct_value () [template]
  62. // CHECK:STDOUT: %.10: type = ptr_type %.1 [template]
  63. // CHECK:STDOUT: %.11: type = assoc_entity_type @Inc, %Op.type.2 [template]
  64. // CHECK:STDOUT: %.12: %.11 = assoc_entity element0, file.%import_ref.10 [template]
  65. // CHECK:STDOUT: %TestAddAssignNonRef.type: type = fn_type @TestAddAssignNonRef [template]
  66. // CHECK:STDOUT: %TestAddAssignNonRef: %TestAddAssignNonRef.type = struct_value () [template]
  67. // CHECK:STDOUT: %.13: type = assoc_entity_type @AddAssign, %Op.type.4 [template]
  68. // CHECK:STDOUT: %.14: %.13 = assoc_entity element0, file.%import_ref.12 [template]
  69. // CHECK:STDOUT: }
  70. // CHECK:STDOUT:
  71. // CHECK:STDOUT: file {
  72. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  73. // CHECK:STDOUT: .Core = %Core
  74. // CHECK:STDOUT: .C = %C.decl
  75. // CHECK:STDOUT: .TestIncNonRef = %TestIncNonRef.decl
  76. // CHECK:STDOUT: .TestAddAssignNonRef = %TestAddAssignNonRef.decl
  77. // CHECK:STDOUT: }
  78. // CHECK:STDOUT: %Core: <namespace> = namespace [template] {}
  79. // CHECK:STDOUT: %C.decl: type = class_decl @C [template = constants.%C] {}
  80. // CHECK:STDOUT: %import_ref.1: type = import_ref ir4, inst+49, loaded [template = constants.%.2]
  81. // CHECK:STDOUT: %import_ref.2 = import_ref ir4, inst+51, unloaded
  82. // CHECK:STDOUT: %import_ref.3: %.11 = import_ref ir4, inst+65, loaded [template = constants.%.12]
  83. // CHECK:STDOUT: %import_ref.4: %Op.type.2 = import_ref ir4, inst+61, loaded [template = constants.%Op.2]
  84. // CHECK:STDOUT: impl_decl @impl.1 {
  85. // CHECK:STDOUT: %C.ref.loc15: type = name_ref C, %C.decl [template = constants.%C]
  86. // CHECK:STDOUT: %Core.ref.loc15: <namespace> = name_ref Core, %Core [template = %Core]
  87. // CHECK:STDOUT: %Inc.ref: type = name_ref Inc, %import_ref.1 [template = constants.%.2]
  88. // CHECK:STDOUT: }
  89. // CHECK:STDOUT: %import_ref.5: type = import_ref ir4, inst+26, loaded [template = constants.%.7]
  90. // CHECK:STDOUT: %import_ref.6 = import_ref ir4, inst+28, unloaded
  91. // CHECK:STDOUT: %import_ref.7: %.13 = import_ref ir4, inst+47, loaded [template = constants.%.14]
  92. // CHECK:STDOUT: %import_ref.8: %Op.type.4 = import_ref ir4, inst+43, loaded [template = constants.%Op.4]
  93. // CHECK:STDOUT: impl_decl @impl.2 {
  94. // CHECK:STDOUT: %C.ref.loc18: type = name_ref C, %C.decl [template = constants.%C]
  95. // CHECK:STDOUT: %Core.ref.loc18: <namespace> = name_ref Core, %Core [template = %Core]
  96. // CHECK:STDOUT: %AddAssign.ref: type = name_ref AddAssign, %import_ref.5 [template = constants.%.7]
  97. // CHECK:STDOUT: }
  98. // CHECK:STDOUT: %TestIncNonRef.decl: %TestIncNonRef.type = fn_decl @TestIncNonRef [template = constants.%TestIncNonRef] {
  99. // CHECK:STDOUT: %C.ref.loc22: type = name_ref C, %C.decl [template = constants.%C]
  100. // CHECK:STDOUT: %a.loc22_18.1: %C = param a
  101. // CHECK:STDOUT: @TestIncNonRef.%a: %C = bind_name a, %a.loc22_18.1
  102. // CHECK:STDOUT: }
  103. // CHECK:STDOUT: %import_ref.9: type = import_ref ir4, inst+49, loaded [template = constants.%.2]
  104. // CHECK:STDOUT: %import_ref.10 = import_ref ir4, inst+61, unloaded
  105. // CHECK:STDOUT: %TestAddAssignNonRef.decl: %TestAddAssignNonRef.type = fn_decl @TestAddAssignNonRef [template = constants.%TestAddAssignNonRef] {
  106. // CHECK:STDOUT: %C.ref.loc33_27: type = name_ref C, %C.decl [template = constants.%C]
  107. // CHECK:STDOUT: %a.loc33_24.1: %C = param a
  108. // CHECK:STDOUT: @TestAddAssignNonRef.%a: %C = bind_name a, %a.loc33_24.1
  109. // CHECK:STDOUT: %C.ref.loc33_33: type = name_ref C, %C.decl [template = constants.%C]
  110. // CHECK:STDOUT: %b.loc33_30.1: %C = param b
  111. // CHECK:STDOUT: @TestAddAssignNonRef.%b: %C = bind_name b, %b.loc33_30.1
  112. // CHECK:STDOUT: }
  113. // CHECK:STDOUT: %import_ref.11: type = import_ref ir4, inst+26, loaded [template = constants.%.7]
  114. // CHECK:STDOUT: %import_ref.12 = import_ref ir4, inst+43, unloaded
  115. // CHECK:STDOUT: }
  116. // CHECK:STDOUT:
  117. // CHECK:STDOUT: interface @Inc {
  118. // CHECK:STDOUT: !members:
  119. // CHECK:STDOUT: .Self = file.%import_ref.2
  120. // CHECK:STDOUT: .Op = file.%import_ref.3
  121. // CHECK:STDOUT: witness = (file.%import_ref.4)
  122. // CHECK:STDOUT: }
  123. // CHECK:STDOUT:
  124. // CHECK:STDOUT: interface @AddAssign {
  125. // CHECK:STDOUT: !members:
  126. // CHECK:STDOUT: .Self = file.%import_ref.6
  127. // CHECK:STDOUT: .Op = file.%import_ref.7
  128. // CHECK:STDOUT: witness = (file.%import_ref.8)
  129. // CHECK:STDOUT: }
  130. // CHECK:STDOUT:
  131. // CHECK:STDOUT: impl @impl.1: %C as %.2 {
  132. // CHECK:STDOUT: %Op.decl: %Op.type.1 = fn_decl @Op.1 [template = constants.%Op.1] {
  133. // CHECK:STDOUT: %C.ref: type = name_ref C, file.%C.decl [template = constants.%C]
  134. // CHECK:STDOUT: %.loc16_21: type = ptr_type %C [template = constants.%.3]
  135. // CHECK:STDOUT: %self.loc16_14.1: %.3 = param self
  136. // CHECK:STDOUT: %self.loc16_14.3: %.3 = bind_name self, %self.loc16_14.1
  137. // CHECK:STDOUT: %.loc16_9: %.3 = addr_pattern %self.loc16_14.3
  138. // CHECK:STDOUT: }
  139. // CHECK:STDOUT: %.1: <witness> = interface_witness (%Op.decl) [template = constants.%.6]
  140. // CHECK:STDOUT:
  141. // CHECK:STDOUT: !members:
  142. // CHECK:STDOUT: .Op = %Op.decl
  143. // CHECK:STDOUT: witness = %.1
  144. // CHECK:STDOUT: }
  145. // CHECK:STDOUT:
  146. // CHECK:STDOUT: impl @impl.2: %C as %.7 {
  147. // CHECK:STDOUT: %Op.decl: %Op.type.3 = fn_decl @Op.3 [template = constants.%Op.3] {
  148. // CHECK:STDOUT: %C.ref.loc19_20: type = name_ref C, file.%C.decl [template = constants.%C]
  149. // CHECK:STDOUT: %.loc19_21: type = ptr_type %C [template = constants.%.3]
  150. // CHECK:STDOUT: %self.loc19_14.1: %.3 = param self
  151. // CHECK:STDOUT: %self.loc19_14.3: %.3 = bind_name self, %self.loc19_14.1
  152. // CHECK:STDOUT: %.loc19_9: %.3 = addr_pattern %self.loc19_14.3
  153. // CHECK:STDOUT: %C.ref.loc19_31: type = name_ref C, file.%C.decl [template = constants.%C]
  154. // CHECK:STDOUT: %other.loc19_24.1: %C = param other
  155. // CHECK:STDOUT: %other.loc19_24.2: %C = bind_name other, %other.loc19_24.1
  156. // CHECK:STDOUT: }
  157. // CHECK:STDOUT: %.1: <witness> = interface_witness (%Op.decl) [template = constants.%.9]
  158. // CHECK:STDOUT:
  159. // CHECK:STDOUT: !members:
  160. // CHECK:STDOUT: .Op = %Op.decl
  161. // CHECK:STDOUT: witness = %.1
  162. // CHECK:STDOUT: }
  163. // CHECK:STDOUT:
  164. // CHECK:STDOUT: class @C {
  165. // CHECK:STDOUT: !members:
  166. // CHECK:STDOUT: .Self = constants.%C
  167. // CHECK:STDOUT: }
  168. // CHECK:STDOUT:
  169. // CHECK:STDOUT: fn @Op.1[addr @impl.1.%self.loc16_14.3: %.3]();
  170. // CHECK:STDOUT:
  171. // CHECK:STDOUT: fn @Op.2[addr %self: %.5]();
  172. // CHECK:STDOUT:
  173. // CHECK:STDOUT: fn @Op.3[addr @impl.2.%self.loc19_14.3: %.3](@impl.2.%other.loc19_24.2: %C);
  174. // CHECK:STDOUT:
  175. // CHECK:STDOUT: fn @Op.4[addr %self: %.8](%other: %Self.2);
  176. // CHECK:STDOUT:
  177. // CHECK:STDOUT: fn @TestIncNonRef(%a: %C) {
  178. // CHECK:STDOUT: !entry:
  179. // CHECK:STDOUT: %a.ref: %C = name_ref a, %a
  180. // CHECK:STDOUT: %.1: %Op.type.2 = interface_witness_access @impl.1.%.1, element0 [template = constants.%Op.1]
  181. // CHECK:STDOUT: %.loc30: <bound method> = bound_method %a.ref, %.1
  182. // CHECK:STDOUT: %Op.call: init %.4 = call %.loc30(<invalid>) [template = <error>]
  183. // CHECK:STDOUT: return
  184. // CHECK:STDOUT: }
  185. // CHECK:STDOUT:
  186. // CHECK:STDOUT: fn @TestAddAssignNonRef(%a: %C, %b: %C) {
  187. // CHECK:STDOUT: !entry:
  188. // CHECK:STDOUT: %a.ref: %C = name_ref a, %a
  189. // CHECK:STDOUT: %b.ref: %C = name_ref b, %b
  190. // CHECK:STDOUT: %.1: %Op.type.4 = interface_witness_access @impl.2.%.1, element0 [template = constants.%Op.3]
  191. // CHECK:STDOUT: %.loc40: <bound method> = bound_method %a.ref, %.1
  192. // CHECK:STDOUT: %Op.call: init %.4 = call %.loc40(<invalid>) [template = <error>]
  193. // CHECK:STDOUT: return
  194. // CHECK:STDOUT: }
  195. // CHECK:STDOUT: