fail_assign_non_ref.carbon 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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]]:5: error: `addr self` method cannot be invoked on a value [AddrSelfIsNonRef]
  20. // CHECK:STDERR: ++a;
  21. // CHECK:STDERR: ^
  22. // CHECK:STDERR: fail_assign_non_ref.carbon:[[@LINE-10]]:9: note: initializing function parameter [InCallToFunctionParam]
  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+7]]:3: error: `addr self` method cannot be invoked on a value [AddrSelfIsNonRef]
  30. // CHECK:STDERR: a += b;
  31. // CHECK:STDERR: ^
  32. // CHECK:STDERR: fail_assign_non_ref.carbon:[[@LINE-18]]:9: note: initializing function parameter [InCallToFunctionParam]
  33. // CHECK:STDERR: fn Op[addr self: C*](other: C);
  34. // CHECK:STDERR: ^~~~~~~~~~~~~
  35. // CHECK:STDERR:
  36. a += b;
  37. }
  38. // CHECK:STDOUT: --- fail_assign_non_ref.carbon
  39. // CHECK:STDOUT:
  40. // CHECK:STDOUT: constants {
  41. // CHECK:STDOUT: %C: type = class_type @C [concrete]
  42. // CHECK:STDOUT: %empty_struct_type: type = struct_type {} [concrete]
  43. // CHECK:STDOUT: %complete_type: <witness> = complete_type_witness %empty_struct_type [concrete]
  44. // CHECK:STDOUT: %Inc.type: type = facet_type <@Inc> [concrete]
  45. // CHECK:STDOUT: %Op.type.e3a: type = fn_type @Op.1 [concrete]
  46. // CHECK:STDOUT: %empty_tuple.type: type = tuple_type () [concrete]
  47. // CHECK:STDOUT: %pattern_type.f6d: type = pattern_type auto [concrete]
  48. // CHECK:STDOUT: %Inc.impl_witness: <witness> = impl_witness file.%Inc.impl_witness_table [concrete]
  49. // CHECK:STDOUT: %ptr.019: type = ptr_type %C [concrete]
  50. // CHECK:STDOUT: %pattern_type.44a: type = pattern_type %ptr.019 [concrete]
  51. // CHECK:STDOUT: %Op.type.73a: type = fn_type @Op.2 [concrete]
  52. // CHECK:STDOUT: %Op.0c9: %Op.type.73a = struct_value () [concrete]
  53. // CHECK:STDOUT: %Inc.facet: %Inc.type = facet_value %C, (%Inc.impl_witness) [concrete]
  54. // CHECK:STDOUT: %AddAssign.type: type = facet_type <@AddAssign> [concrete]
  55. // CHECK:STDOUT: %Op.type.421: type = fn_type @Op.3 [concrete]
  56. // CHECK:STDOUT: %AddAssign.impl_witness: <witness> = impl_witness file.%AddAssign.impl_witness_table [concrete]
  57. // CHECK:STDOUT: %pattern_type.c48: type = pattern_type %C [concrete]
  58. // CHECK:STDOUT: %Op.type.0b8: type = fn_type @Op.4 [concrete]
  59. // CHECK:STDOUT: %Op.d8e: %Op.type.0b8 = struct_value () [concrete]
  60. // CHECK:STDOUT: %AddAssign.facet: %AddAssign.type = facet_value %C, (%AddAssign.impl_witness) [concrete]
  61. // CHECK:STDOUT: %TestIncNonRef.type: type = fn_type @TestIncNonRef [concrete]
  62. // CHECK:STDOUT: %TestIncNonRef: %TestIncNonRef.type = struct_value () [concrete]
  63. // CHECK:STDOUT: %.bc7: type = fn_type_with_self_type %Op.type.e3a, %Inc.facet [concrete]
  64. // CHECK:STDOUT: %TestAddAssignNonRef.type: type = fn_type @TestAddAssignNonRef [concrete]
  65. // CHECK:STDOUT: %TestAddAssignNonRef: %TestAddAssignNonRef.type = struct_value () [concrete]
  66. // CHECK:STDOUT: %.6d3: type = fn_type_with_self_type %Op.type.421, %AddAssign.facet [concrete]
  67. // CHECK:STDOUT: }
  68. // CHECK:STDOUT:
  69. // CHECK:STDOUT: imports {
  70. // CHECK:STDOUT: %Core: <namespace> = namespace file.%Core.import, [concrete] {
  71. // CHECK:STDOUT: .Inc = %Core.Inc
  72. // CHECK:STDOUT: .AddAssign = %Core.AddAssign
  73. // CHECK:STDOUT: import Core//prelude
  74. // CHECK:STDOUT: import Core//prelude/...
  75. // CHECK:STDOUT: }
  76. // CHECK:STDOUT: %Core.Inc: type = import_ref Core//prelude/operators/arithmetic, Inc, loaded [concrete = constants.%Inc.type]
  77. // CHECK:STDOUT: %Core.AddAssign: type = import_ref Core//prelude/operators/arithmetic, AddAssign, loaded [concrete = constants.%AddAssign.type]
  78. // CHECK:STDOUT: }
  79. // CHECK:STDOUT:
  80. // CHECK:STDOUT: file {
  81. // CHECK:STDOUT: package: <namespace> = namespace [concrete] {
  82. // CHECK:STDOUT: .Core = imports.%Core
  83. // CHECK:STDOUT: .C = %C.decl
  84. // CHECK:STDOUT: .TestIncNonRef = %TestIncNonRef.decl
  85. // CHECK:STDOUT: .TestAddAssignNonRef = %TestAddAssignNonRef.decl
  86. // CHECK:STDOUT: }
  87. // CHECK:STDOUT: %Core.import = import Core
  88. // CHECK:STDOUT: %C.decl: type = class_decl @C [concrete = constants.%C] {} {}
  89. // CHECK:STDOUT: impl_decl @impl.c51 [concrete] {} {
  90. // CHECK:STDOUT: %C.ref: type = name_ref C, file.%C.decl [concrete = constants.%C]
  91. // CHECK:STDOUT: %Core.ref: <namespace> = name_ref Core, imports.%Core [concrete = imports.%Core]
  92. // CHECK:STDOUT: %Inc.ref: type = name_ref Inc, imports.%Core.Inc [concrete = constants.%Inc.type]
  93. // CHECK:STDOUT: }
  94. // CHECK:STDOUT: %Inc.impl_witness_table = impl_witness_table (@impl.c51.%Op.decl), @impl.c51 [concrete]
  95. // CHECK:STDOUT: %Inc.impl_witness: <witness> = impl_witness %Inc.impl_witness_table [concrete = constants.%Inc.impl_witness]
  96. // CHECK:STDOUT: impl_decl @impl.14c [concrete] {} {
  97. // CHECK:STDOUT: %C.ref: type = name_ref C, file.%C.decl [concrete = constants.%C]
  98. // CHECK:STDOUT: %Core.ref: <namespace> = name_ref Core, imports.%Core [concrete = imports.%Core]
  99. // CHECK:STDOUT: %AddAssign.ref: type = name_ref AddAssign, imports.%Core.AddAssign [concrete = constants.%AddAssign.type]
  100. // CHECK:STDOUT: }
  101. // CHECK:STDOUT: %AddAssign.impl_witness_table = impl_witness_table (@impl.14c.%Op.decl), @impl.14c [concrete]
  102. // CHECK:STDOUT: %AddAssign.impl_witness: <witness> = impl_witness %AddAssign.impl_witness_table [concrete = constants.%AddAssign.impl_witness]
  103. // CHECK:STDOUT: %TestIncNonRef.decl: %TestIncNonRef.type = fn_decl @TestIncNonRef [concrete = constants.%TestIncNonRef] {
  104. // CHECK:STDOUT: %a.patt: %pattern_type.c48 = binding_pattern a [concrete]
  105. // CHECK:STDOUT: %a.param_patt: %pattern_type.c48 = value_param_pattern %a.patt, call_param0 [concrete]
  106. // CHECK:STDOUT: } {
  107. // CHECK:STDOUT: %a.param: %C = value_param call_param0
  108. // CHECK:STDOUT: %C.ref: type = name_ref C, file.%C.decl [concrete = constants.%C]
  109. // CHECK:STDOUT: %a: %C = bind_name a, %a.param
  110. // CHECK:STDOUT: }
  111. // CHECK:STDOUT: %TestAddAssignNonRef.decl: %TestAddAssignNonRef.type = fn_decl @TestAddAssignNonRef [concrete = constants.%TestAddAssignNonRef] {
  112. // CHECK:STDOUT: %a.patt: %pattern_type.c48 = binding_pattern a [concrete]
  113. // CHECK:STDOUT: %a.param_patt: %pattern_type.c48 = value_param_pattern %a.patt, call_param0 [concrete]
  114. // CHECK:STDOUT: %b.patt: %pattern_type.c48 = binding_pattern b [concrete]
  115. // CHECK:STDOUT: %b.param_patt: %pattern_type.c48 = value_param_pattern %b.patt, call_param1 [concrete]
  116. // CHECK:STDOUT: } {
  117. // CHECK:STDOUT: %a.param: %C = value_param call_param0
  118. // CHECK:STDOUT: %C.ref.loc33_27: type = name_ref C, file.%C.decl [concrete = constants.%C]
  119. // CHECK:STDOUT: %a: %C = bind_name a, %a.param
  120. // CHECK:STDOUT: %b.param: %C = value_param call_param1
  121. // CHECK:STDOUT: %C.ref.loc33_33: type = name_ref C, file.%C.decl [concrete = constants.%C]
  122. // CHECK:STDOUT: %b: %C = bind_name b, %b.param
  123. // CHECK:STDOUT: }
  124. // CHECK:STDOUT: }
  125. // CHECK:STDOUT:
  126. // CHECK:STDOUT: impl @impl.c51: %C.ref as %Inc.ref {
  127. // CHECK:STDOUT: %Op.decl: %Op.type.73a = fn_decl @Op.2 [concrete = constants.%Op.0c9] {
  128. // CHECK:STDOUT: %self.patt: %pattern_type.44a = binding_pattern self [concrete]
  129. // CHECK:STDOUT: %self.param_patt: %pattern_type.44a = value_param_pattern %self.patt, call_param0 [concrete]
  130. // CHECK:STDOUT: %.loc16_9: %pattern_type.f6d = addr_pattern %self.param_patt [concrete]
  131. // CHECK:STDOUT: } {
  132. // CHECK:STDOUT: %self.param: %ptr.019 = value_param call_param0
  133. // CHECK:STDOUT: %.loc16_21: type = splice_block %ptr [concrete = constants.%ptr.019] {
  134. // CHECK:STDOUT: %C.ref: type = name_ref C, file.%C.decl [concrete = constants.%C]
  135. // CHECK:STDOUT: %ptr: type = ptr_type %C.ref [concrete = constants.%ptr.019]
  136. // CHECK:STDOUT: }
  137. // CHECK:STDOUT: %self: %ptr.019 = bind_name self, %self.param
  138. // CHECK:STDOUT: }
  139. // CHECK:STDOUT:
  140. // CHECK:STDOUT: !members:
  141. // CHECK:STDOUT: .C = <poisoned>
  142. // CHECK:STDOUT: .Op = %Op.decl
  143. // CHECK:STDOUT: witness = file.%Inc.impl_witness
  144. // CHECK:STDOUT: }
  145. // CHECK:STDOUT:
  146. // CHECK:STDOUT: impl @impl.14c: %C.ref as %AddAssign.ref {
  147. // CHECK:STDOUT: %Op.decl: %Op.type.0b8 = fn_decl @Op.4 [concrete = constants.%Op.d8e] {
  148. // CHECK:STDOUT: %self.patt: %pattern_type.44a = binding_pattern self [concrete]
  149. // CHECK:STDOUT: %self.param_patt: %pattern_type.44a = value_param_pattern %self.patt, call_param0 [concrete]
  150. // CHECK:STDOUT: %.loc19_9: %pattern_type.f6d = addr_pattern %self.param_patt [concrete]
  151. // CHECK:STDOUT: %other.patt: %pattern_type.c48 = binding_pattern other [concrete]
  152. // CHECK:STDOUT: %other.param_patt: %pattern_type.c48 = value_param_pattern %other.patt, call_param1 [concrete]
  153. // CHECK:STDOUT: } {
  154. // CHECK:STDOUT: %self.param: %ptr.019 = value_param call_param0
  155. // CHECK:STDOUT: %.loc19_21: type = splice_block %ptr [concrete = constants.%ptr.019] {
  156. // CHECK:STDOUT: %C.ref.loc19_20: type = name_ref C, file.%C.decl [concrete = constants.%C]
  157. // CHECK:STDOUT: %ptr: type = ptr_type %C.ref.loc19_20 [concrete = constants.%ptr.019]
  158. // CHECK:STDOUT: }
  159. // CHECK:STDOUT: %self: %ptr.019 = bind_name self, %self.param
  160. // CHECK:STDOUT: %other.param: %C = value_param call_param1
  161. // CHECK:STDOUT: %C.ref.loc19_31: type = name_ref C, file.%C.decl [concrete = constants.%C]
  162. // CHECK:STDOUT: %other: %C = bind_name other, %other.param
  163. // CHECK:STDOUT: }
  164. // CHECK:STDOUT:
  165. // CHECK:STDOUT: !members:
  166. // CHECK:STDOUT: .C = <poisoned>
  167. // CHECK:STDOUT: .Op = %Op.decl
  168. // CHECK:STDOUT: witness = file.%AddAssign.impl_witness
  169. // CHECK:STDOUT: }
  170. // CHECK:STDOUT:
  171. // CHECK:STDOUT: class @C {
  172. // CHECK:STDOUT: %empty_struct_type: type = struct_type {} [concrete = constants.%empty_struct_type]
  173. // CHECK:STDOUT: %complete_type: <witness> = complete_type_witness %empty_struct_type [concrete = constants.%complete_type]
  174. // CHECK:STDOUT: complete_type_witness = %complete_type
  175. // CHECK:STDOUT:
  176. // CHECK:STDOUT: !members:
  177. // CHECK:STDOUT: .Self = constants.%C
  178. // CHECK:STDOUT: }
  179. // CHECK:STDOUT:
  180. // CHECK:STDOUT: fn @Op.2(%self.param: %ptr.019);
  181. // CHECK:STDOUT:
  182. // CHECK:STDOUT: fn @Op.4(%self.param: %ptr.019, %other.param: %C);
  183. // CHECK:STDOUT:
  184. // CHECK:STDOUT: fn @TestIncNonRef(%a.param: %C) {
  185. // CHECK:STDOUT: !entry:
  186. // CHECK:STDOUT: %a.ref: %C = name_ref a, %a
  187. // CHECK:STDOUT: %impl.elem0: %.bc7 = impl_witness_access constants.%Inc.impl_witness, element0 [concrete = constants.%Op.0c9]
  188. // CHECK:STDOUT: %bound_method: <bound method> = bound_method %a.ref, %impl.elem0
  189. // CHECK:STDOUT: %.loc30: ref %C = temporary_storage
  190. // CHECK:STDOUT: %addr: %ptr.019 = addr_of %.loc30
  191. // CHECK:STDOUT: %Op.call: init %empty_tuple.type = call %bound_method(%addr)
  192. // CHECK:STDOUT: return
  193. // CHECK:STDOUT: }
  194. // CHECK:STDOUT:
  195. // CHECK:STDOUT: fn @TestAddAssignNonRef(%a.param: %C, %b.param: %C) {
  196. // CHECK:STDOUT: !entry:
  197. // CHECK:STDOUT: %a.ref: %C = name_ref a, %a
  198. // CHECK:STDOUT: %b.ref: %C = name_ref b, %b
  199. // CHECK:STDOUT: %impl.elem0: %.6d3 = impl_witness_access constants.%AddAssign.impl_witness, element0 [concrete = constants.%Op.d8e]
  200. // CHECK:STDOUT: %bound_method: <bound method> = bound_method %a.ref, %impl.elem0
  201. // CHECK:STDOUT: %.loc41: ref %C = temporary_storage
  202. // CHECK:STDOUT: %addr: %ptr.019 = addr_of %.loc41
  203. // CHECK:STDOUT: %Op.call: init %empty_tuple.type = call %bound_method(%addr, %b.ref)
  204. // CHECK:STDOUT: return
  205. // CHECK:STDOUT: }
  206. // CHECK:STDOUT: