fail_negative_indexing.carbon 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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: cannot access member of interface `Negate` in type `i32` that does not implement that interface
  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: %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 2 [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 42 [template]
  25. // CHECK:STDOUT: %.6: type = tuple_type (i32, i32) [template]
  26. // CHECK:STDOUT: %.7: i32 = int_literal 0 [template]
  27. // CHECK:STDOUT: %.8: i32 = int_literal 1 [template]
  28. // CHECK:STDOUT: %array: %.3 = tuple_value (%.5, %.5) [template]
  29. // CHECK:STDOUT: %.9: i32 = int_literal 10 [template]
  30. // CHECK:STDOUT: %.10: type = interface_type @Negate [template]
  31. // CHECK:STDOUT: %Self: %.10 = bind_symbolic_name Self 0 [symbolic]
  32. // CHECK:STDOUT: %Op.type: type = fn_type @Op [template]
  33. // CHECK:STDOUT: %Op: %Op.type = struct_value () [template]
  34. // CHECK:STDOUT: %.11: type = assoc_entity_type %.10, %Op.type [template]
  35. // CHECK:STDOUT: %.12: %.11 = assoc_entity element0, imports.%import_ref.6 [template]
  36. // CHECK:STDOUT: }
  37. // CHECK:STDOUT:
  38. // CHECK:STDOUT: imports {
  39. // CHECK:STDOUT: %Core: <namespace> = namespace file.%Core.import, [template] {
  40. // CHECK:STDOUT: .Int32 = %import_ref.1
  41. // CHECK:STDOUT: .Negate = %import_ref.2
  42. // CHECK:STDOUT: import Core//prelude
  43. // CHECK:STDOUT: import Core//prelude/operators
  44. // CHECK:STDOUT: import Core//prelude/types
  45. // CHECK:STDOUT: import Core//prelude/operators/arithmetic
  46. // CHECK:STDOUT: import Core//prelude/operators/as
  47. // CHECK:STDOUT: import Core//prelude/operators/bitwise
  48. // CHECK:STDOUT: import Core//prelude/operators/comparison
  49. // CHECK:STDOUT: import Core//prelude/types/bool
  50. // CHECK:STDOUT: }
  51. // CHECK:STDOUT: %import_ref.1: %Int32.type = import_ref Core//prelude/types, inst+4, loaded [template = constants.%Int32]
  52. // CHECK:STDOUT: %import_ref.2: type = import_ref Core//prelude/operators/arithmetic, inst+77, loaded [template = constants.%.10]
  53. // CHECK:STDOUT: %import_ref.3 = import_ref Core//prelude/operators/arithmetic, inst+79, unloaded
  54. // CHECK:STDOUT: %import_ref.4: %.11 = import_ref Core//prelude/operators/arithmetic, inst+96, loaded [template = constants.%.12]
  55. // CHECK:STDOUT: %import_ref.5 = import_ref Core//prelude/operators/arithmetic, inst+91, unloaded
  56. // CHECK:STDOUT: %import_ref.6 = import_ref Core//prelude/operators/arithmetic, inst+91, unloaded
  57. // CHECK:STDOUT: }
  58. // CHECK:STDOUT:
  59. // CHECK:STDOUT: file {
  60. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  61. // CHECK:STDOUT: .Core = imports.%Core
  62. // CHECK:STDOUT: .c = %c
  63. // CHECK:STDOUT: .d = %d
  64. // CHECK:STDOUT: }
  65. // CHECK:STDOUT: %Core.import = import Core
  66. // CHECK:STDOUT: %int.make_type_32.loc11: init type = call constants.%Int32() [template = i32]
  67. // CHECK:STDOUT: %.loc11_14: i32 = int_literal 2 [template = constants.%.2]
  68. // CHECK:STDOUT: %.loc11_9.1: type = value_of_initializer %int.make_type_32.loc11 [template = i32]
  69. // CHECK:STDOUT: %.loc11_9.2: type = converted %int.make_type_32.loc11, %.loc11_9.1 [template = i32]
  70. // CHECK:STDOUT: %.loc11_15: type = array_type %.loc11_14, i32 [template = constants.%.3]
  71. // CHECK:STDOUT: %c.var: ref %.3 = var c
  72. // CHECK:STDOUT: %c: ref %.3 = bind_name c, %c.var
  73. // CHECK:STDOUT: %int.make_type_32.loc15: init type = call constants.%Int32() [template = i32]
  74. // CHECK:STDOUT: %.loc15_8.1: type = value_of_initializer %int.make_type_32.loc15 [template = i32]
  75. // CHECK:STDOUT: %.loc15_8.2: type = converted %int.make_type_32.loc15, %.loc15_8.1 [template = i32]
  76. // CHECK:STDOUT: %d.var: ref i32 = var d
  77. // CHECK:STDOUT: %d: ref i32 = bind_name d, %d.var
  78. // CHECK:STDOUT: }
  79. // CHECK:STDOUT:
  80. // CHECK:STDOUT: interface @Negate {
  81. // CHECK:STDOUT: !members:
  82. // CHECK:STDOUT: .Self = imports.%import_ref.3
  83. // CHECK:STDOUT: .Op = imports.%import_ref.4
  84. // CHECK:STDOUT: witness = (imports.%import_ref.5)
  85. // CHECK:STDOUT: }
  86. // CHECK:STDOUT:
  87. // CHECK:STDOUT: fn @Int32() -> type = "int.make_type_32";
  88. // CHECK:STDOUT:
  89. // CHECK:STDOUT: generic fn @Op(constants.%Self: %.10) {
  90. // CHECK:STDOUT: %Self: %.10 = bind_symbolic_name Self 0 [symbolic = %Self (constants.%Self)]
  91. // CHECK:STDOUT:
  92. // CHECK:STDOUT: fn[%self: @Op.%Self (%Self)]() -> @Op.%Self (%Self);
  93. // CHECK:STDOUT: }
  94. // CHECK:STDOUT:
  95. // CHECK:STDOUT: fn @__global_init() {
  96. // CHECK:STDOUT: !entry:
  97. // CHECK:STDOUT: %.loc11_20: i32 = int_literal 42 [template = constants.%.5]
  98. // CHECK:STDOUT: %.loc11_24: i32 = int_literal 42 [template = constants.%.5]
  99. // CHECK:STDOUT: %.loc11_26.1: %.6 = tuple_literal (%.loc11_20, %.loc11_24)
  100. // CHECK:STDOUT: %.loc11_26.2: i32 = int_literal 0 [template = constants.%.7]
  101. // CHECK:STDOUT: %.loc11_26.3: ref i32 = array_index file.%c.var, %.loc11_26.2
  102. // CHECK:STDOUT: %.loc11_26.4: init i32 = initialize_from %.loc11_20 to %.loc11_26.3 [template = constants.%.5]
  103. // CHECK:STDOUT: %.loc11_26.5: i32 = int_literal 1 [template = constants.%.8]
  104. // CHECK:STDOUT: %.loc11_26.6: ref i32 = array_index file.%c.var, %.loc11_26.5
  105. // CHECK:STDOUT: %.loc11_26.7: init i32 = initialize_from %.loc11_24 to %.loc11_26.6 [template = constants.%.5]
  106. // CHECK:STDOUT: %.loc11_26.8: init %.3 = array_init (%.loc11_26.4, %.loc11_26.7) to file.%c.var [template = constants.%array]
  107. // CHECK:STDOUT: %.loc11_27: init %.3 = converted %.loc11_26.1, %.loc11_26.8 [template = constants.%array]
  108. // CHECK:STDOUT: assign file.%c.var, %.loc11_27
  109. // CHECK:STDOUT: %c.ref: ref %.3 = name_ref c, file.%c
  110. // CHECK:STDOUT: %.loc15_17: i32 = int_literal 10 [template = constants.%.9]
  111. // CHECK:STDOUT: %Op.ref: %.11 = name_ref Op, imports.%import_ref.4 [template = constants.%.12]
  112. // CHECK:STDOUT: %.loc15_19.1: ref i32 = array_index %c.ref, <error> [template = <error>]
  113. // CHECK:STDOUT: %.loc15_19.2: i32 = bind_value %.loc15_19.1
  114. // CHECK:STDOUT: assign file.%d.var, %.loc15_19.2
  115. // CHECK:STDOUT: return
  116. // CHECK:STDOUT: }
  117. // CHECK:STDOUT:
  118. // CHECK:STDOUT: specific @Op(constants.%Self) {
  119. // CHECK:STDOUT: %Self => constants.%Self
  120. // CHECK:STDOUT: }
  121. // CHECK:STDOUT: