self.carbon 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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/self.carbon
  8. // TIP: To dump output, run:
  9. // TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/class/self.carbon
  10. // --- self.carbon
  11. library "[[@TEST_NAME]]";
  12. class Class {
  13. fn F[self: Self]() -> i32;
  14. fn G[addr self: Self*]() -> i32;
  15. var n: i32;
  16. }
  17. fn Class.F[self: Self]() -> i32 {
  18. return self.n;
  19. }
  20. fn Class.G[addr self: Self*]() -> i32 {
  21. return (*self).n;
  22. }
  23. // --- fail_return_self_value.carbon
  24. library "[[@TEST_NAME]]";
  25. class Class {
  26. // CHECK:STDERR: fail_return_self_value.carbon:[[@LINE+6]]:25: error: cannot implicitly convert from `Class` to `type` [ImplicitAsConversionFailure]
  27. // CHECK:STDERR: fn F[self: Self]() -> self;
  28. // CHECK:STDERR: ^~~~
  29. // CHECK:STDERR: fail_return_self_value.carbon:[[@LINE+3]]:25: note: type `Class` does not implement interface `Core.ImplicitAs(type)` [MissingImplInMemberAccessNote]
  30. // CHECK:STDERR: fn F[self: Self]() -> self;
  31. // CHECK:STDERR: ^~~~
  32. fn F[self: Self]() -> self;
  33. }
  34. // CHECK:STDOUT: --- self.carbon
  35. // CHECK:STDOUT:
  36. // CHECK:STDOUT: constants {
  37. // CHECK:STDOUT: %Class: type = class_type @Class [template]
  38. // CHECK:STDOUT: %int_32: Core.IntLiteral = int_value 32 [template]
  39. // CHECK:STDOUT: %i32: type = class_type @Int, @Int(%int_32) [template]
  40. // CHECK:STDOUT: %F.type: type = fn_type @F [template]
  41. // CHECK:STDOUT: %F: %F.type = struct_value () [template]
  42. // CHECK:STDOUT: %ptr.e71: type = ptr_type %Class [template]
  43. // CHECK:STDOUT: %G.type: type = fn_type @G [template]
  44. // CHECK:STDOUT: %G: %G.type = struct_value () [template]
  45. // CHECK:STDOUT: %Class.elem: type = unbound_element_type %Class, %i32 [template]
  46. // CHECK:STDOUT: %struct_type.n: type = struct_type {.n: %i32} [template]
  47. // CHECK:STDOUT: %complete_type.9b7: <witness> = complete_type_witness %struct_type.n [template]
  48. // CHECK:STDOUT: }
  49. // CHECK:STDOUT:
  50. // CHECK:STDOUT: imports {
  51. // CHECK:STDOUT: %Core: <namespace> = namespace file.%Core.import, [template] {
  52. // CHECK:STDOUT: .Int = %import_ref.187
  53. // CHECK:STDOUT: import Core//prelude
  54. // CHECK:STDOUT: import Core//prelude/...
  55. // CHECK:STDOUT: }
  56. // CHECK:STDOUT: }
  57. // CHECK:STDOUT:
  58. // CHECK:STDOUT: file {
  59. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  60. // CHECK:STDOUT: .Core = imports.%Core
  61. // CHECK:STDOUT: .Class = %Class.decl
  62. // CHECK:STDOUT: }
  63. // CHECK:STDOUT: %Core.import = import Core
  64. // CHECK:STDOUT: %Class.decl: type = class_decl @Class [template = constants.%Class] {} {}
  65. // CHECK:STDOUT: %F.decl: %F.type = fn_decl @F [template = constants.%F] {
  66. // CHECK:STDOUT: %self.patt: %Class = binding_pattern self
  67. // CHECK:STDOUT: %self.param_patt: %Class = value_param_pattern %self.patt, runtime_param0
  68. // CHECK:STDOUT: %return.patt: %i32 = return_slot_pattern
  69. // CHECK:STDOUT: %return.param_patt: %i32 = out_param_pattern %return.patt, runtime_param1
  70. // CHECK:STDOUT: } {
  71. // CHECK:STDOUT: %int_32.loc11: Core.IntLiteral = int_value 32 [template = constants.%int_32]
  72. // CHECK:STDOUT: %i32.loc11: type = class_type @Int, @Int(constants.%int_32) [template = constants.%i32]
  73. // CHECK:STDOUT: %self.param.loc11: %Class = value_param runtime_param0
  74. // CHECK:STDOUT: %Self.ref.loc11: type = name_ref Self, constants.%Class [template = constants.%Class]
  75. // CHECK:STDOUT: %self.loc11: %Class = bind_name self, %self.param.loc11
  76. // CHECK:STDOUT: %return.param.loc11: ref %i32 = out_param runtime_param1
  77. // CHECK:STDOUT: %return.loc11: ref %i32 = return_slot %return.param.loc11
  78. // CHECK:STDOUT: }
  79. // CHECK:STDOUT: %G.decl: %G.type = fn_decl @G [template = constants.%G] {
  80. // CHECK:STDOUT: %self.patt: %ptr.e71 = binding_pattern self
  81. // CHECK:STDOUT: %self.param_patt: %ptr.e71 = value_param_pattern %self.patt, runtime_param0
  82. // CHECK:STDOUT: %.loc15_12: auto = addr_pattern %self.param_patt
  83. // CHECK:STDOUT: %return.patt: %i32 = return_slot_pattern
  84. // CHECK:STDOUT: %return.param_patt: %i32 = out_param_pattern %return.patt, runtime_param1
  85. // CHECK:STDOUT: } {
  86. // CHECK:STDOUT: %int_32.loc15: Core.IntLiteral = int_value 32 [template = constants.%int_32]
  87. // CHECK:STDOUT: %i32.loc15: type = class_type @Int, @Int(constants.%int_32) [template = constants.%i32]
  88. // CHECK:STDOUT: %self.param.loc15: %ptr.e71 = value_param runtime_param0
  89. // CHECK:STDOUT: %.loc15_27: type = splice_block %ptr.loc15 [template = constants.%ptr.e71] {
  90. // CHECK:STDOUT: %Self.ref.loc15: type = name_ref Self, constants.%Class [template = constants.%Class]
  91. // CHECK:STDOUT: %ptr.loc15: type = ptr_type %Class [template = constants.%ptr.e71]
  92. // CHECK:STDOUT: }
  93. // CHECK:STDOUT: %self.loc15: %ptr.e71 = bind_name self, %self.param.loc15
  94. // CHECK:STDOUT: %return.param.loc15: ref %i32 = out_param runtime_param1
  95. // CHECK:STDOUT: %return.loc15: ref %i32 = return_slot %return.param.loc15
  96. // CHECK:STDOUT: }
  97. // CHECK:STDOUT: }
  98. // CHECK:STDOUT:
  99. // CHECK:STDOUT: class @Class {
  100. // CHECK:STDOUT: %F.decl: %F.type = fn_decl @F [template = constants.%F] {
  101. // CHECK:STDOUT: %self.patt: %Class = binding_pattern self
  102. // CHECK:STDOUT: %self.param_patt: %Class = value_param_pattern %self.patt, runtime_param0
  103. // CHECK:STDOUT: %return.patt: %i32 = return_slot_pattern
  104. // CHECK:STDOUT: %return.param_patt: %i32 = out_param_pattern %return.patt, runtime_param1
  105. // CHECK:STDOUT: } {
  106. // CHECK:STDOUT: %int_32.loc5: Core.IntLiteral = int_value 32 [template = constants.%int_32]
  107. // CHECK:STDOUT: %i32.loc5: type = class_type @Int, @Int(constants.%int_32) [template = constants.%i32]
  108. // CHECK:STDOUT: %self.param.loc5: %Class = value_param runtime_param0
  109. // CHECK:STDOUT: %Self.ref.loc5: type = name_ref Self, constants.%Class [template = constants.%Class]
  110. // CHECK:STDOUT: %self.loc5: %Class = bind_name self, %self.param.loc5
  111. // CHECK:STDOUT: %return.param.loc5: ref %i32 = out_param runtime_param1
  112. // CHECK:STDOUT: %return.loc5: ref %i32 = return_slot %return.param.loc5
  113. // CHECK:STDOUT: }
  114. // CHECK:STDOUT: %G.decl: %G.type = fn_decl @G [template = constants.%G] {
  115. // CHECK:STDOUT: %self.patt: %ptr.e71 = binding_pattern self
  116. // CHECK:STDOUT: %self.param_patt: %ptr.e71 = value_param_pattern %self.patt, runtime_param0
  117. // CHECK:STDOUT: %.loc15_12: auto = addr_pattern %self.param_patt
  118. // CHECK:STDOUT: %return.patt: %i32 = return_slot_pattern
  119. // CHECK:STDOUT: %return.param_patt: %i32 = out_param_pattern %return.patt, runtime_param1
  120. // CHECK:STDOUT: } {
  121. // CHECK:STDOUT: %int_32.loc6: Core.IntLiteral = int_value 32 [template = constants.%int_32]
  122. // CHECK:STDOUT: %i32.loc6: type = class_type @Int, @Int(constants.%int_32) [template = constants.%i32]
  123. // CHECK:STDOUT: %self.param.loc6: %ptr.e71 = value_param runtime_param0
  124. // CHECK:STDOUT: %.loc6: type = splice_block %ptr.loc6 [template = constants.%ptr.e71] {
  125. // CHECK:STDOUT: %Self.ref.loc6: type = name_ref Self, constants.%Class [template = constants.%Class]
  126. // CHECK:STDOUT: %ptr.loc6: type = ptr_type %Class [template = constants.%ptr.e71]
  127. // CHECK:STDOUT: }
  128. // CHECK:STDOUT: %self.loc6: %ptr.e71 = bind_name self, %self.param.loc6
  129. // CHECK:STDOUT: %return.param.loc6: ref %i32 = out_param runtime_param1
  130. // CHECK:STDOUT: %return.loc6: ref %i32 = return_slot %return.param.loc6
  131. // CHECK:STDOUT: }
  132. // CHECK:STDOUT: %.loc8: %Class.elem = field_decl n, element0 [template]
  133. // CHECK:STDOUT: %complete_type: <witness> = complete_type_witness %struct_type.n [template = constants.%complete_type.9b7]
  134. // CHECK:STDOUT:
  135. // CHECK:STDOUT: !members:
  136. // CHECK:STDOUT: .Self = constants.%Class
  137. // CHECK:STDOUT: .F = %F.decl
  138. // CHECK:STDOUT: .G = %G.decl
  139. // CHECK:STDOUT: .n = %.loc8
  140. // CHECK:STDOUT: complete_type_witness = %complete_type
  141. // CHECK:STDOUT: }
  142. // CHECK:STDOUT:
  143. // CHECK:STDOUT: fn @F[%self.param_patt: %Class]() -> %i32 {
  144. // CHECK:STDOUT: !entry:
  145. // CHECK:STDOUT: %self.ref: %Class = name_ref self, %self.loc11
  146. // CHECK:STDOUT: %n.ref: %Class.elem = name_ref n, @Class.%.loc8 [template = @Class.%.loc8]
  147. // CHECK:STDOUT: %.loc12_14.1: ref %i32 = class_element_access %self.ref, element0
  148. // CHECK:STDOUT: %.loc12_14.2: %i32 = bind_value %.loc12_14.1
  149. // CHECK:STDOUT: return %.loc12_14.2
  150. // CHECK:STDOUT: }
  151. // CHECK:STDOUT:
  152. // CHECK:STDOUT: fn @G[addr %self.param_patt: %ptr.e71]() -> %i32 {
  153. // CHECK:STDOUT: !entry:
  154. // CHECK:STDOUT: %self.ref: %ptr.e71 = name_ref self, %self.loc15
  155. // CHECK:STDOUT: %.loc16_11: ref %Class = deref %self.ref
  156. // CHECK:STDOUT: %n.ref: %Class.elem = name_ref n, @Class.%.loc8 [template = @Class.%.loc8]
  157. // CHECK:STDOUT: %.loc16_17.1: ref %i32 = class_element_access %.loc16_11, element0
  158. // CHECK:STDOUT: %.loc16_17.2: %i32 = bind_value %.loc16_17.1
  159. // CHECK:STDOUT: return %.loc16_17.2
  160. // CHECK:STDOUT: }
  161. // CHECK:STDOUT:
  162. // CHECK:STDOUT: --- fail_return_self_value.carbon
  163. // CHECK:STDOUT:
  164. // CHECK:STDOUT: constants {
  165. // CHECK:STDOUT: %Class: type = class_type @Class [template]
  166. // CHECK:STDOUT: %F.type: type = fn_type @F [template]
  167. // CHECK:STDOUT: %F: %F.type = struct_value () [template]
  168. // CHECK:STDOUT: %empty_struct_type: type = struct_type {} [template]
  169. // CHECK:STDOUT: %complete_type: <witness> = complete_type_witness %empty_struct_type [template]
  170. // CHECK:STDOUT: }
  171. // CHECK:STDOUT:
  172. // CHECK:STDOUT: imports {
  173. // CHECK:STDOUT: %Core: <namespace> = namespace file.%Core.import, [template] {
  174. // CHECK:STDOUT: .ImplicitAs = %import_ref.a69
  175. // CHECK:STDOUT: import Core//prelude
  176. // CHECK:STDOUT: import Core//prelude/...
  177. // CHECK:STDOUT: }
  178. // CHECK:STDOUT: }
  179. // CHECK:STDOUT:
  180. // CHECK:STDOUT: file {
  181. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  182. // CHECK:STDOUT: .Core = imports.%Core
  183. // CHECK:STDOUT: .Class = %Class.decl
  184. // CHECK:STDOUT: }
  185. // CHECK:STDOUT: %Core.import = import Core
  186. // CHECK:STDOUT: %Class.decl: type = class_decl @Class [template = constants.%Class] {} {}
  187. // CHECK:STDOUT: }
  188. // CHECK:STDOUT:
  189. // CHECK:STDOUT: class @Class {
  190. // CHECK:STDOUT: %F.decl: %F.type = fn_decl @F [template = constants.%F] {
  191. // CHECK:STDOUT: %self.patt: %Class = binding_pattern self
  192. // CHECK:STDOUT: %self.param_patt: %Class = value_param_pattern %self.patt, runtime_param0
  193. // CHECK:STDOUT: %return.patt: <error> = return_slot_pattern
  194. // CHECK:STDOUT: %return.param_patt: <error> = out_param_pattern %return.patt, runtime_param1
  195. // CHECK:STDOUT: } {
  196. // CHECK:STDOUT: %self.ref: %Class = name_ref self, %self
  197. // CHECK:STDOUT: %.loc11: type = converted %self.ref, <error> [template = <error>]
  198. // CHECK:STDOUT: %self.param: %Class = value_param runtime_param0
  199. // CHECK:STDOUT: %Self.ref: type = name_ref Self, constants.%Class [template = constants.%Class]
  200. // CHECK:STDOUT: %self: %Class = bind_name self, %self.param
  201. // CHECK:STDOUT: %return.param: ref <error> = out_param runtime_param1
  202. // CHECK:STDOUT: %return: ref <error> = return_slot %return.param
  203. // CHECK:STDOUT: }
  204. // CHECK:STDOUT: %complete_type: <witness> = complete_type_witness %empty_struct_type [template = constants.%complete_type]
  205. // CHECK:STDOUT:
  206. // CHECK:STDOUT: !members:
  207. // CHECK:STDOUT: .Self = constants.%Class
  208. // CHECK:STDOUT: .F = %F.decl
  209. // CHECK:STDOUT: complete_type_witness = %complete_type
  210. // CHECK:STDOUT: }
  211. // CHECK:STDOUT:
  212. // CHECK:STDOUT: fn @F[%self.param_patt: %Class]() -> <error>;
  213. // CHECK:STDOUT: