fail_assign_non_ref.carbon 9.8 KB

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