fail_out_of_bound_non_literal.carbon 5.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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/array/fail_out_of_bound_non_literal.carbon
  8. // TIP: To dump output, run:
  9. // TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/array/fail_out_of_bound_non_literal.carbon
  10. var a: [i32; 3] = (1, 2, 3);
  11. // CHECK:STDERR: fail_out_of_bound_non_literal.carbon:[[@LINE+3]]:16: ERROR: Array index `3` is past the end of type `[i32; 3]`.
  12. // CHECK:STDERR: var b: i32 = a[{.index = 3}.index];
  13. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~
  14. var b: i32 = a[{.index = 3}.index];
  15. // CHECK:STDOUT: --- fail_out_of_bound_non_literal.carbon
  16. // CHECK:STDOUT:
  17. // CHECK:STDOUT: constants {
  18. // CHECK:STDOUT: %Int32.type: type = fn_type @Int32 [template]
  19. // CHECK:STDOUT: %.1: type = tuple_type () [template]
  20. // CHECK:STDOUT: %Int32: %Int32.type = struct_value () [template]
  21. // CHECK:STDOUT: %.2: i32 = int_literal 3 [template]
  22. // CHECK:STDOUT: %.3: type = array_type %.2, i32 [template]
  23. // CHECK:STDOUT: %.4: type = ptr_type %.3 [template]
  24. // CHECK:STDOUT: %.5: i32 = int_literal 1 [template]
  25. // CHECK:STDOUT: %.6: i32 = int_literal 2 [template]
  26. // CHECK:STDOUT: %.7: type = tuple_type (i32, i32, i32) [template]
  27. // CHECK:STDOUT: %.8: i32 = int_literal 0 [template]
  28. // CHECK:STDOUT: %array: %.3 = tuple_value (%.5, %.6, %.2) [template]
  29. // CHECK:STDOUT: %.9: type = struct_type {.index: i32} [template]
  30. // CHECK:STDOUT: %struct: %.9 = struct_value (%.2) [template]
  31. // CHECK:STDOUT: }
  32. // CHECK:STDOUT:
  33. // CHECK:STDOUT: file {
  34. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  35. // CHECK:STDOUT: .Core = %Core
  36. // CHECK:STDOUT: .a = %a
  37. // CHECK:STDOUT: .b = %b
  38. // CHECK:STDOUT: }
  39. // CHECK:STDOUT: %Core: <namespace> = namespace [template] {}
  40. // CHECK:STDOUT: %import_ref.1: %Int32.type = import_ref ir3, inst+3, loaded [template = constants.%Int32]
  41. // CHECK:STDOUT: %int.make_type_32.loc11: init type = call constants.%Int32() [template = i32]
  42. // CHECK:STDOUT: %.loc11_14: i32 = int_literal 3 [template = constants.%.2]
  43. // CHECK:STDOUT: %.loc11_9.1: type = value_of_initializer %int.make_type_32.loc11 [template = i32]
  44. // CHECK:STDOUT: %.loc11_9.2: type = converted %int.make_type_32.loc11, %.loc11_9.1 [template = i32]
  45. // CHECK:STDOUT: %.loc11_15: type = array_type %.loc11_14, i32 [template = constants.%.3]
  46. // CHECK:STDOUT: %a.var: ref %.3 = var a
  47. // CHECK:STDOUT: %a: ref %.3 = bind_name a, %a.var
  48. // CHECK:STDOUT: %import_ref.2: %Int32.type = import_ref ir3, inst+3, loaded [template = constants.%Int32]
  49. // CHECK:STDOUT: %int.make_type_32.loc15: init type = call constants.%Int32() [template = i32]
  50. // CHECK:STDOUT: %.loc15_8.1: type = value_of_initializer %int.make_type_32.loc15 [template = i32]
  51. // CHECK:STDOUT: %.loc15_8.2: type = converted %int.make_type_32.loc15, %.loc15_8.1 [template = i32]
  52. // CHECK:STDOUT: %b.var: ref i32 = var b
  53. // CHECK:STDOUT: %b: ref i32 = bind_name b, %b.var
  54. // CHECK:STDOUT: }
  55. // CHECK:STDOUT:
  56. // CHECK:STDOUT: fn @Int32() -> type = "int.make_type_32";
  57. // CHECK:STDOUT:
  58. // CHECK:STDOUT: fn @__global_init() {
  59. // CHECK:STDOUT: !entry:
  60. // CHECK:STDOUT: %.loc11_20: i32 = int_literal 1 [template = constants.%.5]
  61. // CHECK:STDOUT: %.loc11_23: i32 = int_literal 2 [template = constants.%.6]
  62. // CHECK:STDOUT: %.loc11_26: i32 = int_literal 3 [template = constants.%.2]
  63. // CHECK:STDOUT: %.loc11_27.1: %.7 = tuple_literal (%.loc11_20, %.loc11_23, %.loc11_26)
  64. // CHECK:STDOUT: %.loc11_27.2: i32 = int_literal 0 [template = constants.%.8]
  65. // CHECK:STDOUT: %.loc11_27.3: ref i32 = array_index file.%a.var, %.loc11_27.2
  66. // CHECK:STDOUT: %.loc11_27.4: init i32 = initialize_from %.loc11_20 to %.loc11_27.3 [template = constants.%.5]
  67. // CHECK:STDOUT: %.loc11_27.5: i32 = int_literal 1 [template = constants.%.5]
  68. // CHECK:STDOUT: %.loc11_27.6: ref i32 = array_index file.%a.var, %.loc11_27.5
  69. // CHECK:STDOUT: %.loc11_27.7: init i32 = initialize_from %.loc11_23 to %.loc11_27.6 [template = constants.%.6]
  70. // CHECK:STDOUT: %.loc11_27.8: i32 = int_literal 2 [template = constants.%.6]
  71. // CHECK:STDOUT: %.loc11_27.9: ref i32 = array_index file.%a.var, %.loc11_27.8
  72. // CHECK:STDOUT: %.loc11_27.10: init i32 = initialize_from %.loc11_26 to %.loc11_27.9 [template = constants.%.2]
  73. // CHECK:STDOUT: %.loc11_27.11: init %.3 = array_init (%.loc11_27.4, %.loc11_27.7, %.loc11_27.10) to file.%a.var [template = constants.%array]
  74. // CHECK:STDOUT: %.loc11_28: init %.3 = converted %.loc11_27.1, %.loc11_27.11 [template = constants.%array]
  75. // CHECK:STDOUT: assign file.%a.var, %.loc11_28
  76. // CHECK:STDOUT: %a.ref: ref %.3 = name_ref a, file.%a
  77. // CHECK:STDOUT: %.loc15_26: i32 = int_literal 3 [template = constants.%.2]
  78. // CHECK:STDOUT: %.loc15_27.1: %.9 = struct_literal (%.loc15_26)
  79. // CHECK:STDOUT: %struct: %.9 = struct_value (%.loc15_26) [template = constants.%struct]
  80. // CHECK:STDOUT: %.loc15_27.2: %.9 = converted %.loc15_27.1, %struct [template = constants.%struct]
  81. // CHECK:STDOUT: %.loc15_28: i32 = struct_access %.loc15_27.2, element0 [template = constants.%.2]
  82. // CHECK:STDOUT: %.loc15_34.1: ref i32 = array_index %a.ref, %.loc15_28 [template = <error>]
  83. // CHECK:STDOUT: %.loc15_34.2: i32 = bind_value %.loc15_34.1
  84. // CHECK:STDOUT: assign file.%b.var, %.loc15_34.2
  85. // CHECK:STDOUT: return
  86. // CHECK:STDOUT: }
  87. // CHECK:STDOUT: