fail_array_out_of_bound_access.carbon 6.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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_array_out_of_bound_access.carbon
  8. // TIP: To dump output, run:
  9. // TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/index/fail_array_out_of_bound_access.carbon
  10. var a: [i32; 1] = (12,);
  11. // CHECK:STDERR: fail_array_out_of_bound_access.carbon:[[@LINE+3]]:16: error: array index `1` is past the end of type `[i32; 1]` [ArrayIndexOutOfBounds]
  12. // CHECK:STDERR: var b: i32 = a[1];
  13. // CHECK:STDERR: ^
  14. var b: i32 = a[1];
  15. // CHECK:STDOUT: --- fail_array_out_of_bound_access.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_1.1: Core.IntLiteral = int_value 1 [template]
  21. // CHECK:STDOUT: %array_type: type = array_type %int_1.1, %i32 [template]
  22. // CHECK:STDOUT: %int_12.1: Core.IntLiteral = int_value 12 [template]
  23. // CHECK:STDOUT: %tuple.type: type = tuple_type (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.5: <witness> = interface_witness (%Convert.10) [template]
  29. // CHECK:STDOUT: %Convert.bound.1: <bound method> = bound_method %int_12.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_12.2: %i32 = int_value 12 [template]
  32. // CHECK:STDOUT: %array: %array_type = tuple_value (%int_12.2) [template]
  33. // CHECK:STDOUT: %Convert.bound.2: <bound method> = bound_method %int_1.1, %Convert.10 [template]
  34. // CHECK:STDOUT: %Convert.specific_fn.2: <specific function> = specific_function %Convert.bound.2, @Convert.2(%int_32) [template]
  35. // CHECK:STDOUT: %int_1.2: %i32 = int_value 1 [template]
  36. // CHECK:STDOUT: }
  37. // CHECK:STDOUT:
  38. // CHECK:STDOUT: imports {
  39. // CHECK:STDOUT: %Core: <namespace> = namespace file.%Core.import, [template] {
  40. // CHECK:STDOUT: .Int = %import_ref.1
  41. // CHECK:STDOUT: .ImplicitAs = %import_ref.5
  42. // CHECK:STDOUT: import Core//prelude
  43. // CHECK:STDOUT: import Core//prelude/...
  44. // CHECK:STDOUT: }
  45. // CHECK:STDOUT: }
  46. // CHECK:STDOUT:
  47. // CHECK:STDOUT: file {
  48. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  49. // CHECK:STDOUT: .Core = imports.%Core
  50. // CHECK:STDOUT: .a = %a
  51. // CHECK:STDOUT: .b = %b
  52. // CHECK:STDOUT: }
  53. // CHECK:STDOUT: %Core.import = import Core
  54. // CHECK:STDOUT: %a.var: ref %array_type = var a
  55. // CHECK:STDOUT: %a: ref %array_type = bind_name a, %a.var
  56. // CHECK:STDOUT: %b.var: ref %i32 = var b
  57. // CHECK:STDOUT: %b: ref %i32 = bind_name b, %b.var
  58. // CHECK:STDOUT: }
  59. // CHECK:STDOUT:
  60. // CHECK:STDOUT: fn @__global_init() {
  61. // CHECK:STDOUT: !entry:
  62. // CHECK:STDOUT: %int_12: Core.IntLiteral = int_value 12 [template = constants.%int_12.1]
  63. // CHECK:STDOUT: %.loc11_23.1: %tuple.type = tuple_literal (%int_12)
  64. // CHECK:STDOUT: %impl.elem0.loc11: %Convert.type.2 = interface_witness_access constants.%interface.5, element0 [template = constants.%Convert.10]
  65. // CHECK:STDOUT: %Convert.bound.loc11: <bound method> = bound_method %int_12, %impl.elem0.loc11 [template = constants.%Convert.bound.1]
  66. // CHECK:STDOUT: %Convert.specific_fn.loc11: <specific function> = specific_function %Convert.bound.loc11, @Convert.2(constants.%int_32) [template = constants.%Convert.specific_fn.1]
  67. // CHECK:STDOUT: %int.convert_checked.loc11: init %i32 = call %Convert.specific_fn.loc11(%int_12) [template = constants.%int_12.2]
  68. // CHECK:STDOUT: %.loc11_23.2: init %i32 = converted %int_12, %int.convert_checked.loc11 [template = constants.%int_12.2]
  69. // CHECK:STDOUT: %int_0: Core.IntLiteral = int_value 0 [template = constants.%int_0]
  70. // CHECK:STDOUT: %.loc11_23.3: ref %i32 = array_index file.%a.var, %int_0
  71. // CHECK:STDOUT: %.loc11_23.4: init %i32 = initialize_from %.loc11_23.2 to %.loc11_23.3 [template = constants.%int_12.2]
  72. // CHECK:STDOUT: %.loc11_23.5: init %array_type = array_init (%.loc11_23.4) to file.%a.var [template = constants.%array]
  73. // CHECK:STDOUT: %.loc11_24: init %array_type = converted %.loc11_23.1, %.loc11_23.5 [template = constants.%array]
  74. // CHECK:STDOUT: assign file.%a.var, %.loc11_24
  75. // CHECK:STDOUT: %a.ref: ref %array_type = name_ref a, file.%a
  76. // CHECK:STDOUT: %int_1: Core.IntLiteral = int_value 1 [template = constants.%int_1.1]
  77. // CHECK:STDOUT: %int_32: Core.IntLiteral = int_value 32 [template = constants.%int_32]
  78. // CHECK:STDOUT: %i32: type = class_type @Int, @Int(constants.%int_32) [template = constants.%i32]
  79. // CHECK:STDOUT: %impl.elem0.loc15: %Convert.type.2 = interface_witness_access constants.%interface.5, element0 [template = constants.%Convert.10]
  80. // CHECK:STDOUT: %Convert.bound.loc15: <bound method> = bound_method %int_1, %impl.elem0.loc15 [template = constants.%Convert.bound.2]
  81. // CHECK:STDOUT: %Convert.specific_fn.loc15: <specific function> = specific_function %Convert.bound.loc15, @Convert.2(constants.%int_32) [template = constants.%Convert.specific_fn.2]
  82. // CHECK:STDOUT: %int.convert_checked.loc15: init %i32 = call %Convert.specific_fn.loc15(%int_1) [template = constants.%int_1.2]
  83. // CHECK:STDOUT: %.loc15_16.1: %i32 = value_of_initializer %int.convert_checked.loc15 [template = constants.%int_1.2]
  84. // CHECK:STDOUT: %.loc15_16.2: %i32 = converted %int_1, %.loc15_16.1 [template = constants.%int_1.2]
  85. // CHECK:STDOUT: %.loc15_17.1: ref %i32 = array_index %a.ref, %.loc15_16.2 [template = <error>]
  86. // CHECK:STDOUT: %.loc15_17.2: %i32 = bind_value %.loc15_17.1
  87. // CHECK:STDOUT: assign file.%b.var, %.loc15_17.2
  88. // CHECK:STDOUT: return
  89. // CHECK:STDOUT: }
  90. // CHECK:STDOUT: