fail_self.carbon 9.6 KB

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