member_inline.carbon 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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
  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: %Class.type: type = generic_class_type @Class [template]
  37. // CHECK:STDOUT: %.1: type = tuple_type () [template]
  38. // CHECK:STDOUT: %Class.1: %Class.type = struct_value () [template]
  39. // CHECK:STDOUT: %Class.2: type = class_type @Class, @Class(%T) [symbolic]
  40. // CHECK:STDOUT: %F.type: type = fn_type @F, @Class(%T) [symbolic]
  41. // CHECK:STDOUT: %F: %F.type = struct_value () [symbolic]
  42. // CHECK:STDOUT: %G.type: type = fn_type @G, @Class(%T) [symbolic]
  43. // CHECK:STDOUT: %G: %G.type = struct_value () [symbolic]
  44. // CHECK:STDOUT: %.2: type = unbound_element_type %Class.2, %T [symbolic]
  45. // CHECK:STDOUT: %.3: type = struct_type {.n: %T} [symbolic]
  46. // CHECK:STDOUT: %.4: <witness> = complete_type_witness %.3 [symbolic]
  47. // CHECK:STDOUT: %.5: type = ptr_type %.3 [symbolic]
  48. // CHECK:STDOUT: }
  49. // CHECK:STDOUT:
  50. // CHECK:STDOUT: imports {
  51. // CHECK:STDOUT: %Core: <namespace> = namespace file.%Core.import, [template] {
  52. // CHECK:STDOUT: import Core//prelude
  53. // CHECK:STDOUT: import Core//prelude/operators
  54. // CHECK:STDOUT: import Core//prelude/types
  55. // CHECK:STDOUT: import Core//prelude/operators/arithmetic
  56. // CHECK:STDOUT: import Core//prelude/operators/as
  57. // CHECK:STDOUT: import Core//prelude/operators/bitwise
  58. // CHECK:STDOUT: import Core//prelude/operators/comparison
  59. // CHECK:STDOUT: import Core//prelude/types/bool
  60. // CHECK:STDOUT: }
  61. // CHECK:STDOUT: }
  62. // CHECK:STDOUT:
  63. // CHECK:STDOUT: file {
  64. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  65. // CHECK:STDOUT: .Core = imports.%Core
  66. // CHECK:STDOUT: .Class = %Class.decl
  67. // CHECK:STDOUT: }
  68. // CHECK:STDOUT: %Core.import = import Core
  69. // CHECK:STDOUT: %Class.decl: %Class.type = class_decl @Class [template = constants.%Class.1] {
  70. // CHECK:STDOUT: %T.patt: type = symbolic_binding_pattern T, 0
  71. // CHECK:STDOUT: } {
  72. // CHECK:STDOUT: %T.param: type = param T, runtime_param<invalid>
  73. // CHECK:STDOUT: %T.loc4_13.1: type = bind_symbolic_name T, 0, %T.param [symbolic = %T.loc4_13.2 (constants.%T)]
  74. // CHECK:STDOUT: }
  75. // CHECK:STDOUT: }
  76. // CHECK:STDOUT:
  77. // CHECK:STDOUT: generic class @Class(%T.loc4_13.1: type) {
  78. // CHECK:STDOUT: %T.loc4_13.2: type = bind_symbolic_name T, 0 [symbolic = %T.loc4_13.2 (constants.%T)]
  79. // CHECK:STDOUT:
  80. // CHECK:STDOUT: !definition:
  81. // CHECK:STDOUT: %F.type: type = fn_type @F, @Class(%T.loc4_13.2) [symbolic = %F.type (constants.%F.type)]
  82. // CHECK:STDOUT: %F: @Class.%F.type (%F.type) = struct_value () [symbolic = %F (constants.%F)]
  83. // CHECK:STDOUT: %G.type: type = fn_type @G, @Class(%T.loc4_13.2) [symbolic = %G.type (constants.%G.type)]
  84. // CHECK:STDOUT: %G: @Class.%G.type (%G.type) = struct_value () [symbolic = %G (constants.%G)]
  85. // CHECK:STDOUT: %Class: type = class_type @Class, @Class(%T.loc4_13.2) [symbolic = %Class (constants.%Class.2)]
  86. // 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)]
  87. // CHECK:STDOUT: %.loc14_1.2: type = struct_type {.n: @Class.%T.loc4_13.2 (%T)} [symbolic = %.loc14_1.2 (constants.%.3)]
  88. // CHECK:STDOUT: %.loc14_1.3: <witness> = complete_type_witness @Class.%.loc14_1.2 (%.3) [symbolic = %.loc14_1.3 (constants.%.4)]
  89. // CHECK:STDOUT:
  90. // CHECK:STDOUT: class {
  91. // CHECK:STDOUT: %F.decl: @Class.%F.type (%F.type) = fn_decl @F [symbolic = @Class.%F (constants.%F)] {
  92. // CHECK:STDOUT: %n.patt: @F.%T (%T) = binding_pattern n
  93. // CHECK:STDOUT: } {
  94. // CHECK:STDOUT: %T.ref.loc5_11: type = name_ref T, @Class.%T.loc4_13.1 [symbolic = %T (constants.%T)]
  95. // CHECK:STDOUT: %n.param: @F.%T (%T) = param n, runtime_param0
  96. // CHECK:STDOUT: %n: @F.%T (%T) = bind_name n, %n.param
  97. // CHECK:STDOUT: %T.ref.loc5_17: type = name_ref T, @Class.%T.loc4_13.1 [symbolic = %T (constants.%T)]
  98. // CHECK:STDOUT: %return: ref @F.%T (%T) = var <return slot>
  99. // CHECK:STDOUT: }
  100. // CHECK:STDOUT: %G.decl: @Class.%G.type (%G.type) = fn_decl @G [symbolic = @Class.%G (constants.%G)] {
  101. // CHECK:STDOUT: %self.patt: @G.%Class (%Class.2) = binding_pattern self
  102. // CHECK:STDOUT: } {
  103. // CHECK:STDOUT: %.loc9: type = specific_constant constants.%Class.2, @Class(constants.%T) [symbolic = %Class (constants.%Class.2)]
  104. // CHECK:STDOUT: %Self.ref: type = name_ref Self, %.loc9 [symbolic = %Class (constants.%Class.2)]
  105. // CHECK:STDOUT: %self.param: @G.%Class (%Class.2) = param self, runtime_param0
  106. // CHECK:STDOUT: %self: @G.%Class (%Class.2) = bind_name self, %self.param
  107. // CHECK:STDOUT: %T.ref: type = name_ref T, @Class.%T.loc4_13.1 [symbolic = %T (constants.%T)]
  108. // CHECK:STDOUT: %return: ref @G.%T (%T) = var <return slot>
  109. // CHECK:STDOUT: }
  110. // CHECK:STDOUT: %T.ref: type = name_ref T, %T.loc4_13.1 [symbolic = %T.loc4_13.2 (constants.%T)]
  111. // CHECK:STDOUT: %.loc13_8.1: @Class.%.loc13_8.2 (%.2) = field_decl n, element0 [template]
  112. // CHECK:STDOUT: %.loc14_1.1: <witness> = complete_type_witness %.3 [symbolic = %.loc14_1.3 (constants.%.4)]
  113. // CHECK:STDOUT:
  114. // CHECK:STDOUT: !members:
  115. // CHECK:STDOUT: .Self = constants.%Class.2
  116. // CHECK:STDOUT: .F = %F.decl
  117. // CHECK:STDOUT: .G = %G.decl
  118. // CHECK:STDOUT: .n = %.loc13_8.1
  119. // CHECK:STDOUT: }
  120. // CHECK:STDOUT: }
  121. // CHECK:STDOUT:
  122. // CHECK:STDOUT: generic fn @F(@Class.%T.loc4_13.1: type) {
  123. // CHECK:STDOUT: %T: type = bind_symbolic_name T, 0 [symbolic = %T (constants.%T)]
  124. // CHECK:STDOUT:
  125. // CHECK:STDOUT: !definition:
  126. // CHECK:STDOUT:
  127. // CHECK:STDOUT: fn(%n: @F.%T (%T)) -> @F.%T (%T) {
  128. // CHECK:STDOUT: !entry:
  129. // CHECK:STDOUT: %n.ref: @F.%T (%T) = name_ref n, %n
  130. // CHECK:STDOUT: return %n.ref
  131. // CHECK:STDOUT: }
  132. // CHECK:STDOUT: }
  133. // CHECK:STDOUT:
  134. // CHECK:STDOUT: generic fn @G(@Class.%T.loc4_13.1: type) {
  135. // CHECK:STDOUT: %T: type = bind_symbolic_name T, 0 [symbolic = %T (constants.%T)]
  136. // CHECK:STDOUT: %Class: type = class_type @Class, @Class(%T) [symbolic = %Class (constants.%Class.2)]
  137. // CHECK:STDOUT:
  138. // CHECK:STDOUT: !definition:
  139. // CHECK:STDOUT: %.loc10_16.3: type = unbound_element_type @G.%Class (%Class.2), @G.%T (%T) [symbolic = %.loc10_16.3 (constants.%.2)]
  140. // CHECK:STDOUT:
  141. // CHECK:STDOUT: fn[%self: @G.%Class (%Class.2)]() -> @G.%T (%T) {
  142. // CHECK:STDOUT: !entry:
  143. // CHECK:STDOUT: %self.ref: @G.%Class (%Class.2) = name_ref self, %self
  144. // CHECK:STDOUT: %n.ref: @G.%.loc10_16.3 (%.2) = name_ref n, @Class.%.loc13_8.1 [template = @Class.%.loc13_8.1]
  145. // CHECK:STDOUT: %.loc10_16.1: ref @G.%T (%T) = class_element_access %self.ref, element0
  146. // CHECK:STDOUT: %.loc10_16.2: @G.%T (%T) = bind_value %.loc10_16.1
  147. // CHECK:STDOUT: return %.loc10_16.2
  148. // CHECK:STDOUT: }
  149. // CHECK:STDOUT: }
  150. // CHECK:STDOUT:
  151. // CHECK:STDOUT: specific @Class(constants.%T) {
  152. // CHECK:STDOUT: %T.loc4_13.2 => constants.%T
  153. // CHECK:STDOUT:
  154. // CHECK:STDOUT: !definition:
  155. // CHECK:STDOUT: %F.type => constants.%F.type
  156. // CHECK:STDOUT: %F => constants.%F
  157. // CHECK:STDOUT: %G.type => constants.%G.type
  158. // CHECK:STDOUT: %G => constants.%G
  159. // CHECK:STDOUT: %Class => constants.%Class.2
  160. // CHECK:STDOUT: %.loc13_8.2 => constants.%.2
  161. // CHECK:STDOUT: %.loc14_1.2 => constants.%.3
  162. // CHECK:STDOUT: %.loc14_1.3 => constants.%.4
  163. // CHECK:STDOUT: }
  164. // CHECK:STDOUT:
  165. // CHECK:STDOUT: specific @F(constants.%T) {
  166. // CHECK:STDOUT: %T => constants.%T
  167. // CHECK:STDOUT: }
  168. // CHECK:STDOUT:
  169. // CHECK:STDOUT: specific @Class(@G.%T) {
  170. // CHECK:STDOUT: %T.loc4_13.2 => constants.%T
  171. // CHECK:STDOUT: }
  172. // CHECK:STDOUT:
  173. // CHECK:STDOUT: specific @G(constants.%T) {
  174. // CHECK:STDOUT: %T => constants.%T
  175. // CHECK:STDOUT: %Class => constants.%Class.2
  176. // CHECK:STDOUT: }
  177. // CHECK:STDOUT:
  178. // CHECK:STDOUT: specific @Class(@Class.%T.loc4_13.2) {
  179. // CHECK:STDOUT: %T.loc4_13.2 => constants.%T
  180. // CHECK:STDOUT: }
  181. // CHECK:STDOUT:
  182. // CHECK:STDOUT: --- fail_member_inline.carbon
  183. // CHECK:STDOUT:
  184. // CHECK:STDOUT: constants {
  185. // CHECK:STDOUT: %T: type = bind_symbolic_name T, 0 [symbolic]
  186. // CHECK:STDOUT: %C.type: type = generic_class_type @C [template]
  187. // CHECK:STDOUT: %.1: type = tuple_type () [template]
  188. // CHECK:STDOUT: %C.1: %C.type = struct_value () [template]
  189. // CHECK:STDOUT: %C.2: type = class_type @C, @C(%T) [symbolic]
  190. // CHECK:STDOUT: %F.type: type = fn_type @F, @C(%T) [symbolic]
  191. // CHECK:STDOUT: %F: %F.type = struct_value () [symbolic]
  192. // CHECK:STDOUT: %.2: type = struct_type {} [template]
  193. // CHECK:STDOUT: %.3: type = unbound_element_type %C.2, %.2 [symbolic]
  194. // CHECK:STDOUT: %.4: type = struct_type {.data: %.2} [template]
  195. // CHECK:STDOUT: %.5: <witness> = complete_type_witness %.4 [template]
  196. // CHECK:STDOUT: }
  197. // CHECK:STDOUT:
  198. // CHECK:STDOUT: imports {
  199. // CHECK:STDOUT: %Core: <namespace> = namespace file.%Core.import, [template] {
  200. // CHECK:STDOUT: import Core//prelude
  201. // CHECK:STDOUT: import Core//prelude/operators
  202. // CHECK:STDOUT: import Core//prelude/types
  203. // CHECK:STDOUT: import Core//prelude/operators/arithmetic
  204. // CHECK:STDOUT: import Core//prelude/operators/as
  205. // CHECK:STDOUT: import Core//prelude/operators/bitwise
  206. // CHECK:STDOUT: import Core//prelude/operators/comparison
  207. // CHECK:STDOUT: import Core//prelude/types/bool
  208. // CHECK:STDOUT: }
  209. // CHECK:STDOUT: }
  210. // CHECK:STDOUT:
  211. // CHECK:STDOUT: file {
  212. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  213. // CHECK:STDOUT: .Core = imports.%Core
  214. // CHECK:STDOUT: .C = %C.decl
  215. // CHECK:STDOUT: }
  216. // CHECK:STDOUT: %Core.import = import Core
  217. // CHECK:STDOUT: %C.decl: %C.type = class_decl @C [template = constants.%C.1] {
  218. // CHECK:STDOUT: %T.patt: type = symbolic_binding_pattern T, 0
  219. // CHECK:STDOUT: } {
  220. // CHECK:STDOUT: %T.param: type = param T, runtime_param<invalid>
  221. // CHECK:STDOUT: %T.loc4_9.1: type = bind_symbolic_name T, 0, %T.param [symbolic = %T.loc4_9.2 (constants.%T)]
  222. // CHECK:STDOUT: }
  223. // CHECK:STDOUT: }
  224. // CHECK:STDOUT:
  225. // CHECK:STDOUT: generic class @C(%T.loc4_9.1: type) {
  226. // CHECK:STDOUT: %T.loc4_9.2: type = bind_symbolic_name T, 0 [symbolic = %T.loc4_9.2 (constants.%T)]
  227. // CHECK:STDOUT:
  228. // CHECK:STDOUT: !definition:
  229. // CHECK:STDOUT: %F.type: type = fn_type @F, @C(%T.loc4_9.2) [symbolic = %F.type (constants.%F.type)]
  230. // CHECK:STDOUT: %F: @C.%F.type (%F.type) = struct_value () [symbolic = %F (constants.%F)]
  231. // CHECK:STDOUT: %C: type = class_type @C, @C(%T.loc4_9.2) [symbolic = %C (constants.%C.2)]
  232. // CHECK:STDOUT: %.loc11_11.2: type = unbound_element_type @C.%C (%C.2), %.2 [symbolic = %.loc11_11.2 (constants.%.3)]
  233. // CHECK:STDOUT:
  234. // CHECK:STDOUT: class {
  235. // CHECK:STDOUT: %F.decl: @C.%F.type (%F.type) = fn_decl @F [symbolic = @C.%F (constants.%F)] {} {}
  236. // CHECK:STDOUT: %.loc11_14.1: %.2 = struct_literal ()
  237. // CHECK:STDOUT: %.loc11_14.2: type = converted %.loc11_14.1, constants.%.2 [template = constants.%.2]
  238. // CHECK:STDOUT: %.loc11_11.1: @C.%.loc11_11.2 (%.3) = field_decl data, element0 [template]
  239. // CHECK:STDOUT: %.loc12: <witness> = complete_type_witness %.4 [template = constants.%.5]
  240. // CHECK:STDOUT:
  241. // CHECK:STDOUT: !members:
  242. // CHECK:STDOUT: .Self = constants.%C.2
  243. // CHECK:STDOUT: .F = %F.decl
  244. // CHECK:STDOUT: .data = %.loc11_11.1
  245. // CHECK:STDOUT: }
  246. // CHECK:STDOUT: }
  247. // CHECK:STDOUT:
  248. // CHECK:STDOUT: generic fn @F(@C.%T.loc4_9.1: type) {
  249. // CHECK:STDOUT: !definition:
  250. // CHECK:STDOUT: %T: type = bind_symbolic_name T, 0 [symbolic = %T (constants.%T)]
  251. // CHECK:STDOUT: %C: type = class_type @C, @C(%T) [symbolic = %C (constants.%C.2)]
  252. // CHECK:STDOUT: %.loc9: type = unbound_element_type @F.%C (%C.2), %.2 [symbolic = %.loc9 (constants.%.3)]
  253. // CHECK:STDOUT:
  254. // CHECK:STDOUT: fn() {
  255. // CHECK:STDOUT: !entry:
  256. // CHECK:STDOUT: %data.ref: @F.%.loc9 (%.3) = name_ref data, @C.%.loc11_11.1 [template = @C.%.loc11_11.1]
  257. // CHECK:STDOUT: return
  258. // CHECK:STDOUT: }
  259. // CHECK:STDOUT: }
  260. // CHECK:STDOUT:
  261. // CHECK:STDOUT: specific @C(constants.%T) {
  262. // CHECK:STDOUT: %T.loc4_9.2 => constants.%T
  263. // CHECK:STDOUT:
  264. // CHECK:STDOUT: !definition:
  265. // CHECK:STDOUT: %F.type => constants.%F.type
  266. // CHECK:STDOUT: %F => constants.%F
  267. // CHECK:STDOUT: %C => constants.%C.2
  268. // CHECK:STDOUT: %.loc11_11.2 => constants.%.3
  269. // CHECK:STDOUT: }
  270. // CHECK:STDOUT:
  271. // CHECK:STDOUT: specific @F(constants.%T) {}
  272. // CHECK:STDOUT:
  273. // CHECK:STDOUT: specific @C(@C.%T.loc4_9.2) {
  274. // CHECK:STDOUT: %T.loc4_9.2 => constants.%T
  275. // CHECK:STDOUT: }
  276. // CHECK:STDOUT:
  277. // CHECK:STDOUT: specific @C(@F.%T) {
  278. // CHECK:STDOUT: %T.loc4_9.2 => constants.%T
  279. // CHECK:STDOUT: }
  280. // CHECK:STDOUT: