fail_non_tuple_access.carbon 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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: %int_0: Core.IntLiteral = int_value 0 [template]
  28. // CHECK:STDOUT: %int_1: Core.IntLiteral = int_value 1 [template]
  29. // CHECK:STDOUT: %int_32: Core.IntLiteral = int_value 32 [template]
  30. // CHECK:STDOUT: %i32: type = class_type @Int, @Int(%int_32) [template]
  31. // CHECK:STDOUT: %int_2: Core.IntLiteral = int_value 2 [template]
  32. // CHECK:STDOUT: %array_type: type = array_type %int_2, %i32 [template]
  33. // CHECK:STDOUT: %int_5.64b: Core.IntLiteral = int_value 5 [template]
  34. // CHECK:STDOUT: %tuple.type: type = tuple_type (Core.IntLiteral, Core.IntLiteral) [template]
  35. // CHECK:STDOUT: %Convert.type.cd1: type = fn_type @Convert.1, @ImplicitAs(%i32) [template]
  36. // CHECK:STDOUT: %impl_witness.5b0: <witness> = impl_witness (imports.%import_ref.723), @impl.1(%int_32) [template]
  37. // CHECK:STDOUT: %Convert.type.466: type = fn_type @Convert.2, @impl.1(%int_32) [template]
  38. // CHECK:STDOUT: %Convert.925: %Convert.type.466 = struct_value () [template]
  39. // CHECK:STDOUT: %Convert.bound: <bound method> = bound_method %int_5.64b, %Convert.925 [template]
  40. // CHECK:STDOUT: %Convert.specific_fn: <specific function> = specific_function %Convert.bound, @Convert.2(%int_32) [template]
  41. // CHECK:STDOUT: %int_5.70b: %i32 = int_value 5 [template]
  42. // CHECK:STDOUT: %array: %array_type = tuple_value (%int_5.70b, %int_5.70b) [template]
  43. // CHECK:STDOUT: }
  44. // CHECK:STDOUT:
  45. // CHECK:STDOUT: imports {
  46. // CHECK:STDOUT: %Core: <namespace> = namespace file.%Core.import, [template] {
  47. // CHECK:STDOUT: .IndexWith = %import_ref.76d
  48. // CHECK:STDOUT: .Int = %import_ref.187
  49. // CHECK:STDOUT: .ImplicitAs = %import_ref.a69
  50. // CHECK:STDOUT: import Core//prelude
  51. // CHECK:STDOUT: import Core//prelude/...
  52. // CHECK:STDOUT: }
  53. // CHECK:STDOUT: }
  54. // CHECK:STDOUT:
  55. // CHECK:STDOUT: file {
  56. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  57. // CHECK:STDOUT: .Core = imports.%Core
  58. // CHECK:STDOUT: .Main = %Main.decl
  59. // CHECK:STDOUT: }
  60. // CHECK:STDOUT: %Core.import = import Core
  61. // CHECK:STDOUT: %Main.decl: %Main.type = fn_decl @Main [template = constants.%Main] {} {}
  62. // CHECK:STDOUT: }
  63. // CHECK:STDOUT:
  64. // CHECK:STDOUT: fn @Main() {
  65. // CHECK:STDOUT: !entry:
  66. // CHECK:STDOUT: %int_0.loc16: Core.IntLiteral = int_value 0 [template = constants.%int_0]
  67. // CHECK:STDOUT: %int_1.loc16: Core.IntLiteral = int_value 1 [template = constants.%int_1]
  68. // CHECK:STDOUT: %non_tuple.var: ref %array_type = var non_tuple
  69. // CHECK:STDOUT: %non_tuple: ref %array_type = bind_name non_tuple, %non_tuple.var
  70. // CHECK:STDOUT: %int_5.loc18_30: Core.IntLiteral = int_value 5 [template = constants.%int_5.64b]
  71. // CHECK:STDOUT: %int_5.loc18_33: Core.IntLiteral = int_value 5 [template = constants.%int_5.64b]
  72. // CHECK:STDOUT: %.loc18_34.1: %tuple.type = tuple_literal (%int_5.loc18_30, %int_5.loc18_33)
  73. // CHECK:STDOUT: %impl.elem0.loc18_34.1: %Convert.type.cd1 = impl_witness_access constants.%impl_witness.5b0, element0 [template = constants.%Convert.925]
  74. // CHECK:STDOUT: %Convert.bound.loc18_34.1: <bound method> = bound_method %int_5.loc18_30, %impl.elem0.loc18_34.1 [template = constants.%Convert.bound]
  75. // CHECK:STDOUT: %Convert.specific_fn.loc18_34.1: <specific function> = specific_function %Convert.bound.loc18_34.1, @Convert.2(constants.%int_32) [template = constants.%Convert.specific_fn]
  76. // CHECK:STDOUT: %int.convert_checked.loc18_34.1: init %i32 = call %Convert.specific_fn.loc18_34.1(%int_5.loc18_30) [template = constants.%int_5.70b]
  77. // CHECK:STDOUT: %.loc18_34.2: init %i32 = converted %int_5.loc18_30, %int.convert_checked.loc18_34.1 [template = constants.%int_5.70b]
  78. // CHECK:STDOUT: %int_0.loc18: Core.IntLiteral = int_value 0 [template = constants.%int_0]
  79. // CHECK:STDOUT: %.loc18_34.3: ref %i32 = array_index %non_tuple.var, %int_0.loc18
  80. // CHECK:STDOUT: %.loc18_34.4: init %i32 = initialize_from %.loc18_34.2 to %.loc18_34.3 [template = constants.%int_5.70b]
  81. // CHECK:STDOUT: %impl.elem0.loc18_34.2: %Convert.type.cd1 = impl_witness_access constants.%impl_witness.5b0, element0 [template = constants.%Convert.925]
  82. // CHECK:STDOUT: %Convert.bound.loc18_34.2: <bound method> = bound_method %int_5.loc18_33, %impl.elem0.loc18_34.2 [template = constants.%Convert.bound]
  83. // CHECK:STDOUT: %Convert.specific_fn.loc18_34.2: <specific function> = specific_function %Convert.bound.loc18_34.2, @Convert.2(constants.%int_32) [template = constants.%Convert.specific_fn]
  84. // CHECK:STDOUT: %int.convert_checked.loc18_34.2: init %i32 = call %Convert.specific_fn.loc18_34.2(%int_5.loc18_33) [template = constants.%int_5.70b]
  85. // CHECK:STDOUT: %.loc18_34.5: init %i32 = converted %int_5.loc18_33, %int.convert_checked.loc18_34.2 [template = constants.%int_5.70b]
  86. // CHECK:STDOUT: %int_1.loc18: Core.IntLiteral = int_value 1 [template = constants.%int_1]
  87. // CHECK:STDOUT: %.loc18_34.6: ref %i32 = array_index %non_tuple.var, %int_1.loc18
  88. // CHECK:STDOUT: %.loc18_34.7: init %i32 = initialize_from %.loc18_34.5 to %.loc18_34.6 [template = constants.%int_5.70b]
  89. // CHECK:STDOUT: %.loc18_34.8: init %array_type = array_init (%.loc18_34.4, %.loc18_34.7) to %non_tuple.var [template = constants.%array]
  90. // CHECK:STDOUT: %.loc18_35: init %array_type = converted %.loc18_34.1, %.loc18_34.8 [template = constants.%array]
  91. // CHECK:STDOUT: assign %non_tuple.var, %.loc18_35
  92. // CHECK:STDOUT: %first.var: ref %i32 = var first
  93. // CHECK:STDOUT: %first: ref %i32 = bind_name first, %first.var
  94. // CHECK:STDOUT: %non_tuple.ref: ref %array_type = name_ref non_tuple, %non_tuple
  95. // CHECK:STDOUT: %int_0.loc22: Core.IntLiteral = int_value 0 [template = constants.%int_0]
  96. // CHECK:STDOUT: assign %first.var, <error>
  97. // CHECK:STDOUT: return
  98. // CHECK:STDOUT: }
  99. // CHECK:STDOUT: