fail_assign_non_ref.carbon 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. // INCLUDE-FILE: toolchain/testing/testdata/min_prelude/full.carbon
  6. // TODO: Add ranges and switch to "--dump-sem-ir-ranges=only".
  7. // EXTRA-ARGS: --dump-sem-ir-ranges=if-present
  8. //
  9. // AUTOUPDATE
  10. // TIP: To test this file alone, run:
  11. // TIP: bazel test //toolchain/testing:file_test --test_arg=--file_tests=toolchain/check/testdata/operators/overloaded/fail_assign_non_ref.carbon
  12. // TIP: To dump output, run:
  13. // TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/operators/overloaded/fail_assign_non_ref.carbon
  14. package User;
  15. class C {};
  16. impl C as Core.Inc {
  17. fn Op[ref self: C]();
  18. }
  19. impl C as Core.AddAssignWith(C) {
  20. fn Op[ref self: C](other: C);
  21. }
  22. fn TestIncNonRef(a: C) {
  23. // CHECK:STDERR: fail_assign_non_ref.carbon:[[@LINE+7]]:5: error: value expression passed to reference parameter [ValueForRefParam]
  24. // CHECK:STDERR: ++a;
  25. // CHECK:STDERR: ^
  26. // CHECK:STDERR: fail_assign_non_ref.carbon:[[@LINE-10]]:9: note: initializing function parameter [InCallToFunctionParam]
  27. // CHECK:STDERR: fn Op[ref self: C]();
  28. // CHECK:STDERR: ^~~~~~~~~~~
  29. // CHECK:STDERR:
  30. ++a;
  31. }
  32. fn TestAddAssignNonRef(a: C, b: C) {
  33. // CHECK:STDERR: fail_assign_non_ref.carbon:[[@LINE+7]]:3: error: value expression passed to reference parameter [ValueForRefParam]
  34. // CHECK:STDERR: a += b;
  35. // CHECK:STDERR: ^
  36. // CHECK:STDERR: fail_assign_non_ref.carbon:[[@LINE-18]]:9: note: initializing function parameter [InCallToFunctionParam]
  37. // CHECK:STDERR: fn Op[ref self: C](other: C);
  38. // CHECK:STDERR: ^~~~~~~~~~~
  39. // CHECK:STDERR:
  40. a += b;
  41. }
  42. // CHECK:STDOUT: --- fail_assign_non_ref.carbon
  43. // CHECK:STDOUT:
  44. // CHECK:STDOUT: constants {
  45. // CHECK:STDOUT: %C: type = class_type @C [concrete]
  46. // CHECK:STDOUT: %empty_struct_type: type = struct_type {} [concrete]
  47. // CHECK:STDOUT: %complete_type: <witness> = complete_type_witness %empty_struct_type [concrete]
  48. // CHECK:STDOUT: %Inc.type: type = facet_type <@Inc> [concrete]
  49. // CHECK:STDOUT: %Inc.impl_witness: <witness> = impl_witness @C.as.Inc.impl.%Inc.impl_witness_table [concrete]
  50. // CHECK:STDOUT: %pattern_type.476: type = pattern_type %C [concrete]
  51. // CHECK:STDOUT: %C.as.Inc.impl.Op.type: type = fn_type @C.as.Inc.impl.Op [concrete]
  52. // CHECK:STDOUT: %empty_tuple.type: type = tuple_type () [concrete]
  53. // CHECK:STDOUT: %C.as.Inc.impl.Op: %C.as.Inc.impl.Op.type = struct_value () [concrete]
  54. // CHECK:STDOUT: %Inc.facet: %Inc.type = facet_value %C, (%Inc.impl_witness) [concrete]
  55. // CHECK:STDOUT: %Inc.WithSelf.Op.type.700: type = fn_type @Inc.WithSelf.Op, @Inc.WithSelf(%Inc.facet) [concrete]
  56. // CHECK:STDOUT: %AddAssignWith.type.fc6: type = generic_interface_type @AddAssignWith [concrete]
  57. // CHECK:STDOUT: %AddAssignWith.generic: %AddAssignWith.type.fc6 = struct_value () [concrete]
  58. // CHECK:STDOUT: %AddAssignWith.type.16b: type = facet_type <@AddAssignWith, @AddAssignWith(%C)> [concrete]
  59. // CHECK:STDOUT: %AddAssignWith.impl_witness: <witness> = impl_witness @C.as.AddAssignWith.impl.%AddAssignWith.impl_witness_table [concrete]
  60. // CHECK:STDOUT: %C.as.AddAssignWith.impl.Op.type: type = fn_type @C.as.AddAssignWith.impl.Op [concrete]
  61. // CHECK:STDOUT: %C.as.AddAssignWith.impl.Op: %C.as.AddAssignWith.impl.Op.type = struct_value () [concrete]
  62. // CHECK:STDOUT: %AddAssignWith.facet: %AddAssignWith.type.16b = facet_value %C, (%AddAssignWith.impl_witness) [concrete]
  63. // CHECK:STDOUT: %AddAssignWith.WithSelf.Op.type.2b1: type = fn_type @AddAssignWith.WithSelf.Op, @AddAssignWith.WithSelf(%C, %AddAssignWith.facet) [concrete]
  64. // CHECK:STDOUT: %TestIncNonRef.type: type = fn_type @TestIncNonRef [concrete]
  65. // CHECK:STDOUT: %TestIncNonRef: %TestIncNonRef.type = struct_value () [concrete]
  66. // CHECK:STDOUT: %.38a: type = fn_type_with_self_type %Inc.WithSelf.Op.type.700, %Inc.facet [concrete]
  67. // CHECK:STDOUT: %TestAddAssignNonRef.type: type = fn_type @TestAddAssignNonRef [concrete]
  68. // CHECK:STDOUT: %TestAddAssignNonRef: %TestAddAssignNonRef.type = struct_value () [concrete]
  69. // CHECK:STDOUT: %.168: type = fn_type_with_self_type %AddAssignWith.WithSelf.Op.type.2b1, %AddAssignWith.facet [concrete]
  70. // CHECK:STDOUT: }
  71. // CHECK:STDOUT:
  72. // CHECK:STDOUT: imports {
  73. // CHECK:STDOUT: %Core: <namespace> = namespace file.%Core.import, [concrete] {
  74. // CHECK:STDOUT: .Inc = %Core.Inc
  75. // CHECK:STDOUT: .AddAssignWith = %Core.AddAssignWith
  76. // CHECK:STDOUT: import Core//prelude
  77. // CHECK:STDOUT: import Core//prelude/...
  78. // CHECK:STDOUT: }
  79. // CHECK:STDOUT: %Core.Inc: type = import_ref Core//prelude/operators/arithmetic, Inc, loaded [concrete = constants.%Inc.type]
  80. // CHECK:STDOUT: %Core.AddAssignWith: %AddAssignWith.type.fc6 = import_ref Core//prelude/operators/arithmetic, AddAssignWith, loaded [concrete = constants.%AddAssignWith.generic]
  81. // CHECK:STDOUT: }
  82. // CHECK:STDOUT:
  83. // CHECK:STDOUT: file {
  84. // CHECK:STDOUT: package: <namespace> = namespace [concrete] {
  85. // CHECK:STDOUT: .Core = imports.%Core
  86. // CHECK:STDOUT: .C = %C.decl
  87. // CHECK:STDOUT: .TestIncNonRef = %TestIncNonRef.decl
  88. // CHECK:STDOUT: .TestAddAssignNonRef = %TestAddAssignNonRef.decl
  89. // CHECK:STDOUT: }
  90. // CHECK:STDOUT: %Core.import = import Core
  91. // CHECK:STDOUT: %C.decl: type = class_decl @C [concrete = constants.%C] {} {}
  92. // CHECK:STDOUT: impl_decl @C.as.Inc.impl [concrete] {} {
  93. // CHECK:STDOUT: %C.ref: type = name_ref C, file.%C.decl [concrete = constants.%C]
  94. // CHECK:STDOUT: %Core.ref: <namespace> = name_ref Core, imports.%Core [concrete = imports.%Core]
  95. // CHECK:STDOUT: %Inc.ref: type = name_ref Inc, imports.%Core.Inc [concrete = constants.%Inc.type]
  96. // CHECK:STDOUT: }
  97. // CHECK:STDOUT: impl_decl @C.as.AddAssignWith.impl [concrete] {} {
  98. // CHECK:STDOUT: %C.ref.loc22_6: type = name_ref C, file.%C.decl [concrete = constants.%C]
  99. // CHECK:STDOUT: %Core.ref: <namespace> = name_ref Core, imports.%Core [concrete = imports.%Core]
  100. // CHECK:STDOUT: %AddAssignWith.ref: %AddAssignWith.type.fc6 = name_ref AddAssignWith, imports.%Core.AddAssignWith [concrete = constants.%AddAssignWith.generic]
  101. // CHECK:STDOUT: %C.ref.loc22_30: type = name_ref C, file.%C.decl [concrete = constants.%C]
  102. // CHECK:STDOUT: %AddAssignWith.type: type = facet_type <@AddAssignWith, @AddAssignWith(constants.%C)> [concrete = constants.%AddAssignWith.type.16b]
  103. // CHECK:STDOUT: }
  104. // CHECK:STDOUT: %TestIncNonRef.decl: %TestIncNonRef.type = fn_decl @TestIncNonRef [concrete = constants.%TestIncNonRef] {
  105. // CHECK:STDOUT: %a.param_patt: %pattern_type.476 = value_param_pattern [concrete]
  106. // CHECK:STDOUT: %a.patt: %pattern_type.476 = at_binding_pattern a, %a.param_patt [concrete]
  107. // CHECK:STDOUT: } {
  108. // CHECK:STDOUT: %a.param: %C = value_param call_param0
  109. // CHECK:STDOUT: %C.ref: type = name_ref C, file.%C.decl [concrete = constants.%C]
  110. // CHECK:STDOUT: %a: %C = value_binding a, %a.param
  111. // CHECK:STDOUT: }
  112. // CHECK:STDOUT: %TestAddAssignNonRef.decl: %TestAddAssignNonRef.type = fn_decl @TestAddAssignNonRef [concrete = constants.%TestAddAssignNonRef] {
  113. // CHECK:STDOUT: %a.param_patt: %pattern_type.476 = value_param_pattern [concrete]
  114. // CHECK:STDOUT: %a.patt: %pattern_type.476 = at_binding_pattern a, %a.param_patt [concrete]
  115. // CHECK:STDOUT: %b.param_patt: %pattern_type.476 = value_param_pattern [concrete]
  116. // CHECK:STDOUT: %b.patt: %pattern_type.476 = at_binding_pattern b, %b.param_patt [concrete]
  117. // CHECK:STDOUT: } {
  118. // CHECK:STDOUT: %a.param: %C = value_param call_param0
  119. // CHECK:STDOUT: %C.ref.loc37_27: type = name_ref C, file.%C.decl [concrete = constants.%C]
  120. // CHECK:STDOUT: %a: %C = value_binding a, %a.param
  121. // CHECK:STDOUT: %b.param: %C = value_param call_param1
  122. // CHECK:STDOUT: %C.ref.loc37_33: type = name_ref C, file.%C.decl [concrete = constants.%C]
  123. // CHECK:STDOUT: %b: %C = value_binding b, %b.param
  124. // CHECK:STDOUT: }
  125. // CHECK:STDOUT: }
  126. // CHECK:STDOUT:
  127. // CHECK:STDOUT: impl @C.as.Inc.impl: %C.ref as %Inc.ref {
  128. // CHECK:STDOUT: %C.as.Inc.impl.Op.decl: %C.as.Inc.impl.Op.type = fn_decl @C.as.Inc.impl.Op [concrete = constants.%C.as.Inc.impl.Op] {
  129. // CHECK:STDOUT: %self.param_patt: %pattern_type.476 = ref_param_pattern [concrete]
  130. // CHECK:STDOUT: %self.patt: %pattern_type.476 = at_binding_pattern self, %self.param_patt [concrete]
  131. // CHECK:STDOUT: } {
  132. // CHECK:STDOUT: %self.param: ref %C = ref_param call_param0
  133. // CHECK:STDOUT: %C.ref: type = name_ref C, file.%C.decl [concrete = constants.%C]
  134. // CHECK:STDOUT: %self: ref %C = ref_binding self, %self.param
  135. // CHECK:STDOUT: }
  136. // CHECK:STDOUT: %Inc.impl_witness_table = impl_witness_table (%C.as.Inc.impl.Op.decl), @C.as.Inc.impl [concrete]
  137. // CHECK:STDOUT: %Inc.impl_witness: <witness> = impl_witness %Inc.impl_witness_table [concrete = constants.%Inc.impl_witness]
  138. // CHECK:STDOUT:
  139. // CHECK:STDOUT: !members:
  140. // CHECK:STDOUT: .C = <poisoned>
  141. // CHECK:STDOUT: .Op = %C.as.Inc.impl.Op.decl
  142. // CHECK:STDOUT: witness = %Inc.impl_witness
  143. // CHECK:STDOUT: }
  144. // CHECK:STDOUT:
  145. // CHECK:STDOUT: impl @C.as.AddAssignWith.impl: %C.ref.loc22_6 as %AddAssignWith.type {
  146. // CHECK:STDOUT: %C.as.AddAssignWith.impl.Op.decl: %C.as.AddAssignWith.impl.Op.type = fn_decl @C.as.AddAssignWith.impl.Op [concrete = constants.%C.as.AddAssignWith.impl.Op] {
  147. // CHECK:STDOUT: %self.param_patt: %pattern_type.476 = ref_param_pattern [concrete]
  148. // CHECK:STDOUT: %self.patt: %pattern_type.476 = at_binding_pattern self, %self.param_patt [concrete]
  149. // CHECK:STDOUT: %other.param_patt: %pattern_type.476 = value_param_pattern [concrete]
  150. // CHECK:STDOUT: %other.patt: %pattern_type.476 = at_binding_pattern other, %other.param_patt [concrete]
  151. // CHECK:STDOUT: } {
  152. // CHECK:STDOUT: %self.param: ref %C = ref_param call_param0
  153. // CHECK:STDOUT: %C.ref.loc23_19: type = name_ref C, file.%C.decl [concrete = constants.%C]
  154. // CHECK:STDOUT: %self: ref %C = ref_binding self, %self.param
  155. // CHECK:STDOUT: %other.param: %C = value_param call_param1
  156. // CHECK:STDOUT: %C.ref.loc23_29: type = name_ref C, file.%C.decl [concrete = constants.%C]
  157. // CHECK:STDOUT: %other: %C = value_binding other, %other.param
  158. // CHECK:STDOUT: }
  159. // CHECK:STDOUT: %AddAssignWith.impl_witness_table = impl_witness_table (%C.as.AddAssignWith.impl.Op.decl), @C.as.AddAssignWith.impl [concrete]
  160. // CHECK:STDOUT: %AddAssignWith.impl_witness: <witness> = impl_witness %AddAssignWith.impl_witness_table [concrete = constants.%AddAssignWith.impl_witness]
  161. // CHECK:STDOUT:
  162. // CHECK:STDOUT: !members:
  163. // CHECK:STDOUT: .C = <poisoned>
  164. // CHECK:STDOUT: .Op = %C.as.AddAssignWith.impl.Op.decl
  165. // CHECK:STDOUT: witness = %AddAssignWith.impl_witness
  166. // CHECK:STDOUT: }
  167. // CHECK:STDOUT:
  168. // CHECK:STDOUT: class @C {
  169. // CHECK:STDOUT: %complete_type: <witness> = complete_type_witness constants.%empty_struct_type [concrete = constants.%complete_type]
  170. // CHECK:STDOUT: complete_type_witness = %complete_type
  171. // CHECK:STDOUT:
  172. // CHECK:STDOUT: !members:
  173. // CHECK:STDOUT: .Self = constants.%C
  174. // CHECK:STDOUT: }
  175. // CHECK:STDOUT:
  176. // CHECK:STDOUT: fn @C.as.Inc.impl.Op(%self.param: ref %C);
  177. // CHECK:STDOUT:
  178. // CHECK:STDOUT: fn @C.as.AddAssignWith.impl.Op(%self.param: ref %C, %other.param: %C);
  179. // CHECK:STDOUT:
  180. // CHECK:STDOUT: fn @TestIncNonRef(%a.param: %C) {
  181. // CHECK:STDOUT: !entry:
  182. // CHECK:STDOUT: %a.ref: %C = name_ref a, %a
  183. // CHECK:STDOUT: %impl.elem0: %.38a = impl_witness_access constants.%Inc.impl_witness, element0 [concrete = constants.%C.as.Inc.impl.Op]
  184. // CHECK:STDOUT: %bound_method: <bound method> = bound_method %a.ref, %impl.elem0
  185. // CHECK:STDOUT: %C.as.Inc.impl.Op.call: init %empty_tuple.type = call %bound_method(<error>)
  186. // CHECK:STDOUT: return
  187. // CHECK:STDOUT: }
  188. // CHECK:STDOUT:
  189. // CHECK:STDOUT: fn @TestAddAssignNonRef(%a.param: %C, %b.param: %C) {
  190. // CHECK:STDOUT: !entry:
  191. // CHECK:STDOUT: %a.ref: %C = name_ref a, %a
  192. // CHECK:STDOUT: %b.ref: %C = name_ref b, %b
  193. // CHECK:STDOUT: %impl.elem0: %.168 = impl_witness_access constants.%AddAssignWith.impl_witness, element0 [concrete = constants.%C.as.AddAssignWith.impl.Op]
  194. // CHECK:STDOUT: %bound_method: <bound method> = bound_method %a.ref, %impl.elem0
  195. // CHECK:STDOUT: %C.as.AddAssignWith.impl.Op.call: init %empty_tuple.type = call %bound_method(<error>, %b.ref)
  196. // CHECK:STDOUT: return
  197. // CHECK:STDOUT: }
  198. // CHECK:STDOUT: