fail_self.carbon 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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/class/fail_self.carbon
  11. // TIP: To dump output, run:
  12. // TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/class/fail_self.carbon
  13. class Class {
  14. // CHECK:STDERR: fail_self.carbon:[[@LINE+4]]:8: error: `self` can only be declared in an implicit parameter list [SelfOutsideImplicitParamList]
  15. // CHECK:STDERR: fn F(self: Self);
  16. // CHECK:STDERR: ^~~~~~~~~~
  17. // CHECK:STDERR:
  18. fn F(self: Self);
  19. fn G() -> Self;
  20. }
  21. // CHECK:STDERR: fail_self.carbon:[[@LINE+4]]:12: error: `self` can only be declared in an implicit parameter list [SelfOutsideImplicitParamList]
  22. // CHECK:STDERR: fn Class.F(self: Self) {
  23. // CHECK:STDERR: ^~~~~~~~~~
  24. // CHECK:STDERR:
  25. fn Class.F(self: Self) {
  26. }
  27. fn Class.G() -> Self {
  28. // CHECK:STDERR: fail_self.carbon:[[@LINE+4]]:7: error: `self` can only be declared in an implicit parameter list [SelfOutsideImplicitParamList]
  29. // CHECK:STDERR: var self: Self;
  30. // CHECK:STDERR: ^~~~~~~~~~
  31. // CHECK:STDERR:
  32. var self: Self;
  33. // CHECK:STDERR: fail_self.carbon:[[@LINE+4]]:10: error: cannot copy value of type `Class` [CopyOfUncopyableType]
  34. // CHECK:STDERR: return self;
  35. // CHECK:STDERR: ^~~~
  36. // CHECK:STDERR:
  37. return self;
  38. }
  39. class WrongSelf {
  40. fn F[self: Class]();
  41. }
  42. fn CallWrongSelf(ws: WrongSelf) {
  43. // CHECK:STDERR: fail_self.carbon:[[@LINE+10]]:3: error: cannot implicitly convert expression of type `WrongSelf` to `Class` [ConversionFailure]
  44. // CHECK:STDERR: ws.F();
  45. // CHECK:STDERR: ^~
  46. // CHECK:STDERR: fail_self.carbon:[[@LINE+7]]:3: note: type `WrongSelf` does not implement interface `Core.ImplicitAs(Class)` [MissingImplInMemberAccessNote]
  47. // CHECK:STDERR: ws.F();
  48. // CHECK:STDERR: ^~
  49. // CHECK:STDERR: fail_self.carbon:[[@LINE-10]]:8: note: initializing function parameter [InCallToFunctionParam]
  50. // CHECK:STDERR: fn F[self: Class]();
  51. // CHECK:STDERR: ^~~~~~~~~~~
  52. // CHECK:STDERR:
  53. ws.F();
  54. }
  55. // CHECK:STDOUT: --- fail_self.carbon
  56. // CHECK:STDOUT:
  57. // CHECK:STDOUT: constants {
  58. // CHECK:STDOUT: %Class: type = class_type @Class [concrete]
  59. // CHECK:STDOUT: %pattern_type.761: type = pattern_type %Class [concrete]
  60. // CHECK:STDOUT: %F.type.f1b: type = fn_type @F.1 [concrete]
  61. // CHECK:STDOUT: %empty_tuple.type: type = tuple_type () [concrete]
  62. // CHECK:STDOUT: %F.1f2: %F.type.f1b = struct_value () [concrete]
  63. // CHECK:STDOUT: %G.type: type = fn_type @G [concrete]
  64. // CHECK:STDOUT: %G: %G.type = struct_value () [concrete]
  65. // CHECK:STDOUT: %empty_struct_type: type = struct_type {} [concrete]
  66. // CHECK:STDOUT: %complete_type: <witness> = complete_type_witness %empty_struct_type [concrete]
  67. // CHECK:STDOUT: %WrongSelf: type = class_type @WrongSelf [concrete]
  68. // CHECK:STDOUT: %F.type.25f: type = fn_type @F.2 [concrete]
  69. // CHECK:STDOUT: %F.3a3: %F.type.25f = struct_value () [concrete]
  70. // CHECK:STDOUT: %pattern_type.381: type = pattern_type %WrongSelf [concrete]
  71. // CHECK:STDOUT: %CallWrongSelf.type: type = fn_type @CallWrongSelf [concrete]
  72. // CHECK:STDOUT: %CallWrongSelf: %CallWrongSelf.type = struct_value () [concrete]
  73. // CHECK:STDOUT: %ImplicitAs.type.cc7: type = generic_interface_type @ImplicitAs [concrete]
  74. // CHECK:STDOUT: %ImplicitAs.generic: %ImplicitAs.type.cc7 = struct_value () [concrete]
  75. // CHECK:STDOUT: }
  76. // CHECK:STDOUT:
  77. // CHECK:STDOUT: imports {
  78. // CHECK:STDOUT: %Core: <namespace> = namespace file.%Core.import, [concrete] {
  79. // CHECK:STDOUT: .ImplicitAs = %Core.ImplicitAs
  80. // CHECK:STDOUT: import Core//prelude
  81. // CHECK:STDOUT: import Core//prelude/...
  82. // CHECK:STDOUT: }
  83. // CHECK:STDOUT: %Core.ImplicitAs: %ImplicitAs.type.cc7 = import_ref Core//prelude/operators/as, ImplicitAs, loaded [concrete = constants.%ImplicitAs.generic]
  84. // CHECK:STDOUT: }
  85. // CHECK:STDOUT:
  86. // CHECK:STDOUT: file {
  87. // CHECK:STDOUT: package: <namespace> = namespace [concrete] {
  88. // CHECK:STDOUT: .Core = imports.%Core
  89. // CHECK:STDOUT: .Class = %Class.decl
  90. // CHECK:STDOUT: .WrongSelf = %WrongSelf.decl
  91. // CHECK:STDOUT: .CallWrongSelf = %CallWrongSelf.decl
  92. // CHECK:STDOUT: }
  93. // CHECK:STDOUT: %Core.import = import Core
  94. // CHECK:STDOUT: %Class.decl: type = class_decl @Class [concrete = constants.%Class] {} {}
  95. // CHECK:STDOUT: %F.decl: %F.type.f1b = fn_decl @F.1 [concrete = constants.%F.1f2] {
  96. // CHECK:STDOUT: %self.patt: %pattern_type.761 = binding_pattern self [concrete]
  97. // CHECK:STDOUT: %self.param_patt: %pattern_type.761 = value_param_pattern %self.patt, call_param0 [concrete]
  98. // CHECK:STDOUT: } {
  99. // CHECK:STDOUT: %self.param.loc28: %Class = value_param call_param0
  100. // CHECK:STDOUT: %Self.ref.loc28: type = name_ref Self, constants.%Class [concrete = constants.%Class]
  101. // CHECK:STDOUT: %self.loc28: %Class = bind_name self, %self.param.loc28
  102. // CHECK:STDOUT: }
  103. // CHECK:STDOUT: %G.decl: %G.type = fn_decl @G [concrete = constants.%G] {
  104. // CHECK:STDOUT: %return.patt: %pattern_type.761 = return_slot_pattern [concrete]
  105. // CHECK:STDOUT: %return.param_patt: %pattern_type.761 = out_param_pattern %return.patt, call_param0 [concrete]
  106. // CHECK:STDOUT: } {
  107. // CHECK:STDOUT: %Self.ref.loc31: type = name_ref Self, constants.%Class [concrete = constants.%Class]
  108. // CHECK:STDOUT: %return.param.loc31: ref %Class = out_param call_param0
  109. // CHECK:STDOUT: %return.loc31: ref %Class = return_slot %return.param.loc31
  110. // CHECK:STDOUT: }
  111. // CHECK:STDOUT: %WrongSelf.decl: type = class_decl @WrongSelf [concrete = constants.%WrongSelf] {} {}
  112. // CHECK:STDOUT: %CallWrongSelf.decl: %CallWrongSelf.type = fn_decl @CallWrongSelf [concrete = constants.%CallWrongSelf] {
  113. // CHECK:STDOUT: %ws.patt: %pattern_type.381 = binding_pattern ws [concrete]
  114. // CHECK:STDOUT: %ws.param_patt: %pattern_type.381 = value_param_pattern %ws.patt, call_param0 [concrete]
  115. // CHECK:STDOUT: } {
  116. // CHECK:STDOUT: %ws.param: %WrongSelf = value_param call_param0
  117. // CHECK:STDOUT: %WrongSelf.ref: type = name_ref WrongSelf, file.%WrongSelf.decl [concrete = constants.%WrongSelf]
  118. // CHECK:STDOUT: %ws: %WrongSelf = bind_name ws, %ws.param
  119. // CHECK:STDOUT: }
  120. // CHECK:STDOUT: }
  121. // CHECK:STDOUT:
  122. // CHECK:STDOUT: class @Class {
  123. // CHECK:STDOUT: %F.decl: %F.type.f1b = fn_decl @F.1 [concrete = constants.%F.1f2] {
  124. // CHECK:STDOUT: %self.patt: %pattern_type.761 = binding_pattern self [concrete]
  125. // CHECK:STDOUT: %self.param_patt: %pattern_type.761 = value_param_pattern %self.patt, call_param0 [concrete]
  126. // CHECK:STDOUT: } {
  127. // CHECK:STDOUT: %self.param.loc19: %Class = value_param call_param0
  128. // CHECK:STDOUT: %Self.ref.loc19: type = name_ref Self, constants.%Class [concrete = constants.%Class]
  129. // CHECK:STDOUT: %self.loc19: %Class = bind_name self, %self.param.loc19
  130. // CHECK:STDOUT: }
  131. // CHECK:STDOUT: %G.decl: %G.type = fn_decl @G [concrete = constants.%G] {
  132. // CHECK:STDOUT: %return.patt: %pattern_type.761 = return_slot_pattern [concrete]
  133. // CHECK:STDOUT: %return.param_patt: %pattern_type.761 = out_param_pattern %return.patt, call_param0 [concrete]
  134. // CHECK:STDOUT: } {
  135. // CHECK:STDOUT: %Self.ref.loc21: type = name_ref Self, constants.%Class [concrete = constants.%Class]
  136. // CHECK:STDOUT: %return.param.loc21: ref %Class = out_param call_param0
  137. // CHECK:STDOUT: %return.loc21: ref %Class = return_slot %return.param.loc21
  138. // CHECK:STDOUT: }
  139. // CHECK:STDOUT: %empty_struct_type: type = struct_type {} [concrete = constants.%empty_struct_type]
  140. // CHECK:STDOUT: %complete_type: <witness> = complete_type_witness %empty_struct_type [concrete = constants.%complete_type]
  141. // CHECK:STDOUT: complete_type_witness = %complete_type
  142. // CHECK:STDOUT:
  143. // CHECK:STDOUT: !members:
  144. // CHECK:STDOUT: .Self = constants.%Class
  145. // CHECK:STDOUT: .F = %F.decl
  146. // CHECK:STDOUT: .G = %G.decl
  147. // CHECK:STDOUT: }
  148. // CHECK:STDOUT:
  149. // CHECK:STDOUT: class @WrongSelf {
  150. // CHECK:STDOUT: %F.decl: %F.type.25f = fn_decl @F.2 [concrete = constants.%F.3a3] {
  151. // CHECK:STDOUT: %self.patt: %pattern_type.761 = binding_pattern self [concrete]
  152. // CHECK:STDOUT: %self.param_patt: %pattern_type.761 = value_param_pattern %self.patt, call_param0 [concrete]
  153. // CHECK:STDOUT: } {
  154. // CHECK:STDOUT: %self.param: %Class = value_param call_param0
  155. // CHECK:STDOUT: %Class.ref: type = name_ref Class, file.%Class.decl [concrete = constants.%Class]
  156. // CHECK:STDOUT: %self: %Class = bind_name self, %self.param
  157. // CHECK:STDOUT: }
  158. // CHECK:STDOUT: %empty_struct_type: type = struct_type {} [concrete = constants.%empty_struct_type]
  159. // CHECK:STDOUT: %complete_type: <witness> = complete_type_witness %empty_struct_type [concrete = constants.%complete_type]
  160. // CHECK:STDOUT: complete_type_witness = %complete_type
  161. // CHECK:STDOUT:
  162. // CHECK:STDOUT: !members:
  163. // CHECK:STDOUT: .Self = constants.%WrongSelf
  164. // CHECK:STDOUT: .Class = <poisoned>
  165. // CHECK:STDOUT: .F = %F.decl
  166. // CHECK:STDOUT: }
  167. // CHECK:STDOUT:
  168. // CHECK:STDOUT: fn @F.1(%self.param.loc28: %Class) {
  169. // CHECK:STDOUT: !entry:
  170. // CHECK:STDOUT: return
  171. // CHECK:STDOUT: }
  172. // CHECK:STDOUT:
  173. // CHECK:STDOUT: fn @G() -> %return.param.loc31: %Class {
  174. // CHECK:STDOUT: !entry:
  175. // CHECK:STDOUT: name_binding_decl {
  176. // CHECK:STDOUT: %self.patt: %pattern_type.761 = binding_pattern self [concrete]
  177. // CHECK:STDOUT: %self.var_patt: %pattern_type.761 = var_pattern %self.patt [concrete]
  178. // CHECK:STDOUT: }
  179. // CHECK:STDOUT: %self.var: ref %Class = var %self.var_patt
  180. // CHECK:STDOUT: %Self.ref.loc36: type = name_ref Self, constants.%Class [concrete = constants.%Class]
  181. // CHECK:STDOUT: %self: ref %Class = bind_name self, %self.var
  182. // CHECK:STDOUT: %self.ref: ref %Class = name_ref self, %self
  183. // CHECK:STDOUT: %.loc41: %Class = bind_value %self.ref
  184. // CHECK:STDOUT: return <error> to %return.loc31
  185. // CHECK:STDOUT: }
  186. // CHECK:STDOUT:
  187. // CHECK:STDOUT: fn @F.2(%self.param: %Class);
  188. // CHECK:STDOUT:
  189. // CHECK:STDOUT: fn @CallWrongSelf(%ws.param: %WrongSelf) {
  190. // CHECK:STDOUT: !entry:
  191. // CHECK:STDOUT: %ws.ref: %WrongSelf = name_ref ws, %ws
  192. // CHECK:STDOUT: %F.ref: %F.type.25f = name_ref F, @WrongSelf.%F.decl [concrete = constants.%F.3a3]
  193. // CHECK:STDOUT: %F.bound: <bound method> = bound_method %ws.ref, %F.ref
  194. // CHECK:STDOUT: %.loc59: %Class = converted %ws.ref, <error> [concrete = <error>]
  195. // CHECK:STDOUT: %F.call: init %empty_tuple.type = call %F.bound(<error>)
  196. // CHECK:STDOUT: return
  197. // CHECK:STDOUT: }
  198. // CHECK:STDOUT: