fail_invalid_base.carbon 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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/index/fail_invalid_base.carbon
  8. // TIP: To dump output, run:
  9. // TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/index/fail_invalid_base.carbon
  10. namespace N;
  11. // CHECK:STDERR: fail_invalid_base.carbon:[[@LINE+4]]:14: error: expression cannot be used as a value [UseOfNonExprAsValue]
  12. // CHECK:STDERR: var a: i32 = N[0];
  13. // CHECK:STDERR: ^
  14. // CHECK:STDERR:
  15. var a: i32 = N[0];
  16. fn F();
  17. // CHECK:STDERR: fail_invalid_base.carbon:[[@LINE+4]]:14: error: expression cannot be used as a value [UseOfNonExprAsValue]
  18. // CHECK:STDERR: var b: i32 = F[1];
  19. // CHECK:STDERR: ^
  20. // CHECK:STDERR:
  21. var b: i32 = F[1];
  22. // CHECK:STDERR: fail_invalid_base.carbon:[[@LINE+4]]:14: error: type `{.a: i32, .b: i32}` does not support indexing [TypeNotIndexable]
  23. // CHECK:STDERR: var c: i32 = {.a = 1, .b = 2}[0];
  24. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~
  25. // CHECK:STDERR:
  26. var c: i32 = {.a = 1, .b = 2}[0];
  27. // CHECK:STDERR: fail_invalid_base.carbon:[[@LINE+3]]:14: error: type `type` does not support indexing [TypeNotIndexable]
  28. // CHECK:STDERR: var d: i32 = {.a: i32, .b: i32}[0];
  29. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~
  30. var d: i32 = {.a: i32, .b: i32}[0];
  31. // CHECK:STDOUT: --- fail_invalid_base.carbon
  32. // CHECK:STDOUT:
  33. // CHECK:STDOUT: constants {
  34. // CHECK:STDOUT: %Int32.type: type = fn_type @Int32 [template]
  35. // CHECK:STDOUT: %.1: type = tuple_type () [template]
  36. // CHECK:STDOUT: %Int32: %Int32.type = struct_value () [template]
  37. // CHECK:STDOUT: %.2: i32 = int_literal 0 [template]
  38. // CHECK:STDOUT: %F.type: type = fn_type @F [template]
  39. // CHECK:STDOUT: %F: %F.type = struct_value () [template]
  40. // CHECK:STDOUT: %.3: i32 = int_literal 1 [template]
  41. // CHECK:STDOUT: %.4: i32 = int_literal 2 [template]
  42. // CHECK:STDOUT: %.5: type = struct_type {.a: i32, .b: i32} [template]
  43. // CHECK:STDOUT: %.6: type = ptr_type %.5 [template]
  44. // CHECK:STDOUT: %struct: %.5 = struct_value (%.3, %.4) [template]
  45. // CHECK:STDOUT: %IndexWith.type.1: type = generic_interface_type @IndexWith [template]
  46. // CHECK:STDOUT: %IndexWith: %IndexWith.type.1 = struct_value () [template]
  47. // CHECK:STDOUT: %ElementType: type = bind_symbolic_name ElementType, 1 [symbolic]
  48. // CHECK:STDOUT: %SubscriptType: type = bind_symbolic_name SubscriptType, 0 [symbolic]
  49. // CHECK:STDOUT: %IndexWith.type.2: type = interface_type @IndexWith, @IndexWith(%SubscriptType, %ElementType) [symbolic]
  50. // CHECK:STDOUT: %Self.1: @IndexWith.%IndexWith.type (%IndexWith.type.2) = bind_symbolic_name Self, 2 [symbolic]
  51. // CHECK:STDOUT: %ElementType.patt: type = symbolic_binding_pattern ElementType, 1 [symbolic]
  52. // CHECK:STDOUT: %SubscriptType.patt: type = symbolic_binding_pattern SubscriptType, 0 [symbolic]
  53. // CHECK:STDOUT: %Self.2: %IndexWith.type.2 = bind_symbolic_name Self, 2 [symbolic]
  54. // CHECK:STDOUT: %At.type: type = fn_type @At, @IndexWith(%SubscriptType, %ElementType) [symbolic]
  55. // CHECK:STDOUT: %At: %At.type = struct_value () [symbolic]
  56. // CHECK:STDOUT: %.7: type = assoc_entity_type %IndexWith.type.2, %At.type [symbolic]
  57. // CHECK:STDOUT: %.8: %.7 = assoc_entity element0, imports.%import_ref.6 [symbolic]
  58. // CHECK:STDOUT: }
  59. // CHECK:STDOUT:
  60. // CHECK:STDOUT: imports {
  61. // CHECK:STDOUT: %Core: <namespace> = namespace file.%Core.import, [template] {
  62. // CHECK:STDOUT: .Int32 = %import_ref.1
  63. // CHECK:STDOUT: .IndexWith = %import_ref.2
  64. // CHECK:STDOUT: import Core//prelude
  65. // CHECK:STDOUT: import Core//prelude/...
  66. // CHECK:STDOUT: }
  67. // CHECK:STDOUT: %import_ref.1: %Int32.type = import_ref Core//prelude/types, inst+15, loaded [template = constants.%Int32]
  68. // CHECK:STDOUT: %import_ref.2: %IndexWith.type.1 = import_ref Core//prelude/operators/index, inst+13, loaded [template = constants.%IndexWith]
  69. // CHECK:STDOUT: %import_ref.3 = import_ref Core//prelude/operators/index, inst+22, unloaded
  70. // CHECK:STDOUT: %import_ref.4 = import_ref Core//prelude/operators/index, inst+50, unloaded
  71. // CHECK:STDOUT: %import_ref.5 = import_ref Core//prelude/operators/index, inst+42, unloaded
  72. // CHECK:STDOUT: %import_ref.6 = import_ref Core//prelude/operators/index, inst+42, unloaded
  73. // CHECK:STDOUT: }
  74. // CHECK:STDOUT:
  75. // CHECK:STDOUT: file {
  76. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  77. // CHECK:STDOUT: .Core = imports.%Core
  78. // CHECK:STDOUT: .N = %N
  79. // CHECK:STDOUT: .a = %a
  80. // CHECK:STDOUT: .F = %F.decl
  81. // CHECK:STDOUT: .b = %b
  82. // CHECK:STDOUT: .c = %c
  83. // CHECK:STDOUT: .d = %d
  84. // CHECK:STDOUT: }
  85. // CHECK:STDOUT: %Core.import = import Core
  86. // CHECK:STDOUT: %N: <namespace> = namespace [template] {}
  87. // CHECK:STDOUT: %int.make_type_32.loc16: init type = call constants.%Int32() [template = i32]
  88. // CHECK:STDOUT: %.loc16_8.1: type = value_of_initializer %int.make_type_32.loc16 [template = i32]
  89. // CHECK:STDOUT: %.loc16_8.2: type = converted %int.make_type_32.loc16, %.loc16_8.1 [template = i32]
  90. // CHECK:STDOUT: %a.var: ref i32 = var a
  91. // CHECK:STDOUT: %a: ref i32 = bind_name a, %a.var
  92. // CHECK:STDOUT: %F.decl: %F.type = fn_decl @F [template = constants.%F] {} {}
  93. // CHECK:STDOUT: %int.make_type_32.loc23: init type = call constants.%Int32() [template = i32]
  94. // CHECK:STDOUT: %.loc23_8.1: type = value_of_initializer %int.make_type_32.loc23 [template = i32]
  95. // CHECK:STDOUT: %.loc23_8.2: type = converted %int.make_type_32.loc23, %.loc23_8.1 [template = i32]
  96. // CHECK:STDOUT: %b.var: ref i32 = var b
  97. // CHECK:STDOUT: %b: ref i32 = bind_name b, %b.var
  98. // CHECK:STDOUT: %int.make_type_32.loc29: init type = call constants.%Int32() [template = i32]
  99. // CHECK:STDOUT: %.loc29_8.1: type = value_of_initializer %int.make_type_32.loc29 [template = i32]
  100. // CHECK:STDOUT: %.loc29_8.2: type = converted %int.make_type_32.loc29, %.loc29_8.1 [template = i32]
  101. // CHECK:STDOUT: %c.var: ref i32 = var c
  102. // CHECK:STDOUT: %c: ref i32 = bind_name c, %c.var
  103. // CHECK:STDOUT: %int.make_type_32.loc34: init type = call constants.%Int32() [template = i32]
  104. // CHECK:STDOUT: %.loc34_8.1: type = value_of_initializer %int.make_type_32.loc34 [template = i32]
  105. // CHECK:STDOUT: %.loc34_8.2: type = converted %int.make_type_32.loc34, %.loc34_8.1 [template = i32]
  106. // CHECK:STDOUT: %d.var: ref i32 = var d
  107. // CHECK:STDOUT: %d: ref i32 = bind_name d, %d.var
  108. // CHECK:STDOUT: }
  109. // CHECK:STDOUT:
  110. // CHECK:STDOUT: generic interface @IndexWith(constants.%SubscriptType: type, constants.%ElementType: type) {
  111. // CHECK:STDOUT: %SubscriptType: type = bind_symbolic_name SubscriptType, 0 [symbolic = %SubscriptType (constants.%SubscriptType)]
  112. // CHECK:STDOUT: %SubscriptType.patt: type = symbolic_binding_pattern SubscriptType, 0 [symbolic = %SubscriptType.patt (constants.%SubscriptType.patt)]
  113. // CHECK:STDOUT: %ElementType: type = bind_symbolic_name ElementType, 1 [symbolic = %ElementType (constants.%ElementType)]
  114. // CHECK:STDOUT: %ElementType.patt: type = symbolic_binding_pattern ElementType, 1 [symbolic = %ElementType.patt (constants.%ElementType.patt)]
  115. // CHECK:STDOUT:
  116. // CHECK:STDOUT: !definition:
  117. // CHECK:STDOUT: %IndexWith.type: type = interface_type @IndexWith, @IndexWith(%SubscriptType, %ElementType) [symbolic = %IndexWith.type (constants.%IndexWith.type.2)]
  118. // CHECK:STDOUT: %Self: %IndexWith.type.2 = bind_symbolic_name Self, 2 [symbolic = %Self (constants.%Self.2)]
  119. // CHECK:STDOUT: %At.type: type = fn_type @At, @IndexWith(%SubscriptType, %ElementType) [symbolic = %At.type (constants.%At.type)]
  120. // CHECK:STDOUT: %At: @IndexWith.%At.type (%At.type) = struct_value () [symbolic = %At (constants.%At)]
  121. // CHECK:STDOUT: %.1: type = assoc_entity_type @IndexWith.%IndexWith.type (%IndexWith.type.2), @IndexWith.%At.type (%At.type) [symbolic = %.1 (constants.%.7)]
  122. // CHECK:STDOUT: %.2: @IndexWith.%.1 (%.7) = assoc_entity element0, imports.%import_ref.6 [symbolic = %.2 (constants.%.8)]
  123. // CHECK:STDOUT:
  124. // CHECK:STDOUT: interface {
  125. // CHECK:STDOUT: !members:
  126. // CHECK:STDOUT: .Self = imports.%import_ref.3
  127. // CHECK:STDOUT: .At = imports.%import_ref.4
  128. // CHECK:STDOUT: witness = (imports.%import_ref.5)
  129. // CHECK:STDOUT: }
  130. // CHECK:STDOUT: }
  131. // CHECK:STDOUT:
  132. // CHECK:STDOUT: fn @Int32() -> type = "int.make_type_32";
  133. // CHECK:STDOUT:
  134. // CHECK:STDOUT: fn @F();
  135. // CHECK:STDOUT:
  136. // CHECK:STDOUT: generic fn @At(constants.%SubscriptType: type, constants.%ElementType: type, constants.%Self.1: @IndexWith.%IndexWith.type (%IndexWith.type.2)) {
  137. // CHECK:STDOUT: %SubscriptType: type = bind_symbolic_name SubscriptType, 0 [symbolic = %SubscriptType (constants.%SubscriptType)]
  138. // CHECK:STDOUT: %ElementType: type = bind_symbolic_name ElementType, 1 [symbolic = %ElementType (constants.%ElementType)]
  139. // CHECK:STDOUT: %IndexWith.type: type = interface_type @IndexWith, @IndexWith(%SubscriptType, %ElementType) [symbolic = %IndexWith.type (constants.%IndexWith.type.2)]
  140. // CHECK:STDOUT: %Self: %IndexWith.type.2 = bind_symbolic_name Self, 2 [symbolic = %Self (constants.%Self.2)]
  141. // CHECK:STDOUT:
  142. // CHECK:STDOUT: fn[%self.param_patt: @At.%Self (%Self.2)](%subscript.param_patt: @At.%SubscriptType (%SubscriptType)) -> @At.%ElementType (%ElementType);
  143. // CHECK:STDOUT: }
  144. // CHECK:STDOUT:
  145. // CHECK:STDOUT: fn @__global_init() {
  146. // CHECK:STDOUT: !entry:
  147. // CHECK:STDOUT: %N.ref: <namespace> = name_ref N, file.%N [template = file.%N]
  148. // CHECK:STDOUT: %.loc16: i32 = int_literal 0 [template = constants.%.2]
  149. // CHECK:STDOUT: assign file.%a.var, <error>
  150. // CHECK:STDOUT: %F.ref: %F.type = name_ref F, file.%F.decl [template = constants.%F]
  151. // CHECK:STDOUT: %.loc23: i32 = int_literal 1 [template = constants.%.3]
  152. // CHECK:STDOUT: assign file.%b.var, <error>
  153. // CHECK:STDOUT: %.loc29_20: i32 = int_literal 1 [template = constants.%.3]
  154. // CHECK:STDOUT: %.loc29_28: i32 = int_literal 2 [template = constants.%.4]
  155. // CHECK:STDOUT: %.loc29_29.1: %.5 = struct_literal (%.loc29_20, %.loc29_28)
  156. // CHECK:STDOUT: %.loc29_31: i32 = int_literal 0 [template = constants.%.2]
  157. // CHECK:STDOUT: %struct: %.5 = struct_value (%.loc29_20, %.loc29_28) [template = constants.%struct]
  158. // CHECK:STDOUT: %.loc29_29.2: %.5 = converted %.loc29_29.1, %struct [template = constants.%struct]
  159. // CHECK:STDOUT: assign file.%c.var, <error>
  160. // CHECK:STDOUT: %int.make_type_32.loc34_19: init type = call constants.%Int32() [template = i32]
  161. // CHECK:STDOUT: %.loc34_19.1: type = value_of_initializer %int.make_type_32.loc34_19 [template = i32]
  162. // CHECK:STDOUT: %.loc34_19.2: type = converted %int.make_type_32.loc34_19, %.loc34_19.1 [template = i32]
  163. // CHECK:STDOUT: %int.make_type_32.loc34_28: init type = call constants.%Int32() [template = i32]
  164. // CHECK:STDOUT: %.loc34_28.1: type = value_of_initializer %int.make_type_32.loc34_28 [template = i32]
  165. // CHECK:STDOUT: %.loc34_28.2: type = converted %int.make_type_32.loc34_28, %.loc34_28.1 [template = i32]
  166. // CHECK:STDOUT: %.loc34_31: type = struct_type {.a: i32, .b: i32} [template = constants.%.5]
  167. // CHECK:STDOUT: %.loc34_33: i32 = int_literal 0 [template = constants.%.2]
  168. // CHECK:STDOUT: assign file.%d.var, <error>
  169. // CHECK:STDOUT: return
  170. // CHECK:STDOUT: }
  171. // CHECK:STDOUT:
  172. // CHECK:STDOUT: specific @IndexWith(constants.%SubscriptType, constants.%ElementType) {
  173. // CHECK:STDOUT: %SubscriptType => constants.%SubscriptType
  174. // CHECK:STDOUT: %SubscriptType.patt => constants.%SubscriptType
  175. // CHECK:STDOUT: %ElementType => constants.%ElementType
  176. // CHECK:STDOUT: %ElementType.patt => constants.%ElementType
  177. // CHECK:STDOUT: }
  178. // CHECK:STDOUT:
  179. // CHECK:STDOUT: specific @IndexWith(@IndexWith.%SubscriptType, @IndexWith.%ElementType) {
  180. // CHECK:STDOUT: %SubscriptType => constants.%SubscriptType
  181. // CHECK:STDOUT: %SubscriptType.patt => constants.%SubscriptType
  182. // CHECK:STDOUT: %ElementType => constants.%ElementType
  183. // CHECK:STDOUT: %ElementType.patt => constants.%ElementType
  184. // CHECK:STDOUT: }
  185. // CHECK:STDOUT:
  186. // CHECK:STDOUT: specific @IndexWith(@At.%SubscriptType, @At.%ElementType) {
  187. // CHECK:STDOUT: %SubscriptType => constants.%SubscriptType
  188. // CHECK:STDOUT: %SubscriptType.patt => constants.%SubscriptType
  189. // CHECK:STDOUT: %ElementType => constants.%ElementType
  190. // CHECK:STDOUT: %ElementType.patt => constants.%ElementType
  191. // CHECK:STDOUT: }
  192. // CHECK:STDOUT:
  193. // CHECK:STDOUT: specific @At(constants.%SubscriptType, constants.%ElementType, constants.%Self.1) {
  194. // CHECK:STDOUT: %SubscriptType => constants.%SubscriptType
  195. // CHECK:STDOUT: %ElementType => constants.%ElementType
  196. // CHECK:STDOUT: %IndexWith.type => constants.%IndexWith.type.2
  197. // CHECK:STDOUT: %Self => constants.%Self.1
  198. // CHECK:STDOUT: }
  199. // CHECK:STDOUT: