fail_assign_non_ref.carbon 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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. // TODO: Add ranges and switch to "--dump-sem-ir-ranges=only".
  6. // EXTRA-ARGS: --dump-sem-ir-ranges=if-present
  7. //
  8. // AUTOUPDATE
  9. // TIP: To test this file alone, run:
  10. // TIP: bazel test //toolchain/testing:file_test --test_arg=--file_tests=toolchain/check/testdata/operators/overloaded/fail_assign_non_ref.carbon
  11. // TIP: To dump output, run:
  12. // TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/operators/overloaded/fail_assign_non_ref.carbon
  13. package User;
  14. class C {};
  15. impl C as Core.Inc {
  16. fn Op[addr self: C*]();
  17. }
  18. impl C as Core.AddAssignWith(C) {
  19. fn Op[addr self: C*](other: C);
  20. }
  21. fn TestIncNonRef(a: C) {
  22. // CHECK:STDERR: fail_assign_non_ref.carbon:[[@LINE+7]]:5: error: `addr self` method cannot be invoked on a value [AddrSelfIsNonRef]
  23. // CHECK:STDERR: ++a;
  24. // CHECK:STDERR: ^
  25. // CHECK:STDERR: fail_assign_non_ref.carbon:[[@LINE-10]]:9: note: initializing function parameter [InCallToFunctionParam]
  26. // CHECK:STDERR: fn Op[addr self: C*]();
  27. // CHECK:STDERR: ^~~~~~~~~~~~~
  28. // CHECK:STDERR:
  29. ++a;
  30. }
  31. fn TestAddAssignNonRef(a: C, b: C) {
  32. // CHECK:STDERR: fail_assign_non_ref.carbon:[[@LINE+7]]:3: error: `addr self` method cannot be invoked on a value [AddrSelfIsNonRef]
  33. // CHECK:STDERR: a += b;
  34. // CHECK:STDERR: ^
  35. // CHECK:STDERR: fail_assign_non_ref.carbon:[[@LINE-18]]:9: note: initializing function parameter [InCallToFunctionParam]
  36. // CHECK:STDERR: fn Op[addr self: C*](other: C);
  37. // CHECK:STDERR: ^~~~~~~~~~~~~
  38. // CHECK:STDERR:
  39. a += b;
  40. }
  41. // CHECK:STDOUT: --- fail_assign_non_ref.carbon
  42. // CHECK:STDOUT:
  43. // CHECK:STDOUT: constants {
  44. // CHECK:STDOUT: %C: type = class_type @C [concrete]
  45. // CHECK:STDOUT: %empty_struct_type: type = struct_type {} [concrete]
  46. // CHECK:STDOUT: %complete_type.357: <witness> = complete_type_witness %empty_struct_type [concrete]
  47. // CHECK:STDOUT: %Inc.type: type = facet_type <@Inc> [concrete]
  48. // CHECK:STDOUT: %Op.type.e3a: type = fn_type @Op.1 [concrete]
  49. // CHECK:STDOUT: %empty_tuple.type: type = tuple_type () [concrete]
  50. // CHECK:STDOUT: %pattern_type.f6d: type = pattern_type auto [concrete]
  51. // CHECK:STDOUT: %Inc.impl_witness: <witness> = impl_witness file.%Inc.impl_witness_table [concrete]
  52. // CHECK:STDOUT: %ptr.019: type = ptr_type %C [concrete]
  53. // CHECK:STDOUT: %pattern_type.44a: type = pattern_type %ptr.019 [concrete]
  54. // CHECK:STDOUT: %Op.type.73a: type = fn_type @Op.loc19 [concrete]
  55. // CHECK:STDOUT: %Op.0c9: %Op.type.73a = struct_value () [concrete]
  56. // CHECK:STDOUT: %Inc.facet: %Inc.type = facet_value %C, (%Inc.impl_witness) [concrete]
  57. // CHECK:STDOUT: %AddAssignWith.type.fc6: type = generic_interface_type @AddAssignWith [concrete]
  58. // CHECK:STDOUT: %AddAssignWith.generic: %AddAssignWith.type.fc6 = struct_value () [concrete]
  59. // CHECK:STDOUT: %AddAssignWith.type.393: type = facet_type <@AddAssignWith, @AddAssignWith(%C)> [concrete]
  60. // CHECK:STDOUT: %Op.type.5af: type = fn_type @Op.2, @AddAssignWith(%C) [concrete]
  61. // CHECK:STDOUT: %AddAssignWith.impl_witness: <witness> = impl_witness file.%AddAssignWith.impl_witness_table [concrete]
  62. // CHECK:STDOUT: %pattern_type.c48: type = pattern_type %C [concrete]
  63. // CHECK:STDOUT: %Op.type.0d7: type = fn_type @Op.loc22 [concrete]
  64. // CHECK:STDOUT: %Op.572: %Op.type.0d7 = struct_value () [concrete]
  65. // CHECK:STDOUT: %AddAssignWith.facet: %AddAssignWith.type.393 = facet_value %C, (%AddAssignWith.impl_witness) [concrete]
  66. // CHECK:STDOUT: %TestIncNonRef.type: type = fn_type @TestIncNonRef [concrete]
  67. // CHECK:STDOUT: %TestIncNonRef: %TestIncNonRef.type = struct_value () [concrete]
  68. // CHECK:STDOUT: %.bc7: type = fn_type_with_self_type %Op.type.e3a, %Inc.facet [concrete]
  69. // CHECK:STDOUT: %Destroy.type: type = facet_type <@Destroy> [concrete]
  70. // CHECK:STDOUT: %Op.type.153: type = fn_type @Op.9, @Destroy.impl(%C) [concrete]
  71. // CHECK:STDOUT: %Op.5d7: %Op.type.153 = struct_value () [concrete]
  72. // CHECK:STDOUT: %Op.specific_fn: <specific function> = specific_function %Op.5d7, @Op.9(%C) [concrete]
  73. // CHECK:STDOUT: %TestAddAssignNonRef.type: type = fn_type @TestAddAssignNonRef [concrete]
  74. // CHECK:STDOUT: %TestAddAssignNonRef: %TestAddAssignNonRef.type = struct_value () [concrete]
  75. // CHECK:STDOUT: %.e2d: type = fn_type_with_self_type %Op.type.5af, %AddAssignWith.facet [concrete]
  76. // CHECK:STDOUT: }
  77. // CHECK:STDOUT:
  78. // CHECK:STDOUT: imports {
  79. // CHECK:STDOUT: %Core: <namespace> = namespace file.%Core.import, [concrete] {
  80. // CHECK:STDOUT: .Inc = %Core.Inc
  81. // CHECK:STDOUT: .AddAssignWith = %Core.AddAssignWith
  82. // CHECK:STDOUT: .Destroy = %Core.Destroy
  83. // CHECK:STDOUT: import Core//prelude
  84. // CHECK:STDOUT: import Core//prelude/...
  85. // CHECK:STDOUT: }
  86. // CHECK:STDOUT: %Core.Inc: type = import_ref Core//prelude/operators/arithmetic, Inc, loaded [concrete = constants.%Inc.type]
  87. // CHECK:STDOUT: %Core.AddAssignWith: %AddAssignWith.type.fc6 = import_ref Core//prelude/operators/arithmetic, AddAssignWith, loaded [concrete = constants.%AddAssignWith.generic]
  88. // CHECK:STDOUT: %Core.Destroy: type = import_ref Core//prelude/destroy, Destroy, loaded [concrete = constants.%Destroy.type]
  89. // CHECK:STDOUT: }
  90. // CHECK:STDOUT:
  91. // CHECK:STDOUT: file {
  92. // CHECK:STDOUT: package: <namespace> = namespace [concrete] {
  93. // CHECK:STDOUT: .Core = imports.%Core
  94. // CHECK:STDOUT: .C = %C.decl
  95. // CHECK:STDOUT: .TestIncNonRef = %TestIncNonRef.decl
  96. // CHECK:STDOUT: .TestAddAssignNonRef = %TestAddAssignNonRef.decl
  97. // CHECK:STDOUT: }
  98. // CHECK:STDOUT: %Core.import = import Core
  99. // CHECK:STDOUT: %C.decl: type = class_decl @C [concrete = constants.%C] {} {}
  100. // CHECK:STDOUT: impl_decl @Inc.impl [concrete] {} {
  101. // CHECK:STDOUT: %C.ref: type = name_ref C, file.%C.decl [concrete = constants.%C]
  102. // CHECK:STDOUT: %Core.ref: <namespace> = name_ref Core, imports.%Core [concrete = imports.%Core]
  103. // CHECK:STDOUT: %Inc.ref: type = name_ref Inc, imports.%Core.Inc [concrete = constants.%Inc.type]
  104. // CHECK:STDOUT: }
  105. // CHECK:STDOUT: %Inc.impl_witness_table = impl_witness_table (@Inc.impl.%Op.decl), @Inc.impl [concrete]
  106. // CHECK:STDOUT: %Inc.impl_witness: <witness> = impl_witness %Inc.impl_witness_table [concrete = constants.%Inc.impl_witness]
  107. // CHECK:STDOUT: impl_decl @AddAssignWith.impl [concrete] {} {
  108. // CHECK:STDOUT: %C.ref.loc21_6: type = name_ref C, file.%C.decl [concrete = constants.%C]
  109. // CHECK:STDOUT: %Core.ref: <namespace> = name_ref Core, imports.%Core [concrete = imports.%Core]
  110. // CHECK:STDOUT: %AddAssignWith.ref: %AddAssignWith.type.fc6 = name_ref AddAssignWith, imports.%Core.AddAssignWith [concrete = constants.%AddAssignWith.generic]
  111. // CHECK:STDOUT: %C.ref.loc21_30: type = name_ref C, file.%C.decl [concrete = constants.%C]
  112. // CHECK:STDOUT: %AddAssignWith.type: type = facet_type <@AddAssignWith, @AddAssignWith(constants.%C)> [concrete = constants.%AddAssignWith.type.393]
  113. // CHECK:STDOUT: }
  114. // CHECK:STDOUT: %AddAssignWith.impl_witness_table = impl_witness_table (@AddAssignWith.impl.%Op.decl), @AddAssignWith.impl [concrete]
  115. // CHECK:STDOUT: %AddAssignWith.impl_witness: <witness> = impl_witness %AddAssignWith.impl_witness_table [concrete = constants.%AddAssignWith.impl_witness]
  116. // CHECK:STDOUT: %TestIncNonRef.decl: %TestIncNonRef.type = fn_decl @TestIncNonRef [concrete = constants.%TestIncNonRef] {
  117. // CHECK:STDOUT: %a.patt: %pattern_type.c48 = binding_pattern a [concrete]
  118. // CHECK:STDOUT: %a.param_patt: %pattern_type.c48 = value_param_pattern %a.patt, call_param0 [concrete]
  119. // CHECK:STDOUT: } {
  120. // CHECK:STDOUT: %a.param: %C = value_param call_param0
  121. // CHECK:STDOUT: %C.ref: type = name_ref C, file.%C.decl [concrete = constants.%C]
  122. // CHECK:STDOUT: %a: %C = bind_name a, %a.param
  123. // CHECK:STDOUT: }
  124. // CHECK:STDOUT: %TestAddAssignNonRef.decl: %TestAddAssignNonRef.type = fn_decl @TestAddAssignNonRef [concrete = constants.%TestAddAssignNonRef] {
  125. // CHECK:STDOUT: %a.patt: %pattern_type.c48 = binding_pattern a [concrete]
  126. // CHECK:STDOUT: %a.param_patt: %pattern_type.c48 = value_param_pattern %a.patt, call_param0 [concrete]
  127. // CHECK:STDOUT: %b.patt: %pattern_type.c48 = binding_pattern b [concrete]
  128. // CHECK:STDOUT: %b.param_patt: %pattern_type.c48 = value_param_pattern %b.patt, call_param1 [concrete]
  129. // CHECK:STDOUT: } {
  130. // CHECK:STDOUT: %a.param: %C = value_param call_param0
  131. // CHECK:STDOUT: %C.ref.loc36_27: type = name_ref C, file.%C.decl [concrete = constants.%C]
  132. // CHECK:STDOUT: %a: %C = bind_name a, %a.param
  133. // CHECK:STDOUT: %b.param: %C = value_param call_param1
  134. // CHECK:STDOUT: %C.ref.loc36_33: type = name_ref C, file.%C.decl [concrete = constants.%C]
  135. // CHECK:STDOUT: %b: %C = bind_name b, %b.param
  136. // CHECK:STDOUT: }
  137. // CHECK:STDOUT: }
  138. // CHECK:STDOUT:
  139. // CHECK:STDOUT: impl @Inc.impl: %C.ref as %Inc.ref {
  140. // CHECK:STDOUT: %Op.decl: %Op.type.73a = fn_decl @Op.loc19 [concrete = constants.%Op.0c9] {
  141. // CHECK:STDOUT: %self.patt: %pattern_type.44a = binding_pattern self [concrete]
  142. // CHECK:STDOUT: %self.param_patt: %pattern_type.44a = value_param_pattern %self.patt, call_param0 [concrete]
  143. // CHECK:STDOUT: %.loc19_9: %pattern_type.f6d = addr_pattern %self.param_patt [concrete]
  144. // CHECK:STDOUT: } {
  145. // CHECK:STDOUT: %self.param: %ptr.019 = value_param call_param0
  146. // CHECK:STDOUT: %.loc19_21: type = splice_block %ptr [concrete = constants.%ptr.019] {
  147. // CHECK:STDOUT: %C.ref: type = name_ref C, file.%C.decl [concrete = constants.%C]
  148. // CHECK:STDOUT: %ptr: type = ptr_type %C.ref [concrete = constants.%ptr.019]
  149. // CHECK:STDOUT: }
  150. // CHECK:STDOUT: %self: %ptr.019 = bind_name self, %self.param
  151. // CHECK:STDOUT: }
  152. // CHECK:STDOUT:
  153. // CHECK:STDOUT: !members:
  154. // CHECK:STDOUT: .C = <poisoned>
  155. // CHECK:STDOUT: .Op = %Op.decl
  156. // CHECK:STDOUT: witness = file.%Inc.impl_witness
  157. // CHECK:STDOUT: }
  158. // CHECK:STDOUT:
  159. // CHECK:STDOUT: impl @AddAssignWith.impl: %C.ref.loc21_6 as %AddAssignWith.type {
  160. // CHECK:STDOUT: %Op.decl: %Op.type.0d7 = fn_decl @Op.loc22 [concrete = constants.%Op.572] {
  161. // CHECK:STDOUT: %self.patt: %pattern_type.44a = binding_pattern self [concrete]
  162. // CHECK:STDOUT: %self.param_patt: %pattern_type.44a = value_param_pattern %self.patt, call_param0 [concrete]
  163. // CHECK:STDOUT: %.loc22_9: %pattern_type.f6d = addr_pattern %self.param_patt [concrete]
  164. // CHECK:STDOUT: %other.patt: %pattern_type.c48 = binding_pattern other [concrete]
  165. // CHECK:STDOUT: %other.param_patt: %pattern_type.c48 = value_param_pattern %other.patt, call_param1 [concrete]
  166. // CHECK:STDOUT: } {
  167. // CHECK:STDOUT: %self.param: %ptr.019 = value_param call_param0
  168. // CHECK:STDOUT: %.loc22_21: type = splice_block %ptr [concrete = constants.%ptr.019] {
  169. // CHECK:STDOUT: %C.ref.loc22_20: type = name_ref C, file.%C.decl [concrete = constants.%C]
  170. // CHECK:STDOUT: %ptr: type = ptr_type %C.ref.loc22_20 [concrete = constants.%ptr.019]
  171. // CHECK:STDOUT: }
  172. // CHECK:STDOUT: %self: %ptr.019 = bind_name self, %self.param
  173. // CHECK:STDOUT: %other.param: %C = value_param call_param1
  174. // CHECK:STDOUT: %C.ref.loc22_31: type = name_ref C, file.%C.decl [concrete = constants.%C]
  175. // CHECK:STDOUT: %other: %C = bind_name other, %other.param
  176. // CHECK:STDOUT: }
  177. // CHECK:STDOUT:
  178. // CHECK:STDOUT: !members:
  179. // CHECK:STDOUT: .C = <poisoned>
  180. // CHECK:STDOUT: .Op = %Op.decl
  181. // CHECK:STDOUT: witness = file.%AddAssignWith.impl_witness
  182. // CHECK:STDOUT: }
  183. // CHECK:STDOUT:
  184. // CHECK:STDOUT: class @C {
  185. // CHECK:STDOUT: %empty_struct_type: type = struct_type {} [concrete = constants.%empty_struct_type]
  186. // CHECK:STDOUT: %complete_type: <witness> = complete_type_witness %empty_struct_type [concrete = constants.%complete_type.357]
  187. // CHECK:STDOUT: complete_type_witness = %complete_type
  188. // CHECK:STDOUT:
  189. // CHECK:STDOUT: !members:
  190. // CHECK:STDOUT: .Self = constants.%C
  191. // CHECK:STDOUT: }
  192. // CHECK:STDOUT:
  193. // CHECK:STDOUT: fn @Op.loc19(%self.param: %ptr.019);
  194. // CHECK:STDOUT:
  195. // CHECK:STDOUT: fn @Op.loc22(%self.param: %ptr.019, %other.param: %C);
  196. // CHECK:STDOUT:
  197. // CHECK:STDOUT: fn @TestIncNonRef(%a.param: %C) {
  198. // CHECK:STDOUT: !entry:
  199. // CHECK:STDOUT: %a.ref: %C = name_ref a, %a
  200. // CHECK:STDOUT: %impl.elem0: %.bc7 = impl_witness_access constants.%Inc.impl_witness, element0 [concrete = constants.%Op.0c9]
  201. // CHECK:STDOUT: %bound_method.loc33_3: <bound method> = bound_method %a.ref, %impl.elem0
  202. // CHECK:STDOUT: %.loc33: ref %C = temporary_storage
  203. // CHECK:STDOUT: %addr.loc33_5.1: %ptr.019 = addr_of %.loc33
  204. // CHECK:STDOUT: %Op.call: init %empty_tuple.type = call %bound_method.loc33_3(%addr.loc33_5.1)
  205. // CHECK:STDOUT: %Op.bound: <bound method> = bound_method %.loc33, constants.%Op.5d7
  206. // CHECK:STDOUT: %Op.specific_fn: <specific function> = specific_function constants.%Op.5d7, @Op.9(constants.%C) [concrete = constants.%Op.specific_fn]
  207. // CHECK:STDOUT: %bound_method.loc33_5: <bound method> = bound_method %.loc33, %Op.specific_fn
  208. // CHECK:STDOUT: %addr.loc33_5.2: %ptr.019 = addr_of %.loc33
  209. // CHECK:STDOUT: %no_op: init %empty_tuple.type = call %bound_method.loc33_5(%addr.loc33_5.2)
  210. // CHECK:STDOUT: return
  211. // CHECK:STDOUT: }
  212. // CHECK:STDOUT:
  213. // CHECK:STDOUT: fn @TestAddAssignNonRef(%a.param: %C, %b.param: %C) {
  214. // CHECK:STDOUT: !entry:
  215. // CHECK:STDOUT: %a.ref: %C = name_ref a, %a
  216. // CHECK:STDOUT: %b.ref: %C = name_ref b, %b
  217. // CHECK:STDOUT: %impl.elem0: %.e2d = impl_witness_access constants.%AddAssignWith.impl_witness, element0 [concrete = constants.%Op.572]
  218. // CHECK:STDOUT: %bound_method.loc44_5: <bound method> = bound_method %a.ref, %impl.elem0
  219. // CHECK:STDOUT: %.loc44: ref %C = temporary_storage
  220. // CHECK:STDOUT: %addr.loc44_3.1: %ptr.019 = addr_of %.loc44
  221. // CHECK:STDOUT: %Op.call: init %empty_tuple.type = call %bound_method.loc44_5(%addr.loc44_3.1, %b.ref)
  222. // CHECK:STDOUT: %Op.bound: <bound method> = bound_method %.loc44, constants.%Op.5d7
  223. // CHECK:STDOUT: %Op.specific_fn: <specific function> = specific_function constants.%Op.5d7, @Op.9(constants.%C) [concrete = constants.%Op.specific_fn]
  224. // CHECK:STDOUT: %bound_method.loc44_3: <bound method> = bound_method %.loc44, %Op.specific_fn
  225. // CHECK:STDOUT: %addr.loc44_3.2: %ptr.019 = addr_of %.loc44
  226. // CHECK:STDOUT: %no_op: init %empty_tuple.type = call %bound_method.loc44_3(%addr.loc44_3.2)
  227. // CHECK:STDOUT: return
  228. // CHECK:STDOUT: }
  229. // CHECK:STDOUT: