member_inline.carbon 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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/generic/member_inline.carbon
  8. // TIP: To dump output, run:
  9. // TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/class/generic/member_inline.carbon
  10. // --- member_inline.carbon
  11. library "member_inline.carbon";
  12. class Class(T:! type) {
  13. fn F(n: T) -> T {
  14. return n;
  15. }
  16. fn G[self: Self]() -> T {
  17. return self.n;
  18. }
  19. var n: T;
  20. }
  21. // --- fail_member_inline.carbon
  22. library "fail_member_inline.carbon";
  23. class C(T:! type) {
  24. fn F() {
  25. // CHECK:STDERR: fail_member_inline.carbon:[[@LINE+3]]:5: error: expression cannot be used as a value [UseOfNonExprAsValue]
  26. // CHECK:STDERR: data;
  27. // CHECK:STDERR: ^~~~
  28. data;
  29. }
  30. var data: {};
  31. }
  32. // CHECK:STDOUT: --- member_inline.carbon
  33. // CHECK:STDOUT:
  34. // CHECK:STDOUT: constants {
  35. // CHECK:STDOUT: %T: type = bind_symbolic_name T, 0 [symbolic]
  36. // CHECK:STDOUT: %T.patt: type = symbolic_binding_pattern T, 0 [symbolic]
  37. // CHECK:STDOUT: %Class.type: type = generic_class_type @Class [template]
  38. // CHECK:STDOUT: %.1: type = tuple_type () [template]
  39. // CHECK:STDOUT: %Class.1: %Class.type = struct_value () [template]
  40. // CHECK:STDOUT: %Class.2: type = class_type @Class, @Class(%T) [symbolic]
  41. // CHECK:STDOUT: %F.type: type = fn_type @F, @Class(%T) [symbolic]
  42. // CHECK:STDOUT: %F: %F.type = struct_value () [symbolic]
  43. // CHECK:STDOUT: %G.type: type = fn_type @G, @Class(%T) [symbolic]
  44. // CHECK:STDOUT: %G: %G.type = struct_value () [symbolic]
  45. // CHECK:STDOUT: %.2: type = unbound_element_type %Class.2, %T [symbolic]
  46. // CHECK:STDOUT: %.3: type = struct_type {.n: %T} [symbolic]
  47. // CHECK:STDOUT: %.4: <witness> = complete_type_witness %.3 [symbolic]
  48. // CHECK:STDOUT: %.5: type = ptr_type %.3 [symbolic]
  49. // CHECK:STDOUT: }
  50. // CHECK:STDOUT:
  51. // CHECK:STDOUT: imports {
  52. // CHECK:STDOUT: %Core: <namespace> = namespace file.%Core.import, [template] {
  53. // CHECK:STDOUT: import Core//prelude
  54. // CHECK:STDOUT: import Core//prelude/operators
  55. // CHECK:STDOUT: import Core//prelude/types
  56. // CHECK:STDOUT: import Core//prelude/operators/arithmetic
  57. // CHECK:STDOUT: import Core//prelude/operators/as
  58. // CHECK:STDOUT: import Core//prelude/operators/index
  59. // CHECK:STDOUT: import Core//prelude/operators/bitwise
  60. // CHECK:STDOUT: import Core//prelude/operators/comparison
  61. // CHECK:STDOUT: import Core//prelude/types/bool
  62. // CHECK:STDOUT: }
  63. // CHECK:STDOUT: }
  64. // CHECK:STDOUT:
  65. // CHECK:STDOUT: file {
  66. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  67. // CHECK:STDOUT: .Core = imports.%Core
  68. // CHECK:STDOUT: .Class = %Class.decl
  69. // CHECK:STDOUT: }
  70. // CHECK:STDOUT: %Core.import = import Core
  71. // CHECK:STDOUT: %Class.decl: %Class.type = class_decl @Class [template = constants.%Class.1] {
  72. // CHECK:STDOUT: %T.patt.loc4_13.1: type = symbolic_binding_pattern T, 0 [symbolic = %T.patt.loc4_13.2 (constants.%T.patt)]
  73. // CHECK:STDOUT: %T.param_patt: type = value_param_pattern %T.patt.loc4_13.1, runtime_param<invalid> [symbolic = %T.patt.loc4_13.2 (constants.%T.patt)]
  74. // CHECK:STDOUT: } {
  75. // CHECK:STDOUT: %T.param: type = value_param runtime_param<invalid>
  76. // CHECK:STDOUT: %T.loc4_13.1: type = bind_symbolic_name T, 0, %T.param [symbolic = %T.loc4_13.2 (constants.%T)]
  77. // CHECK:STDOUT: }
  78. // CHECK:STDOUT: }
  79. // CHECK:STDOUT:
  80. // CHECK:STDOUT: generic class @Class(%T.loc4_13.1: type) {
  81. // CHECK:STDOUT: %T.loc4_13.2: type = bind_symbolic_name T, 0 [symbolic = %T.loc4_13.2 (constants.%T)]
  82. // CHECK:STDOUT: %T.patt.loc4_13.2: type = symbolic_binding_pattern T, 0 [symbolic = %T.patt.loc4_13.2 (constants.%T.patt)]
  83. // CHECK:STDOUT:
  84. // CHECK:STDOUT: !definition:
  85. // CHECK:STDOUT: %F.type: type = fn_type @F, @Class(%T.loc4_13.2) [symbolic = %F.type (constants.%F.type)]
  86. // CHECK:STDOUT: %F: @Class.%F.type (%F.type) = struct_value () [symbolic = %F (constants.%F)]
  87. // CHECK:STDOUT: %G.type: type = fn_type @G, @Class(%T.loc4_13.2) [symbolic = %G.type (constants.%G.type)]
  88. // CHECK:STDOUT: %G: @Class.%G.type (%G.type) = struct_value () [symbolic = %G (constants.%G)]
  89. // CHECK:STDOUT: %Class: type = class_type @Class, @Class(%T.loc4_13.2) [symbolic = %Class (constants.%Class.2)]
  90. // CHECK:STDOUT: %.loc13_8.2: type = unbound_element_type @Class.%Class (%Class.2), @Class.%T.loc4_13.2 (%T) [symbolic = %.loc13_8.2 (constants.%.2)]
  91. // CHECK:STDOUT: %.loc14_1.2: type = struct_type {.n: @Class.%T.loc4_13.2 (%T)} [symbolic = %.loc14_1.2 (constants.%.3)]
  92. // CHECK:STDOUT: %.loc14_1.3: <witness> = complete_type_witness @Class.%.loc14_1.2 (%.3) [symbolic = %.loc14_1.3 (constants.%.4)]
  93. // CHECK:STDOUT:
  94. // CHECK:STDOUT: class {
  95. // CHECK:STDOUT: %F.decl: @Class.%F.type (%F.type) = fn_decl @F [symbolic = @Class.%F (constants.%F)] {
  96. // CHECK:STDOUT: %n.patt: @F.%T (%T) = binding_pattern n
  97. // CHECK:STDOUT: %n.param_patt: @F.%T (%T) = value_param_pattern %n.patt, runtime_param0
  98. // CHECK:STDOUT: %return.patt: @F.%T (%T) = return_slot_pattern
  99. // CHECK:STDOUT: %return.param_patt: @F.%T (%T) = out_param_pattern %return.patt, runtime_param1
  100. // CHECK:STDOUT: } {
  101. // CHECK:STDOUT: %T.ref.loc5_11: type = name_ref T, @Class.%T.loc4_13.1 [symbolic = %T (constants.%T)]
  102. // CHECK:STDOUT: %T.ref.loc5_17: type = name_ref T, @Class.%T.loc4_13.1 [symbolic = %T (constants.%T)]
  103. // CHECK:STDOUT: %n.param: @F.%T (%T) = value_param runtime_param0
  104. // CHECK:STDOUT: %n: @F.%T (%T) = bind_name n, %n.param
  105. // CHECK:STDOUT: %return.param: ref @F.%T (%T) = out_param runtime_param1
  106. // CHECK:STDOUT: %return: ref @F.%T (%T) = return_slot %return.param
  107. // CHECK:STDOUT: }
  108. // CHECK:STDOUT: %G.decl: @Class.%G.type (%G.type) = fn_decl @G [symbolic = @Class.%G (constants.%G)] {
  109. // CHECK:STDOUT: %self.patt: @G.%Class (%Class.2) = binding_pattern self
  110. // CHECK:STDOUT: %self.param_patt: @G.%Class (%Class.2) = value_param_pattern %self.patt, runtime_param0
  111. // CHECK:STDOUT: %return.patt: @G.%T (%T) = return_slot_pattern
  112. // CHECK:STDOUT: %return.param_patt: @G.%T (%T) = out_param_pattern %return.patt, runtime_param1
  113. // CHECK:STDOUT: } {
  114. // CHECK:STDOUT: %.loc9: type = specific_constant constants.%Class.2, @Class(constants.%T) [symbolic = %Class (constants.%Class.2)]
  115. // CHECK:STDOUT: %Self.ref: type = name_ref Self, %.loc9 [symbolic = %Class (constants.%Class.2)]
  116. // CHECK:STDOUT: %T.ref: type = name_ref T, @Class.%T.loc4_13.1 [symbolic = %T (constants.%T)]
  117. // CHECK:STDOUT: %self.param: @G.%Class (%Class.2) = value_param runtime_param0
  118. // CHECK:STDOUT: %self: @G.%Class (%Class.2) = bind_name self, %self.param
  119. // CHECK:STDOUT: %return.param: ref @G.%T (%T) = out_param runtime_param1
  120. // CHECK:STDOUT: %return: ref @G.%T (%T) = return_slot %return.param
  121. // CHECK:STDOUT: }
  122. // CHECK:STDOUT: %T.ref: type = name_ref T, %T.loc4_13.1 [symbolic = %T.loc4_13.2 (constants.%T)]
  123. // CHECK:STDOUT: %.loc13_8.1: @Class.%.loc13_8.2 (%.2) = field_decl n, element0 [template]
  124. // CHECK:STDOUT: %.loc14_1.1: <witness> = complete_type_witness %.3 [symbolic = %.loc14_1.3 (constants.%.4)]
  125. // CHECK:STDOUT:
  126. // CHECK:STDOUT: !members:
  127. // CHECK:STDOUT: .Self = constants.%Class.2
  128. // CHECK:STDOUT: .F = %F.decl
  129. // CHECK:STDOUT: .G = %G.decl
  130. // CHECK:STDOUT: .n = %.loc13_8.1
  131. // CHECK:STDOUT: }
  132. // CHECK:STDOUT: }
  133. // CHECK:STDOUT:
  134. // CHECK:STDOUT: generic fn @F(@Class.%T.loc4_13.1: type) {
  135. // CHECK:STDOUT: %T: type = bind_symbolic_name T, 0 [symbolic = %T (constants.%T)]
  136. // CHECK:STDOUT:
  137. // CHECK:STDOUT: !definition:
  138. // CHECK:STDOUT:
  139. // CHECK:STDOUT: fn(%n.param_patt: @F.%T (%T)) -> @F.%T (%T) {
  140. // CHECK:STDOUT: !entry:
  141. // CHECK:STDOUT: %n.ref: @F.%T (%T) = name_ref n, %n
  142. // CHECK:STDOUT: return %n.ref
  143. // CHECK:STDOUT: }
  144. // CHECK:STDOUT: }
  145. // CHECK:STDOUT:
  146. // CHECK:STDOUT: generic fn @G(@Class.%T.loc4_13.1: type) {
  147. // CHECK:STDOUT: %T: type = bind_symbolic_name T, 0 [symbolic = %T (constants.%T)]
  148. // CHECK:STDOUT: %Class: type = class_type @Class, @Class(%T) [symbolic = %Class (constants.%Class.2)]
  149. // CHECK:STDOUT:
  150. // CHECK:STDOUT: !definition:
  151. // CHECK:STDOUT: %.loc10_16.3: type = unbound_element_type @G.%Class (%Class.2), @G.%T (%T) [symbolic = %.loc10_16.3 (constants.%.2)]
  152. // CHECK:STDOUT:
  153. // CHECK:STDOUT: fn[%self.param_patt: @G.%Class (%Class.2)]() -> @G.%T (%T) {
  154. // CHECK:STDOUT: !entry:
  155. // CHECK:STDOUT: %self.ref: @G.%Class (%Class.2) = name_ref self, %self
  156. // CHECK:STDOUT: %n.ref: @G.%.loc10_16.3 (%.2) = name_ref n, @Class.%.loc13_8.1 [template = @Class.%.loc13_8.1]
  157. // CHECK:STDOUT: %.loc10_16.1: ref @G.%T (%T) = class_element_access %self.ref, element0
  158. // CHECK:STDOUT: %.loc10_16.2: @G.%T (%T) = bind_value %.loc10_16.1
  159. // CHECK:STDOUT: return %.loc10_16.2
  160. // CHECK:STDOUT: }
  161. // CHECK:STDOUT: }
  162. // CHECK:STDOUT:
  163. // CHECK:STDOUT: specific @Class(constants.%T) {
  164. // CHECK:STDOUT: %T.loc4_13.2 => constants.%T
  165. // CHECK:STDOUT: %T.patt.loc4_13.2 => constants.%T
  166. // CHECK:STDOUT:
  167. // CHECK:STDOUT: !definition:
  168. // CHECK:STDOUT: %F.type => constants.%F.type
  169. // CHECK:STDOUT: %F => constants.%F
  170. // CHECK:STDOUT: %G.type => constants.%G.type
  171. // CHECK:STDOUT: %G => constants.%G
  172. // CHECK:STDOUT: %Class => constants.%Class.2
  173. // CHECK:STDOUT: %.loc13_8.2 => constants.%.2
  174. // CHECK:STDOUT: %.loc14_1.2 => constants.%.3
  175. // CHECK:STDOUT: %.loc14_1.3 => constants.%.4
  176. // CHECK:STDOUT: }
  177. // CHECK:STDOUT:
  178. // CHECK:STDOUT: specific @F(constants.%T) {
  179. // CHECK:STDOUT: %T => constants.%T
  180. // CHECK:STDOUT: }
  181. // CHECK:STDOUT:
  182. // CHECK:STDOUT: specific @Class(@G.%T) {
  183. // CHECK:STDOUT: %T.loc4_13.2 => constants.%T
  184. // CHECK:STDOUT: %T.patt.loc4_13.2 => constants.%T
  185. // CHECK:STDOUT: }
  186. // CHECK:STDOUT:
  187. // CHECK:STDOUT: specific @G(constants.%T) {
  188. // CHECK:STDOUT: %T => constants.%T
  189. // CHECK:STDOUT: %Class => constants.%Class.2
  190. // CHECK:STDOUT: }
  191. // CHECK:STDOUT:
  192. // CHECK:STDOUT: specific @Class(@Class.%T.loc4_13.2) {
  193. // CHECK:STDOUT: %T.loc4_13.2 => constants.%T
  194. // CHECK:STDOUT: %T.patt.loc4_13.2 => constants.%T
  195. // CHECK:STDOUT: }
  196. // CHECK:STDOUT:
  197. // CHECK:STDOUT: --- fail_member_inline.carbon
  198. // CHECK:STDOUT:
  199. // CHECK:STDOUT: constants {
  200. // CHECK:STDOUT: %T: type = bind_symbolic_name T, 0 [symbolic]
  201. // CHECK:STDOUT: %T.patt: type = symbolic_binding_pattern T, 0 [symbolic]
  202. // CHECK:STDOUT: %C.type: type = generic_class_type @C [template]
  203. // CHECK:STDOUT: %.1: type = tuple_type () [template]
  204. // CHECK:STDOUT: %C.1: %C.type = struct_value () [template]
  205. // CHECK:STDOUT: %C.2: type = class_type @C, @C(%T) [symbolic]
  206. // CHECK:STDOUT: %F.type: type = fn_type @F, @C(%T) [symbolic]
  207. // CHECK:STDOUT: %F: %F.type = struct_value () [symbolic]
  208. // CHECK:STDOUT: %.2: type = struct_type {} [template]
  209. // CHECK:STDOUT: %.3: type = unbound_element_type %C.2, %.2 [symbolic]
  210. // CHECK:STDOUT: %.4: type = struct_type {.data: %.2} [template]
  211. // CHECK:STDOUT: %.5: <witness> = complete_type_witness %.4 [template]
  212. // CHECK:STDOUT: }
  213. // CHECK:STDOUT:
  214. // CHECK:STDOUT: imports {
  215. // CHECK:STDOUT: %Core: <namespace> = namespace file.%Core.import, [template] {
  216. // CHECK:STDOUT: import Core//prelude
  217. // CHECK:STDOUT: import Core//prelude/operators
  218. // CHECK:STDOUT: import Core//prelude/types
  219. // CHECK:STDOUT: import Core//prelude/operators/arithmetic
  220. // CHECK:STDOUT: import Core//prelude/operators/as
  221. // CHECK:STDOUT: import Core//prelude/operators/index
  222. // CHECK:STDOUT: import Core//prelude/operators/bitwise
  223. // CHECK:STDOUT: import Core//prelude/operators/comparison
  224. // CHECK:STDOUT: import Core//prelude/types/bool
  225. // CHECK:STDOUT: }
  226. // CHECK:STDOUT: }
  227. // CHECK:STDOUT:
  228. // CHECK:STDOUT: file {
  229. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  230. // CHECK:STDOUT: .Core = imports.%Core
  231. // CHECK:STDOUT: .C = %C.decl
  232. // CHECK:STDOUT: }
  233. // CHECK:STDOUT: %Core.import = import Core
  234. // CHECK:STDOUT: %C.decl: %C.type = class_decl @C [template = constants.%C.1] {
  235. // CHECK:STDOUT: %T.patt.loc4_9.1: type = symbolic_binding_pattern T, 0 [symbolic = %T.patt.loc4_9.2 (constants.%T.patt)]
  236. // CHECK:STDOUT: %T.param_patt: type = value_param_pattern %T.patt.loc4_9.1, runtime_param<invalid> [symbolic = %T.patt.loc4_9.2 (constants.%T.patt)]
  237. // CHECK:STDOUT: } {
  238. // CHECK:STDOUT: %T.param: type = value_param runtime_param<invalid>
  239. // CHECK:STDOUT: %T.loc4_9.1: type = bind_symbolic_name T, 0, %T.param [symbolic = %T.loc4_9.2 (constants.%T)]
  240. // CHECK:STDOUT: }
  241. // CHECK:STDOUT: }
  242. // CHECK:STDOUT:
  243. // CHECK:STDOUT: generic class @C(%T.loc4_9.1: type) {
  244. // CHECK:STDOUT: %T.loc4_9.2: type = bind_symbolic_name T, 0 [symbolic = %T.loc4_9.2 (constants.%T)]
  245. // CHECK:STDOUT: %T.patt.loc4_9.2: type = symbolic_binding_pattern T, 0 [symbolic = %T.patt.loc4_9.2 (constants.%T.patt)]
  246. // CHECK:STDOUT:
  247. // CHECK:STDOUT: !definition:
  248. // CHECK:STDOUT: %F.type: type = fn_type @F, @C(%T.loc4_9.2) [symbolic = %F.type (constants.%F.type)]
  249. // CHECK:STDOUT: %F: @C.%F.type (%F.type) = struct_value () [symbolic = %F (constants.%F)]
  250. // CHECK:STDOUT: %C: type = class_type @C, @C(%T.loc4_9.2) [symbolic = %C (constants.%C.2)]
  251. // CHECK:STDOUT: %.loc11_11.2: type = unbound_element_type @C.%C (%C.2), %.2 [symbolic = %.loc11_11.2 (constants.%.3)]
  252. // CHECK:STDOUT:
  253. // CHECK:STDOUT: class {
  254. // CHECK:STDOUT: %F.decl: @C.%F.type (%F.type) = fn_decl @F [symbolic = @C.%F (constants.%F)] {} {}
  255. // CHECK:STDOUT: %.loc11_14.1: %.2 = struct_literal ()
  256. // CHECK:STDOUT: %.loc11_14.2: type = converted %.loc11_14.1, constants.%.2 [template = constants.%.2]
  257. // CHECK:STDOUT: %.loc11_11.1: @C.%.loc11_11.2 (%.3) = field_decl data, element0 [template]
  258. // CHECK:STDOUT: %.loc12: <witness> = complete_type_witness %.4 [template = constants.%.5]
  259. // CHECK:STDOUT:
  260. // CHECK:STDOUT: !members:
  261. // CHECK:STDOUT: .Self = constants.%C.2
  262. // CHECK:STDOUT: .F = %F.decl
  263. // CHECK:STDOUT: .data = %.loc11_11.1
  264. // CHECK:STDOUT: }
  265. // CHECK:STDOUT: }
  266. // CHECK:STDOUT:
  267. // CHECK:STDOUT: generic fn @F(@C.%T.loc4_9.1: type) {
  268. // CHECK:STDOUT: !definition:
  269. // CHECK:STDOUT: %T: type = bind_symbolic_name T, 0 [symbolic = %T (constants.%T)]
  270. // CHECK:STDOUT: %C: type = class_type @C, @C(%T) [symbolic = %C (constants.%C.2)]
  271. // CHECK:STDOUT: %.loc9: type = unbound_element_type @F.%C (%C.2), %.2 [symbolic = %.loc9 (constants.%.3)]
  272. // CHECK:STDOUT:
  273. // CHECK:STDOUT: fn() {
  274. // CHECK:STDOUT: !entry:
  275. // CHECK:STDOUT: %data.ref: @F.%.loc9 (%.3) = name_ref data, @C.%.loc11_11.1 [template = @C.%.loc11_11.1]
  276. // CHECK:STDOUT: return
  277. // CHECK:STDOUT: }
  278. // CHECK:STDOUT: }
  279. // CHECK:STDOUT:
  280. // CHECK:STDOUT: specific @C(constants.%T) {
  281. // CHECK:STDOUT: %T.loc4_9.2 => constants.%T
  282. // CHECK:STDOUT: %T.patt.loc4_9.2 => constants.%T
  283. // CHECK:STDOUT:
  284. // CHECK:STDOUT: !definition:
  285. // CHECK:STDOUT: %F.type => constants.%F.type
  286. // CHECK:STDOUT: %F => constants.%F
  287. // CHECK:STDOUT: %C => constants.%C.2
  288. // CHECK:STDOUT: %.loc11_11.2 => constants.%.3
  289. // CHECK:STDOUT: }
  290. // CHECK:STDOUT:
  291. // CHECK:STDOUT: specific @F(constants.%T) {}
  292. // CHECK:STDOUT:
  293. // CHECK:STDOUT: specific @C(@C.%T.loc4_9.2) {
  294. // CHECK:STDOUT: %T.loc4_9.2 => constants.%T
  295. // CHECK:STDOUT: %T.patt.loc4_9.2 => constants.%T
  296. // CHECK:STDOUT: }
  297. // CHECK:STDOUT:
  298. // CHECK:STDOUT: specific @C(@F.%T) {
  299. // CHECK:STDOUT: %T.loc4_9.2 => constants.%T
  300. // CHECK:STDOUT: %T.patt.loc4_9.2 => constants.%T
  301. // CHECK:STDOUT: }
  302. // CHECK:STDOUT: