self.carbon 12 KB

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