fail_assign_non_ref.carbon 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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 [template]
  42. // CHECK:STDOUT: %empty_struct_type: type = struct_type {} [template]
  43. // CHECK:STDOUT: %complete_type: <witness> = complete_type_witness %empty_struct_type [template]
  44. // CHECK:STDOUT: %Inc.type: type = facet_type <@Inc> [template]
  45. // CHECK:STDOUT: %empty_tuple.type: type = tuple_type () [template]
  46. // CHECK:STDOUT: %Op.type.e3a: type = fn_type @Op.1 [template]
  47. // CHECK:STDOUT: %impl_witness.ec3: <witness> = impl_witness (@impl.1.%Op.decl) [template]
  48. // CHECK:STDOUT: %ptr.019: type = ptr_type %C [template]
  49. // CHECK:STDOUT: %Op.type.73a: type = fn_type @Op.2 [template]
  50. // CHECK:STDOUT: %Op.0c9: %Op.type.73a = struct_value () [template]
  51. // CHECK:STDOUT: %Inc.facet: %Inc.type = facet_value %C, %impl_witness.ec3 [template]
  52. // CHECK:STDOUT: %AddAssign.type: type = facet_type <@AddAssign> [template]
  53. // CHECK:STDOUT: %Op.type.421: type = fn_type @Op.3 [template]
  54. // CHECK:STDOUT: %impl_witness.95d: <witness> = impl_witness (@impl.2.%Op.decl) [template]
  55. // CHECK:STDOUT: %Op.type.0b8: type = fn_type @Op.4 [template]
  56. // CHECK:STDOUT: %Op.d8e: %Op.type.0b8 = struct_value () [template]
  57. // CHECK:STDOUT: %AddAssign.facet: %AddAssign.type = facet_value %C, %impl_witness.95d [template]
  58. // CHECK:STDOUT: %TestIncNonRef.type: type = fn_type @TestIncNonRef [template]
  59. // CHECK:STDOUT: %TestIncNonRef: %TestIncNonRef.type = struct_value () [template]
  60. // CHECK:STDOUT: %.e6d: type = fn_type_with_self_type %Op.type.e3a, %Inc.facet [template]
  61. // CHECK:STDOUT: %TestAddAssignNonRef.type: type = fn_type @TestAddAssignNonRef [template]
  62. // CHECK:STDOUT: %TestAddAssignNonRef: %TestAddAssignNonRef.type = struct_value () [template]
  63. // CHECK:STDOUT: %.499: type = fn_type_with_self_type %Op.type.421, %AddAssign.facet [template]
  64. // CHECK:STDOUT: }
  65. // CHECK:STDOUT:
  66. // CHECK:STDOUT: imports {
  67. // CHECK:STDOUT: %Core: <namespace> = namespace file.%Core.import, [template] {
  68. // CHECK:STDOUT: .Inc = %Core.Inc
  69. // CHECK:STDOUT: .AddAssign = %Core.AddAssign
  70. // CHECK:STDOUT: import Core//prelude
  71. // CHECK:STDOUT: import Core//prelude/...
  72. // CHECK:STDOUT: }
  73. // CHECK:STDOUT: %Core.Inc: type = import_ref Core//prelude/operators/arithmetic, Inc, loaded [template = constants.%Inc.type]
  74. // CHECK:STDOUT: %Core.AddAssign: type = import_ref Core//prelude/operators/arithmetic, AddAssign, loaded [template = constants.%AddAssign.type]
  75. // CHECK:STDOUT: }
  76. // CHECK:STDOUT:
  77. // CHECK:STDOUT: file {
  78. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  79. // CHECK:STDOUT: .Core = imports.%Core
  80. // CHECK:STDOUT: .C = %C.decl
  81. // CHECK:STDOUT: .TestIncNonRef = %TestIncNonRef.decl
  82. // CHECK:STDOUT: .TestAddAssignNonRef = %TestAddAssignNonRef.decl
  83. // CHECK:STDOUT: }
  84. // CHECK:STDOUT: %Core.import = import Core
  85. // CHECK:STDOUT: %C.decl: type = class_decl @C [template = constants.%C] {} {}
  86. // CHECK:STDOUT: impl_decl @impl.1 [template] {} {
  87. // CHECK:STDOUT: %C.ref: type = name_ref C, file.%C.decl [template = constants.%C]
  88. // CHECK:STDOUT: %Core.ref: <namespace> = name_ref Core, imports.%Core [template = imports.%Core]
  89. // CHECK:STDOUT: %Inc.ref: type = name_ref Inc, imports.%Core.Inc [template = constants.%Inc.type]
  90. // CHECK:STDOUT: }
  91. // CHECK:STDOUT: %impl_witness.loc15: <witness> = impl_witness (@impl.1.%Op.decl) [template = constants.%impl_witness.ec3]
  92. // CHECK:STDOUT: impl_decl @impl.2 [template] {} {
  93. // CHECK:STDOUT: %C.ref: type = name_ref C, file.%C.decl [template = constants.%C]
  94. // CHECK:STDOUT: %Core.ref: <namespace> = name_ref Core, imports.%Core [template = imports.%Core]
  95. // CHECK:STDOUT: %AddAssign.ref: type = name_ref AddAssign, imports.%Core.AddAssign [template = constants.%AddAssign.type]
  96. // CHECK:STDOUT: }
  97. // CHECK:STDOUT: %impl_witness.loc18: <witness> = impl_witness (@impl.2.%Op.decl) [template = constants.%impl_witness.95d]
  98. // CHECK:STDOUT: %TestIncNonRef.decl: %TestIncNonRef.type = fn_decl @TestIncNonRef [template = constants.%TestIncNonRef] {
  99. // CHECK:STDOUT: %a.patt: %C = binding_pattern a
  100. // CHECK:STDOUT: %a.param_patt: %C = value_param_pattern %a.patt, runtime_param0
  101. // CHECK:STDOUT: } {
  102. // CHECK:STDOUT: %a.param: %C = value_param runtime_param0
  103. // CHECK:STDOUT: %C.ref: type = name_ref C, file.%C.decl [template = constants.%C]
  104. // CHECK:STDOUT: %a: %C = bind_name a, %a.param
  105. // CHECK:STDOUT: }
  106. // CHECK:STDOUT: %TestAddAssignNonRef.decl: %TestAddAssignNonRef.type = fn_decl @TestAddAssignNonRef [template = constants.%TestAddAssignNonRef] {
  107. // CHECK:STDOUT: %a.patt: %C = binding_pattern a
  108. // CHECK:STDOUT: %a.param_patt: %C = value_param_pattern %a.patt, runtime_param0
  109. // CHECK:STDOUT: %b.patt: %C = binding_pattern b
  110. // CHECK:STDOUT: %b.param_patt: %C = value_param_pattern %b.patt, runtime_param1
  111. // CHECK:STDOUT: } {
  112. // CHECK:STDOUT: %a.param: %C = value_param runtime_param0
  113. // CHECK:STDOUT: %C.ref.loc33_27: type = name_ref C, file.%C.decl [template = constants.%C]
  114. // CHECK:STDOUT: %a: %C = bind_name a, %a.param
  115. // CHECK:STDOUT: %b.param: %C = value_param runtime_param1
  116. // CHECK:STDOUT: %C.ref.loc33_33: type = name_ref C, file.%C.decl [template = constants.%C]
  117. // CHECK:STDOUT: %b: %C = bind_name b, %b.param
  118. // CHECK:STDOUT: }
  119. // CHECK:STDOUT: }
  120. // CHECK:STDOUT:
  121. // CHECK:STDOUT: impl @impl.1: %C.ref as %Inc.ref {
  122. // CHECK:STDOUT: %Op.decl: %Op.type.73a = fn_decl @Op.2 [template = constants.%Op.0c9] {
  123. // CHECK:STDOUT: %self.patt: %ptr.019 = binding_pattern self
  124. // CHECK:STDOUT: %self.param_patt: %ptr.019 = value_param_pattern %self.patt, runtime_param0
  125. // CHECK:STDOUT: %.loc16_9: auto = addr_pattern %self.param_patt
  126. // CHECK:STDOUT: } {
  127. // CHECK:STDOUT: %self.param: %ptr.019 = value_param runtime_param0
  128. // CHECK:STDOUT: %.loc16_21: type = splice_block %ptr [template = constants.%ptr.019] {
  129. // CHECK:STDOUT: %C.ref: type = name_ref C, file.%C.decl [template = constants.%C]
  130. // CHECK:STDOUT: %ptr: type = ptr_type %C [template = constants.%ptr.019]
  131. // CHECK:STDOUT: }
  132. // CHECK:STDOUT: %self: %ptr.019 = bind_name self, %self.param
  133. // CHECK:STDOUT: }
  134. // CHECK:STDOUT:
  135. // CHECK:STDOUT: !members:
  136. // CHECK:STDOUT: .Op = %Op.decl
  137. // CHECK:STDOUT: witness = file.%impl_witness.loc15
  138. // CHECK:STDOUT: }
  139. // CHECK:STDOUT:
  140. // CHECK:STDOUT: impl @impl.2: %C.ref as %AddAssign.ref {
  141. // CHECK:STDOUT: %Op.decl: %Op.type.0b8 = fn_decl @Op.4 [template = constants.%Op.d8e] {
  142. // CHECK:STDOUT: %self.patt: %ptr.019 = binding_pattern self
  143. // CHECK:STDOUT: %self.param_patt: %ptr.019 = value_param_pattern %self.patt, runtime_param0
  144. // CHECK:STDOUT: %.loc19_9: auto = addr_pattern %self.param_patt
  145. // CHECK:STDOUT: %other.patt: %C = binding_pattern other
  146. // CHECK:STDOUT: %other.param_patt: %C = value_param_pattern %other.patt, runtime_param1
  147. // CHECK:STDOUT: } {
  148. // CHECK:STDOUT: %self.param: %ptr.019 = value_param runtime_param0
  149. // CHECK:STDOUT: %.loc19_21: type = splice_block %ptr [template = constants.%ptr.019] {
  150. // CHECK:STDOUT: %C.ref.loc19_20: type = name_ref C, file.%C.decl [template = constants.%C]
  151. // CHECK:STDOUT: %ptr: type = ptr_type %C [template = constants.%ptr.019]
  152. // CHECK:STDOUT: }
  153. // CHECK:STDOUT: %self: %ptr.019 = bind_name self, %self.param
  154. // CHECK:STDOUT: %other.param: %C = value_param runtime_param1
  155. // CHECK:STDOUT: %C.ref.loc19_31: type = name_ref C, file.%C.decl [template = constants.%C]
  156. // CHECK:STDOUT: %other: %C = bind_name other, %other.param
  157. // CHECK:STDOUT: }
  158. // CHECK:STDOUT:
  159. // CHECK:STDOUT: !members:
  160. // CHECK:STDOUT: .Op = %Op.decl
  161. // CHECK:STDOUT: witness = file.%impl_witness.loc18
  162. // CHECK:STDOUT: }
  163. // CHECK:STDOUT:
  164. // CHECK:STDOUT: class @C {
  165. // CHECK:STDOUT: %complete_type: <witness> = complete_type_witness %empty_struct_type [template = constants.%complete_type]
  166. // CHECK:STDOUT: complete_type_witness = %complete_type
  167. // CHECK:STDOUT:
  168. // CHECK:STDOUT: !members:
  169. // CHECK:STDOUT: .Self = constants.%C
  170. // CHECK:STDOUT: }
  171. // CHECK:STDOUT:
  172. // CHECK:STDOUT: fn @Op.2[addr %self.param_patt: %ptr.019]();
  173. // CHECK:STDOUT:
  174. // CHECK:STDOUT: fn @Op.4[addr %self.param_patt: %ptr.019](%other.param_patt: %C);
  175. // CHECK:STDOUT:
  176. // CHECK:STDOUT: fn @TestIncNonRef(%a.param_patt: %C) {
  177. // CHECK:STDOUT: !entry:
  178. // CHECK:STDOUT: %a.ref: %C = name_ref a, %a
  179. // CHECK:STDOUT: %impl.elem0: %.e6d = impl_witness_access constants.%impl_witness.ec3, element0 [template = constants.%Op.0c9]
  180. // CHECK:STDOUT: %bound_method: <bound method> = bound_method %a.ref, %impl.elem0
  181. // CHECK:STDOUT: %Op.call: init %empty_tuple.type = call %bound_method(<error>)
  182. // CHECK:STDOUT: return
  183. // CHECK:STDOUT: }
  184. // CHECK:STDOUT:
  185. // CHECK:STDOUT: fn @TestAddAssignNonRef(%a.param_patt: %C, %b.param_patt: %C) {
  186. // CHECK:STDOUT: !entry:
  187. // CHECK:STDOUT: %a.ref: %C = name_ref a, %a
  188. // CHECK:STDOUT: %b.ref: %C = name_ref b, %b
  189. // CHECK:STDOUT: %impl.elem0: %.499 = impl_witness_access constants.%impl_witness.95d, element0 [template = constants.%Op.d8e]
  190. // CHECK:STDOUT: %bound_method: <bound method> = bound_method %a.ref, %impl.elem0
  191. // CHECK:STDOUT: %Op.call: init %empty_tuple.type = call %bound_method(<error>, %b.ref)
  192. // CHECK:STDOUT: return
  193. // CHECK:STDOUT: }
  194. // CHECK:STDOUT: