fail_negative_indexing.carbon 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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/index/fail_negative_indexing.carbon
  8. // TIP: To dump output, run:
  9. // TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/index/fail_negative_indexing.carbon
  10. var c: [i32; 2] = (42, 42);
  11. // CHECK:STDERR: fail_negative_indexing.carbon:[[@LINE+3]]:16: error: array index `-10` is past the end of type `[i32; 2]` [ArrayIndexOutOfBounds]
  12. // CHECK:STDERR: var d: i32 = c[-10];
  13. // CHECK:STDERR: ^~~
  14. var d: i32 = c[-10];
  15. // CHECK:STDOUT: --- fail_negative_indexing.carbon
  16. // CHECK:STDOUT:
  17. // CHECK:STDOUT: constants {
  18. // CHECK:STDOUT: %int_32: Core.IntLiteral = int_value 32 [template]
  19. // CHECK:STDOUT: %i32: type = class_type @Int, @Int(%int_32) [template]
  20. // CHECK:STDOUT: %int_2: Core.IntLiteral = int_value 2 [template]
  21. // CHECK:STDOUT: %array_type: type = array_type %int_2, %i32 [template]
  22. // CHECK:STDOUT: %int_42.1: Core.IntLiteral = int_value 42 [template]
  23. // CHECK:STDOUT: %tuple.type: type = tuple_type (Core.IntLiteral, Core.IntLiteral) [template]
  24. // CHECK:STDOUT: %int_0: Core.IntLiteral = int_value 0 [template]
  25. // CHECK:STDOUT: %Convert.type.2: type = fn_type @Convert.1, @ImplicitAs(%i32) [template]
  26. // CHECK:STDOUT: %Convert.type.10: type = fn_type @Convert.2, @impl.1(%int_32) [template]
  27. // CHECK:STDOUT: %Convert.10: %Convert.type.10 = struct_value () [template]
  28. // CHECK:STDOUT: %interface.19: <witness> = interface_witness (%Convert.10) [template]
  29. // CHECK:STDOUT: %Convert.bound.1: <bound method> = bound_method %int_42.1, %Convert.10 [template]
  30. // CHECK:STDOUT: %Convert.specific_fn.1: <specific function> = specific_function %Convert.bound.1, @Convert.2(%int_32) [template]
  31. // CHECK:STDOUT: %int_42.2: %i32 = int_value 42 [template]
  32. // CHECK:STDOUT: %int_1: Core.IntLiteral = int_value 1 [template]
  33. // CHECK:STDOUT: %array: %array_type = tuple_value (%int_42.2, %int_42.2) [template]
  34. // CHECK:STDOUT: %int_10: Core.IntLiteral = int_value 10 [template]
  35. // CHECK:STDOUT: %Op.type.13: type = fn_type @Op.13 [template]
  36. // CHECK:STDOUT: %Op.type.14: type = fn_type @Op.14 [template]
  37. // CHECK:STDOUT: %Op.14: %Op.type.14 = struct_value () [template]
  38. // CHECK:STDOUT: %interface.20: <witness> = interface_witness (%Op.14) [template]
  39. // CHECK:STDOUT: %Op.bound: <bound method> = bound_method %int_10, %Op.14 [template]
  40. // CHECK:STDOUT: %int_-10.1: Core.IntLiteral = int_value -10 [template]
  41. // CHECK:STDOUT: %Convert.bound.2: <bound method> = bound_method %int_-10.1, %Convert.10 [template]
  42. // CHECK:STDOUT: %Convert.specific_fn.2: <specific function> = specific_function %Convert.bound.2, @Convert.2(%int_32) [template]
  43. // CHECK:STDOUT: %int_-10.2: %i32 = int_value -10 [template]
  44. // CHECK:STDOUT: }
  45. // CHECK:STDOUT:
  46. // CHECK:STDOUT: imports {
  47. // CHECK:STDOUT: %Core: <namespace> = namespace file.%Core.import, [template] {
  48. // CHECK:STDOUT: .Int = %import_ref.1
  49. // CHECK:STDOUT: .ImplicitAs = %import_ref.5
  50. // CHECK:STDOUT: .Negate = %import_ref.229
  51. // CHECK:STDOUT: import Core//prelude
  52. // CHECK:STDOUT: import Core//prelude/...
  53. // CHECK:STDOUT: }
  54. // CHECK:STDOUT: }
  55. // CHECK:STDOUT:
  56. // CHECK:STDOUT: file {
  57. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  58. // CHECK:STDOUT: .Core = imports.%Core
  59. // CHECK:STDOUT: .c = %c
  60. // CHECK:STDOUT: .d = %d
  61. // CHECK:STDOUT: }
  62. // CHECK:STDOUT: %Core.import = import Core
  63. // CHECK:STDOUT: %c.var: ref %array_type = var c
  64. // CHECK:STDOUT: %c: ref %array_type = bind_name c, %c.var
  65. // CHECK:STDOUT: %d.var: ref %i32 = var d
  66. // CHECK:STDOUT: %d: ref %i32 = bind_name d, %d.var
  67. // CHECK:STDOUT: }
  68. // CHECK:STDOUT:
  69. // CHECK:STDOUT: fn @__global_init() {
  70. // CHECK:STDOUT: !entry:
  71. // CHECK:STDOUT: %int_42.loc11_20: Core.IntLiteral = int_value 42 [template = constants.%int_42.1]
  72. // CHECK:STDOUT: %int_42.loc11_24: Core.IntLiteral = int_value 42 [template = constants.%int_42.1]
  73. // CHECK:STDOUT: %.loc11_26.1: %tuple.type = tuple_literal (%int_42.loc11_20, %int_42.loc11_24)
  74. // CHECK:STDOUT: %impl.elem0.loc11_26.1: %Convert.type.2 = interface_witness_access constants.%interface.19, element0 [template = constants.%Convert.10]
  75. // CHECK:STDOUT: %Convert.bound.loc11_26.1: <bound method> = bound_method %int_42.loc11_20, %impl.elem0.loc11_26.1 [template = constants.%Convert.bound.1]
  76. // CHECK:STDOUT: %Convert.specific_fn.loc11_26.1: <specific function> = specific_function %Convert.bound.loc11_26.1, @Convert.2(constants.%int_32) [template = constants.%Convert.specific_fn.1]
  77. // CHECK:STDOUT: %int.convert_checked.loc11_26.1: init %i32 = call %Convert.specific_fn.loc11_26.1(%int_42.loc11_20) [template = constants.%int_42.2]
  78. // CHECK:STDOUT: %.loc11_26.2: init %i32 = converted %int_42.loc11_20, %int.convert_checked.loc11_26.1 [template = constants.%int_42.2]
  79. // CHECK:STDOUT: %int_0: Core.IntLiteral = int_value 0 [template = constants.%int_0]
  80. // CHECK:STDOUT: %.loc11_26.3: ref %i32 = array_index file.%c.var, %int_0
  81. // CHECK:STDOUT: %.loc11_26.4: init %i32 = initialize_from %.loc11_26.2 to %.loc11_26.3 [template = constants.%int_42.2]
  82. // CHECK:STDOUT: %impl.elem0.loc11_26.2: %Convert.type.2 = interface_witness_access constants.%interface.19, element0 [template = constants.%Convert.10]
  83. // CHECK:STDOUT: %Convert.bound.loc11_26.2: <bound method> = bound_method %int_42.loc11_24, %impl.elem0.loc11_26.2 [template = constants.%Convert.bound.1]
  84. // CHECK:STDOUT: %Convert.specific_fn.loc11_26.2: <specific function> = specific_function %Convert.bound.loc11_26.2, @Convert.2(constants.%int_32) [template = constants.%Convert.specific_fn.1]
  85. // CHECK:STDOUT: %int.convert_checked.loc11_26.2: init %i32 = call %Convert.specific_fn.loc11_26.2(%int_42.loc11_24) [template = constants.%int_42.2]
  86. // CHECK:STDOUT: %.loc11_26.5: init %i32 = converted %int_42.loc11_24, %int.convert_checked.loc11_26.2 [template = constants.%int_42.2]
  87. // CHECK:STDOUT: %int_1: Core.IntLiteral = int_value 1 [template = constants.%int_1]
  88. // CHECK:STDOUT: %.loc11_26.6: ref %i32 = array_index file.%c.var, %int_1
  89. // CHECK:STDOUT: %.loc11_26.7: init %i32 = initialize_from %.loc11_26.5 to %.loc11_26.6 [template = constants.%int_42.2]
  90. // CHECK:STDOUT: %.loc11_26.8: init %array_type = array_init (%.loc11_26.4, %.loc11_26.7) to file.%c.var [template = constants.%array]
  91. // CHECK:STDOUT: %.loc11_27: init %array_type = converted %.loc11_26.1, %.loc11_26.8 [template = constants.%array]
  92. // CHECK:STDOUT: assign file.%c.var, %.loc11_27
  93. // CHECK:STDOUT: %c.ref: ref %array_type = name_ref c, file.%c
  94. // CHECK:STDOUT: %int_10: Core.IntLiteral = int_value 10 [template = constants.%int_10]
  95. // CHECK:STDOUT: %impl.elem0.loc15_16.1: %Op.type.13 = interface_witness_access constants.%interface.20, element0 [template = constants.%Op.14]
  96. // CHECK:STDOUT: %Op.bound: <bound method> = bound_method %int_10, %impl.elem0.loc15_16.1 [template = constants.%Op.bound]
  97. // CHECK:STDOUT: %int.snegate: init Core.IntLiteral = call %Op.bound(%int_10) [template = constants.%int_-10.1]
  98. // CHECK:STDOUT: %int_32: Core.IntLiteral = int_value 32 [template = constants.%int_32]
  99. // CHECK:STDOUT: %i32: type = class_type @Int, @Int(constants.%int_32) [template = constants.%i32]
  100. // CHECK:STDOUT: %impl.elem0.loc15_16.2: %Convert.type.2 = interface_witness_access constants.%interface.19, element0 [template = constants.%Convert.10]
  101. // CHECK:STDOUT: %Convert.bound.loc15: <bound method> = bound_method %int.snegate, %impl.elem0.loc15_16.2 [template = constants.%Convert.bound.2]
  102. // CHECK:STDOUT: %Convert.specific_fn.loc15: <specific function> = specific_function %Convert.bound.loc15, @Convert.2(constants.%int_32) [template = constants.%Convert.specific_fn.2]
  103. // CHECK:STDOUT: %.loc15_16.1: Core.IntLiteral = value_of_initializer %int.snegate [template = constants.%int_-10.1]
  104. // CHECK:STDOUT: %.loc15_16.2: Core.IntLiteral = converted %int.snegate, %.loc15_16.1 [template = constants.%int_-10.1]
  105. // CHECK:STDOUT: %int.convert_checked.loc15: init %i32 = call %Convert.specific_fn.loc15(%.loc15_16.2) [template = constants.%int_-10.2]
  106. // CHECK:STDOUT: %.loc15_16.3: %i32 = value_of_initializer %int.convert_checked.loc15 [template = constants.%int_-10.2]
  107. // CHECK:STDOUT: %.loc15_16.4: %i32 = converted %int.snegate, %.loc15_16.3 [template = constants.%int_-10.2]
  108. // CHECK:STDOUT: %.loc15_19.1: ref %i32 = array_index %c.ref, %.loc15_16.4 [template = <error>]
  109. // CHECK:STDOUT: %.loc15_19.2: %i32 = bind_value %.loc15_19.1
  110. // CHECK:STDOUT: assign file.%d.var, %.loc15_19.2
  111. // CHECK:STDOUT: return
  112. // CHECK:STDOUT: }
  113. // CHECK:STDOUT: