fail_self.carbon 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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
  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
  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
  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`
  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+9]]:3: error: cannot implicitly convert from `WrongSelf` to `Class`
  41. // CHECK:STDERR: ws.F();
  42. // CHECK:STDERR: ^~~~~
  43. // CHECK:STDERR: fail_self.carbon:[[@LINE+6]]:3: note: type `WrongSelf` does not implement interface `ImplicitAs`
  44. // CHECK:STDERR: ws.F();
  45. // CHECK:STDERR: ^~~~~
  46. // CHECK:STDERR: fail_self.carbon:[[@LINE-10]]:8: note: initializing `self` parameter of method declared here
  47. // CHECK:STDERR: fn F[self: Class]();
  48. // CHECK:STDERR: ^~~~
  49. ws.F();
  50. }
  51. // CHECK:STDOUT: --- fail_self.carbon
  52. // CHECK:STDOUT:
  53. // CHECK:STDOUT: constants {
  54. // CHECK:STDOUT: %Class: type = class_type @Class [template]
  55. // CHECK:STDOUT: %F.type.1: type = fn_type @F.1 [template]
  56. // CHECK:STDOUT: %.1: type = tuple_type () [template]
  57. // CHECK:STDOUT: %F.1: %F.type.1 = struct_value () [template]
  58. // CHECK:STDOUT: %G.type: type = fn_type @G [template]
  59. // CHECK:STDOUT: %G: %G.type = struct_value () [template]
  60. // CHECK:STDOUT: %.2: type = struct_type {} [template]
  61. // CHECK:STDOUT: %.3: <witness> = complete_type_witness %.2 [template]
  62. // CHECK:STDOUT: %.4: type = ptr_type %.2 [template]
  63. // CHECK:STDOUT: %WrongSelf: type = class_type @WrongSelf [template]
  64. // CHECK:STDOUT: %F.type.2: type = fn_type @F.2 [template]
  65. // CHECK:STDOUT: %F.2: %F.type.2 = 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: %ImplicitAs.type: type = generic_interface_type @ImplicitAs [template]
  69. // CHECK:STDOUT: %ImplicitAs: %ImplicitAs.type = struct_value () [template]
  70. // CHECK:STDOUT: %Dest: type = bind_symbolic_name Dest 0 [symbolic]
  71. // CHECK:STDOUT: %.5: type = interface_type @ImplicitAs, @ImplicitAs(%Dest) [symbolic]
  72. // CHECK:STDOUT: %Self.1: @ImplicitAs.%.1 (%.5) = bind_symbolic_name Self 1 [symbolic]
  73. // CHECK:STDOUT: %Self.2: %.5 = bind_symbolic_name Self 1 [symbolic]
  74. // CHECK:STDOUT: %Convert.type.1: type = fn_type @Convert, @ImplicitAs(%Dest) [symbolic]
  75. // CHECK:STDOUT: %Convert.1: %Convert.type.1 = struct_value () [symbolic]
  76. // CHECK:STDOUT: %.6: type = assoc_entity_type %.5, %Convert.type.1 [symbolic]
  77. // CHECK:STDOUT: %.7: %.6 = assoc_entity element0, imports.%import_ref.5 [symbolic]
  78. // CHECK:STDOUT: %.8: type = interface_type @ImplicitAs, @ImplicitAs(%Class) [template]
  79. // CHECK:STDOUT: %Convert.type.2: type = fn_type @Convert, @ImplicitAs(%Class) [template]
  80. // CHECK:STDOUT: %Convert.2: %Convert.type.2 = struct_value () [template]
  81. // CHECK:STDOUT: %.9: type = assoc_entity_type %.8, %Convert.type.2 [template]
  82. // CHECK:STDOUT: %.10: %.9 = assoc_entity element0, imports.%import_ref.5 [template]
  83. // CHECK:STDOUT: %.11: %.6 = assoc_entity element0, imports.%import_ref.6 [symbolic]
  84. // CHECK:STDOUT: }
  85. // CHECK:STDOUT:
  86. // CHECK:STDOUT: imports {
  87. // CHECK:STDOUT: %Core: <namespace> = namespace file.%Core.import, [template] {
  88. // CHECK:STDOUT: .ImplicitAs = %import_ref.1
  89. // CHECK:STDOUT: import Core//prelude
  90. // CHECK:STDOUT: import Core//prelude/operators
  91. // CHECK:STDOUT: import Core//prelude/types
  92. // CHECK:STDOUT: import Core//prelude/operators/arithmetic
  93. // CHECK:STDOUT: import Core//prelude/operators/as
  94. // CHECK:STDOUT: import Core//prelude/operators/bitwise
  95. // CHECK:STDOUT: import Core//prelude/operators/comparison
  96. // CHECK:STDOUT: import Core//prelude/types/bool
  97. // CHECK:STDOUT: }
  98. // CHECK:STDOUT: %import_ref.1: %ImplicitAs.type = import_ref Core//prelude/operators/as, inst+40, loaded [template = constants.%ImplicitAs]
  99. // CHECK:STDOUT: %import_ref.2 = import_ref Core//prelude/operators/as, inst+45, unloaded
  100. // CHECK:STDOUT: %import_ref.3: @ImplicitAs.%.2 (%.6) = import_ref Core//prelude/operators/as, inst+63, loaded [symbolic = @ImplicitAs.%.3 (constants.%.11)]
  101. // CHECK:STDOUT: %import_ref.4 = import_ref Core//prelude/operators/as, inst+56, unloaded
  102. // CHECK:STDOUT: %import_ref.5 = import_ref Core//prelude/operators/as, inst+56, unloaded
  103. // CHECK:STDOUT: %import_ref.6 = import_ref Core//prelude/operators/as, inst+56, unloaded
  104. // CHECK:STDOUT: }
  105. // CHECK:STDOUT:
  106. // CHECK:STDOUT: file {
  107. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  108. // CHECK:STDOUT: .Core = imports.%Core
  109. // CHECK:STDOUT: .Class = %Class.decl
  110. // CHECK:STDOUT: .WrongSelf = %WrongSelf.decl
  111. // CHECK:STDOUT: .CallWrongSelf = %CallWrongSelf.decl
  112. // CHECK:STDOUT: }
  113. // CHECK:STDOUT: %Core.import = import Core
  114. // CHECK:STDOUT: %Class.decl: type = class_decl @Class [template = constants.%Class] {} {}
  115. // CHECK:STDOUT: %F.decl: %F.type.1 = fn_decl @F.1 [template = constants.%F.1] {
  116. // CHECK:STDOUT: %self.patt: %Class = binding_pattern self
  117. // CHECK:STDOUT: } {
  118. // CHECK:STDOUT: %Self.ref.loc25: type = name_ref Self, constants.%Class [template = constants.%Class]
  119. // CHECK:STDOUT: %self.param.loc25: %Class = param self, runtime_param0
  120. // CHECK:STDOUT: %self.loc25: %Class = bind_name self, %self.param.loc25
  121. // CHECK:STDOUT: }
  122. // CHECK:STDOUT: %G.decl: %G.type = fn_decl @G [template = constants.%G] {} {
  123. // CHECK:STDOUT: %Self.ref.loc28: type = name_ref Self, constants.%Class [template = constants.%Class]
  124. // CHECK:STDOUT: %return: ref %Class = var <return slot>
  125. // CHECK:STDOUT: }
  126. // CHECK:STDOUT: %WrongSelf.decl: type = class_decl @WrongSelf [template = constants.%WrongSelf] {} {}
  127. // CHECK:STDOUT: %CallWrongSelf.decl: %CallWrongSelf.type = fn_decl @CallWrongSelf [template = constants.%CallWrongSelf] {
  128. // CHECK:STDOUT: %ws.patt: %WrongSelf = binding_pattern ws
  129. // CHECK:STDOUT: } {
  130. // CHECK:STDOUT: %WrongSelf.ref: type = name_ref WrongSelf, file.%WrongSelf.decl [template = constants.%WrongSelf]
  131. // CHECK:STDOUT: %ws.param: %WrongSelf = param ws, runtime_param0
  132. // CHECK:STDOUT: %ws: %WrongSelf = bind_name ws, %ws.param
  133. // CHECK:STDOUT: }
  134. // CHECK:STDOUT: }
  135. // CHECK:STDOUT:
  136. // CHECK:STDOUT: generic interface @ImplicitAs(constants.%Dest: type) {
  137. // CHECK:STDOUT: %Dest: type = bind_symbolic_name Dest 0 [symbolic = %Dest (constants.%Dest)]
  138. // CHECK:STDOUT:
  139. // CHECK:STDOUT: !definition:
  140. // CHECK:STDOUT: %.1: type = interface_type @ImplicitAs, @ImplicitAs(%Dest) [symbolic = %.1 (constants.%.5)]
  141. // CHECK:STDOUT: %Self: %.5 = bind_symbolic_name Self 1 [symbolic = %Self (constants.%Self.2)]
  142. // CHECK:STDOUT: %Convert.type: type = fn_type @Convert, @ImplicitAs(%Dest) [symbolic = %Convert.type (constants.%Convert.type.1)]
  143. // CHECK:STDOUT: %Convert: @ImplicitAs.%Convert.type (%Convert.type.1) = struct_value () [symbolic = %Convert (constants.%Convert.1)]
  144. // CHECK:STDOUT: %.2: type = assoc_entity_type @ImplicitAs.%.1 (%.5), @ImplicitAs.%Convert.type (%Convert.type.1) [symbolic = %.2 (constants.%.6)]
  145. // CHECK:STDOUT: %.3: @ImplicitAs.%.2 (%.6) = assoc_entity element0, imports.%import_ref.5 [symbolic = %.3 (constants.%.7)]
  146. // CHECK:STDOUT:
  147. // CHECK:STDOUT: interface {
  148. // CHECK:STDOUT: !members:
  149. // CHECK:STDOUT: .Self = imports.%import_ref.2
  150. // CHECK:STDOUT: .Convert = imports.%import_ref.3
  151. // CHECK:STDOUT: witness = (imports.%import_ref.4)
  152. // CHECK:STDOUT: }
  153. // CHECK:STDOUT: }
  154. // CHECK:STDOUT:
  155. // CHECK:STDOUT: class @Class {
  156. // CHECK:STDOUT: %F.decl: %F.type.1 = fn_decl @F.1 [template = constants.%F.1] {
  157. // CHECK:STDOUT: %self.patt: %Class = binding_pattern self
  158. // CHECK:STDOUT: } {
  159. // CHECK:STDOUT: %Self.ref.loc16: type = name_ref Self, constants.%Class [template = constants.%Class]
  160. // CHECK:STDOUT: %self.param.loc16: %Class = param self, runtime_param0
  161. // CHECK:STDOUT: %self.loc16: %Class = bind_name self, %self.param.loc16
  162. // CHECK:STDOUT: }
  163. // CHECK:STDOUT: %G.decl: %G.type = fn_decl @G [template = constants.%G] {} {
  164. // CHECK:STDOUT: %Self.ref.loc18: type = name_ref Self, constants.%Class [template = constants.%Class]
  165. // CHECK:STDOUT: %return.var.loc18: ref %Class = var <return slot>
  166. // CHECK:STDOUT: }
  167. // CHECK:STDOUT: %.loc19: <witness> = complete_type_witness %.2 [template = constants.%.3]
  168. // CHECK:STDOUT:
  169. // CHECK:STDOUT: !members:
  170. // CHECK:STDOUT: .Self = constants.%Class
  171. // CHECK:STDOUT: .F = %F.decl
  172. // CHECK:STDOUT: .G = %G.decl
  173. // CHECK:STDOUT: }
  174. // CHECK:STDOUT:
  175. // CHECK:STDOUT: class @WrongSelf {
  176. // CHECK:STDOUT: %F.decl: %F.type.2 = fn_decl @F.2 [template = constants.%F.2] {
  177. // CHECK:STDOUT: %self.patt: %Class = binding_pattern self
  178. // CHECK:STDOUT: } {
  179. // CHECK:STDOUT: %Class.ref: type = name_ref Class, file.%Class.decl [template = constants.%Class]
  180. // CHECK:STDOUT: %self.param: %Class = param self, runtime_param0
  181. // CHECK:STDOUT: %self: %Class = bind_name self, %self.param
  182. // CHECK:STDOUT: }
  183. // CHECK:STDOUT: %.loc43: <witness> = complete_type_witness %.2 [template = constants.%.3]
  184. // CHECK:STDOUT:
  185. // CHECK:STDOUT: !members:
  186. // CHECK:STDOUT: .Self = constants.%WrongSelf
  187. // CHECK:STDOUT: .F = %F.decl
  188. // CHECK:STDOUT: }
  189. // CHECK:STDOUT:
  190. // CHECK:STDOUT: fn @F.1(%self.loc25: %Class) {
  191. // CHECK:STDOUT: !entry:
  192. // CHECK:STDOUT: return
  193. // CHECK:STDOUT: }
  194. // CHECK:STDOUT:
  195. // CHECK:STDOUT: fn @G() -> %return: %Class {
  196. // CHECK:STDOUT: !entry:
  197. // CHECK:STDOUT: %Self.ref.loc33: type = name_ref Self, constants.%Class [template = constants.%Class]
  198. // CHECK:STDOUT: %self.var: ref %Class = var self
  199. // CHECK:STDOUT: %self: ref %Class = bind_name self, %self.var
  200. // CHECK:STDOUT: %self.ref: ref %Class = name_ref self, %self
  201. // CHECK:STDOUT: %.loc38: %Class = bind_value %self.ref
  202. // CHECK:STDOUT: return <error> to %return
  203. // CHECK:STDOUT: }
  204. // CHECK:STDOUT:
  205. // CHECK:STDOUT: fn @F.2[%self: %Class]();
  206. // CHECK:STDOUT:
  207. // CHECK:STDOUT: fn @CallWrongSelf(%ws: %WrongSelf) {
  208. // CHECK:STDOUT: !entry:
  209. // CHECK:STDOUT: %ws.ref: %WrongSelf = name_ref ws, %ws
  210. // CHECK:STDOUT: %F.ref: %F.type.2 = name_ref F, @WrongSelf.%F.decl [template = constants.%F.2]
  211. // CHECK:STDOUT: %.loc55_5: <bound method> = bound_method %ws.ref, %F.ref
  212. // CHECK:STDOUT: %.loc55_7.1: type = interface_type @ImplicitAs, @ImplicitAs(constants.%Class) [template = constants.%.8]
  213. // CHECK:STDOUT: %.loc55_7.2: %.9 = specific_constant imports.%import_ref.3, @ImplicitAs(constants.%Class) [template = constants.%.10]
  214. // CHECK:STDOUT: %Convert.ref: %.9 = name_ref Convert, %.loc55_7.2 [template = constants.%.10]
  215. // CHECK:STDOUT: %.loc55_7.3: %Class = converted %ws.ref, <error> [template = <error>]
  216. // CHECK:STDOUT: %F.call: init %.1 = call %.loc55_5(<invalid>) [template = <error>]
  217. // CHECK:STDOUT: return
  218. // CHECK:STDOUT: }
  219. // CHECK:STDOUT:
  220. // CHECK:STDOUT: generic fn @Convert(constants.%Dest: type, constants.%Self.1: @ImplicitAs.%.1 (%.5)) {
  221. // CHECK:STDOUT: %Dest: type = bind_symbolic_name Dest 0 [symbolic = %Dest (constants.%Dest)]
  222. // CHECK:STDOUT: %.1: type = interface_type @ImplicitAs, @ImplicitAs(%Dest) [symbolic = %.1 (constants.%.5)]
  223. // CHECK:STDOUT: %Self: %.5 = bind_symbolic_name Self 1 [symbolic = %Self (constants.%Self.2)]
  224. // CHECK:STDOUT:
  225. // CHECK:STDOUT: fn[%self: @Convert.%Self (%Self.2)]() -> @Convert.%Dest (%Dest);
  226. // CHECK:STDOUT: }
  227. // CHECK:STDOUT:
  228. // CHECK:STDOUT: specific @ImplicitAs(constants.%Dest) {
  229. // CHECK:STDOUT: %Dest => constants.%Dest
  230. // CHECK:STDOUT: }
  231. // CHECK:STDOUT:
  232. // CHECK:STDOUT: specific @ImplicitAs(@ImplicitAs.%Dest) {
  233. // CHECK:STDOUT: %Dest => constants.%Dest
  234. // CHECK:STDOUT: }
  235. // CHECK:STDOUT:
  236. // CHECK:STDOUT: specific @ImplicitAs(@Convert.%Dest) {
  237. // CHECK:STDOUT: %Dest => constants.%Dest
  238. // CHECK:STDOUT: }
  239. // CHECK:STDOUT:
  240. // CHECK:STDOUT: specific @Convert(constants.%Dest, constants.%Self.1) {
  241. // CHECK:STDOUT: %Dest => constants.%Dest
  242. // CHECK:STDOUT: %.1 => constants.%.5
  243. // CHECK:STDOUT: %Self => constants.%Self.1
  244. // CHECK:STDOUT: }
  245. // CHECK:STDOUT:
  246. // CHECK:STDOUT: specific @ImplicitAs(constants.%Class) {
  247. // CHECK:STDOUT: %Dest => constants.%Class
  248. // CHECK:STDOUT:
  249. // CHECK:STDOUT: !definition:
  250. // CHECK:STDOUT: %.1 => constants.%.8
  251. // CHECK:STDOUT: %Self => constants.%Self.2
  252. // CHECK:STDOUT: %Convert.type => constants.%Convert.type.2
  253. // CHECK:STDOUT: %Convert => constants.%Convert.2
  254. // CHECK:STDOUT: %.2 => constants.%.9
  255. // CHECK:STDOUT: %.3 => constants.%.10
  256. // CHECK:STDOUT: }
  257. // CHECK:STDOUT: