fail_negative_indexing.carbon 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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: array(i32, 2) = (42, 42);
  11. // CHECK:STDERR: fail_negative_indexing.carbon:[[@LINE+4]]:16: error: array index `-10` is past the end of type `array(i32, 2)` [ArrayIndexOutOfBounds]
  12. // CHECK:STDERR: var d: i32 = c[-10];
  13. // CHECK:STDERR: ^~~
  14. // CHECK:STDERR:
  15. var d: i32 = c[-10];
  16. // CHECK:STDOUT: --- fail_negative_indexing.carbon
  17. // CHECK:STDOUT:
  18. // CHECK:STDOUT: constants {
  19. // CHECK:STDOUT: %int_32: Core.IntLiteral = int_value 32 [concrete]
  20. // CHECK:STDOUT: %Int.type: type = generic_class_type @Int [concrete]
  21. // CHECK:STDOUT: %Int.generic: %Int.type = struct_value () [concrete]
  22. // CHECK:STDOUT: %i32: type = class_type @Int, @Int(%int_32) [concrete]
  23. // CHECK:STDOUT: %int_2: Core.IntLiteral = int_value 2 [concrete]
  24. // CHECK:STDOUT: %array_type: type = array_type %int_2, %i32 [concrete]
  25. // CHECK:STDOUT: %pattern_type.ae7: type = pattern_type %array_type [concrete]
  26. // CHECK:STDOUT: %int_42.20e: Core.IntLiteral = int_value 42 [concrete]
  27. // CHECK:STDOUT: %tuple.type: type = tuple_type (Core.IntLiteral, Core.IntLiteral) [concrete]
  28. // CHECK:STDOUT: %int_0: Core.IntLiteral = int_value 0 [concrete]
  29. // CHECK:STDOUT: %ImplicitAs.type.cc7: type = generic_interface_type @ImplicitAs [concrete]
  30. // CHECK:STDOUT: %ImplicitAs.generic: %ImplicitAs.type.cc7 = struct_value () [concrete]
  31. // CHECK:STDOUT: %ImplicitAs.type.205: type = facet_type <@ImplicitAs, @ImplicitAs(%i32)> [concrete]
  32. // CHECK:STDOUT: %Convert.type.1b6: type = fn_type @Convert.1, @ImplicitAs(%i32) [concrete]
  33. // CHECK:STDOUT: %To.c80: Core.IntLiteral = bind_symbolic_name To, 0 [symbolic]
  34. // CHECK:STDOUT: %Convert.type.0f9: type = fn_type @Convert.2, @impl.4f9(%To.c80) [symbolic]
  35. // CHECK:STDOUT: %Convert.f06: %Convert.type.0f9 = struct_value () [symbolic]
  36. // CHECK:STDOUT: %Negate.type: type = facet_type <@Negate> [concrete]
  37. // CHECK:STDOUT: %ImplicitAs.impl_witness.c75: <witness> = impl_witness imports.%ImplicitAs.impl_witness_table.a2f, @impl.4f9(%int_32) [concrete]
  38. // CHECK:STDOUT: %Convert.type.035: type = fn_type @Convert.2, @impl.4f9(%int_32) [concrete]
  39. // CHECK:STDOUT: %Convert.956: %Convert.type.035 = struct_value () [concrete]
  40. // CHECK:STDOUT: %ImplicitAs.facet: %ImplicitAs.type.205 = facet_value Core.IntLiteral, (%ImplicitAs.impl_witness.c75) [concrete]
  41. // CHECK:STDOUT: %.9c3: type = fn_type_with_self_type %Convert.type.1b6, %ImplicitAs.facet [concrete]
  42. // CHECK:STDOUT: %Convert.bound.132: <bound method> = bound_method %int_42.20e, %Convert.956 [concrete]
  43. // CHECK:STDOUT: %pattern_type.7ce: type = pattern_type %i32 [concrete]
  44. // CHECK:STDOUT: %Convert.specific_fn: <specific function> = specific_function %Convert.956, @Convert.2(%int_32) [concrete]
  45. // CHECK:STDOUT: %bound_method.8c1: <bound method> = bound_method %int_42.20e, %Convert.specific_fn [concrete]
  46. // CHECK:STDOUT: %int_42.c68: %i32 = int_value 42 [concrete]
  47. // CHECK:STDOUT: %int_1: Core.IntLiteral = int_value 1 [concrete]
  48. // CHECK:STDOUT: %array: %array_type = tuple_value (%int_42.c68, %int_42.c68) [concrete]
  49. // CHECK:STDOUT: %int_10: Core.IntLiteral = int_value 10 [concrete]
  50. // CHECK:STDOUT: %Op.type.e42: type = fn_type @Op.23 [concrete]
  51. // CHECK:STDOUT: %Negate.impl_witness.561: <witness> = impl_witness imports.%Negate.impl_witness_table.e09 [concrete]
  52. // CHECK:STDOUT: %Negate.facet: %Negate.type = facet_value Core.IntLiteral, (%Negate.impl_witness.561) [concrete]
  53. // CHECK:STDOUT: %.63a: type = fn_type_with_self_type %Op.type.e42, %Negate.facet [concrete]
  54. // CHECK:STDOUT: %Op.type.1be: type = fn_type @Op.24 [concrete]
  55. // CHECK:STDOUT: %Op.bba: %Op.type.1be = struct_value () [concrete]
  56. // CHECK:STDOUT: %Op.bound: <bound method> = bound_method %int_10, %Op.bba [concrete]
  57. // CHECK:STDOUT: %int_-10.06d: Core.IntLiteral = int_value -10 [concrete]
  58. // CHECK:STDOUT: %Convert.bound.1ca: <bound method> = bound_method %int_-10.06d, %Convert.956 [concrete]
  59. // CHECK:STDOUT: %bound_method.af0: <bound method> = bound_method %int_-10.06d, %Convert.specific_fn [concrete]
  60. // CHECK:STDOUT: %int_-10.c17: %i32 = int_value -10 [concrete]
  61. // CHECK:STDOUT: }
  62. // CHECK:STDOUT:
  63. // CHECK:STDOUT: imports {
  64. // CHECK:STDOUT: %Core: <namespace> = namespace file.%Core.import, [concrete] {
  65. // CHECK:STDOUT: .Int = %Core.Int
  66. // CHECK:STDOUT: .ImplicitAs = %Core.ImplicitAs
  67. // CHECK:STDOUT: .Negate = %Core.Negate
  68. // CHECK:STDOUT: import Core//prelude
  69. // CHECK:STDOUT: import Core//prelude/...
  70. // CHECK:STDOUT: }
  71. // CHECK:STDOUT: %Core.Int: %Int.type = import_ref Core//prelude/types/int, Int, loaded [concrete = constants.%Int.generic]
  72. // CHECK:STDOUT: %Core.ImplicitAs: %ImplicitAs.type.cc7 = import_ref Core//prelude/operators/as, ImplicitAs, loaded [concrete = constants.%ImplicitAs.generic]
  73. // CHECK:STDOUT: %Core.import_ref.a5b: @impl.4f9.%Convert.type (%Convert.type.0f9) = import_ref Core//prelude/types/int, loc19_39, loaded [symbolic = @impl.4f9.%Convert (constants.%Convert.f06)]
  74. // CHECK:STDOUT: %ImplicitAs.impl_witness_table.a2f = impl_witness_table (%Core.import_ref.a5b), @impl.4f9 [concrete]
  75. // CHECK:STDOUT: %Core.Negate: type = import_ref Core//prelude/operators/arithmetic, Negate, loaded [concrete = constants.%Negate.type]
  76. // CHECK:STDOUT: %Core.import_ref.c15: %Op.type.1be = import_ref Core//prelude/operators/arithmetic, loc94_31, loaded [concrete = constants.%Op.bba]
  77. // CHECK:STDOUT: %Negate.impl_witness_table.e09 = impl_witness_table (%Core.import_ref.c15), @impl.8cb [concrete]
  78. // CHECK:STDOUT: }
  79. // CHECK:STDOUT:
  80. // CHECK:STDOUT: file {
  81. // CHECK:STDOUT: package: <namespace> = namespace [concrete] {
  82. // CHECK:STDOUT: .Core = imports.%Core
  83. // CHECK:STDOUT: .c = %c
  84. // CHECK:STDOUT: .d = %d
  85. // CHECK:STDOUT: }
  86. // CHECK:STDOUT: %Core.import = import Core
  87. // CHECK:STDOUT: name_binding_decl {
  88. // CHECK:STDOUT: %c.patt: %pattern_type.ae7 = binding_pattern c [concrete]
  89. // CHECK:STDOUT: %.loc11_1: %pattern_type.ae7 = var_pattern %c.patt [concrete]
  90. // CHECK:STDOUT: }
  91. // CHECK:STDOUT: %c.var: ref %array_type = var c
  92. // CHECK:STDOUT: %.loc11_20: type = splice_block %array_type [concrete = constants.%array_type] {
  93. // CHECK:STDOUT: %int_32.loc11: Core.IntLiteral = int_value 32 [concrete = constants.%int_32]
  94. // CHECK:STDOUT: %i32.loc11: type = class_type @Int, @Int(constants.%int_32) [concrete = constants.%i32]
  95. // CHECK:STDOUT: %int_2: Core.IntLiteral = int_value 2 [concrete = constants.%int_2]
  96. // CHECK:STDOUT: %array_type: type = array_type %int_2, %i32.loc11 [concrete = constants.%array_type]
  97. // CHECK:STDOUT: }
  98. // CHECK:STDOUT: %c: ref %array_type = bind_name c, %c.var
  99. // CHECK:STDOUT: name_binding_decl {
  100. // CHECK:STDOUT: %d.patt: %pattern_type.7ce = binding_pattern d [concrete]
  101. // CHECK:STDOUT: %.loc16_1: %pattern_type.7ce = var_pattern %d.patt [concrete]
  102. // CHECK:STDOUT: }
  103. // CHECK:STDOUT: %d.var: ref %i32 = var d
  104. // CHECK:STDOUT: %.loc16_8: type = splice_block %i32.loc16 [concrete = constants.%i32] {
  105. // CHECK:STDOUT: %int_32.loc16: Core.IntLiteral = int_value 32 [concrete = constants.%int_32]
  106. // CHECK:STDOUT: %i32.loc16: type = class_type @Int, @Int(constants.%int_32) [concrete = constants.%i32]
  107. // CHECK:STDOUT: }
  108. // CHECK:STDOUT: %d: ref %i32 = bind_name d, %d.var
  109. // CHECK:STDOUT: }
  110. // CHECK:STDOUT:
  111. // CHECK:STDOUT: fn @__global_init() {
  112. // CHECK:STDOUT: !entry:
  113. // CHECK:STDOUT: %int_42.loc11_25: Core.IntLiteral = int_value 42 [concrete = constants.%int_42.20e]
  114. // CHECK:STDOUT: %int_42.loc11_29: Core.IntLiteral = int_value 42 [concrete = constants.%int_42.20e]
  115. // CHECK:STDOUT: %.loc11_31.1: %tuple.type = tuple_literal (%int_42.loc11_25, %int_42.loc11_29)
  116. // CHECK:STDOUT: %impl.elem0.loc11_31.1: %.9c3 = impl_witness_access constants.%ImplicitAs.impl_witness.c75, element0 [concrete = constants.%Convert.956]
  117. // CHECK:STDOUT: %bound_method.loc11_31.1: <bound method> = bound_method %int_42.loc11_25, %impl.elem0.loc11_31.1 [concrete = constants.%Convert.bound.132]
  118. // CHECK:STDOUT: %specific_fn.loc11_31.1: <specific function> = specific_function %impl.elem0.loc11_31.1, @Convert.2(constants.%int_32) [concrete = constants.%Convert.specific_fn]
  119. // CHECK:STDOUT: %bound_method.loc11_31.2: <bound method> = bound_method %int_42.loc11_25, %specific_fn.loc11_31.1 [concrete = constants.%bound_method.8c1]
  120. // CHECK:STDOUT: %int.convert_checked.loc11_31.1: init %i32 = call %bound_method.loc11_31.2(%int_42.loc11_25) [concrete = constants.%int_42.c68]
  121. // CHECK:STDOUT: %.loc11_31.2: init %i32 = converted %int_42.loc11_25, %int.convert_checked.loc11_31.1 [concrete = constants.%int_42.c68]
  122. // CHECK:STDOUT: %int_0: Core.IntLiteral = int_value 0 [concrete = constants.%int_0]
  123. // CHECK:STDOUT: %.loc11_31.3: ref %i32 = array_index file.%c.var, %int_0
  124. // CHECK:STDOUT: %.loc11_31.4: init %i32 = initialize_from %.loc11_31.2 to %.loc11_31.3 [concrete = constants.%int_42.c68]
  125. // CHECK:STDOUT: %impl.elem0.loc11_31.2: %.9c3 = impl_witness_access constants.%ImplicitAs.impl_witness.c75, element0 [concrete = constants.%Convert.956]
  126. // CHECK:STDOUT: %bound_method.loc11_31.3: <bound method> = bound_method %int_42.loc11_29, %impl.elem0.loc11_31.2 [concrete = constants.%Convert.bound.132]
  127. // CHECK:STDOUT: %specific_fn.loc11_31.2: <specific function> = specific_function %impl.elem0.loc11_31.2, @Convert.2(constants.%int_32) [concrete = constants.%Convert.specific_fn]
  128. // CHECK:STDOUT: %bound_method.loc11_31.4: <bound method> = bound_method %int_42.loc11_29, %specific_fn.loc11_31.2 [concrete = constants.%bound_method.8c1]
  129. // CHECK:STDOUT: %int.convert_checked.loc11_31.2: init %i32 = call %bound_method.loc11_31.4(%int_42.loc11_29) [concrete = constants.%int_42.c68]
  130. // CHECK:STDOUT: %.loc11_31.5: init %i32 = converted %int_42.loc11_29, %int.convert_checked.loc11_31.2 [concrete = constants.%int_42.c68]
  131. // CHECK:STDOUT: %int_1: Core.IntLiteral = int_value 1 [concrete = constants.%int_1]
  132. // CHECK:STDOUT: %.loc11_31.6: ref %i32 = array_index file.%c.var, %int_1
  133. // CHECK:STDOUT: %.loc11_31.7: init %i32 = initialize_from %.loc11_31.5 to %.loc11_31.6 [concrete = constants.%int_42.c68]
  134. // CHECK:STDOUT: %.loc11_31.8: init %array_type = array_init (%.loc11_31.4, %.loc11_31.7) to file.%c.var [concrete = constants.%array]
  135. // CHECK:STDOUT: %.loc11_1: init %array_type = converted %.loc11_31.1, %.loc11_31.8 [concrete = constants.%array]
  136. // CHECK:STDOUT: assign file.%c.var, %.loc11_1
  137. // CHECK:STDOUT: %c.ref: ref %array_type = name_ref c, file.%c
  138. // CHECK:STDOUT: %int_10: Core.IntLiteral = int_value 10 [concrete = constants.%int_10]
  139. // CHECK:STDOUT: %impl.elem0.loc16_16.1: %.63a = impl_witness_access constants.%Negate.impl_witness.561, element0 [concrete = constants.%Op.bba]
  140. // CHECK:STDOUT: %bound_method.loc16_16.1: <bound method> = bound_method %int_10, %impl.elem0.loc16_16.1 [concrete = constants.%Op.bound]
  141. // CHECK:STDOUT: %int.snegate: init Core.IntLiteral = call %bound_method.loc16_16.1(%int_10) [concrete = constants.%int_-10.06d]
  142. // CHECK:STDOUT: %int_32: Core.IntLiteral = int_value 32 [concrete = constants.%int_32]
  143. // CHECK:STDOUT: %i32: type = class_type @Int, @Int(constants.%int_32) [concrete = constants.%i32]
  144. // CHECK:STDOUT: %impl.elem0.loc16_16.2: %.9c3 = impl_witness_access constants.%ImplicitAs.impl_witness.c75, element0 [concrete = constants.%Convert.956]
  145. // CHECK:STDOUT: %bound_method.loc16_16.2: <bound method> = bound_method %int.snegate, %impl.elem0.loc16_16.2 [concrete = constants.%Convert.bound.1ca]
  146. // CHECK:STDOUT: %specific_fn.loc16: <specific function> = specific_function %impl.elem0.loc16_16.2, @Convert.2(constants.%int_32) [concrete = constants.%Convert.specific_fn]
  147. // CHECK:STDOUT: %bound_method.loc16_16.3: <bound method> = bound_method %int.snegate, %specific_fn.loc16 [concrete = constants.%bound_method.af0]
  148. // CHECK:STDOUT: %.loc16_16.1: Core.IntLiteral = value_of_initializer %int.snegate [concrete = constants.%int_-10.06d]
  149. // CHECK:STDOUT: %.loc16_16.2: Core.IntLiteral = converted %int.snegate, %.loc16_16.1 [concrete = constants.%int_-10.06d]
  150. // CHECK:STDOUT: %int.convert_checked.loc16: init %i32 = call %bound_method.loc16_16.3(%.loc16_16.2) [concrete = constants.%int_-10.c17]
  151. // CHECK:STDOUT: %.loc16_16.3: %i32 = value_of_initializer %int.convert_checked.loc16 [concrete = constants.%int_-10.c17]
  152. // CHECK:STDOUT: %.loc16_16.4: %i32 = converted %int.snegate, %.loc16_16.3 [concrete = constants.%int_-10.c17]
  153. // CHECK:STDOUT: %.loc16_19.1: ref %i32 = array_index %c.ref, %.loc16_16.4 [concrete = <error>]
  154. // CHECK:STDOUT: %.loc16_19.2: %i32 = bind_value %.loc16_19.1 [concrete = <error>]
  155. // CHECK:STDOUT: assign file.%d.var, %.loc16_19.2
  156. // CHECK:STDOUT: return
  157. // CHECK:STDOUT: }
  158. // CHECK:STDOUT: