fail_non_tuple_access.carbon 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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/tuple/access/fail_non_tuple_access.carbon
  8. // TIP: To dump output, run:
  9. // TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/tuple/access/fail_non_tuple_access.carbon
  10. fn Main() {
  11. // CHECK:STDERR: fail_non_tuple_access.carbon:[[@LINE+4]]:3: error: type `Core.IntLiteral` does not support indexing [TypeNotIndexable]
  12. // CHECK:STDERR: 0[1];
  13. // CHECK:STDERR: ^~~~
  14. // CHECK:STDERR:
  15. 0[1];
  16. var non_tuple: array(i32, 2) = (5, 5);
  17. // CHECK:STDERR: fail_non_tuple_access.carbon:[[@LINE+4]]:20: error: type `array(i32, 2)` does not support tuple indexing; only tuples can be indexed that way [TupleIndexOnANonTupleType]
  18. // CHECK:STDERR: var first: i32 = non_tuple.0;
  19. // CHECK:STDERR: ^~~~~~~~~~~
  20. // CHECK:STDERR:
  21. var first: i32 = non_tuple.0;
  22. }
  23. // CHECK:STDOUT: --- fail_non_tuple_access.carbon
  24. // CHECK:STDOUT:
  25. // CHECK:STDOUT: constants {
  26. // CHECK:STDOUT: %Main.type: type = fn_type @Main [concrete]
  27. // CHECK:STDOUT: %Main: %Main.type = struct_value () [concrete]
  28. // CHECK:STDOUT: %int_0: Core.IntLiteral = int_value 0 [concrete]
  29. // CHECK:STDOUT: %int_1: Core.IntLiteral = int_value 1 [concrete]
  30. // CHECK:STDOUT: %IndexWith.type.504: type = generic_interface_type @IndexWith [concrete]
  31. // CHECK:STDOUT: %IndexWith.generic: %IndexWith.type.504 = struct_value () [concrete]
  32. // CHECK:STDOUT: %int_32: Core.IntLiteral = int_value 32 [concrete]
  33. // CHECK:STDOUT: %Int.type: type = generic_class_type @Int [concrete]
  34. // CHECK:STDOUT: %Int.generic: %Int.type = struct_value () [concrete]
  35. // CHECK:STDOUT: %i32: type = class_type @Int, @Int(%int_32) [concrete]
  36. // CHECK:STDOUT: %int_2: Core.IntLiteral = int_value 2 [concrete]
  37. // CHECK:STDOUT: %array_type: type = array_type %int_2, %i32 [concrete]
  38. // CHECK:STDOUT: %pattern_type.ae7: type = pattern_type %array_type [concrete]
  39. // CHECK:STDOUT: %int_5.64b: Core.IntLiteral = int_value 5 [concrete]
  40. // CHECK:STDOUT: %tuple.type: type = tuple_type (Core.IntLiteral, Core.IntLiteral) [concrete]
  41. // CHECK:STDOUT: %ImplicitAs.type.cc7: type = generic_interface_type @ImplicitAs [concrete]
  42. // CHECK:STDOUT: %ImplicitAs.generic: %ImplicitAs.type.cc7 = struct_value () [concrete]
  43. // CHECK:STDOUT: %ImplicitAs.type.205: type = facet_type <@ImplicitAs, @ImplicitAs(%i32)> [concrete]
  44. // CHECK:STDOUT: %Convert.type.1b6: type = fn_type @Convert.1, @ImplicitAs(%i32) [concrete]
  45. // CHECK:STDOUT: %To.c80: Core.IntLiteral = bind_symbolic_name To, 0 [symbolic]
  46. // CHECK:STDOUT: %Convert.type.0f9: type = fn_type @Convert.2, @impl.4f9(%To.c80) [symbolic]
  47. // CHECK:STDOUT: %Convert.f06: %Convert.type.0f9 = struct_value () [symbolic]
  48. // CHECK:STDOUT: %ImplicitAs.impl_witness.c75: <witness> = impl_witness imports.%ImplicitAs.impl_witness_table.a2f, @impl.4f9(%int_32) [concrete]
  49. // CHECK:STDOUT: %Convert.type.035: type = fn_type @Convert.2, @impl.4f9(%int_32) [concrete]
  50. // CHECK:STDOUT: %Convert.956: %Convert.type.035 = struct_value () [concrete]
  51. // CHECK:STDOUT: %ImplicitAs.facet: %ImplicitAs.type.205 = facet_value Core.IntLiteral, (%ImplicitAs.impl_witness.c75) [concrete]
  52. // CHECK:STDOUT: %.9c3: type = fn_type_with_self_type %Convert.type.1b6, %ImplicitAs.facet [concrete]
  53. // CHECK:STDOUT: %Convert.bound: <bound method> = bound_method %int_5.64b, %Convert.956 [concrete]
  54. // CHECK:STDOUT: %pattern_type.7ce: type = pattern_type %i32 [concrete]
  55. // CHECK:STDOUT: %Convert.specific_fn: <specific function> = specific_function %Convert.956, @Convert.2(%int_32) [concrete]
  56. // CHECK:STDOUT: %bound_method: <bound method> = bound_method %int_5.64b, %Convert.specific_fn [concrete]
  57. // CHECK:STDOUT: %int_5.0f6: %i32 = int_value 5 [concrete]
  58. // CHECK:STDOUT: %array: %array_type = tuple_value (%int_5.0f6, %int_5.0f6) [concrete]
  59. // CHECK:STDOUT: }
  60. // CHECK:STDOUT:
  61. // CHECK:STDOUT: imports {
  62. // CHECK:STDOUT: %Core: <namespace> = namespace file.%Core.import, [concrete] {
  63. // CHECK:STDOUT: .IndexWith = %Core.IndexWith
  64. // CHECK:STDOUT: .Int = %Core.Int
  65. // CHECK:STDOUT: .ImplicitAs = %Core.ImplicitAs
  66. // CHECK:STDOUT: import Core//prelude
  67. // CHECK:STDOUT: import Core//prelude/...
  68. // CHECK:STDOUT: }
  69. // CHECK:STDOUT: %Core.IndexWith: %IndexWith.type.504 = import_ref Core//prelude/operators/index, IndexWith, loaded [concrete = constants.%IndexWith.generic]
  70. // CHECK:STDOUT: %Core.Int: %Int.type = import_ref Core//prelude/types/int, Int, loaded [concrete = constants.%Int.generic]
  71. // CHECK:STDOUT: %Core.ImplicitAs: %ImplicitAs.type.cc7 = import_ref Core//prelude/operators/as, ImplicitAs, loaded [concrete = constants.%ImplicitAs.generic]
  72. // CHECK:STDOUT: %Core.import_ref.a5b: @impl.4f9.%Convert.type (%Convert.type.0f9) = import_ref Core//prelude/types/int, loc19_39, loaded [symbolic = @impl.4f9.%Convert (constants.%Convert.f06)]
  73. // CHECK:STDOUT: %ImplicitAs.impl_witness_table.a2f = impl_witness_table (%Core.import_ref.a5b), @impl.4f9 [concrete]
  74. // CHECK:STDOUT: }
  75. // CHECK:STDOUT:
  76. // CHECK:STDOUT: file {
  77. // CHECK:STDOUT: package: <namespace> = namespace [concrete] {
  78. // CHECK:STDOUT: .Core = imports.%Core
  79. // CHECK:STDOUT: .Main = %Main.decl
  80. // CHECK:STDOUT: }
  81. // CHECK:STDOUT: %Core.import = import Core
  82. // CHECK:STDOUT: %Main.decl: %Main.type = fn_decl @Main [concrete = constants.%Main] {} {}
  83. // CHECK:STDOUT: }
  84. // CHECK:STDOUT:
  85. // CHECK:STDOUT: fn @Main() {
  86. // CHECK:STDOUT: !entry:
  87. // CHECK:STDOUT: %int_0.loc16: Core.IntLiteral = int_value 0 [concrete = constants.%int_0]
  88. // CHECK:STDOUT: %int_1.loc16: Core.IntLiteral = int_value 1 [concrete = constants.%int_1]
  89. // CHECK:STDOUT: name_binding_decl {
  90. // CHECK:STDOUT: %non_tuple.patt: %pattern_type.ae7 = binding_pattern non_tuple [concrete]
  91. // CHECK:STDOUT: %non_tuple.var_patt: %pattern_type.ae7 = var_pattern %non_tuple.patt [concrete]
  92. // CHECK:STDOUT: }
  93. // CHECK:STDOUT: %non_tuple.var: ref %array_type = var %non_tuple.var_patt
  94. // CHECK:STDOUT: %int_5.loc18_35: Core.IntLiteral = int_value 5 [concrete = constants.%int_5.64b]
  95. // CHECK:STDOUT: %int_5.loc18_38: Core.IntLiteral = int_value 5 [concrete = constants.%int_5.64b]
  96. // CHECK:STDOUT: %.loc18_39.1: %tuple.type = tuple_literal (%int_5.loc18_35, %int_5.loc18_38)
  97. // CHECK:STDOUT: %impl.elem0.loc18_39.1: %.9c3 = impl_witness_access constants.%ImplicitAs.impl_witness.c75, element0 [concrete = constants.%Convert.956]
  98. // CHECK:STDOUT: %bound_method.loc18_39.1: <bound method> = bound_method %int_5.loc18_35, %impl.elem0.loc18_39.1 [concrete = constants.%Convert.bound]
  99. // CHECK:STDOUT: %specific_fn.loc18_39.1: <specific function> = specific_function %impl.elem0.loc18_39.1, @Convert.2(constants.%int_32) [concrete = constants.%Convert.specific_fn]
  100. // CHECK:STDOUT: %bound_method.loc18_39.2: <bound method> = bound_method %int_5.loc18_35, %specific_fn.loc18_39.1 [concrete = constants.%bound_method]
  101. // CHECK:STDOUT: %int.convert_checked.loc18_39.1: init %i32 = call %bound_method.loc18_39.2(%int_5.loc18_35) [concrete = constants.%int_5.0f6]
  102. // CHECK:STDOUT: %.loc18_39.2: init %i32 = converted %int_5.loc18_35, %int.convert_checked.loc18_39.1 [concrete = constants.%int_5.0f6]
  103. // CHECK:STDOUT: %int_0.loc18: Core.IntLiteral = int_value 0 [concrete = constants.%int_0]
  104. // CHECK:STDOUT: %.loc18_39.3: ref %i32 = array_index %non_tuple.var, %int_0.loc18
  105. // CHECK:STDOUT: %.loc18_39.4: init %i32 = initialize_from %.loc18_39.2 to %.loc18_39.3 [concrete = constants.%int_5.0f6]
  106. // CHECK:STDOUT: %impl.elem0.loc18_39.2: %.9c3 = impl_witness_access constants.%ImplicitAs.impl_witness.c75, element0 [concrete = constants.%Convert.956]
  107. // CHECK:STDOUT: %bound_method.loc18_39.3: <bound method> = bound_method %int_5.loc18_38, %impl.elem0.loc18_39.2 [concrete = constants.%Convert.bound]
  108. // CHECK:STDOUT: %specific_fn.loc18_39.2: <specific function> = specific_function %impl.elem0.loc18_39.2, @Convert.2(constants.%int_32) [concrete = constants.%Convert.specific_fn]
  109. // CHECK:STDOUT: %bound_method.loc18_39.4: <bound method> = bound_method %int_5.loc18_38, %specific_fn.loc18_39.2 [concrete = constants.%bound_method]
  110. // CHECK:STDOUT: %int.convert_checked.loc18_39.2: init %i32 = call %bound_method.loc18_39.4(%int_5.loc18_38) [concrete = constants.%int_5.0f6]
  111. // CHECK:STDOUT: %.loc18_39.5: init %i32 = converted %int_5.loc18_38, %int.convert_checked.loc18_39.2 [concrete = constants.%int_5.0f6]
  112. // CHECK:STDOUT: %int_1.loc18: Core.IntLiteral = int_value 1 [concrete = constants.%int_1]
  113. // CHECK:STDOUT: %.loc18_39.6: ref %i32 = array_index %non_tuple.var, %int_1.loc18
  114. // CHECK:STDOUT: %.loc18_39.7: init %i32 = initialize_from %.loc18_39.5 to %.loc18_39.6 [concrete = constants.%int_5.0f6]
  115. // CHECK:STDOUT: %.loc18_39.8: init %array_type = array_init (%.loc18_39.4, %.loc18_39.7) to %non_tuple.var [concrete = constants.%array]
  116. // CHECK:STDOUT: %.loc18_3: init %array_type = converted %.loc18_39.1, %.loc18_39.8 [concrete = constants.%array]
  117. // CHECK:STDOUT: assign %non_tuple.var, %.loc18_3
  118. // CHECK:STDOUT: %.loc18_30: type = splice_block %array_type [concrete = constants.%array_type] {
  119. // CHECK:STDOUT: %int_32.loc18: Core.IntLiteral = int_value 32 [concrete = constants.%int_32]
  120. // CHECK:STDOUT: %i32.loc18: type = class_type @Int, @Int(constants.%int_32) [concrete = constants.%i32]
  121. // CHECK:STDOUT: %int_2: Core.IntLiteral = int_value 2 [concrete = constants.%int_2]
  122. // CHECK:STDOUT: %array_type: type = array_type %int_2, %i32.loc18 [concrete = constants.%array_type]
  123. // CHECK:STDOUT: }
  124. // CHECK:STDOUT: %non_tuple: ref %array_type = bind_name non_tuple, %non_tuple.var
  125. // CHECK:STDOUT: name_binding_decl {
  126. // CHECK:STDOUT: %first.patt: %pattern_type.7ce = binding_pattern first [concrete]
  127. // CHECK:STDOUT: %first.var_patt: %pattern_type.7ce = var_pattern %first.patt [concrete]
  128. // CHECK:STDOUT: }
  129. // CHECK:STDOUT: %first.var: ref %i32 = var %first.var_patt
  130. // CHECK:STDOUT: %non_tuple.ref: ref %array_type = name_ref non_tuple, %non_tuple
  131. // CHECK:STDOUT: %int_0.loc23: Core.IntLiteral = int_value 0 [concrete = constants.%int_0]
  132. // CHECK:STDOUT: assign %first.var, <error>
  133. // CHECK:STDOUT: %.loc23: type = splice_block %i32.loc23 [concrete = constants.%i32] {
  134. // CHECK:STDOUT: %int_32.loc23: Core.IntLiteral = int_value 32 [concrete = constants.%int_32]
  135. // CHECK:STDOUT: %i32.loc23: type = class_type @Int, @Int(constants.%int_32) [concrete = constants.%i32]
  136. // CHECK:STDOUT: }
  137. // CHECK:STDOUT: %first: ref %i32 = bind_name first, %first.var
  138. // CHECK:STDOUT: return
  139. // CHECK:STDOUT: }
  140. // CHECK:STDOUT: