fail_array_non_int_indexing.carbon 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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_non_int_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_array_non_int_indexing.carbon
  10. var a: [i32; 1] = (12,);
  11. // CHECK:STDERR: fail_array_non_int_indexing.carbon:[[@LINE+6]]:16: error: cannot implicitly convert from `f64` to `i32`
  12. // CHECK:STDERR: var b: i32 = a[2.6];
  13. // CHECK:STDERR: ^~~
  14. // CHECK:STDERR: fail_array_non_int_indexing.carbon:[[@LINE+3]]:16: note: type `f64` does not implement interface `ImplicitAs`
  15. // CHECK:STDERR: var b: i32 = a[2.6];
  16. // CHECK:STDERR: ^~~
  17. var b: i32 = a[2.6];
  18. // CHECK:STDOUT: --- fail_array_non_int_indexing.carbon
  19. // CHECK:STDOUT:
  20. // CHECK:STDOUT: constants {
  21. // CHECK:STDOUT: %Int32.type: type = fn_type @Int32 [template]
  22. // CHECK:STDOUT: %.1: type = tuple_type () [template]
  23. // CHECK:STDOUT: %Int32: %Int32.type = struct_value () [template]
  24. // CHECK:STDOUT: %.2: i32 = int_literal 1 [template]
  25. // CHECK:STDOUT: %.3: type = array_type %.2, i32 [template]
  26. // CHECK:STDOUT: %.4: type = ptr_type %.3 [template]
  27. // CHECK:STDOUT: %.5: i32 = int_literal 12 [template]
  28. // CHECK:STDOUT: %.6: type = tuple_type (i32) [template]
  29. // CHECK:STDOUT: %.7: i32 = int_literal 0 [template]
  30. // CHECK:STDOUT: %array: %.3 = tuple_value (%.5) [template]
  31. // CHECK:STDOUT: %.8: f64 = float_literal 2.6000000000000001 [template]
  32. // CHECK:STDOUT: %ImplicitAs.type: type = generic_interface_type @ImplicitAs [template]
  33. // CHECK:STDOUT: %ImplicitAs: %ImplicitAs.type = struct_value () [template]
  34. // CHECK:STDOUT: %Dest: type = bind_symbolic_name Dest 0 [symbolic]
  35. // CHECK:STDOUT: %.9: type = interface_type @ImplicitAs, @ImplicitAs(%Dest) [symbolic]
  36. // CHECK:STDOUT: %Self.1: @ImplicitAs.%.1 (%.9) = bind_symbolic_name Self 1 [symbolic]
  37. // CHECK:STDOUT: %Self.2: %.9 = bind_symbolic_name Self 1 [symbolic]
  38. // CHECK:STDOUT: %Convert.type.1: type = fn_type @Convert, @ImplicitAs(%Dest) [symbolic]
  39. // CHECK:STDOUT: %Convert.1: %Convert.type.1 = struct_value () [symbolic]
  40. // CHECK:STDOUT: %.10: type = assoc_entity_type %.9, %Convert.type.1 [symbolic]
  41. // CHECK:STDOUT: %.11: %.10 = assoc_entity element0, imports.%import_ref.6 [symbolic]
  42. // CHECK:STDOUT: %.12: type = interface_type @ImplicitAs, @ImplicitAs(i32) [template]
  43. // CHECK:STDOUT: %Convert.type.2: type = fn_type @Convert, @ImplicitAs(i32) [template]
  44. // CHECK:STDOUT: %Convert.2: %Convert.type.2 = struct_value () [template]
  45. // CHECK:STDOUT: %.13: type = assoc_entity_type %.12, %Convert.type.2 [template]
  46. // CHECK:STDOUT: %.14: %.13 = assoc_entity element0, imports.%import_ref.6 [template]
  47. // CHECK:STDOUT: %.15: %.10 = assoc_entity element0, imports.%import_ref.7 [symbolic]
  48. // CHECK:STDOUT: }
  49. // CHECK:STDOUT:
  50. // CHECK:STDOUT: imports {
  51. // CHECK:STDOUT: %Core: <namespace> = namespace file.%Core.import, [template] {
  52. // CHECK:STDOUT: .Int32 = %import_ref.1
  53. // CHECK:STDOUT: .ImplicitAs = %import_ref.2
  54. // CHECK:STDOUT: import Core//prelude
  55. // CHECK:STDOUT: import Core//prelude/operators
  56. // CHECK:STDOUT: import Core//prelude/types
  57. // CHECK:STDOUT: import Core//prelude/operators/arithmetic
  58. // CHECK:STDOUT: import Core//prelude/operators/as
  59. // CHECK:STDOUT: import Core//prelude/operators/bitwise
  60. // CHECK:STDOUT: import Core//prelude/operators/comparison
  61. // CHECK:STDOUT: import Core//prelude/types/bool
  62. // CHECK:STDOUT: }
  63. // CHECK:STDOUT: %import_ref.1: %Int32.type = import_ref Core//prelude/types, inst+4, loaded [template = constants.%Int32]
  64. // CHECK:STDOUT: %import_ref.2: %ImplicitAs.type = import_ref Core//prelude/operators/as, inst+40, loaded [template = constants.%ImplicitAs]
  65. // CHECK:STDOUT: %import_ref.3 = import_ref Core//prelude/operators/as, inst+45, unloaded
  66. // CHECK:STDOUT: %import_ref.4: @ImplicitAs.%.2 (%.10) = import_ref Core//prelude/operators/as, inst+63, loaded [symbolic = @ImplicitAs.%.3 (constants.%.15)]
  67. // CHECK:STDOUT: %import_ref.5 = import_ref Core//prelude/operators/as, inst+56, unloaded
  68. // CHECK:STDOUT: %import_ref.6 = import_ref Core//prelude/operators/as, inst+56, unloaded
  69. // CHECK:STDOUT: %import_ref.7 = import_ref Core//prelude/operators/as, inst+56, unloaded
  70. // CHECK:STDOUT: }
  71. // CHECK:STDOUT:
  72. // CHECK:STDOUT: file {
  73. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  74. // CHECK:STDOUT: .Core = imports.%Core
  75. // CHECK:STDOUT: .a = %a
  76. // CHECK:STDOUT: .b = %b
  77. // CHECK:STDOUT: }
  78. // CHECK:STDOUT: %Core.import = import Core
  79. // CHECK:STDOUT: %int.make_type_32.loc11: init type = call constants.%Int32() [template = i32]
  80. // CHECK:STDOUT: %.loc11_14: i32 = int_literal 1 [template = constants.%.2]
  81. // CHECK:STDOUT: %.loc11_9.1: type = value_of_initializer %int.make_type_32.loc11 [template = i32]
  82. // CHECK:STDOUT: %.loc11_9.2: type = converted %int.make_type_32.loc11, %.loc11_9.1 [template = i32]
  83. // CHECK:STDOUT: %.loc11_15: type = array_type %.loc11_14, i32 [template = constants.%.3]
  84. // CHECK:STDOUT: %a.var: ref %.3 = var a
  85. // CHECK:STDOUT: %a: ref %.3 = bind_name a, %a.var
  86. // CHECK:STDOUT: %int.make_type_32.loc18: init type = call constants.%Int32() [template = i32]
  87. // CHECK:STDOUT: %.loc18_8.1: type = value_of_initializer %int.make_type_32.loc18 [template = i32]
  88. // CHECK:STDOUT: %.loc18_8.2: type = converted %int.make_type_32.loc18, %.loc18_8.1 [template = i32]
  89. // CHECK:STDOUT: %b.var: ref i32 = var b
  90. // CHECK:STDOUT: %b: ref i32 = bind_name b, %b.var
  91. // CHECK:STDOUT: }
  92. // CHECK:STDOUT:
  93. // CHECK:STDOUT: generic interface @ImplicitAs(constants.%Dest: type) {
  94. // CHECK:STDOUT: %Dest: type = bind_symbolic_name Dest 0 [symbolic = %Dest (constants.%Dest)]
  95. // CHECK:STDOUT:
  96. // CHECK:STDOUT: !definition:
  97. // CHECK:STDOUT: %.1: type = interface_type @ImplicitAs, @ImplicitAs(%Dest) [symbolic = %.1 (constants.%.9)]
  98. // CHECK:STDOUT: %Self: %.9 = bind_symbolic_name Self 1 [symbolic = %Self (constants.%Self.2)]
  99. // CHECK:STDOUT: %Convert.type: type = fn_type @Convert, @ImplicitAs(%Dest) [symbolic = %Convert.type (constants.%Convert.type.1)]
  100. // CHECK:STDOUT: %Convert: @ImplicitAs.%Convert.type (%Convert.type.1) = struct_value () [symbolic = %Convert (constants.%Convert.1)]
  101. // CHECK:STDOUT: %.2: type = assoc_entity_type @ImplicitAs.%.1 (%.9), @ImplicitAs.%Convert.type (%Convert.type.1) [symbolic = %.2 (constants.%.10)]
  102. // CHECK:STDOUT: %.3: @ImplicitAs.%.2 (%.10) = assoc_entity element0, imports.%import_ref.6 [symbolic = %.3 (constants.%.11)]
  103. // CHECK:STDOUT:
  104. // CHECK:STDOUT: interface {
  105. // CHECK:STDOUT: !members:
  106. // CHECK:STDOUT: .Self = imports.%import_ref.3
  107. // CHECK:STDOUT: .Convert = imports.%import_ref.4
  108. // CHECK:STDOUT: witness = (imports.%import_ref.5)
  109. // CHECK:STDOUT: }
  110. // CHECK:STDOUT: }
  111. // CHECK:STDOUT:
  112. // CHECK:STDOUT: fn @Int32() -> type = "int.make_type_32";
  113. // CHECK:STDOUT:
  114. // CHECK:STDOUT: generic fn @Convert(constants.%Dest: type, constants.%Self.1: @ImplicitAs.%.1 (%.9)) {
  115. // CHECK:STDOUT: %Dest: type = bind_symbolic_name Dest 0 [symbolic = %Dest (constants.%Dest)]
  116. // CHECK:STDOUT: %.1: type = interface_type @ImplicitAs, @ImplicitAs(%Dest) [symbolic = %.1 (constants.%.9)]
  117. // CHECK:STDOUT: %Self: %.9 = bind_symbolic_name Self 1 [symbolic = %Self (constants.%Self.2)]
  118. // CHECK:STDOUT:
  119. // CHECK:STDOUT: fn[%self: @Convert.%Self (%Self.2)]() -> @Convert.%Dest (%Dest);
  120. // CHECK:STDOUT: }
  121. // CHECK:STDOUT:
  122. // CHECK:STDOUT: fn @__global_init() {
  123. // CHECK:STDOUT: !entry:
  124. // CHECK:STDOUT: %.loc11_20: i32 = int_literal 12 [template = constants.%.5]
  125. // CHECK:STDOUT: %.loc11_23.1: %.6 = tuple_literal (%.loc11_20)
  126. // CHECK:STDOUT: %.loc11_23.2: i32 = int_literal 0 [template = constants.%.7]
  127. // CHECK:STDOUT: %.loc11_23.3: ref i32 = array_index file.%a.var, %.loc11_23.2
  128. // CHECK:STDOUT: %.loc11_23.4: init i32 = initialize_from %.loc11_20 to %.loc11_23.3 [template = constants.%.5]
  129. // CHECK:STDOUT: %.loc11_23.5: init %.3 = array_init (%.loc11_23.4) to file.%a.var [template = constants.%array]
  130. // CHECK:STDOUT: %.loc11_24: init %.3 = converted %.loc11_23.1, %.loc11_23.5 [template = constants.%array]
  131. // CHECK:STDOUT: assign file.%a.var, %.loc11_24
  132. // CHECK:STDOUT: %a.ref: ref %.3 = name_ref a, file.%a
  133. // CHECK:STDOUT: %.loc18_16.1: f64 = float_literal 2.6000000000000001 [template = constants.%.8]
  134. // CHECK:STDOUT: %.loc18_16.2: type = interface_type @ImplicitAs, @ImplicitAs(i32) [template = constants.%.12]
  135. // CHECK:STDOUT: %.loc18_16.3: %.13 = specific_constant imports.%import_ref.4, @ImplicitAs(i32) [template = constants.%.14]
  136. // CHECK:STDOUT: %Convert.ref: %.13 = name_ref Convert, %.loc18_16.3 [template = constants.%.14]
  137. // CHECK:STDOUT: %.loc18_16.4: i32 = converted %.loc18_16.1, <error> [template = <error>]
  138. // CHECK:STDOUT: %.loc18_19.1: ref i32 = array_index %a.ref, <error> [template = <error>]
  139. // CHECK:STDOUT: %.loc18_19.2: i32 = bind_value %.loc18_19.1
  140. // CHECK:STDOUT: assign file.%b.var, %.loc18_19.2
  141. // CHECK:STDOUT: return
  142. // CHECK:STDOUT: }
  143. // CHECK:STDOUT:
  144. // CHECK:STDOUT: specific @ImplicitAs(constants.%Dest) {
  145. // CHECK:STDOUT: %Dest => constants.%Dest
  146. // CHECK:STDOUT: }
  147. // CHECK:STDOUT:
  148. // CHECK:STDOUT: specific @ImplicitAs(@ImplicitAs.%Dest) {
  149. // CHECK:STDOUT: %Dest => constants.%Dest
  150. // CHECK:STDOUT: }
  151. // CHECK:STDOUT:
  152. // CHECK:STDOUT: specific @ImplicitAs(@Convert.%Dest) {
  153. // CHECK:STDOUT: %Dest => constants.%Dest
  154. // CHECK:STDOUT: }
  155. // CHECK:STDOUT:
  156. // CHECK:STDOUT: specific @Convert(constants.%Dest, constants.%Self.1) {
  157. // CHECK:STDOUT: %Dest => constants.%Dest
  158. // CHECK:STDOUT: %.1 => constants.%.9
  159. // CHECK:STDOUT: %Self => constants.%Self.1
  160. // CHECK:STDOUT: }
  161. // CHECK:STDOUT:
  162. // CHECK:STDOUT: specific @ImplicitAs(i32) {
  163. // CHECK:STDOUT: %Dest => i32
  164. // CHECK:STDOUT:
  165. // CHECK:STDOUT: !definition:
  166. // CHECK:STDOUT: %.1 => constants.%.12
  167. // CHECK:STDOUT: %Self => constants.%Self.2
  168. // CHECK:STDOUT: %Convert.type => constants.%Convert.type.2
  169. // CHECK:STDOUT: %Convert => constants.%Convert.2
  170. // CHECK:STDOUT: %.2 => constants.%.13
  171. // CHECK:STDOUT: %.3 => constants.%.14
  172. // CHECK:STDOUT: }
  173. // CHECK:STDOUT: