index.carbon 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549
  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/operators/overloaded/index.carbon
  8. // TIP: To dump output, run:
  9. // TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/operators/overloaded/index.carbon
  10. // --- overloaded_index.carbon
  11. library "[[@TEST_NAME]]";
  12. class C {}
  13. class ElementType {}
  14. class SubscriptType {}
  15. impl C as Core.IndexWith(SubscriptType, ElementType) {
  16. fn At[self: Self](subscript: SubscriptType) -> ElementType {
  17. return {};
  18. }
  19. }
  20. let s: SubscriptType = {};
  21. let c: C = {};
  22. let x: ElementType = c[s];
  23. // --- overloaded_builtin.carbon
  24. library "[[@TEST_NAME]]";
  25. impl (i32, i32) as Core.IndexWith(i32, i32) {
  26. fn At[self: Self](subscript: i32) -> i32 {
  27. return self.0;
  28. }
  29. }
  30. let s: (i32, i32) = (1, 5);
  31. let e: i32 = s[0];
  32. // --- fail_invalid_subscript_type.carbon
  33. library "[[@TEST_NAME]]";
  34. class C { }
  35. class ElementType {}
  36. class SubscriptType {}
  37. impl C as Core.IndexWith(SubscriptType, ElementType) {
  38. fn At[self: Self](subscript: SubscriptType) -> ElementType {
  39. return {};
  40. }
  41. }
  42. let c: C = {};
  43. // CHECK:STDERR: fail_invalid_subscript_type.carbon:[[@LINE+7]]:22: error: cannot implicitly convert from `Core.IntLiteral` to `SubscriptType` [ImplicitAsConversionFailure]
  44. // CHECK:STDERR: let x: ElementType = c[0];
  45. // CHECK:STDERR: ^~~~
  46. // CHECK:STDERR: fail_invalid_subscript_type.carbon:[[@LINE+4]]:22: note: type `Core.IntLiteral` does not implement interface `Core.ImplicitAs(SubscriptType)` [MissingImplInMemberAccessNote]
  47. // CHECK:STDERR: let x: ElementType = c[0];
  48. // CHECK:STDERR: ^~~~
  49. // CHECK:STDERR:
  50. let x: ElementType = c[0];
  51. // --- fail_index_with_not_implemented.carbon
  52. library "[[@TEST_NAME]]";
  53. class C { }
  54. let c: C = {};
  55. // CHECK:STDERR: fail_index_with_not_implemented.carbon:[[@LINE+3]]:14: error: type `C` does not support indexing [TypeNotIndexable]
  56. // CHECK:STDERR: let x: i32 = c[0];
  57. // CHECK:STDERR: ^~~~
  58. let x: i32 = c[0];
  59. // CHECK:STDOUT: --- overloaded_index.carbon
  60. // CHECK:STDOUT:
  61. // CHECK:STDOUT: constants {
  62. // CHECK:STDOUT: %C: type = class_type @C [template]
  63. // CHECK:STDOUT: %empty_struct_type: type = struct_type {} [template]
  64. // CHECK:STDOUT: %complete_type: <witness> = complete_type_witness %empty_struct_type [template]
  65. // CHECK:STDOUT: %ElementType.e6b: type = class_type @ElementType [template]
  66. // CHECK:STDOUT: %SubscriptType.8ee: type = class_type @SubscriptType [template]
  67. // CHECK:STDOUT: %IndexWith.type.504: type = generic_interface_type @IndexWith [template]
  68. // CHECK:STDOUT: %IndexWith.generic: %IndexWith.type.504 = struct_value () [template]
  69. // CHECK:STDOUT: %IndexWith.type.2d3: type = facet_type <@IndexWith, @IndexWith(%SubscriptType.8ee, %ElementType.e6b)> [template]
  70. // CHECK:STDOUT: %At.type.38e: type = fn_type @At.1, @IndexWith(%SubscriptType.8ee, %ElementType.e6b) [template]
  71. // CHECK:STDOUT: %impl_witness: <witness> = impl_witness (@impl.%At.decl) [template]
  72. // CHECK:STDOUT: %At.type.afe: type = fn_type @At.2 [template]
  73. // CHECK:STDOUT: %At.9bf: %At.type.afe = struct_value () [template]
  74. // CHECK:STDOUT: %ElementType.val: %ElementType.e6b = struct_value () [template]
  75. // CHECK:STDOUT: %SubscriptType.val: %SubscriptType.8ee = struct_value () [template]
  76. // CHECK:STDOUT: %C.val: %C = struct_value () [template]
  77. // CHECK:STDOUT: }
  78. // CHECK:STDOUT:
  79. // CHECK:STDOUT: imports {
  80. // CHECK:STDOUT: %Core: <namespace> = namespace file.%Core.import, [template] {
  81. // CHECK:STDOUT: .IndexWith = %import_ref.76d
  82. // CHECK:STDOUT: import Core//prelude
  83. // CHECK:STDOUT: import Core//prelude/...
  84. // CHECK:STDOUT: }
  85. // CHECK:STDOUT: %import_ref.76d: %IndexWith.type.504 = import_ref Core//prelude/operators/index, IndexWith, loaded [template = constants.%IndexWith.generic]
  86. // CHECK:STDOUT: }
  87. // CHECK:STDOUT:
  88. // CHECK:STDOUT: file {
  89. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  90. // CHECK:STDOUT: .Core = imports.%Core
  91. // CHECK:STDOUT: .C = %C.decl
  92. // CHECK:STDOUT: .ElementType = %ElementType.decl
  93. // CHECK:STDOUT: .SubscriptType = %SubscriptType.decl
  94. // CHECK:STDOUT: .s = @__global_init.%s
  95. // CHECK:STDOUT: .c = @__global_init.%c
  96. // CHECK:STDOUT: .x = @__global_init.%x
  97. // CHECK:STDOUT: }
  98. // CHECK:STDOUT: %Core.import = import Core
  99. // CHECK:STDOUT: %C.decl: type = class_decl @C [template = constants.%C] {} {}
  100. // CHECK:STDOUT: %ElementType.decl: type = class_decl @ElementType [template = constants.%ElementType.e6b] {} {}
  101. // CHECK:STDOUT: %SubscriptType.decl: type = class_decl @SubscriptType [template = constants.%SubscriptType.8ee] {} {}
  102. // CHECK:STDOUT: impl_decl @impl [template] {} {
  103. // CHECK:STDOUT: %C.ref: type = name_ref C, file.%C.decl [template = constants.%C]
  104. // CHECK:STDOUT: %Core.ref: <namespace> = name_ref Core, imports.%Core [template = imports.%Core]
  105. // CHECK:STDOUT: %IndexWith.ref: %IndexWith.type.504 = name_ref IndexWith, imports.%import_ref.76d [template = constants.%IndexWith.generic]
  106. // CHECK:STDOUT: %SubscriptType.ref: type = name_ref SubscriptType, file.%SubscriptType.decl [template = constants.%SubscriptType.8ee]
  107. // CHECK:STDOUT: %ElementType.ref: type = name_ref ElementType, file.%ElementType.decl [template = constants.%ElementType.e6b]
  108. // CHECK:STDOUT: %IndexWith.type: type = facet_type <@IndexWith, @IndexWith(constants.%SubscriptType.8ee, constants.%ElementType.e6b)> [template = constants.%IndexWith.type.2d3]
  109. // CHECK:STDOUT: }
  110. // CHECK:STDOUT: %impl_witness: <witness> = impl_witness (@impl.%At.decl) [template = constants.%impl_witness]
  111. // CHECK:STDOUT: }
  112. // CHECK:STDOUT:
  113. // CHECK:STDOUT: impl @impl: %C.ref as %IndexWith.type {
  114. // CHECK:STDOUT: %At.decl: %At.type.afe = fn_decl @At.2 [template = constants.%At.9bf] {
  115. // CHECK:STDOUT: %self.patt: %C = binding_pattern self
  116. // CHECK:STDOUT: %self.param_patt: %C = value_param_pattern %self.patt, runtime_param0
  117. // CHECK:STDOUT: %subscript.patt: %SubscriptType.8ee = binding_pattern subscript
  118. // CHECK:STDOUT: %subscript.param_patt: %SubscriptType.8ee = value_param_pattern %subscript.patt, runtime_param1
  119. // CHECK:STDOUT: %return.patt: %ElementType.e6b = return_slot_pattern
  120. // CHECK:STDOUT: %return.param_patt: %ElementType.e6b = out_param_pattern %return.patt, runtime_param2
  121. // CHECK:STDOUT: } {
  122. // CHECK:STDOUT: %ElementType.ref: type = name_ref ElementType, file.%ElementType.decl [template = constants.%ElementType.e6b]
  123. // CHECK:STDOUT: %self.param: %C = value_param runtime_param0
  124. // CHECK:STDOUT: %Self.ref: type = name_ref Self, @impl.%C.ref [template = constants.%C]
  125. // CHECK:STDOUT: %self: %C = bind_name self, %self.param
  126. // CHECK:STDOUT: %subscript.param: %SubscriptType.8ee = value_param runtime_param1
  127. // CHECK:STDOUT: %SubscriptType.ref: type = name_ref SubscriptType, file.%SubscriptType.decl [template = constants.%SubscriptType.8ee]
  128. // CHECK:STDOUT: %subscript: %SubscriptType.8ee = bind_name subscript, %subscript.param
  129. // CHECK:STDOUT: %return.param: ref %ElementType.e6b = out_param runtime_param2
  130. // CHECK:STDOUT: %return: ref %ElementType.e6b = return_slot %return.param
  131. // CHECK:STDOUT: }
  132. // CHECK:STDOUT:
  133. // CHECK:STDOUT: !members:
  134. // CHECK:STDOUT: .At = %At.decl
  135. // CHECK:STDOUT: witness = file.%impl_witness
  136. // CHECK:STDOUT: }
  137. // CHECK:STDOUT:
  138. // CHECK:STDOUT: class @C {
  139. // CHECK:STDOUT: %complete_type: <witness> = complete_type_witness %empty_struct_type [template = constants.%complete_type]
  140. // CHECK:STDOUT:
  141. // CHECK:STDOUT: !members:
  142. // CHECK:STDOUT: .Self = constants.%C
  143. // CHECK:STDOUT: complete_type_witness = %complete_type
  144. // CHECK:STDOUT: }
  145. // CHECK:STDOUT:
  146. // CHECK:STDOUT: class @ElementType {
  147. // CHECK:STDOUT: %complete_type: <witness> = complete_type_witness %empty_struct_type [template = constants.%complete_type]
  148. // CHECK:STDOUT:
  149. // CHECK:STDOUT: !members:
  150. // CHECK:STDOUT: .Self = constants.%ElementType.e6b
  151. // CHECK:STDOUT: complete_type_witness = %complete_type
  152. // CHECK:STDOUT: }
  153. // CHECK:STDOUT:
  154. // CHECK:STDOUT: class @SubscriptType {
  155. // CHECK:STDOUT: %complete_type: <witness> = complete_type_witness %empty_struct_type [template = constants.%complete_type]
  156. // CHECK:STDOUT:
  157. // CHECK:STDOUT: !members:
  158. // CHECK:STDOUT: .Self = constants.%SubscriptType.8ee
  159. // CHECK:STDOUT: complete_type_witness = %complete_type
  160. // CHECK:STDOUT: }
  161. // CHECK:STDOUT:
  162. // CHECK:STDOUT: fn @At.2[%self.param_patt: %C](%subscript.param_patt: %SubscriptType.8ee) -> %return.param_patt: %ElementType.e6b {
  163. // CHECK:STDOUT: !entry:
  164. // CHECK:STDOUT: %.loc10_13.1: %empty_struct_type = struct_literal ()
  165. // CHECK:STDOUT: %.loc10_13.2: init %ElementType.e6b = class_init (), %return [template = constants.%ElementType.val]
  166. // CHECK:STDOUT: %.loc10_14: init %ElementType.e6b = converted %.loc10_13.1, %.loc10_13.2 [template = constants.%ElementType.val]
  167. // CHECK:STDOUT: return %.loc10_14 to %return
  168. // CHECK:STDOUT: }
  169. // CHECK:STDOUT:
  170. // CHECK:STDOUT: fn @__global_init() {
  171. // CHECK:STDOUT: !entry:
  172. // CHECK:STDOUT: %.loc14_25.1: %empty_struct_type = struct_literal ()
  173. // CHECK:STDOUT: %.loc14_25.2: ref %SubscriptType.8ee = temporary_storage
  174. // CHECK:STDOUT: %.loc14_25.3: init %SubscriptType.8ee = class_init (), %.loc14_25.2 [template = constants.%SubscriptType.val]
  175. // CHECK:STDOUT: %.loc14_25.4: ref %SubscriptType.8ee = temporary %.loc14_25.2, %.loc14_25.3
  176. // CHECK:STDOUT: %.loc14_26.1: ref %SubscriptType.8ee = converted %.loc14_25.1, %.loc14_25.4
  177. // CHECK:STDOUT: %.loc14_26.2: %SubscriptType.8ee = bind_value %.loc14_26.1
  178. // CHECK:STDOUT: %s: %SubscriptType.8ee = bind_name s, %.loc14_26.2
  179. // CHECK:STDOUT: %.loc15_13.1: %empty_struct_type = struct_literal ()
  180. // CHECK:STDOUT: %.loc15_13.2: ref %C = temporary_storage
  181. // CHECK:STDOUT: %.loc15_13.3: init %C = class_init (), %.loc15_13.2 [template = constants.%C.val]
  182. // CHECK:STDOUT: %.loc15_13.4: ref %C = temporary %.loc15_13.2, %.loc15_13.3
  183. // CHECK:STDOUT: %.loc15_14.1: ref %C = converted %.loc15_13.1, %.loc15_13.4
  184. // CHECK:STDOUT: %.loc15_14.2: %C = bind_value %.loc15_14.1
  185. // CHECK:STDOUT: %c: %C = bind_name c, %.loc15_14.2
  186. // CHECK:STDOUT: %c.ref: %C = name_ref c, %c
  187. // CHECK:STDOUT: %s.ref: %SubscriptType.8ee = name_ref s, %s
  188. // CHECK:STDOUT: %impl.elem0: %At.type.38e = impl_witness_access constants.%impl_witness, element0 [template = constants.%At.9bf]
  189. // CHECK:STDOUT: %At.bound: <bound method> = bound_method %c.ref, %impl.elem0
  190. // CHECK:STDOUT: %.loc16_25.1: ref %ElementType.e6b = temporary_storage
  191. // CHECK:STDOUT: %At.call: init %ElementType.e6b = call %At.bound(%c.ref, %s.ref) to %.loc16_25.1
  192. // CHECK:STDOUT: %.loc16_25.2: ref %ElementType.e6b = temporary %.loc16_25.1, %At.call
  193. // CHECK:STDOUT: %.loc16_25.3: %ElementType.e6b = bind_value %.loc16_25.2
  194. // CHECK:STDOUT: %x: %ElementType.e6b = bind_name x, %.loc16_25.3
  195. // CHECK:STDOUT: return
  196. // CHECK:STDOUT: }
  197. // CHECK:STDOUT:
  198. // CHECK:STDOUT: --- overloaded_builtin.carbon
  199. // CHECK:STDOUT:
  200. // CHECK:STDOUT: constants {
  201. // CHECK:STDOUT: %int_32: Core.IntLiteral = int_value 32 [template]
  202. // CHECK:STDOUT: %i32: type = class_type @Int, @Int(%int_32) [template]
  203. // CHECK:STDOUT: %tuple.type.24b: type = tuple_type (type, type) [template]
  204. // CHECK:STDOUT: %tuple.type.471: type = tuple_type (%i32, %i32) [template]
  205. // CHECK:STDOUT: %IndexWith.type.504: type = generic_interface_type @IndexWith [template]
  206. // CHECK:STDOUT: %IndexWith.generic: %IndexWith.type.504 = struct_value () [template]
  207. // CHECK:STDOUT: %IndexWith.type.d22: type = facet_type <@IndexWith, @IndexWith(%i32, %i32)> [template]
  208. // CHECK:STDOUT: %At.type.740: type = fn_type @At.1, @IndexWith(%i32, %i32) [template]
  209. // CHECK:STDOUT: %impl_witness.d5f: <witness> = impl_witness (@impl.1.%At.decl) [template]
  210. // CHECK:STDOUT: %At.type.37e: type = fn_type @At.2 [template]
  211. // CHECK:STDOUT: %At.065: %At.type.37e = struct_value () [template]
  212. // CHECK:STDOUT: %int_0.5c6: Core.IntLiteral = int_value 0 [template]
  213. // CHECK:STDOUT: %int_1.5b8: Core.IntLiteral = int_value 1 [template]
  214. // CHECK:STDOUT: %int_5.64b: Core.IntLiteral = int_value 5 [template]
  215. // CHECK:STDOUT: %tuple.type.f94: type = tuple_type (Core.IntLiteral, Core.IntLiteral) [template]
  216. // CHECK:STDOUT: %Convert.type.cd1: type = fn_type @Convert.1, @ImplicitAs(%i32) [template]
  217. // CHECK:STDOUT: %impl_witness.5b0: <witness> = impl_witness (imports.%import_ref.723), @impl.2(%int_32) [template]
  218. // CHECK:STDOUT: %Convert.type.466: type = fn_type @Convert.2, @impl.2(%int_32) [template]
  219. // CHECK:STDOUT: %Convert.925: %Convert.type.466 = struct_value () [template]
  220. // CHECK:STDOUT: %Convert.bound.afd: <bound method> = bound_method %int_1.5b8, %Convert.925 [template]
  221. // CHECK:STDOUT: %Convert.specific_fn.b73: <specific function> = specific_function %Convert.bound.afd, @Convert.2(%int_32) [template]
  222. // CHECK:STDOUT: %int_1.c60: %i32 = int_value 1 [template]
  223. // CHECK:STDOUT: %Convert.bound.b33: <bound method> = bound_method %int_5.64b, %Convert.925 [template]
  224. // CHECK:STDOUT: %Convert.specific_fn.89d: <specific function> = specific_function %Convert.bound.b33, @Convert.2(%int_32) [template]
  225. // CHECK:STDOUT: %int_5.70b: %i32 = int_value 5 [template]
  226. // CHECK:STDOUT: %tuple: %tuple.type.471 = tuple_value (%int_1.c60, %int_5.70b) [template]
  227. // CHECK:STDOUT: %Convert.bound.b41: <bound method> = bound_method %int_0.5c6, %Convert.925 [template]
  228. // CHECK:STDOUT: %Convert.specific_fn.c35: <specific function> = specific_function %Convert.bound.b41, @Convert.2(%int_32) [template]
  229. // CHECK:STDOUT: %int_0.f61: %i32 = int_value 0 [template]
  230. // CHECK:STDOUT: }
  231. // CHECK:STDOUT:
  232. // CHECK:STDOUT: imports {
  233. // CHECK:STDOUT: %Core: <namespace> = namespace file.%Core.import, [template] {
  234. // CHECK:STDOUT: .Int = %import_ref.187
  235. // CHECK:STDOUT: .IndexWith = %import_ref.76d
  236. // CHECK:STDOUT: .ImplicitAs = %import_ref.a69
  237. // CHECK:STDOUT: import Core//prelude
  238. // CHECK:STDOUT: import Core//prelude/...
  239. // CHECK:STDOUT: }
  240. // CHECK:STDOUT: %import_ref.76d: %IndexWith.type.504 = import_ref Core//prelude/operators/index, IndexWith, loaded [template = constants.%IndexWith.generic]
  241. // CHECK:STDOUT: }
  242. // CHECK:STDOUT:
  243. // CHECK:STDOUT: file {
  244. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  245. // CHECK:STDOUT: .Core = imports.%Core
  246. // CHECK:STDOUT: .s = @__global_init.%s
  247. // CHECK:STDOUT: .e = @__global_init.%e
  248. // CHECK:STDOUT: }
  249. // CHECK:STDOUT: %Core.import = import Core
  250. // CHECK:STDOUT: impl_decl @impl.1 [template] {} {
  251. // CHECK:STDOUT: %int_32.loc4_7: Core.IntLiteral = int_value 32 [template = constants.%int_32]
  252. // CHECK:STDOUT: %i32.loc4_7: type = class_type @Int, @Int(constants.%int_32) [template = constants.%i32]
  253. // CHECK:STDOUT: %int_32.loc4_12: Core.IntLiteral = int_value 32 [template = constants.%int_32]
  254. // CHECK:STDOUT: %i32.loc4_12: type = class_type @Int, @Int(constants.%int_32) [template = constants.%i32]
  255. // CHECK:STDOUT: %.loc4_15.1: %tuple.type.24b = tuple_literal (%i32.loc4_7, %i32.loc4_12)
  256. // CHECK:STDOUT: %.loc4_15.2: type = converted %.loc4_15.1, constants.%tuple.type.471 [template = constants.%tuple.type.471]
  257. // CHECK:STDOUT: %Core.ref: <namespace> = name_ref Core, imports.%Core [template = imports.%Core]
  258. // CHECK:STDOUT: %IndexWith.ref: %IndexWith.type.504 = name_ref IndexWith, imports.%import_ref.76d [template = constants.%IndexWith.generic]
  259. // CHECK:STDOUT: %int_32.loc4_35: Core.IntLiteral = int_value 32 [template = constants.%int_32]
  260. // CHECK:STDOUT: %i32.loc4_35: type = class_type @Int, @Int(constants.%int_32) [template = constants.%i32]
  261. // CHECK:STDOUT: %int_32.loc4_40: Core.IntLiteral = int_value 32 [template = constants.%int_32]
  262. // CHECK:STDOUT: %i32.loc4_40: type = class_type @Int, @Int(constants.%int_32) [template = constants.%i32]
  263. // CHECK:STDOUT: %IndexWith.type: type = facet_type <@IndexWith, @IndexWith(constants.%i32, constants.%i32)> [template = constants.%IndexWith.type.d22]
  264. // CHECK:STDOUT: }
  265. // CHECK:STDOUT: %impl_witness: <witness> = impl_witness (@impl.1.%At.decl) [template = constants.%impl_witness.d5f]
  266. // CHECK:STDOUT: }
  267. // CHECK:STDOUT:
  268. // CHECK:STDOUT: impl @impl.1: %.loc4_15.2 as %IndexWith.type {
  269. // CHECK:STDOUT: %At.decl: %At.type.37e = fn_decl @At.2 [template = constants.%At.065] {
  270. // CHECK:STDOUT: %self.patt: %tuple.type.471 = binding_pattern self
  271. // CHECK:STDOUT: %self.param_patt: %tuple.type.471 = value_param_pattern %self.patt, runtime_param0
  272. // CHECK:STDOUT: %subscript.patt: %i32 = binding_pattern subscript
  273. // CHECK:STDOUT: %subscript.param_patt: %i32 = value_param_pattern %subscript.patt, runtime_param1
  274. // CHECK:STDOUT: %return.patt: %i32 = return_slot_pattern
  275. // CHECK:STDOUT: %return.param_patt: %i32 = out_param_pattern %return.patt, runtime_param2
  276. // CHECK:STDOUT: } {
  277. // CHECK:STDOUT: %int_32.loc5_40: Core.IntLiteral = int_value 32 [template = constants.%int_32]
  278. // CHECK:STDOUT: %i32.loc5_40: type = class_type @Int, @Int(constants.%int_32) [template = constants.%i32]
  279. // CHECK:STDOUT: %self.param: %tuple.type.471 = value_param runtime_param0
  280. // CHECK:STDOUT: %Self.ref: type = name_ref Self, @impl.1.%.loc4_15.2 [template = constants.%tuple.type.471]
  281. // CHECK:STDOUT: %self: %tuple.type.471 = bind_name self, %self.param
  282. // CHECK:STDOUT: %subscript.param: %i32 = value_param runtime_param1
  283. // CHECK:STDOUT: %.loc5: type = splice_block %i32.loc5_32 [template = constants.%i32] {
  284. // CHECK:STDOUT: %int_32.loc5_32: Core.IntLiteral = int_value 32 [template = constants.%int_32]
  285. // CHECK:STDOUT: %i32.loc5_32: type = class_type @Int, @Int(constants.%int_32) [template = constants.%i32]
  286. // CHECK:STDOUT: }
  287. // CHECK:STDOUT: %subscript: %i32 = bind_name subscript, %subscript.param
  288. // CHECK:STDOUT: %return.param: ref %i32 = out_param runtime_param2
  289. // CHECK:STDOUT: %return: ref %i32 = return_slot %return.param
  290. // CHECK:STDOUT: }
  291. // CHECK:STDOUT:
  292. // CHECK:STDOUT: !members:
  293. // CHECK:STDOUT: .At = %At.decl
  294. // CHECK:STDOUT: witness = file.%impl_witness
  295. // CHECK:STDOUT: }
  296. // CHECK:STDOUT:
  297. // CHECK:STDOUT: fn @At.2[%self.param_patt: %tuple.type.471](%subscript.param_patt: %i32) -> %i32 {
  298. // CHECK:STDOUT: !entry:
  299. // CHECK:STDOUT: %self.ref: %tuple.type.471 = name_ref self, %self
  300. // CHECK:STDOUT: %int_0: Core.IntLiteral = int_value 0 [template = constants.%int_0.5c6]
  301. // CHECK:STDOUT: %tuple.elem0: %i32 = tuple_access %self.ref, element0
  302. // CHECK:STDOUT: return %tuple.elem0
  303. // CHECK:STDOUT: }
  304. // CHECK:STDOUT:
  305. // CHECK:STDOUT: fn @__global_init() {
  306. // CHECK:STDOUT: !entry:
  307. // CHECK:STDOUT: %int_1: Core.IntLiteral = int_value 1 [template = constants.%int_1.5b8]
  308. // CHECK:STDOUT: %int_5: Core.IntLiteral = int_value 5 [template = constants.%int_5.64b]
  309. // CHECK:STDOUT: %.loc10_26.1: %tuple.type.f94 = tuple_literal (%int_1, %int_5)
  310. // CHECK:STDOUT: %impl.elem0.loc10_26.1: %Convert.type.cd1 = impl_witness_access constants.%impl_witness.5b0, element0 [template = constants.%Convert.925]
  311. // CHECK:STDOUT: %Convert.bound.loc10_26.1: <bound method> = bound_method %int_1, %impl.elem0.loc10_26.1 [template = constants.%Convert.bound.afd]
  312. // CHECK:STDOUT: %Convert.specific_fn.loc10_26.1: <specific function> = specific_function %Convert.bound.loc10_26.1, @Convert.2(constants.%int_32) [template = constants.%Convert.specific_fn.b73]
  313. // CHECK:STDOUT: %int.convert_checked.loc10_26.1: init %i32 = call %Convert.specific_fn.loc10_26.1(%int_1) [template = constants.%int_1.c60]
  314. // CHECK:STDOUT: %.loc10_26.2: %i32 = value_of_initializer %int.convert_checked.loc10_26.1 [template = constants.%int_1.c60]
  315. // CHECK:STDOUT: %.loc10_26.3: %i32 = converted %int_1, %.loc10_26.2 [template = constants.%int_1.c60]
  316. // CHECK:STDOUT: %impl.elem0.loc10_26.2: %Convert.type.cd1 = impl_witness_access constants.%impl_witness.5b0, element0 [template = constants.%Convert.925]
  317. // CHECK:STDOUT: %Convert.bound.loc10_26.2: <bound method> = bound_method %int_5, %impl.elem0.loc10_26.2 [template = constants.%Convert.bound.b33]
  318. // CHECK:STDOUT: %Convert.specific_fn.loc10_26.2: <specific function> = specific_function %Convert.bound.loc10_26.2, @Convert.2(constants.%int_32) [template = constants.%Convert.specific_fn.89d]
  319. // CHECK:STDOUT: %int.convert_checked.loc10_26.2: init %i32 = call %Convert.specific_fn.loc10_26.2(%int_5) [template = constants.%int_5.70b]
  320. // CHECK:STDOUT: %.loc10_26.4: %i32 = value_of_initializer %int.convert_checked.loc10_26.2 [template = constants.%int_5.70b]
  321. // CHECK:STDOUT: %.loc10_26.5: %i32 = converted %int_5, %.loc10_26.4 [template = constants.%int_5.70b]
  322. // CHECK:STDOUT: %tuple: %tuple.type.471 = tuple_value (%.loc10_26.3, %.loc10_26.5) [template = constants.%tuple]
  323. // CHECK:STDOUT: %.loc10_27: %tuple.type.471 = converted %.loc10_26.1, %tuple [template = constants.%tuple]
  324. // CHECK:STDOUT: %s: %tuple.type.471 = bind_name s, %.loc10_27
  325. // CHECK:STDOUT: %s.ref: %tuple.type.471 = name_ref s, %s
  326. // CHECK:STDOUT: %int_0: Core.IntLiteral = int_value 0 [template = constants.%int_0.5c6]
  327. // CHECK:STDOUT: %impl.elem0.loc11_17.1: %Convert.type.cd1 = impl_witness_access constants.%impl_witness.5b0, element0 [template = constants.%Convert.925]
  328. // CHECK:STDOUT: %Convert.bound.loc11: <bound method> = bound_method %int_0, %impl.elem0.loc11_17.1 [template = constants.%Convert.bound.b41]
  329. // CHECK:STDOUT: %Convert.specific_fn.loc11: <specific function> = specific_function %Convert.bound.loc11, @Convert.2(constants.%int_32) [template = constants.%Convert.specific_fn.c35]
  330. // CHECK:STDOUT: %int.convert_checked.loc11: init %i32 = call %Convert.specific_fn.loc11(%int_0) [template = constants.%int_0.f61]
  331. // CHECK:STDOUT: %.loc11_17.1: %i32 = value_of_initializer %int.convert_checked.loc11 [template = constants.%int_0.f61]
  332. // CHECK:STDOUT: %.loc11_17.2: %i32 = converted %int_0, %.loc11_17.1 [template = constants.%int_0.f61]
  333. // CHECK:STDOUT: %impl.elem0.loc11_17.2: %At.type.740 = impl_witness_access constants.%impl_witness.d5f, element0 [template = constants.%At.065]
  334. // CHECK:STDOUT: %At.bound: <bound method> = bound_method %s.ref, %impl.elem0.loc11_17.2
  335. // CHECK:STDOUT: %At.call: init %i32 = call %At.bound(%s.ref, %.loc11_17.2)
  336. // CHECK:STDOUT: %.loc11_18.1: %i32 = value_of_initializer %At.call
  337. // CHECK:STDOUT: %.loc11_18.2: %i32 = converted %At.call, %.loc11_18.1
  338. // CHECK:STDOUT: %e: %i32 = bind_name e, %.loc11_18.2
  339. // CHECK:STDOUT: return
  340. // CHECK:STDOUT: }
  341. // CHECK:STDOUT:
  342. // CHECK:STDOUT: --- fail_invalid_subscript_type.carbon
  343. // CHECK:STDOUT:
  344. // CHECK:STDOUT: constants {
  345. // CHECK:STDOUT: %C: type = class_type @C [template]
  346. // CHECK:STDOUT: %empty_struct_type: type = struct_type {} [template]
  347. // CHECK:STDOUT: %complete_type: <witness> = complete_type_witness %empty_struct_type [template]
  348. // CHECK:STDOUT: %ElementType.e6b: type = class_type @ElementType [template]
  349. // CHECK:STDOUT: %SubscriptType.8ee: type = class_type @SubscriptType [template]
  350. // CHECK:STDOUT: %IndexWith.type.504: type = generic_interface_type @IndexWith [template]
  351. // CHECK:STDOUT: %IndexWith.generic: %IndexWith.type.504 = struct_value () [template]
  352. // CHECK:STDOUT: %IndexWith.type.2d3: type = facet_type <@IndexWith, @IndexWith(%SubscriptType.8ee, %ElementType.e6b)> [template]
  353. // CHECK:STDOUT: %At.type.38e: type = fn_type @At.1, @IndexWith(%SubscriptType.8ee, %ElementType.e6b) [template]
  354. // CHECK:STDOUT: %impl_witness: <witness> = impl_witness (@impl.%At.decl) [template]
  355. // CHECK:STDOUT: %At.type.afe: type = fn_type @At.2 [template]
  356. // CHECK:STDOUT: %At.9bf: %At.type.afe = struct_value () [template]
  357. // CHECK:STDOUT: %ElementType.val: %ElementType.e6b = struct_value () [template]
  358. // CHECK:STDOUT: %C.val: %C = struct_value () [template]
  359. // CHECK:STDOUT: %int_0: Core.IntLiteral = int_value 0 [template]
  360. // CHECK:STDOUT: }
  361. // CHECK:STDOUT:
  362. // CHECK:STDOUT: imports {
  363. // CHECK:STDOUT: %Core: <namespace> = namespace file.%Core.import, [template] {
  364. // CHECK:STDOUT: .IndexWith = %import_ref.76d
  365. // CHECK:STDOUT: .ImplicitAs = %import_ref.a69
  366. // CHECK:STDOUT: import Core//prelude
  367. // CHECK:STDOUT: import Core//prelude/...
  368. // CHECK:STDOUT: }
  369. // CHECK:STDOUT: %import_ref.76d: %IndexWith.type.504 = import_ref Core//prelude/operators/index, IndexWith, loaded [template = constants.%IndexWith.generic]
  370. // CHECK:STDOUT: }
  371. // CHECK:STDOUT:
  372. // CHECK:STDOUT: file {
  373. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  374. // CHECK:STDOUT: .Core = imports.%Core
  375. // CHECK:STDOUT: .C = %C.decl
  376. // CHECK:STDOUT: .ElementType = %ElementType.decl
  377. // CHECK:STDOUT: .SubscriptType = %SubscriptType.decl
  378. // CHECK:STDOUT: .c = @__global_init.%c
  379. // CHECK:STDOUT: .x = @__global_init.%x
  380. // CHECK:STDOUT: }
  381. // CHECK:STDOUT: %Core.import = import Core
  382. // CHECK:STDOUT: %C.decl: type = class_decl @C [template = constants.%C] {} {}
  383. // CHECK:STDOUT: %ElementType.decl: type = class_decl @ElementType [template = constants.%ElementType.e6b] {} {}
  384. // CHECK:STDOUT: %SubscriptType.decl: type = class_decl @SubscriptType [template = constants.%SubscriptType.8ee] {} {}
  385. // CHECK:STDOUT: impl_decl @impl [template] {} {
  386. // CHECK:STDOUT: %C.ref: type = name_ref C, file.%C.decl [template = constants.%C]
  387. // CHECK:STDOUT: %Core.ref: <namespace> = name_ref Core, imports.%Core [template = imports.%Core]
  388. // CHECK:STDOUT: %IndexWith.ref: %IndexWith.type.504 = name_ref IndexWith, imports.%import_ref.76d [template = constants.%IndexWith.generic]
  389. // CHECK:STDOUT: %SubscriptType.ref: type = name_ref SubscriptType, file.%SubscriptType.decl [template = constants.%SubscriptType.8ee]
  390. // CHECK:STDOUT: %ElementType.ref: type = name_ref ElementType, file.%ElementType.decl [template = constants.%ElementType.e6b]
  391. // CHECK:STDOUT: %IndexWith.type: type = facet_type <@IndexWith, @IndexWith(constants.%SubscriptType.8ee, constants.%ElementType.e6b)> [template = constants.%IndexWith.type.2d3]
  392. // CHECK:STDOUT: }
  393. // CHECK:STDOUT: %impl_witness: <witness> = impl_witness (@impl.%At.decl) [template = constants.%impl_witness]
  394. // CHECK:STDOUT: }
  395. // CHECK:STDOUT:
  396. // CHECK:STDOUT: impl @impl: %C.ref as %IndexWith.type {
  397. // CHECK:STDOUT: %At.decl: %At.type.afe = fn_decl @At.2 [template = constants.%At.9bf] {
  398. // CHECK:STDOUT: %self.patt: %C = binding_pattern self
  399. // CHECK:STDOUT: %self.param_patt: %C = value_param_pattern %self.patt, runtime_param0
  400. // CHECK:STDOUT: %subscript.patt: %SubscriptType.8ee = binding_pattern subscript
  401. // CHECK:STDOUT: %subscript.param_patt: %SubscriptType.8ee = value_param_pattern %subscript.patt, runtime_param1
  402. // CHECK:STDOUT: %return.patt: %ElementType.e6b = return_slot_pattern
  403. // CHECK:STDOUT: %return.param_patt: %ElementType.e6b = out_param_pattern %return.patt, runtime_param2
  404. // CHECK:STDOUT: } {
  405. // CHECK:STDOUT: %ElementType.ref: type = name_ref ElementType, file.%ElementType.decl [template = constants.%ElementType.e6b]
  406. // CHECK:STDOUT: %self.param: %C = value_param runtime_param0
  407. // CHECK:STDOUT: %Self.ref: type = name_ref Self, @impl.%C.ref [template = constants.%C]
  408. // CHECK:STDOUT: %self: %C = bind_name self, %self.param
  409. // CHECK:STDOUT: %subscript.param: %SubscriptType.8ee = value_param runtime_param1
  410. // CHECK:STDOUT: %SubscriptType.ref: type = name_ref SubscriptType, file.%SubscriptType.decl [template = constants.%SubscriptType.8ee]
  411. // CHECK:STDOUT: %subscript: %SubscriptType.8ee = bind_name subscript, %subscript.param
  412. // CHECK:STDOUT: %return.param: ref %ElementType.e6b = out_param runtime_param2
  413. // CHECK:STDOUT: %return: ref %ElementType.e6b = return_slot %return.param
  414. // CHECK:STDOUT: }
  415. // CHECK:STDOUT:
  416. // CHECK:STDOUT: !members:
  417. // CHECK:STDOUT: .At = %At.decl
  418. // CHECK:STDOUT: witness = file.%impl_witness
  419. // CHECK:STDOUT: }
  420. // CHECK:STDOUT:
  421. // CHECK:STDOUT: class @C {
  422. // CHECK:STDOUT: %complete_type: <witness> = complete_type_witness %empty_struct_type [template = constants.%complete_type]
  423. // CHECK:STDOUT:
  424. // CHECK:STDOUT: !members:
  425. // CHECK:STDOUT: .Self = constants.%C
  426. // CHECK:STDOUT: complete_type_witness = %complete_type
  427. // CHECK:STDOUT: }
  428. // CHECK:STDOUT:
  429. // CHECK:STDOUT: class @ElementType {
  430. // CHECK:STDOUT: %complete_type: <witness> = complete_type_witness %empty_struct_type [template = constants.%complete_type]
  431. // CHECK:STDOUT:
  432. // CHECK:STDOUT: !members:
  433. // CHECK:STDOUT: .Self = constants.%ElementType.e6b
  434. // CHECK:STDOUT: complete_type_witness = %complete_type
  435. // CHECK:STDOUT: }
  436. // CHECK:STDOUT:
  437. // CHECK:STDOUT: class @SubscriptType {
  438. // CHECK:STDOUT: %complete_type: <witness> = complete_type_witness %empty_struct_type [template = constants.%complete_type]
  439. // CHECK:STDOUT:
  440. // CHECK:STDOUT: !members:
  441. // CHECK:STDOUT: .Self = constants.%SubscriptType.8ee
  442. // CHECK:STDOUT: complete_type_witness = %complete_type
  443. // CHECK:STDOUT: }
  444. // CHECK:STDOUT:
  445. // CHECK:STDOUT: fn @At.2[%self.param_patt: %C](%subscript.param_patt: %SubscriptType.8ee) -> %return.param_patt: %ElementType.e6b {
  446. // CHECK:STDOUT: !entry:
  447. // CHECK:STDOUT: %.loc10_13.1: %empty_struct_type = struct_literal ()
  448. // CHECK:STDOUT: %.loc10_13.2: init %ElementType.e6b = class_init (), %return [template = constants.%ElementType.val]
  449. // CHECK:STDOUT: %.loc10_14: init %ElementType.e6b = converted %.loc10_13.1, %.loc10_13.2 [template = constants.%ElementType.val]
  450. // CHECK:STDOUT: return %.loc10_14 to %return
  451. // CHECK:STDOUT: }
  452. // CHECK:STDOUT:
  453. // CHECK:STDOUT: fn @__global_init() {
  454. // CHECK:STDOUT: !entry:
  455. // CHECK:STDOUT: %.loc14_13.1: %empty_struct_type = struct_literal ()
  456. // CHECK:STDOUT: %.loc14_13.2: ref %C = temporary_storage
  457. // CHECK:STDOUT: %.loc14_13.3: init %C = class_init (), %.loc14_13.2 [template = constants.%C.val]
  458. // CHECK:STDOUT: %.loc14_13.4: ref %C = temporary %.loc14_13.2, %.loc14_13.3
  459. // CHECK:STDOUT: %.loc14_14.1: ref %C = converted %.loc14_13.1, %.loc14_13.4
  460. // CHECK:STDOUT: %.loc14_14.2: %C = bind_value %.loc14_14.1
  461. // CHECK:STDOUT: %c: %C = bind_name c, %.loc14_14.2
  462. // CHECK:STDOUT: %c.ref: %C = name_ref c, %c
  463. // CHECK:STDOUT: %int_0: Core.IntLiteral = int_value 0 [template = constants.%int_0]
  464. // CHECK:STDOUT: %.loc22_25.1: %SubscriptType.8ee = converted %int_0, <error> [template = <error>]
  465. // CHECK:STDOUT: %impl.elem0: %At.type.38e = impl_witness_access constants.%impl_witness, element0 [template = constants.%At.9bf]
  466. // CHECK:STDOUT: %At.bound: <bound method> = bound_method %c.ref, %impl.elem0
  467. // CHECK:STDOUT: %.loc22_25.2: ref %ElementType.e6b = temporary_storage
  468. // CHECK:STDOUT: %At.call: init %ElementType.e6b = call %At.bound(%c.ref, <error>) to %.loc22_25.2
  469. // CHECK:STDOUT: %.loc22_25.3: ref %ElementType.e6b = temporary %.loc22_25.2, %At.call
  470. // CHECK:STDOUT: %.loc22_25.4: %ElementType.e6b = bind_value %.loc22_25.3
  471. // CHECK:STDOUT: %x: %ElementType.e6b = bind_name x, %.loc22_25.4
  472. // CHECK:STDOUT: return
  473. // CHECK:STDOUT: }
  474. // CHECK:STDOUT:
  475. // CHECK:STDOUT: --- fail_index_with_not_implemented.carbon
  476. // CHECK:STDOUT:
  477. // CHECK:STDOUT: constants {
  478. // CHECK:STDOUT: %C: type = class_type @C [template]
  479. // CHECK:STDOUT: %empty_struct_type: type = struct_type {} [template]
  480. // CHECK:STDOUT: %complete_type.357: <witness> = complete_type_witness %empty_struct_type [template]
  481. // CHECK:STDOUT: %C.val: %C = struct_value () [template]
  482. // CHECK:STDOUT: %int_32: Core.IntLiteral = int_value 32 [template]
  483. // CHECK:STDOUT: %i32: type = class_type @Int, @Int(%int_32) [template]
  484. // CHECK:STDOUT: %int_0: Core.IntLiteral = int_value 0 [template]
  485. // CHECK:STDOUT: }
  486. // CHECK:STDOUT:
  487. // CHECK:STDOUT: imports {
  488. // CHECK:STDOUT: %Core: <namespace> = namespace file.%Core.import, [template] {
  489. // CHECK:STDOUT: .Int = %import_ref.187
  490. // CHECK:STDOUT: .IndexWith = %import_ref.76d
  491. // CHECK:STDOUT: import Core//prelude
  492. // CHECK:STDOUT: import Core//prelude/...
  493. // CHECK:STDOUT: }
  494. // CHECK:STDOUT: }
  495. // CHECK:STDOUT:
  496. // CHECK:STDOUT: file {
  497. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  498. // CHECK:STDOUT: .Core = imports.%Core
  499. // CHECK:STDOUT: .C = %C.decl
  500. // CHECK:STDOUT: .c = @__global_init.%c
  501. // CHECK:STDOUT: .x = @__global_init.%x
  502. // CHECK:STDOUT: }
  503. // CHECK:STDOUT: %Core.import = import Core
  504. // CHECK:STDOUT: %C.decl: type = class_decl @C [template = constants.%C] {} {}
  505. // CHECK:STDOUT: }
  506. // CHECK:STDOUT:
  507. // CHECK:STDOUT: class @C {
  508. // CHECK:STDOUT: %complete_type: <witness> = complete_type_witness %empty_struct_type [template = constants.%complete_type.357]
  509. // CHECK:STDOUT:
  510. // CHECK:STDOUT: !members:
  511. // CHECK:STDOUT: .Self = constants.%C
  512. // CHECK:STDOUT: complete_type_witness = %complete_type
  513. // CHECK:STDOUT: }
  514. // CHECK:STDOUT:
  515. // CHECK:STDOUT: fn @__global_init() {
  516. // CHECK:STDOUT: !entry:
  517. // CHECK:STDOUT: %.loc6_13.1: %empty_struct_type = struct_literal ()
  518. // CHECK:STDOUT: %.loc6_13.2: ref %C = temporary_storage
  519. // CHECK:STDOUT: %.loc6_13.3: init %C = class_init (), %.loc6_13.2 [template = constants.%C.val]
  520. // CHECK:STDOUT: %.loc6_13.4: ref %C = temporary %.loc6_13.2, %.loc6_13.3
  521. // CHECK:STDOUT: %.loc6_14.1: ref %C = converted %.loc6_13.1, %.loc6_13.4
  522. // CHECK:STDOUT: %.loc6_14.2: %C = bind_value %.loc6_14.1
  523. // CHECK:STDOUT: %c: %C = bind_name c, %.loc6_14.2
  524. // CHECK:STDOUT: %c.ref: %C = name_ref c, %c
  525. // CHECK:STDOUT: %int_0: Core.IntLiteral = int_value 0 [template = constants.%int_0]
  526. // CHECK:STDOUT: %x: %i32 = bind_name x, <error>
  527. // CHECK:STDOUT: return
  528. // CHECK:STDOUT: }
  529. // CHECK:STDOUT: