fail_non_tuple_access.carbon 5.7 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 `i32` does not support indexing
  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
  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: %.1: type = tuple_type () [template]
  27. // CHECK:STDOUT: %Main: %Main.type = struct_value () [template]
  28. // CHECK:STDOUT: %.2: i32 = int_literal 0 [template]
  29. // CHECK:STDOUT: %.3: i32 = int_literal 1 [template]
  30. // CHECK:STDOUT: %Int32.type: type = fn_type @Int32 [template]
  31. // CHECK:STDOUT: %Int32: %Int32.type = struct_value () [template]
  32. // CHECK:STDOUT: %.4: i32 = int_literal 2 [template]
  33. // CHECK:STDOUT: %.5: type = array_type %.4, i32 [template]
  34. // CHECK:STDOUT: %.6: type = ptr_type %.5 [template]
  35. // CHECK:STDOUT: %.7: i32 = int_literal 5 [template]
  36. // CHECK:STDOUT: %.8: type = tuple_type (i32, i32) [template]
  37. // CHECK:STDOUT: %array: %.5 = tuple_value (%.7, %.7) [template]
  38. // CHECK:STDOUT: }
  39. // CHECK:STDOUT:
  40. // CHECK:STDOUT: imports {
  41. // CHECK:STDOUT: %Core: <namespace> = namespace file.%Core.import, [template] {
  42. // CHECK:STDOUT: .Int32 = %import_ref
  43. // CHECK:STDOUT: import Core//prelude
  44. // CHECK:STDOUT: import Core//prelude/operators
  45. // CHECK:STDOUT: import Core//prelude/types
  46. // CHECK:STDOUT: import Core//prelude/operators/arithmetic
  47. // CHECK:STDOUT: import Core//prelude/operators/as
  48. // CHECK:STDOUT: import Core//prelude/operators/bitwise
  49. // CHECK:STDOUT: import Core//prelude/operators/comparison
  50. // CHECK:STDOUT: import Core//prelude/types/bool
  51. // CHECK:STDOUT: }
  52. // CHECK:STDOUT: %import_ref: %Int32.type = import_ref Core//prelude/types, inst+9, loaded [template = constants.%Int32]
  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: %.loc16_3: i32 = int_literal 0 [template = constants.%.2]
  67. // CHECK:STDOUT: %.loc16_5: i32 = int_literal 1 [template = constants.%.3]
  68. // CHECK:STDOUT: %int.make_type_32.loc18: init type = call constants.%Int32() [template = i32]
  69. // CHECK:STDOUT: %.loc18_24: i32 = int_literal 2 [template = constants.%.4]
  70. // CHECK:STDOUT: %.loc18_19.1: type = value_of_initializer %int.make_type_32.loc18 [template = i32]
  71. // CHECK:STDOUT: %.loc18_19.2: type = converted %int.make_type_32.loc18, %.loc18_19.1 [template = i32]
  72. // CHECK:STDOUT: %.loc18_25: type = array_type %.loc18_24, i32 [template = constants.%.5]
  73. // CHECK:STDOUT: %non_tuple.var: ref %.5 = var non_tuple
  74. // CHECK:STDOUT: %non_tuple: ref %.5 = bind_name non_tuple, %non_tuple.var
  75. // CHECK:STDOUT: %.loc18_30: i32 = int_literal 5 [template = constants.%.7]
  76. // CHECK:STDOUT: %.loc18_33: i32 = int_literal 5 [template = constants.%.7]
  77. // CHECK:STDOUT: %.loc18_34.1: %.8 = tuple_literal (%.loc18_30, %.loc18_33)
  78. // CHECK:STDOUT: %.loc18_34.2: i32 = int_literal 0 [template = constants.%.2]
  79. // CHECK:STDOUT: %.loc18_34.3: ref i32 = array_index %non_tuple.var, %.loc18_34.2
  80. // CHECK:STDOUT: %.loc18_34.4: init i32 = initialize_from %.loc18_30 to %.loc18_34.3 [template = constants.%.7]
  81. // CHECK:STDOUT: %.loc18_34.5: i32 = int_literal 1 [template = constants.%.3]
  82. // CHECK:STDOUT: %.loc18_34.6: ref i32 = array_index %non_tuple.var, %.loc18_34.5
  83. // CHECK:STDOUT: %.loc18_34.7: init i32 = initialize_from %.loc18_33 to %.loc18_34.6 [template = constants.%.7]
  84. // CHECK:STDOUT: %.loc18_34.8: init %.5 = array_init (%.loc18_34.4, %.loc18_34.7) to %non_tuple.var [template = constants.%array]
  85. // CHECK:STDOUT: %.loc18_35: init %.5 = converted %.loc18_34.1, %.loc18_34.8 [template = constants.%array]
  86. // CHECK:STDOUT: assign %non_tuple.var, %.loc18_35
  87. // CHECK:STDOUT: %int.make_type_32.loc22: init type = call constants.%Int32() [template = i32]
  88. // CHECK:STDOUT: %.loc22_14.1: type = value_of_initializer %int.make_type_32.loc22 [template = i32]
  89. // CHECK:STDOUT: %.loc22_14.2: type = converted %int.make_type_32.loc22, %.loc22_14.1 [template = i32]
  90. // CHECK:STDOUT: %first.var: ref i32 = var first
  91. // CHECK:STDOUT: %first: ref i32 = bind_name first, %first.var
  92. // CHECK:STDOUT: %non_tuple.ref: ref %.5 = name_ref non_tuple, %non_tuple
  93. // CHECK:STDOUT: %.loc22_30: i32 = int_literal 0 [template = constants.%.2]
  94. // CHECK:STDOUT: assign %first.var, <error>
  95. // CHECK:STDOUT: return
  96. // CHECK:STDOUT: }
  97. // CHECK:STDOUT:
  98. // CHECK:STDOUT: fn @Int32() -> type = "int.make_type_32";
  99. // CHECK:STDOUT: