fail_self.carbon 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. class Class {
  7. // CHECK:STDERR: fail_self.carbon:[[@LINE+4]]:8: ERROR: `self` can only be declared in an implicit parameter list.
  8. // CHECK:STDERR: fn F(self: Self);
  9. // CHECK:STDERR: ^~~~~~~~~~
  10. // CHECK:STDERR:
  11. fn F(self: Self);
  12. // CHECK:STDERR: fail_self.carbon:[[@LINE+7]]:10: ERROR: Function returns incomplete type `Class`.
  13. // CHECK:STDERR: fn G() -> Self;
  14. // CHECK:STDERR: ^~~~~~~
  15. // CHECK:STDERR: fail_self.carbon:[[@LINE-10]]:1: Class is incomplete within its definition.
  16. // CHECK:STDERR: class Class {
  17. // CHECK:STDERR: ^~~~~~~~~~~~~
  18. // CHECK:STDERR:
  19. fn G() -> Self;
  20. }
  21. // CHECK:STDERR: fail_self.carbon:[[@LINE+4]]:12: ERROR: `self` can only be declared in an implicit parameter list.
  22. // CHECK:STDERR: fn Class.F(self: Self) {
  23. // CHECK:STDERR: ^~~~~~~~~~
  24. // CHECK:STDERR:
  25. fn Class.F(self: Self) {
  26. }
  27. fn Class.G() -> Self {
  28. // CHECK:STDERR: fail_self.carbon:[[@LINE+4]]:7: ERROR: `self` can only be declared in an implicit parameter list.
  29. // CHECK:STDERR: var self: Self;
  30. // CHECK:STDERR: ^~~~~~~~~~
  31. // CHECK:STDERR:
  32. var self: Self;
  33. // CHECK:STDERR: fail_self.carbon:[[@LINE+4]]:10: ERROR: Cannot copy value of type `Class`.
  34. // CHECK:STDERR: return self;
  35. // CHECK:STDERR: ^~~~
  36. // CHECK:STDERR:
  37. return self;
  38. }
  39. class WrongSelf {
  40. fn F[self: Class]();
  41. }
  42. fn CallWrongSelf(ws: WrongSelf) {
  43. // CHECK:STDERR: fail_self.carbon:[[@LINE+6]]:3: ERROR: Cannot implicitly convert from `WrongSelf` to `Class`.
  44. // CHECK:STDERR: ws.F();
  45. // CHECK:STDERR: ^~~~~
  46. // CHECK:STDERR: fail_self.carbon:[[@LINE-7]]:8: 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: %.1: type = struct_type {} [template]
  56. // CHECK:STDOUT: %.2: type = tuple_type () [template]
  57. // CHECK:STDOUT: %.3: type = ptr_type {} [template]
  58. // CHECK:STDOUT: %WrongSelf: type = class_type @WrongSelf [template]
  59. // CHECK:STDOUT: }
  60. // CHECK:STDOUT:
  61. // CHECK:STDOUT: file {
  62. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  63. // CHECK:STDOUT: .Class = %Class.decl
  64. // CHECK:STDOUT: .WrongSelf = %WrongSelf.decl
  65. // CHECK:STDOUT: .CallWrongSelf = %CallWrongSelf
  66. // CHECK:STDOUT: }
  67. // CHECK:STDOUT: %Class.decl: type = class_decl @Class [template = constants.%Class] {}
  68. // CHECK:STDOUT: %F: <function> = fn_decl @F.1 [template] {
  69. // CHECK:STDOUT: %Self.ref.loc28: type = name_ref Self, constants.%Class [template = constants.%Class]
  70. // CHECK:STDOUT: %self.loc28_12.1: Class = param self
  71. // CHECK:STDOUT: @F.1.%self: Class = bind_name self, %self.loc28_12.1
  72. // CHECK:STDOUT: }
  73. // CHECK:STDOUT: %.loc31: <function> = fn_decl @.1 [template] {
  74. // CHECK:STDOUT: %Self.ref.loc31: type = name_ref Self, constants.%Class [template = constants.%Class]
  75. // CHECK:STDOUT: @.1.%return: ref Class = var <return slot>
  76. // CHECK:STDOUT: }
  77. // CHECK:STDOUT: %WrongSelf.decl: type = class_decl @WrongSelf [template = constants.%WrongSelf] {}
  78. // CHECK:STDOUT: %CallWrongSelf: <function> = fn_decl @CallWrongSelf [template] {
  79. // CHECK:STDOUT: %WrongSelf.ref: type = name_ref WrongSelf, %WrongSelf.decl [template = constants.%WrongSelf]
  80. // CHECK:STDOUT: %ws.loc48_18.1: WrongSelf = param ws
  81. // CHECK:STDOUT: @CallWrongSelf.%ws: WrongSelf = bind_name ws, %ws.loc48_18.1
  82. // CHECK:STDOUT: }
  83. // CHECK:STDOUT: }
  84. // CHECK:STDOUT:
  85. // CHECK:STDOUT: class @Class {
  86. // CHECK:STDOUT: %F: <function> = fn_decl @F.1 [template] {
  87. // CHECK:STDOUT: %Self.ref.loc12: type = name_ref Self, constants.%Class [template = constants.%Class]
  88. // CHECK:STDOUT: %self.loc12_8.1: Class = param self
  89. // CHECK:STDOUT: %self.loc12_8.2: Class = bind_name self, %self.loc12_8.1
  90. // CHECK:STDOUT: }
  91. // CHECK:STDOUT: %G: <function> = fn_decl @G [template] {
  92. // CHECK:STDOUT: %Self.ref.loc21: type = name_ref Self, constants.%Class [template = constants.%Class]
  93. // CHECK:STDOUT: %return.var: ref Class = var <return slot>
  94. // CHECK:STDOUT: }
  95. // CHECK:STDOUT:
  96. // CHECK:STDOUT: !members:
  97. // CHECK:STDOUT: .Self = constants.%Class
  98. // CHECK:STDOUT: .F = %F
  99. // CHECK:STDOUT: .G = %G
  100. // CHECK:STDOUT: }
  101. // CHECK:STDOUT:
  102. // CHECK:STDOUT: class @WrongSelf {
  103. // CHECK:STDOUT: %F: <function> = fn_decl @F.2 [template] {
  104. // CHECK:STDOUT: %Class.ref: type = name_ref Class, file.%Class.decl [template = constants.%Class]
  105. // CHECK:STDOUT: %self.loc45_8.1: Class = param self
  106. // CHECK:STDOUT: %self.loc45_8.2: Class = bind_name self, %self.loc45_8.1
  107. // CHECK:STDOUT: }
  108. // CHECK:STDOUT:
  109. // CHECK:STDOUT: !members:
  110. // CHECK:STDOUT: .Self = constants.%WrongSelf
  111. // CHECK:STDOUT: .F = %F
  112. // CHECK:STDOUT: }
  113. // CHECK:STDOUT:
  114. // CHECK:STDOUT: fn @F.1(%self: Class) {
  115. // CHECK:STDOUT: !entry:
  116. // CHECK:STDOUT: return
  117. // CHECK:STDOUT: }
  118. // CHECK:STDOUT:
  119. // CHECK:STDOUT: fn @G() -> <error>;
  120. // CHECK:STDOUT:
  121. // CHECK:STDOUT: fn @.1() -> %return: Class {
  122. // CHECK:STDOUT: !entry:
  123. // CHECK:STDOUT: %Self.ref: type = name_ref Self, constants.%Class [template = constants.%Class]
  124. // CHECK:STDOUT: %self.var: ref Class = var self
  125. // CHECK:STDOUT: %self: ref Class = bind_name self, %self.var
  126. // CHECK:STDOUT: %self.ref: ref Class = name_ref self, %self
  127. // CHECK:STDOUT: %.loc41: Class = bind_value %self.ref
  128. // CHECK:STDOUT: return <error>
  129. // CHECK:STDOUT: }
  130. // CHECK:STDOUT:
  131. // CHECK:STDOUT: fn @F.2[@WrongSelf.%self.loc45_8.2: Class]();
  132. // CHECK:STDOUT:
  133. // CHECK:STDOUT: fn @CallWrongSelf(%ws: WrongSelf) {
  134. // CHECK:STDOUT: !entry:
  135. // CHECK:STDOUT: %ws.ref: WrongSelf = name_ref ws, %ws
  136. // CHECK:STDOUT: %F.ref: <function> = name_ref F, @WrongSelf.%F [template = @WrongSelf.%F]
  137. // CHECK:STDOUT: %.loc55_5: <bound method> = bound_method %ws.ref, %F.ref
  138. // CHECK:STDOUT: %.loc55_7: init () = call %.loc55_5(<invalid>) [template = <error>]
  139. // CHECK:STDOUT: return
  140. // CHECK:STDOUT: }
  141. // CHECK:STDOUT: