fail_self.carbon 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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+37, loaded [template = constants.%ImplicitAs]
  99. // CHECK:STDOUT: %import_ref.2 = import_ref Core//prelude/operators/as, inst+42, unloaded
  100. // CHECK:STDOUT: %import_ref.3: @ImplicitAs.%.2 (%.6) = import_ref Core//prelude/operators/as, inst+59, loaded [symbolic = @ImplicitAs.%.3 (constants.%.11)]
  101. // CHECK:STDOUT: %import_ref.4 = import_ref Core//prelude/operators/as, inst+52, unloaded
  102. // CHECK:STDOUT: %import_ref.5 = import_ref Core//prelude/operators/as, inst+52, unloaded
  103. // CHECK:STDOUT: %import_ref.6 = import_ref Core//prelude/operators/as, inst+52, 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.ref.loc25: type = name_ref Self, constants.%Class [template = constants.%Class]
  117. // CHECK:STDOUT: %self.loc25_12.1: %Class = param self, runtime_param0
  118. // CHECK:STDOUT: @F.1.%self: %Class = bind_name self, %self.loc25_12.1
  119. // CHECK:STDOUT: }
  120. // CHECK:STDOUT: %G.decl: %G.type = fn_decl @G [template = constants.%G] {
  121. // CHECK:STDOUT: %Self.ref.loc28: type = name_ref Self, constants.%Class [template = constants.%Class]
  122. // CHECK:STDOUT: @G.%return: ref %Class = var <return slot>
  123. // CHECK:STDOUT: }
  124. // CHECK:STDOUT: %WrongSelf.decl: type = class_decl @WrongSelf [template = constants.%WrongSelf] {}
  125. // CHECK:STDOUT: %CallWrongSelf.decl: %CallWrongSelf.type = fn_decl @CallWrongSelf [template = constants.%CallWrongSelf] {
  126. // CHECK:STDOUT: %WrongSelf.ref: type = name_ref WrongSelf, %WrongSelf.decl [template = constants.%WrongSelf]
  127. // CHECK:STDOUT: %ws.loc45_18.1: %WrongSelf = param ws, runtime_param0
  128. // CHECK:STDOUT: @CallWrongSelf.%ws: %WrongSelf = bind_name ws, %ws.loc45_18.1
  129. // CHECK:STDOUT: }
  130. // CHECK:STDOUT: }
  131. // CHECK:STDOUT:
  132. // CHECK:STDOUT: generic interface @ImplicitAs(constants.%Dest: type) {
  133. // CHECK:STDOUT: %Dest: type = bind_symbolic_name Dest 0 [symbolic = %Dest (constants.%Dest)]
  134. // CHECK:STDOUT:
  135. // CHECK:STDOUT: !definition:
  136. // CHECK:STDOUT: %.1: type = interface_type @ImplicitAs, @ImplicitAs(%Dest) [symbolic = %.1 (constants.%.5)]
  137. // CHECK:STDOUT: %Self: %.5 = bind_symbolic_name Self 1 [symbolic = %Self (constants.%Self.2)]
  138. // CHECK:STDOUT: %Convert.type: type = fn_type @Convert, @ImplicitAs(%Dest) [symbolic = %Convert.type (constants.%Convert.type.1)]
  139. // CHECK:STDOUT: %Convert: @ImplicitAs.%Convert.type (%Convert.type.1) = struct_value () [symbolic = %Convert (constants.%Convert.1)]
  140. // CHECK:STDOUT: %.2: type = assoc_entity_type @ImplicitAs.%.1 (%.5), @ImplicitAs.%Convert.type (%Convert.type.1) [symbolic = %.2 (constants.%.6)]
  141. // CHECK:STDOUT: %.3: @ImplicitAs.%.2 (%.6) = assoc_entity element0, imports.%import_ref.5 [symbolic = %.3 (constants.%.7)]
  142. // CHECK:STDOUT:
  143. // CHECK:STDOUT: interface {
  144. // CHECK:STDOUT: !members:
  145. // CHECK:STDOUT: .Self = imports.%import_ref.2
  146. // CHECK:STDOUT: .Convert = imports.%import_ref.3
  147. // CHECK:STDOUT: witness = (imports.%import_ref.4)
  148. // CHECK:STDOUT: }
  149. // CHECK:STDOUT: }
  150. // CHECK:STDOUT:
  151. // CHECK:STDOUT: class @Class {
  152. // CHECK:STDOUT: %F.decl: %F.type.1 = fn_decl @F.1 [template = constants.%F.1] {
  153. // CHECK:STDOUT: %Self.ref.loc16: type = name_ref Self, constants.%Class [template = constants.%Class]
  154. // CHECK:STDOUT: %self.loc16_8.1: %Class = param self, runtime_param0
  155. // CHECK:STDOUT: %self.loc16_8.2: %Class = bind_name self, %self.loc16_8.1
  156. // CHECK:STDOUT: }
  157. // CHECK:STDOUT: %G.decl: %G.type = fn_decl @G [template = constants.%G] {
  158. // CHECK:STDOUT: %Self.ref.loc18: type = name_ref Self, constants.%Class [template = constants.%Class]
  159. // CHECK:STDOUT: %return.var: ref %Class = var <return slot>
  160. // CHECK:STDOUT: }
  161. // CHECK:STDOUT: %.loc19: <witness> = complete_type_witness %.2 [template = constants.%.3]
  162. // CHECK:STDOUT:
  163. // CHECK:STDOUT: !members:
  164. // CHECK:STDOUT: .Self = constants.%Class
  165. // CHECK:STDOUT: .F = %F.decl
  166. // CHECK:STDOUT: .G = %G.decl
  167. // CHECK:STDOUT: }
  168. // CHECK:STDOUT:
  169. // CHECK:STDOUT: class @WrongSelf {
  170. // CHECK:STDOUT: %F.decl: %F.type.2 = fn_decl @F.2 [template = constants.%F.2] {
  171. // CHECK:STDOUT: %Class.ref: type = name_ref Class, file.%Class.decl [template = constants.%Class]
  172. // CHECK:STDOUT: %self.loc42_8.1: %Class = param self, runtime_param0
  173. // CHECK:STDOUT: %self.loc42_8.2: %Class = bind_name self, %self.loc42_8.1
  174. // CHECK:STDOUT: }
  175. // CHECK:STDOUT: %.loc43: <witness> = complete_type_witness %.2 [template = constants.%.3]
  176. // CHECK:STDOUT:
  177. // CHECK:STDOUT: !members:
  178. // CHECK:STDOUT: .Self = constants.%WrongSelf
  179. // CHECK:STDOUT: .F = %F.decl
  180. // CHECK:STDOUT: }
  181. // CHECK:STDOUT:
  182. // CHECK:STDOUT: fn @F.1(%self: %Class) {
  183. // CHECK:STDOUT: !entry:
  184. // CHECK:STDOUT: return
  185. // CHECK:STDOUT: }
  186. // CHECK:STDOUT:
  187. // CHECK:STDOUT: fn @G() -> %return: %Class {
  188. // CHECK:STDOUT: !entry:
  189. // CHECK:STDOUT: %Self.ref: type = name_ref Self, constants.%Class [template = constants.%Class]
  190. // CHECK:STDOUT: %self.var: ref %Class = var self
  191. // CHECK:STDOUT: %self: ref %Class = bind_name self, %self.var
  192. // CHECK:STDOUT: %self.ref: ref %Class = name_ref self, %self
  193. // CHECK:STDOUT: %.loc38: %Class = bind_value %self.ref
  194. // CHECK:STDOUT: return <error> to %return
  195. // CHECK:STDOUT: }
  196. // CHECK:STDOUT:
  197. // CHECK:STDOUT: fn @F.2[@WrongSelf.%self.loc42_8.2: %Class]();
  198. // CHECK:STDOUT:
  199. // CHECK:STDOUT: fn @CallWrongSelf(%ws: %WrongSelf) {
  200. // CHECK:STDOUT: !entry:
  201. // CHECK:STDOUT: %ws.ref: %WrongSelf = name_ref ws, %ws
  202. // CHECK:STDOUT: %F.ref: %F.type.2 = name_ref F, @WrongSelf.%F.decl [template = constants.%F.2]
  203. // CHECK:STDOUT: %.loc55_5: <bound method> = bound_method %ws.ref, %F.ref
  204. // CHECK:STDOUT: %.loc55_7.1: type = interface_type @ImplicitAs, @ImplicitAs(constants.%Class) [template = constants.%.8]
  205. // CHECK:STDOUT: %.loc55_7.2: %.9 = specific_constant imports.%import_ref.3, @ImplicitAs(constants.%Class) [template = constants.%.10]
  206. // CHECK:STDOUT: %Convert.ref: %.9 = name_ref Convert, %.loc55_7.2 [template = constants.%.10]
  207. // CHECK:STDOUT: %.loc55_7.3: %Class = converted %ws.ref, <error> [template = <error>]
  208. // CHECK:STDOUT: %F.call: init %.1 = call %.loc55_5(<invalid>) [template = <error>]
  209. // CHECK:STDOUT: return
  210. // CHECK:STDOUT: }
  211. // CHECK:STDOUT:
  212. // CHECK:STDOUT: generic fn @Convert(constants.%Dest: type, constants.%Self.1: @ImplicitAs.%.1 (%.5)) {
  213. // CHECK:STDOUT: %Dest: type = bind_symbolic_name Dest 0 [symbolic = %Dest (constants.%Dest)]
  214. // CHECK:STDOUT: %.1: type = interface_type @ImplicitAs, @ImplicitAs(%Dest) [symbolic = %.1 (constants.%.5)]
  215. // CHECK:STDOUT: %Self: %.5 = bind_symbolic_name Self 1 [symbolic = %Self (constants.%Self.2)]
  216. // CHECK:STDOUT:
  217. // CHECK:STDOUT: fn[%self: @Convert.%Self (%Self.2)]() -> @Convert.%Dest (%Dest);
  218. // CHECK:STDOUT: }
  219. // CHECK:STDOUT:
  220. // CHECK:STDOUT: specific @ImplicitAs(constants.%Dest) {
  221. // CHECK:STDOUT: %Dest => constants.%Dest
  222. // CHECK:STDOUT: }
  223. // CHECK:STDOUT:
  224. // CHECK:STDOUT: specific @ImplicitAs(@ImplicitAs.%Dest) {
  225. // CHECK:STDOUT: %Dest => constants.%Dest
  226. // CHECK:STDOUT: }
  227. // CHECK:STDOUT:
  228. // CHECK:STDOUT: specific @ImplicitAs(@Convert.%Dest) {
  229. // CHECK:STDOUT: %Dest => constants.%Dest
  230. // CHECK:STDOUT: }
  231. // CHECK:STDOUT:
  232. // CHECK:STDOUT: specific @Convert(constants.%Dest, constants.%Self.1) {
  233. // CHECK:STDOUT: %Dest => constants.%Dest
  234. // CHECK:STDOUT: %.1 => constants.%.5
  235. // CHECK:STDOUT: %Self => constants.%Self.1
  236. // CHECK:STDOUT: }
  237. // CHECK:STDOUT:
  238. // CHECK:STDOUT: specific @ImplicitAs(constants.%Class) {
  239. // CHECK:STDOUT: %Dest => constants.%Class
  240. // CHECK:STDOUT:
  241. // CHECK:STDOUT: !definition:
  242. // CHECK:STDOUT: %.1 => constants.%.8
  243. // CHECK:STDOUT: %Self => constants.%Self.2
  244. // CHECK:STDOUT: %Convert.type => constants.%Convert.type.2
  245. // CHECK:STDOUT: %Convert => constants.%Convert.2
  246. // CHECK:STDOUT: %.2 => constants.%.9
  247. // CHECK:STDOUT: %.3 => constants.%.10
  248. // CHECK:STDOUT: }
  249. // CHECK:STDOUT: