fail_array_out_of_bound_access.carbon 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. // INCLUDE-FILE: toolchain/testing/testdata/min_prelude/int.carbon
  6. // TODO: Add ranges and switch to "--dump-sem-ir-ranges=only".
  7. // EXTRA-ARGS: --dump-sem-ir-ranges=if-present
  8. //
  9. // AUTOUPDATE
  10. // TIP: To test this file alone, run:
  11. // TIP: bazel test //toolchain/testing:file_test --test_arg=--file_tests=toolchain/check/testdata/index/fail_array_out_of_bound_access.carbon
  12. // TIP: To dump output, run:
  13. // TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/index/fail_array_out_of_bound_access.carbon
  14. var a: array(i32, 1) = (12,);
  15. // CHECK:STDERR: fail_array_out_of_bound_access.carbon:[[@LINE+4]]:16: error: array index `1` is past the end of type `array(i32, 1)` [ArrayIndexOutOfBounds]
  16. // CHECK:STDERR: var b: i32 = a[1];
  17. // CHECK:STDERR: ^
  18. // CHECK:STDERR:
  19. var b: i32 = a[1];
  20. // CHECK:STDOUT: --- fail_array_out_of_bound_access.carbon
  21. // CHECK:STDOUT:
  22. // CHECK:STDOUT: constants {
  23. // CHECK:STDOUT: %int_32: Core.IntLiteral = int_value 32 [concrete]
  24. // CHECK:STDOUT: %Int.type: type = generic_class_type @Int [concrete]
  25. // CHECK:STDOUT: %Int.generic: %Int.type = struct_value () [concrete]
  26. // CHECK:STDOUT: %N: Core.IntLiteral = bind_symbolic_name N, 0 [symbolic]
  27. // CHECK:STDOUT: %i32: type = class_type @Int, @Int(%int_32) [concrete]
  28. // CHECK:STDOUT: %int_1.5b8: Core.IntLiteral = int_value 1 [concrete]
  29. // CHECK:STDOUT: %array_type: type = array_type %int_1.5b8, %i32 [concrete]
  30. // CHECK:STDOUT: %pattern_type.a98: type = pattern_type %array_type [concrete]
  31. // CHECK:STDOUT: %int_12.6a3: Core.IntLiteral = int_value 12 [concrete]
  32. // CHECK:STDOUT: %tuple.type.985: type = tuple_type (Core.IntLiteral) [concrete]
  33. // CHECK:STDOUT: %int_0: Core.IntLiteral = int_value 0 [concrete]
  34. // CHECK:STDOUT: %ImplicitAs.type.cc7: type = generic_interface_type @ImplicitAs [concrete]
  35. // CHECK:STDOUT: %ImplicitAs.generic: %ImplicitAs.type.cc7 = struct_value () [concrete]
  36. // CHECK:STDOUT: %ImplicitAs.type.d14: type = facet_type <@ImplicitAs, @ImplicitAs(%i32)> [concrete]
  37. // CHECK:STDOUT: %ImplicitAs.Convert.type.1b6: type = fn_type @ImplicitAs.Convert, @ImplicitAs(%i32) [concrete]
  38. // CHECK:STDOUT: %To: Core.IntLiteral = bind_symbolic_name To, 0 [symbolic]
  39. // CHECK:STDOUT: %Core.IntLiteral.as.ImplicitAs.impl.Convert.type.340: type = fn_type @Core.IntLiteral.as.ImplicitAs.impl.Convert, @Core.IntLiteral.as.ImplicitAs.impl(%To) [symbolic]
  40. // CHECK:STDOUT: %Core.IntLiteral.as.ImplicitAs.impl.Convert.1c0: %Core.IntLiteral.as.ImplicitAs.impl.Convert.type.340 = struct_value () [symbolic]
  41. // CHECK:STDOUT: %ImplicitAs.impl_witness.204: <witness> = impl_witness imports.%ImplicitAs.impl_witness_table.9e9, @Core.IntLiteral.as.ImplicitAs.impl(%int_32) [concrete]
  42. // CHECK:STDOUT: %Core.IntLiteral.as.ImplicitAs.impl.Convert.type.584: type = fn_type @Core.IntLiteral.as.ImplicitAs.impl.Convert, @Core.IntLiteral.as.ImplicitAs.impl(%int_32) [concrete]
  43. // CHECK:STDOUT: %Core.IntLiteral.as.ImplicitAs.impl.Convert.0f0: %Core.IntLiteral.as.ImplicitAs.impl.Convert.type.584 = struct_value () [concrete]
  44. // CHECK:STDOUT: %ImplicitAs.facet: %ImplicitAs.type.d14 = facet_value Core.IntLiteral, (%ImplicitAs.impl_witness.204) [concrete]
  45. // CHECK:STDOUT: %.1df: type = fn_type_with_self_type %ImplicitAs.Convert.type.1b6, %ImplicitAs.facet [concrete]
  46. // CHECK:STDOUT: %Core.IntLiteral.as.ImplicitAs.impl.Convert.bound.7a1: <bound method> = bound_method %int_12.6a3, %Core.IntLiteral.as.ImplicitAs.impl.Convert.0f0 [concrete]
  47. // CHECK:STDOUT: %pattern_type.7ce: type = pattern_type %i32 [concrete]
  48. // CHECK:STDOUT: %Core.IntLiteral.as.ImplicitAs.impl.Convert.specific_fn: <specific function> = specific_function %Core.IntLiteral.as.ImplicitAs.impl.Convert.0f0, @Core.IntLiteral.as.ImplicitAs.impl.Convert(%int_32) [concrete]
  49. // CHECK:STDOUT: %bound_method.9c8: <bound method> = bound_method %int_12.6a3, %Core.IntLiteral.as.ImplicitAs.impl.Convert.specific_fn [concrete]
  50. // CHECK:STDOUT: %int_12.1e1: %i32 = int_value 12 [concrete]
  51. // CHECK:STDOUT: %array: %array_type = tuple_value (%int_12.1e1) [concrete]
  52. // CHECK:STDOUT: %Core.IntLiteral.as.ImplicitAs.impl.Convert.bound.abf: <bound method> = bound_method %int_1.5b8, %Core.IntLiteral.as.ImplicitAs.impl.Convert.0f0 [concrete]
  53. // CHECK:STDOUT: %bound_method.c11: <bound method> = bound_method %int_1.5b8, %Core.IntLiteral.as.ImplicitAs.impl.Convert.specific_fn [concrete]
  54. // CHECK:STDOUT: %int_1.5d2: %i32 = int_value 1 [concrete]
  55. // CHECK:STDOUT: %Copy.type: type = facet_type <@Copy> [concrete]
  56. // CHECK:STDOUT: %Copy.Op.type: type = fn_type @Copy.Op [concrete]
  57. // CHECK:STDOUT: %Int.as.Copy.impl.Op.type.afd: type = fn_type @Int.as.Copy.impl.Op, @Int.as.Copy.impl(%N) [symbolic]
  58. // CHECK:STDOUT: %Int.as.Copy.impl.Op.6cd: %Int.as.Copy.impl.Op.type.afd = struct_value () [symbolic]
  59. // CHECK:STDOUT: %Copy.impl_witness.a32: <witness> = impl_witness imports.%Copy.impl_witness_table.1ed, @Int.as.Copy.impl(%int_32) [concrete]
  60. // CHECK:STDOUT: %Int.as.Copy.impl.Op.type.276: type = fn_type @Int.as.Copy.impl.Op, @Int.as.Copy.impl(%int_32) [concrete]
  61. // CHECK:STDOUT: %Int.as.Copy.impl.Op.f59: %Int.as.Copy.impl.Op.type.276 = struct_value () [concrete]
  62. // CHECK:STDOUT: %Copy.facet.c49: %Copy.type = facet_value %i32, (%Copy.impl_witness.a32) [concrete]
  63. // CHECK:STDOUT: %.7fa: type = fn_type_with_self_type %Copy.Op.type, %Copy.facet.c49 [concrete]
  64. // CHECK:STDOUT: %Int.as.Copy.impl.Op.specific_fn: <specific function> = specific_function %Int.as.Copy.impl.Op.f59, @Int.as.Copy.impl.Op(%int_32) [concrete]
  65. // CHECK:STDOUT: }
  66. // CHECK:STDOUT:
  67. // CHECK:STDOUT: imports {
  68. // CHECK:STDOUT: %Core: <namespace> = namespace file.%Core.import, [concrete] {
  69. // CHECK:STDOUT: .Int = %Core.Int
  70. // CHECK:STDOUT: .ImplicitAs = %Core.ImplicitAs
  71. // CHECK:STDOUT: .Copy = %Core.Copy
  72. // CHECK:STDOUT: import Core//prelude
  73. // CHECK:STDOUT: import Core//prelude/...
  74. // CHECK:STDOUT: }
  75. // CHECK:STDOUT: %Core.Int: %Int.type = import_ref Core//prelude/parts/int, Int, loaded [concrete = constants.%Int.generic]
  76. // CHECK:STDOUT: %Core.ImplicitAs: %ImplicitAs.type.cc7 = import_ref Core//prelude/parts/as, ImplicitAs, loaded [concrete = constants.%ImplicitAs.generic]
  77. // CHECK:STDOUT: %Core.import_ref.ee7: @Core.IntLiteral.as.ImplicitAs.impl.%Core.IntLiteral.as.ImplicitAs.impl.Convert.type (%Core.IntLiteral.as.ImplicitAs.impl.Convert.type.340) = import_ref Core//prelude/parts/int, loc23_39, loaded [symbolic = @Core.IntLiteral.as.ImplicitAs.impl.%Core.IntLiteral.as.ImplicitAs.impl.Convert (constants.%Core.IntLiteral.as.ImplicitAs.impl.Convert.1c0)]
  78. // CHECK:STDOUT: %ImplicitAs.impl_witness_table.9e9 = impl_witness_table (%Core.import_ref.ee7), @Core.IntLiteral.as.ImplicitAs.impl [concrete]
  79. // CHECK:STDOUT: %Core.Copy: type = import_ref Core//prelude/parts/copy, Copy, loaded [concrete = constants.%Copy.type]
  80. // CHECK:STDOUT: %Core.import_ref.d0f6: @Int.as.Copy.impl.%Int.as.Copy.impl.Op.type (%Int.as.Copy.impl.Op.type.afd) = import_ref Core//prelude/parts/int, loc17_31, loaded [symbolic = @Int.as.Copy.impl.%Int.as.Copy.impl.Op (constants.%Int.as.Copy.impl.Op.6cd)]
  81. // CHECK:STDOUT: %Copy.impl_witness_table.1ed = impl_witness_table (%Core.import_ref.d0f6), @Int.as.Copy.impl [concrete]
  82. // CHECK:STDOUT: }
  83. // CHECK:STDOUT:
  84. // CHECK:STDOUT: file {
  85. // CHECK:STDOUT: package: <namespace> = namespace [concrete] {
  86. // CHECK:STDOUT: .Core = imports.%Core
  87. // CHECK:STDOUT: .a = %a
  88. // CHECK:STDOUT: .b = %b
  89. // CHECK:STDOUT: }
  90. // CHECK:STDOUT: %Core.import = import Core
  91. // CHECK:STDOUT: name_binding_decl {
  92. // CHECK:STDOUT: %a.patt: %pattern_type.a98 = binding_pattern a [concrete]
  93. // CHECK:STDOUT: %a.var_patt: %pattern_type.a98 = var_pattern %a.patt [concrete]
  94. // CHECK:STDOUT: }
  95. // CHECK:STDOUT: %a.var: ref %array_type = var %a.var_patt [concrete]
  96. // CHECK:STDOUT: %.loc15: type = splice_block %array_type [concrete = constants.%array_type] {
  97. // CHECK:STDOUT: %int_32.loc15: Core.IntLiteral = int_value 32 [concrete = constants.%int_32]
  98. // CHECK:STDOUT: %i32.loc15: type = class_type @Int, @Int(constants.%int_32) [concrete = constants.%i32]
  99. // CHECK:STDOUT: %int_1: Core.IntLiteral = int_value 1 [concrete = constants.%int_1.5b8]
  100. // CHECK:STDOUT: %array_type: type = array_type %int_1, %i32.loc15 [concrete = constants.%array_type]
  101. // CHECK:STDOUT: }
  102. // CHECK:STDOUT: %a: ref %array_type = bind_name a, %a.var [concrete = %a.var]
  103. // CHECK:STDOUT: name_binding_decl {
  104. // CHECK:STDOUT: %b.patt: %pattern_type.7ce = binding_pattern b [concrete]
  105. // CHECK:STDOUT: %b.var_patt: %pattern_type.7ce = var_pattern %b.patt [concrete]
  106. // CHECK:STDOUT: }
  107. // CHECK:STDOUT: %b.var: ref %i32 = var %b.var_patt [concrete]
  108. // CHECK:STDOUT: %.loc20: type = splice_block %i32.loc20 [concrete = constants.%i32] {
  109. // CHECK:STDOUT: %int_32.loc20: Core.IntLiteral = int_value 32 [concrete = constants.%int_32]
  110. // CHECK:STDOUT: %i32.loc20: type = class_type @Int, @Int(constants.%int_32) [concrete = constants.%i32]
  111. // CHECK:STDOUT: }
  112. // CHECK:STDOUT: %b: ref %i32 = bind_name b, %b.var [concrete = %b.var]
  113. // CHECK:STDOUT: }
  114. // CHECK:STDOUT:
  115. // CHECK:STDOUT: fn @__global_init() {
  116. // CHECK:STDOUT: !entry:
  117. // CHECK:STDOUT: %int_12: Core.IntLiteral = int_value 12 [concrete = constants.%int_12.6a3]
  118. // CHECK:STDOUT: %.loc15_28.1: %tuple.type.985 = tuple_literal (%int_12)
  119. // CHECK:STDOUT: %impl.elem0.loc15: %.1df = impl_witness_access constants.%ImplicitAs.impl_witness.204, element0 [concrete = constants.%Core.IntLiteral.as.ImplicitAs.impl.Convert.0f0]
  120. // CHECK:STDOUT: %bound_method.loc15_28.1: <bound method> = bound_method %int_12, %impl.elem0.loc15 [concrete = constants.%Core.IntLiteral.as.ImplicitAs.impl.Convert.bound.7a1]
  121. // CHECK:STDOUT: %specific_fn.loc15: <specific function> = specific_function %impl.elem0.loc15, @Core.IntLiteral.as.ImplicitAs.impl.Convert(constants.%int_32) [concrete = constants.%Core.IntLiteral.as.ImplicitAs.impl.Convert.specific_fn]
  122. // CHECK:STDOUT: %bound_method.loc15_28.2: <bound method> = bound_method %int_12, %specific_fn.loc15 [concrete = constants.%bound_method.9c8]
  123. // CHECK:STDOUT: %Core.IntLiteral.as.ImplicitAs.impl.Convert.call.loc15: init %i32 = call %bound_method.loc15_28.2(%int_12) [concrete = constants.%int_12.1e1]
  124. // CHECK:STDOUT: %.loc15_28.2: init %i32 = converted %int_12, %Core.IntLiteral.as.ImplicitAs.impl.Convert.call.loc15 [concrete = constants.%int_12.1e1]
  125. // CHECK:STDOUT: %int_0: Core.IntLiteral = int_value 0 [concrete = constants.%int_0]
  126. // CHECK:STDOUT: %.loc15_28.3: ref %i32 = array_index file.%a.var, %int_0
  127. // CHECK:STDOUT: %.loc15_28.4: init %i32 = initialize_from %.loc15_28.2 to %.loc15_28.3 [concrete = constants.%int_12.1e1]
  128. // CHECK:STDOUT: %.loc15_28.5: init %array_type = array_init (%.loc15_28.4) to file.%a.var [concrete = constants.%array]
  129. // CHECK:STDOUT: %.loc15_1: init %array_type = converted %.loc15_28.1, %.loc15_28.5 [concrete = constants.%array]
  130. // CHECK:STDOUT: assign file.%a.var, %.loc15_1
  131. // CHECK:STDOUT: %a.ref: ref %array_type = name_ref a, file.%a [concrete = file.%a.var]
  132. // CHECK:STDOUT: %int_1: Core.IntLiteral = int_value 1 [concrete = constants.%int_1.5b8]
  133. // CHECK:STDOUT: %int_32: Core.IntLiteral = int_value 32 [concrete = constants.%int_32]
  134. // CHECK:STDOUT: %i32: type = class_type @Int, @Int(constants.%int_32) [concrete = constants.%i32]
  135. // CHECK:STDOUT: %impl.elem0.loc20_16: %.1df = impl_witness_access constants.%ImplicitAs.impl_witness.204, element0 [concrete = constants.%Core.IntLiteral.as.ImplicitAs.impl.Convert.0f0]
  136. // CHECK:STDOUT: %bound_method.loc20_16.1: <bound method> = bound_method %int_1, %impl.elem0.loc20_16 [concrete = constants.%Core.IntLiteral.as.ImplicitAs.impl.Convert.bound.abf]
  137. // CHECK:STDOUT: %specific_fn.loc20_16: <specific function> = specific_function %impl.elem0.loc20_16, @Core.IntLiteral.as.ImplicitAs.impl.Convert(constants.%int_32) [concrete = constants.%Core.IntLiteral.as.ImplicitAs.impl.Convert.specific_fn]
  138. // CHECK:STDOUT: %bound_method.loc20_16.2: <bound method> = bound_method %int_1, %specific_fn.loc20_16 [concrete = constants.%bound_method.c11]
  139. // CHECK:STDOUT: %Core.IntLiteral.as.ImplicitAs.impl.Convert.call.loc20: init %i32 = call %bound_method.loc20_16.2(%int_1) [concrete = constants.%int_1.5d2]
  140. // CHECK:STDOUT: %.loc20_16.1: %i32 = value_of_initializer %Core.IntLiteral.as.ImplicitAs.impl.Convert.call.loc20 [concrete = constants.%int_1.5d2]
  141. // CHECK:STDOUT: %.loc20_16.2: %i32 = converted %int_1, %.loc20_16.1 [concrete = constants.%int_1.5d2]
  142. // CHECK:STDOUT: %.loc20_17.1: ref %i32 = array_index %a.ref, %.loc20_16.2 [concrete = <error>]
  143. // CHECK:STDOUT: %.loc20_17.2: %i32 = bind_value %.loc20_17.1 [concrete = <error>]
  144. // CHECK:STDOUT: %impl.elem0.loc20_17: %.7fa = impl_witness_access constants.%Copy.impl_witness.a32, element0 [concrete = constants.%Int.as.Copy.impl.Op.f59]
  145. // CHECK:STDOUT: %bound_method.loc20_17.1: <bound method> = bound_method %.loc20_17.2, %impl.elem0.loc20_17 [concrete = <error>]
  146. // CHECK:STDOUT: %specific_fn.loc20_17: <specific function> = specific_function %impl.elem0.loc20_17, @Int.as.Copy.impl.Op(constants.%int_32) [concrete = constants.%Int.as.Copy.impl.Op.specific_fn]
  147. // CHECK:STDOUT: %bound_method.loc20_17.2: <bound method> = bound_method %.loc20_17.2, %specific_fn.loc20_17 [concrete = <error>]
  148. // CHECK:STDOUT: %Int.as.Copy.impl.Op.call: init %i32 = call %bound_method.loc20_17.2(%.loc20_17.2) [concrete = <error>]
  149. // CHECK:STDOUT: assign file.%b.var, %Int.as.Copy.impl.Op.call
  150. // CHECK:STDOUT: return
  151. // CHECK:STDOUT: }
  152. // CHECK:STDOUT: