fail_non_tuple_access.carbon 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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: [i32; 2] = (5, 5);
  17. // CHECK:STDERR: fail_non_tuple_access.carbon:[[@LINE+4]]:20: error: type `[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: %int_32: Core.IntLiteral = int_value 32 [concrete]
  31. // CHECK:STDOUT: %i32: type = class_type @Int, @Int(%int_32) [concrete]
  32. // CHECK:STDOUT: %int_2: Core.IntLiteral = int_value 2 [concrete]
  33. // CHECK:STDOUT: %array_type: type = array_type %int_2, %i32 [concrete]
  34. // CHECK:STDOUT: %int_5.64b: Core.IntLiteral = int_value 5 [concrete]
  35. // CHECK:STDOUT: %tuple.type: type = tuple_type (Core.IntLiteral, Core.IntLiteral) [concrete]
  36. // CHECK:STDOUT: %ImplicitAs.type.205: type = facet_type <@ImplicitAs, @ImplicitAs(%i32)> [concrete]
  37. // CHECK:STDOUT: %Convert.type.1b6: type = fn_type @Convert.1, @ImplicitAs(%i32) [concrete]
  38. // CHECK:STDOUT: %impl_witness.d39: <witness> = impl_witness (imports.%Core.import_ref.a5b), @impl.1(%int_32) [concrete]
  39. // CHECK:STDOUT: %Convert.type.035: type = fn_type @Convert.2, @impl.1(%int_32) [concrete]
  40. // CHECK:STDOUT: %Convert.956: %Convert.type.035 = struct_value () [concrete]
  41. // CHECK:STDOUT: %ImplicitAs.facet: %ImplicitAs.type.205 = facet_value Core.IntLiteral, %impl_witness.d39 [concrete]
  42. // CHECK:STDOUT: %.a0b: type = fn_type_with_self_type %Convert.type.1b6, %ImplicitAs.facet [concrete]
  43. // CHECK:STDOUT: %Convert.bound: <bound method> = bound_method %int_5.64b, %Convert.956 [concrete]
  44. // CHECK:STDOUT: %Convert.specific_fn: <specific function> = specific_function %Convert.bound, @Convert.2(%int_32) [concrete]
  45. // CHECK:STDOUT: %int_5.0f6: %i32 = int_value 5 [concrete]
  46. // CHECK:STDOUT: %array: %array_type = tuple_value (%int_5.0f6, %int_5.0f6) [concrete]
  47. // CHECK:STDOUT: }
  48. // CHECK:STDOUT:
  49. // CHECK:STDOUT: imports {
  50. // CHECK:STDOUT: %Core: <namespace> = namespace file.%Core.import, [concrete] {
  51. // CHECK:STDOUT: .IndexWith = %Core.IndexWith
  52. // CHECK:STDOUT: .Int = %Core.Int
  53. // CHECK:STDOUT: .ImplicitAs = %Core.ImplicitAs
  54. // CHECK:STDOUT: import Core//prelude
  55. // CHECK:STDOUT: import Core//prelude/...
  56. // CHECK:STDOUT: }
  57. // CHECK:STDOUT: }
  58. // CHECK:STDOUT:
  59. // CHECK:STDOUT: file {
  60. // CHECK:STDOUT: package: <namespace> = namespace [concrete] {
  61. // CHECK:STDOUT: .Core = imports.%Core
  62. // CHECK:STDOUT: .Main = %Main.decl
  63. // CHECK:STDOUT: }
  64. // CHECK:STDOUT: %Core.import = import Core
  65. // CHECK:STDOUT: %Main.decl: %Main.type = fn_decl @Main [concrete = constants.%Main] {} {}
  66. // CHECK:STDOUT: }
  67. // CHECK:STDOUT:
  68. // CHECK:STDOUT: fn @Main() {
  69. // CHECK:STDOUT: !entry:
  70. // CHECK:STDOUT: %int_0.loc16: Core.IntLiteral = int_value 0 [concrete = constants.%int_0]
  71. // CHECK:STDOUT: %int_1.loc16: Core.IntLiteral = int_value 1 [concrete = constants.%int_1]
  72. // CHECK:STDOUT: name_binding_decl {
  73. // CHECK:STDOUT: %non_tuple.patt: %array_type = binding_pattern non_tuple
  74. // CHECK:STDOUT: %.loc18_3.1: %array_type = var_pattern %non_tuple.patt
  75. // CHECK:STDOUT: }
  76. // CHECK:STDOUT: %non_tuple.var: ref %array_type = var non_tuple
  77. // CHECK:STDOUT: %int_5.loc18_30: Core.IntLiteral = int_value 5 [concrete = constants.%int_5.64b]
  78. // CHECK:STDOUT: %int_5.loc18_33: Core.IntLiteral = int_value 5 [concrete = constants.%int_5.64b]
  79. // CHECK:STDOUT: %.loc18_34.1: %tuple.type = tuple_literal (%int_5.loc18_30, %int_5.loc18_33)
  80. // CHECK:STDOUT: %impl.elem0.loc18_34.1: %.a0b = impl_witness_access constants.%impl_witness.d39, element0 [concrete = constants.%Convert.956]
  81. // CHECK:STDOUT: %bound_method.loc18_34.1: <bound method> = bound_method %int_5.loc18_30, %impl.elem0.loc18_34.1 [concrete = constants.%Convert.bound]
  82. // CHECK:STDOUT: %specific_fn.loc18_34.1: <specific function> = specific_function %bound_method.loc18_34.1, @Convert.2(constants.%int_32) [concrete = constants.%Convert.specific_fn]
  83. // CHECK:STDOUT: %int.convert_checked.loc18_34.1: init %i32 = call %specific_fn.loc18_34.1(%int_5.loc18_30) [concrete = constants.%int_5.0f6]
  84. // CHECK:STDOUT: %.loc18_34.2: init %i32 = converted %int_5.loc18_30, %int.convert_checked.loc18_34.1 [concrete = constants.%int_5.0f6]
  85. // CHECK:STDOUT: %int_0.loc18: Core.IntLiteral = int_value 0 [concrete = constants.%int_0]
  86. // CHECK:STDOUT: %.loc18_34.3: ref %i32 = array_index %non_tuple.var, %int_0.loc18
  87. // CHECK:STDOUT: %.loc18_34.4: init %i32 = initialize_from %.loc18_34.2 to %.loc18_34.3 [concrete = constants.%int_5.0f6]
  88. // CHECK:STDOUT: %impl.elem0.loc18_34.2: %.a0b = impl_witness_access constants.%impl_witness.d39, element0 [concrete = constants.%Convert.956]
  89. // CHECK:STDOUT: %bound_method.loc18_34.2: <bound method> = bound_method %int_5.loc18_33, %impl.elem0.loc18_34.2 [concrete = constants.%Convert.bound]
  90. // CHECK:STDOUT: %specific_fn.loc18_34.2: <specific function> = specific_function %bound_method.loc18_34.2, @Convert.2(constants.%int_32) [concrete = constants.%Convert.specific_fn]
  91. // CHECK:STDOUT: %int.convert_checked.loc18_34.2: init %i32 = call %specific_fn.loc18_34.2(%int_5.loc18_33) [concrete = constants.%int_5.0f6]
  92. // CHECK:STDOUT: %.loc18_34.5: init %i32 = converted %int_5.loc18_33, %int.convert_checked.loc18_34.2 [concrete = constants.%int_5.0f6]
  93. // CHECK:STDOUT: %int_1.loc18: Core.IntLiteral = int_value 1 [concrete = constants.%int_1]
  94. // CHECK:STDOUT: %.loc18_34.6: ref %i32 = array_index %non_tuple.var, %int_1.loc18
  95. // CHECK:STDOUT: %.loc18_34.7: init %i32 = initialize_from %.loc18_34.5 to %.loc18_34.6 [concrete = constants.%int_5.0f6]
  96. // CHECK:STDOUT: %.loc18_34.8: init %array_type = array_init (%.loc18_34.4, %.loc18_34.7) to %non_tuple.var [concrete = constants.%array]
  97. // CHECK:STDOUT: %.loc18_3.2: init %array_type = converted %.loc18_34.1, %.loc18_34.8 [concrete = constants.%array]
  98. // CHECK:STDOUT: assign %non_tuple.var, %.loc18_3.2
  99. // CHECK:STDOUT: %.loc18_25: type = splice_block %array_type [concrete = constants.%array_type] {
  100. // CHECK:STDOUT: %int_32.loc18: Core.IntLiteral = int_value 32 [concrete = constants.%int_32]
  101. // CHECK:STDOUT: %i32.loc18: type = class_type @Int, @Int(constants.%int_32) [concrete = constants.%i32]
  102. // CHECK:STDOUT: %int_2: Core.IntLiteral = int_value 2 [concrete = constants.%int_2]
  103. // CHECK:STDOUT: %array_type: type = array_type %int_2, %i32 [concrete = constants.%array_type]
  104. // CHECK:STDOUT: }
  105. // CHECK:STDOUT: %non_tuple: ref %array_type = bind_name non_tuple, %non_tuple.var
  106. // CHECK:STDOUT: name_binding_decl {
  107. // CHECK:STDOUT: %first.patt: %i32 = binding_pattern first
  108. // CHECK:STDOUT: %.loc23_3: %i32 = var_pattern %first.patt
  109. // CHECK:STDOUT: }
  110. // CHECK:STDOUT: %first.var: ref %i32 = var first
  111. // CHECK:STDOUT: %non_tuple.ref: ref %array_type = name_ref non_tuple, %non_tuple
  112. // CHECK:STDOUT: %int_0.loc23: Core.IntLiteral = int_value 0 [concrete = constants.%int_0]
  113. // CHECK:STDOUT: assign %first.var, <error>
  114. // CHECK:STDOUT: %.loc23_14: type = splice_block %i32.loc23 [concrete = constants.%i32] {
  115. // CHECK:STDOUT: %int_32.loc23: Core.IntLiteral = int_value 32 [concrete = constants.%int_32]
  116. // CHECK:STDOUT: %i32.loc23: type = class_type @Int, @Int(constants.%int_32) [concrete = constants.%i32]
  117. // CHECK:STDOUT: }
  118. // CHECK:STDOUT: %first: ref %i32 = bind_name first, %first.var
  119. // CHECK:STDOUT: return
  120. // CHECK:STDOUT: }
  121. // CHECK:STDOUT: