fail_self.carbon 15 KB

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