fail_self.carbon 6.5 KB

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