fail_non_tuple_access.carbon 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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+3]]: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. var first: i32 = non_tuple.0;
  21. }
  22. // CHECK:STDOUT: --- fail_non_tuple_access.carbon
  23. // CHECK:STDOUT:
  24. // CHECK:STDOUT: constants {
  25. // CHECK:STDOUT: %Main.type: type = fn_type @Main [template]
  26. // CHECK:STDOUT: %Main: %Main.type = struct_value () [template]
  27. // CHECK:STDOUT: %.1: Core.IntLiteral = int_value 0 [template]
  28. // CHECK:STDOUT: %.2: Core.IntLiteral = int_value 1 [template]
  29. // CHECK:STDOUT: %.7: Core.IntLiteral = int_value 32 [template]
  30. // CHECK:STDOUT: %Int.type: type = fn_type @Int [template]
  31. // CHECK:STDOUT: %Int: %Int.type = struct_value () [template]
  32. // CHECK:STDOUT: %i32: type = int_type signed, %.7 [template]
  33. // CHECK:STDOUT: %.8: Core.IntLiteral = int_value 2 [template]
  34. // CHECK:STDOUT: %.9: type = array_type %.8, %i32 [template]
  35. // CHECK:STDOUT: %.11: Core.IntLiteral = int_value 5 [template]
  36. // CHECK:STDOUT: %tuple.type: type = tuple_type (Core.IntLiteral, Core.IntLiteral) [template]
  37. // CHECK:STDOUT: %Convert.type.2: type = fn_type @Convert.1, @ImplicitAs(%i32) [template]
  38. // CHECK:STDOUT: %Convert.type.14: type = fn_type @Convert.2, @impl.1(%.7) [template]
  39. // CHECK:STDOUT: %Convert.14: %Convert.type.14 = struct_value () [template]
  40. // CHECK:STDOUT: %.35: <witness> = interface_witness (%Convert.14) [template]
  41. // CHECK:STDOUT: %.36: <bound method> = bound_method %.11, %Convert.14 [template]
  42. // CHECK:STDOUT: %.37: <specific function> = specific_function %.36, @Convert.2(%.7) [template]
  43. // CHECK:STDOUT: %.38: %i32 = int_value 5 [template]
  44. // CHECK:STDOUT: %array: %.9 = tuple_value (%.38, %.38) [template]
  45. // CHECK:STDOUT: }
  46. // CHECK:STDOUT:
  47. // CHECK:STDOUT: imports {
  48. // CHECK:STDOUT: %Core: <namespace> = namespace file.%Core.import, [template] {
  49. // CHECK:STDOUT: .IndexWith = %import_ref.1
  50. // CHECK:STDOUT: .Int = %import_ref.6
  51. // CHECK:STDOUT: .ImplicitAs = %import_ref.7
  52. // CHECK:STDOUT: import Core//prelude
  53. // CHECK:STDOUT: import Core//prelude/...
  54. // CHECK:STDOUT: }
  55. // CHECK:STDOUT: }
  56. // CHECK:STDOUT:
  57. // CHECK:STDOUT: file {
  58. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  59. // CHECK:STDOUT: .Core = imports.%Core
  60. // CHECK:STDOUT: .Main = %Main.decl
  61. // CHECK:STDOUT: }
  62. // CHECK:STDOUT: %Core.import = import Core
  63. // CHECK:STDOUT: %Main.decl: %Main.type = fn_decl @Main [template = constants.%Main] {} {}
  64. // CHECK:STDOUT: }
  65. // CHECK:STDOUT:
  66. // CHECK:STDOUT: fn @Main() {
  67. // CHECK:STDOUT: !entry:
  68. // CHECK:STDOUT: %.loc16_3: Core.IntLiteral = int_value 0 [template = constants.%.1]
  69. // CHECK:STDOUT: %.loc16_5: Core.IntLiteral = int_value 1 [template = constants.%.2]
  70. // CHECK:STDOUT: %.loc18_19.1: Core.IntLiteral = int_value 32 [template = constants.%.7]
  71. // CHECK:STDOUT: %int.make_type_signed.loc18: init type = call constants.%Int(%.loc18_19.1) [template = constants.%i32]
  72. // CHECK:STDOUT: %.loc18_24: Core.IntLiteral = int_value 2 [template = constants.%.8]
  73. // CHECK:STDOUT: %.loc18_19.2: type = value_of_initializer %int.make_type_signed.loc18 [template = constants.%i32]
  74. // CHECK:STDOUT: %.loc18_19.3: type = converted %int.make_type_signed.loc18, %.loc18_19.2 [template = constants.%i32]
  75. // CHECK:STDOUT: %.loc18_25: type = array_type %.loc18_24, %i32 [template = constants.%.9]
  76. // CHECK:STDOUT: %non_tuple.var: ref %.9 = var non_tuple
  77. // CHECK:STDOUT: %non_tuple: ref %.9 = bind_name non_tuple, %non_tuple.var
  78. // CHECK:STDOUT: %.loc18_30: Core.IntLiteral = int_value 5 [template = constants.%.11]
  79. // CHECK:STDOUT: %.loc18_33: Core.IntLiteral = int_value 5 [template = constants.%.11]
  80. // CHECK:STDOUT: %.loc18_34.1: %tuple.type = tuple_literal (%.loc18_30, %.loc18_33)
  81. // CHECK:STDOUT: %.loc18_34.2: %Convert.type.2 = interface_witness_access constants.%.35, element0 [template = constants.%Convert.14]
  82. // CHECK:STDOUT: %.loc18_34.3: <bound method> = bound_method %.loc18_30, %.loc18_34.2 [template = constants.%.36]
  83. // CHECK:STDOUT: %.loc18_34.4: <specific function> = specific_function %.loc18_34.3, @Convert.2(constants.%.7) [template = constants.%.37]
  84. // CHECK:STDOUT: %int.convert_checked.loc18_34.1: init %i32 = call %.loc18_34.4(%.loc18_30) [template = constants.%.38]
  85. // CHECK:STDOUT: %.loc18_34.5: init %i32 = converted %.loc18_30, %int.convert_checked.loc18_34.1 [template = constants.%.38]
  86. // CHECK:STDOUT: %.loc18_34.6: Core.IntLiteral = int_value 0 [template = constants.%.1]
  87. // CHECK:STDOUT: %.loc18_34.7: ref %i32 = array_index %non_tuple.var, %.loc18_34.6
  88. // CHECK:STDOUT: %.loc18_34.8: init %i32 = initialize_from %.loc18_34.5 to %.loc18_34.7 [template = constants.%.38]
  89. // CHECK:STDOUT: %.loc18_34.9: %Convert.type.2 = interface_witness_access constants.%.35, element0 [template = constants.%Convert.14]
  90. // CHECK:STDOUT: %.loc18_34.10: <bound method> = bound_method %.loc18_33, %.loc18_34.9 [template = constants.%.36]
  91. // CHECK:STDOUT: %.loc18_34.11: <specific function> = specific_function %.loc18_34.10, @Convert.2(constants.%.7) [template = constants.%.37]
  92. // CHECK:STDOUT: %int.convert_checked.loc18_34.2: init %i32 = call %.loc18_34.11(%.loc18_33) [template = constants.%.38]
  93. // CHECK:STDOUT: %.loc18_34.12: init %i32 = converted %.loc18_33, %int.convert_checked.loc18_34.2 [template = constants.%.38]
  94. // CHECK:STDOUT: %.loc18_34.13: Core.IntLiteral = int_value 1 [template = constants.%.2]
  95. // CHECK:STDOUT: %.loc18_34.14: ref %i32 = array_index %non_tuple.var, %.loc18_34.13
  96. // CHECK:STDOUT: %.loc18_34.15: init %i32 = initialize_from %.loc18_34.12 to %.loc18_34.14 [template = constants.%.38]
  97. // CHECK:STDOUT: %.loc18_34.16: init %.9 = array_init (%.loc18_34.8, %.loc18_34.15) to %non_tuple.var [template = constants.%array]
  98. // CHECK:STDOUT: %.loc18_35: init %.9 = converted %.loc18_34.1, %.loc18_34.16 [template = constants.%array]
  99. // CHECK:STDOUT: assign %non_tuple.var, %.loc18_35
  100. // CHECK:STDOUT: %.loc22_14.1: Core.IntLiteral = int_value 32 [template = constants.%.7]
  101. // CHECK:STDOUT: %int.make_type_signed.loc22: init type = call constants.%Int(%.loc22_14.1) [template = constants.%i32]
  102. // CHECK:STDOUT: %.loc22_14.2: type = value_of_initializer %int.make_type_signed.loc22 [template = constants.%i32]
  103. // CHECK:STDOUT: %.loc22_14.3: type = converted %int.make_type_signed.loc22, %.loc22_14.2 [template = constants.%i32]
  104. // CHECK:STDOUT: %first.var: ref %i32 = var first
  105. // CHECK:STDOUT: %first: ref %i32 = bind_name first, %first.var
  106. // CHECK:STDOUT: %non_tuple.ref: ref %.9 = name_ref non_tuple, %non_tuple
  107. // CHECK:STDOUT: %.loc22_30: Core.IntLiteral = int_value 0 [template = constants.%.1]
  108. // CHECK:STDOUT: assign %first.var, <error>
  109. // CHECK:STDOUT: return
  110. // CHECK:STDOUT: }
  111. // CHECK:STDOUT: