fail_self.carbon 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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+6]]:3: ERROR: Cannot implicitly convert from `WrongSelf` to `Class`.
  41. // CHECK:STDERR: ws.F();
  42. // CHECK:STDERR: ^~~~~
  43. // CHECK:STDERR: fail_self.carbon:[[@LINE-7]]:8: Initializing `self` parameter of method declared here.
  44. // CHECK:STDERR: fn F[self: Class]();
  45. // CHECK:STDERR: ^~~~
  46. ws.F();
  47. }
  48. // CHECK:STDOUT: --- fail_self.carbon
  49. // CHECK:STDOUT:
  50. // CHECK:STDOUT: constants {
  51. // CHECK:STDOUT: %Class: type = class_type @Class [template]
  52. // CHECK:STDOUT: %F.type.1: type = fn_type @F.1 [template]
  53. // CHECK:STDOUT: %.1: type = tuple_type () [template]
  54. // CHECK:STDOUT: %F.1: %F.type.1 = struct_value () [template]
  55. // CHECK:STDOUT: %G.type: type = fn_type @G [template]
  56. // CHECK:STDOUT: %G: %G.type = struct_value () [template]
  57. // CHECK:STDOUT: %.2: type = struct_type {} [template]
  58. // CHECK:STDOUT: %.3: type = ptr_type %.2 [template]
  59. // CHECK:STDOUT: %WrongSelf: type = class_type @WrongSelf [template]
  60. // CHECK:STDOUT: %F.type.2: type = fn_type @F.2 [template]
  61. // CHECK:STDOUT: %F.2: %F.type.2 = struct_value () [template]
  62. // CHECK:STDOUT: %CallWrongSelf.type: type = fn_type @CallWrongSelf [template]
  63. // CHECK:STDOUT: %CallWrongSelf: %CallWrongSelf.type = struct_value () [template]
  64. // CHECK:STDOUT: }
  65. // CHECK:STDOUT:
  66. // CHECK:STDOUT: file {
  67. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  68. // CHECK:STDOUT: .Core = %Core
  69. // CHECK:STDOUT: .Class = %Class.decl
  70. // CHECK:STDOUT: .WrongSelf = %WrongSelf.decl
  71. // CHECK:STDOUT: .CallWrongSelf = %CallWrongSelf.decl
  72. // CHECK:STDOUT: }
  73. // CHECK:STDOUT: %Core: <namespace> = namespace [template] {}
  74. // CHECK:STDOUT: %Class.decl: type = class_decl @Class [template = constants.%Class] {}
  75. // CHECK:STDOUT: %F.decl: %F.type.1 = fn_decl @F.1 [template = constants.%F.1] {
  76. // CHECK:STDOUT: %Self.ref.loc25: type = name_ref Self, constants.%Class [template = constants.%Class]
  77. // CHECK:STDOUT: %self.loc25_12.1: %Class = param self
  78. // CHECK:STDOUT: @F.1.%self: %Class = bind_name self, %self.loc25_12.1
  79. // CHECK:STDOUT: }
  80. // CHECK:STDOUT: %G.decl: %G.type = fn_decl @G [template = constants.%G] {
  81. // CHECK:STDOUT: %Self.ref.loc28: type = name_ref Self, constants.%Class [template = constants.%Class]
  82. // CHECK:STDOUT: @G.%return: ref %Class = var <return slot>
  83. // CHECK:STDOUT: }
  84. // CHECK:STDOUT: %WrongSelf.decl: type = class_decl @WrongSelf [template = constants.%WrongSelf] {}
  85. // CHECK:STDOUT: %CallWrongSelf.decl: %CallWrongSelf.type = fn_decl @CallWrongSelf [template = constants.%CallWrongSelf] {
  86. // CHECK:STDOUT: %WrongSelf.ref: type = name_ref WrongSelf, %WrongSelf.decl [template = constants.%WrongSelf]
  87. // CHECK:STDOUT: %ws.loc45_18.1: %WrongSelf = param ws
  88. // CHECK:STDOUT: @CallWrongSelf.%ws: %WrongSelf = bind_name ws, %ws.loc45_18.1
  89. // CHECK:STDOUT: }
  90. // CHECK:STDOUT: }
  91. // CHECK:STDOUT:
  92. // CHECK:STDOUT: class @Class {
  93. // CHECK:STDOUT: %F.decl: %F.type.1 = fn_decl @F.1 [template = constants.%F.1] {
  94. // CHECK:STDOUT: %Self.ref.loc16: type = name_ref Self, constants.%Class [template = constants.%Class]
  95. // CHECK:STDOUT: %self.loc16_8.1: %Class = param self
  96. // CHECK:STDOUT: %self.loc16_8.2: %Class = bind_name self, %self.loc16_8.1
  97. // CHECK:STDOUT: }
  98. // CHECK:STDOUT: %G.decl: %G.type = fn_decl @G [template = constants.%G] {
  99. // CHECK:STDOUT: %Self.ref.loc18: type = name_ref Self, constants.%Class [template = constants.%Class]
  100. // CHECK:STDOUT: %return.var: ref %Class = var <return slot>
  101. // CHECK:STDOUT: }
  102. // CHECK:STDOUT:
  103. // CHECK:STDOUT: !members:
  104. // CHECK:STDOUT: .Self = constants.%Class
  105. // CHECK:STDOUT: .F = %F.decl
  106. // CHECK:STDOUT: .G = %G.decl
  107. // CHECK:STDOUT: }
  108. // CHECK:STDOUT:
  109. // CHECK:STDOUT: class @WrongSelf {
  110. // CHECK:STDOUT: %F.decl: %F.type.2 = fn_decl @F.2 [template = constants.%F.2] {
  111. // CHECK:STDOUT: %Class.ref: type = name_ref Class, file.%Class.decl [template = constants.%Class]
  112. // CHECK:STDOUT: %self.loc42_8.1: %Class = param self
  113. // CHECK:STDOUT: %self.loc42_8.2: %Class = bind_name self, %self.loc42_8.1
  114. // CHECK:STDOUT: }
  115. // CHECK:STDOUT:
  116. // CHECK:STDOUT: !members:
  117. // CHECK:STDOUT: .Self = constants.%WrongSelf
  118. // CHECK:STDOUT: .F = %F.decl
  119. // CHECK:STDOUT: }
  120. // CHECK:STDOUT:
  121. // CHECK:STDOUT: fn @F.1(%self: %Class) {
  122. // CHECK:STDOUT: !entry:
  123. // CHECK:STDOUT: return
  124. // CHECK:STDOUT: }
  125. // CHECK:STDOUT:
  126. // CHECK:STDOUT: fn @G() -> %return: %Class {
  127. // CHECK:STDOUT: !entry:
  128. // CHECK:STDOUT: %Self.ref: type = name_ref Self, constants.%Class [template = constants.%Class]
  129. // CHECK:STDOUT: %self.var: ref %Class = var self
  130. // CHECK:STDOUT: %self: ref %Class = bind_name self, %self.var
  131. // CHECK:STDOUT: %self.ref: ref %Class = name_ref self, %self
  132. // CHECK:STDOUT: %.loc38: %Class = bind_value %self.ref
  133. // CHECK:STDOUT: return <error> to %return
  134. // CHECK:STDOUT: }
  135. // CHECK:STDOUT:
  136. // CHECK:STDOUT: fn @F.2[@WrongSelf.%self.loc42_8.2: %Class]();
  137. // CHECK:STDOUT:
  138. // CHECK:STDOUT: fn @CallWrongSelf(%ws: %WrongSelf) {
  139. // CHECK:STDOUT: !entry:
  140. // CHECK:STDOUT: %ws.ref: %WrongSelf = name_ref ws, %ws
  141. // CHECK:STDOUT: %F.ref: %F.type.2 = name_ref F, @WrongSelf.%F.decl [template = constants.%F.2]
  142. // CHECK:STDOUT: %.loc52: <bound method> = bound_method %ws.ref, %F.ref
  143. // CHECK:STDOUT: %F.call: init %.1 = call %.loc52(<invalid>) [template = <error>]
  144. // CHECK:STDOUT: return
  145. // CHECK:STDOUT: }
  146. // CHECK:STDOUT: