self.carbon 12 KB

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