fail_array_non_int_indexing.carbon 11 KB

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