fail_non_int_indexing.carbon 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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/tuple/access/fail_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/tuple/access/fail_non_int_indexing.carbon
  10. var a: (i32, i32) = (12, 6);
  11. // CHECK:STDERR: fail_non_int_indexing.carbon:[[@LINE+6]]:17: error: cannot implicitly convert from `f64` to `i32`
  12. // CHECK:STDERR: var b: i32 = a.(2.6);
  13. // CHECK:STDERR: ^~~
  14. // CHECK:STDERR: fail_non_int_indexing.carbon:[[@LINE+3]]:17: 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_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: type = tuple_type (type, type) [template]
  25. // CHECK:STDOUT: %.3: type = tuple_type (i32, i32) [template]
  26. // CHECK:STDOUT: %.4: type = ptr_type %.3 [template]
  27. // CHECK:STDOUT: %.5: i32 = int_literal 12 [template]
  28. // CHECK:STDOUT: %.6: i32 = int_literal 6 [template]
  29. // CHECK:STDOUT: %tuple: %.3 = tuple_value (%.5, %.6) [template]
  30. // CHECK:STDOUT: %.7: f64 = float_literal 2.6000000000000001 [template]
  31. // CHECK:STDOUT: %ImplicitAs.type.1: type = generic_interface_type @ImplicitAs [template]
  32. // CHECK:STDOUT: %ImplicitAs: %ImplicitAs.type.1 = struct_value () [template]
  33. // CHECK:STDOUT: %Dest: type = bind_symbolic_name Dest, 0 [symbolic]
  34. // CHECK:STDOUT: %ImplicitAs.type.2: type = interface_type @ImplicitAs, @ImplicitAs(%Dest) [symbolic]
  35. // CHECK:STDOUT: %Self.1: @ImplicitAs.%ImplicitAs.type (%ImplicitAs.type.2) = bind_symbolic_name Self, 1 [symbolic]
  36. // CHECK:STDOUT: %Dest.patt: type = symbolic_binding_pattern Dest, 0 [symbolic]
  37. // CHECK:STDOUT: %Self.2: %ImplicitAs.type.2 = 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: %.8: type = assoc_entity_type %ImplicitAs.type.2, %Convert.type.1 [symbolic]
  41. // CHECK:STDOUT: %.9: %.8 = assoc_entity element0, imports.%import_ref.6 [symbolic]
  42. // CHECK:STDOUT: %ImplicitAs.type.3: 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: %.10: type = assoc_entity_type %ImplicitAs.type.3, %Convert.type.2 [template]
  46. // CHECK:STDOUT: %.11: %.10 = assoc_entity element0, imports.%import_ref.6 [template]
  47. // CHECK:STDOUT: %.12: %.8 = 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+9, loaded [template = constants.%Int32]
  64. // CHECK:STDOUT: %import_ref.2: %ImplicitAs.type.1 = import_ref Core//prelude/operators/as, inst+46, loaded [template = constants.%ImplicitAs]
  65. // CHECK:STDOUT: %import_ref.3 = import_ref Core//prelude/operators/as, inst+52, unloaded
  66. // CHECK:STDOUT: %import_ref.4: @ImplicitAs.%.1 (%.8) = import_ref Core//prelude/operators/as, inst+71, loaded [symbolic = @ImplicitAs.%.2 (constants.%.12)]
  67. // CHECK:STDOUT: %import_ref.5 = import_ref Core//prelude/operators/as, inst+64, unloaded
  68. // CHECK:STDOUT: %import_ref.6 = import_ref Core//prelude/operators/as, inst+64, unloaded
  69. // CHECK:STDOUT: %import_ref.7 = import_ref Core//prelude/operators/as, inst+64, 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_9: init type = call constants.%Int32() [template = i32]
  80. // CHECK:STDOUT: %int.make_type_32.loc11_14: init type = call constants.%Int32() [template = i32]
  81. // CHECK:STDOUT: %.loc11_17.1: %.2 = tuple_literal (%int.make_type_32.loc11_9, %int.make_type_32.loc11_14)
  82. // CHECK:STDOUT: %.loc11_17.2: type = value_of_initializer %int.make_type_32.loc11_9 [template = i32]
  83. // CHECK:STDOUT: %.loc11_17.3: type = converted %int.make_type_32.loc11_9, %.loc11_17.2 [template = i32]
  84. // CHECK:STDOUT: %.loc11_17.4: type = value_of_initializer %int.make_type_32.loc11_14 [template = i32]
  85. // CHECK:STDOUT: %.loc11_17.5: type = converted %int.make_type_32.loc11_14, %.loc11_17.4 [template = i32]
  86. // CHECK:STDOUT: %.loc11_17.6: type = converted %.loc11_17.1, constants.%.3 [template = constants.%.3]
  87. // CHECK:STDOUT: %a.var: ref %.3 = var a
  88. // CHECK:STDOUT: %a: ref %.3 = bind_name a, %a.var
  89. // CHECK:STDOUT: %int.make_type_32.loc18: init type = call constants.%Int32() [template = i32]
  90. // CHECK:STDOUT: %.loc18_8.1: type = value_of_initializer %int.make_type_32.loc18 [template = i32]
  91. // CHECK:STDOUT: %.loc18_8.2: type = converted %int.make_type_32.loc18, %.loc18_8.1 [template = i32]
  92. // CHECK:STDOUT: %b.var: ref i32 = var b
  93. // CHECK:STDOUT: %b: ref i32 = bind_name b, %b.var
  94. // CHECK:STDOUT: }
  95. // CHECK:STDOUT:
  96. // CHECK:STDOUT: generic interface @ImplicitAs(constants.%Dest: type) {
  97. // CHECK:STDOUT: %Dest: type = bind_symbolic_name Dest, 0 [symbolic = %Dest (constants.%Dest)]
  98. // CHECK:STDOUT: %Dest.patt: type = symbolic_binding_pattern Dest, 0 [symbolic = %Dest.patt (constants.%Dest.patt)]
  99. // CHECK:STDOUT:
  100. // CHECK:STDOUT: !definition:
  101. // CHECK:STDOUT: %ImplicitAs.type: type = interface_type @ImplicitAs, @ImplicitAs(%Dest) [symbolic = %ImplicitAs.type (constants.%ImplicitAs.type.2)]
  102. // CHECK:STDOUT: %Self: %ImplicitAs.type.2 = bind_symbolic_name Self, 1 [symbolic = %Self (constants.%Self.2)]
  103. // CHECK:STDOUT: %Convert.type: type = fn_type @Convert, @ImplicitAs(%Dest) [symbolic = %Convert.type (constants.%Convert.type.1)]
  104. // CHECK:STDOUT: %Convert: @ImplicitAs.%Convert.type (%Convert.type.1) = struct_value () [symbolic = %Convert (constants.%Convert.1)]
  105. // CHECK:STDOUT: %.1: type = assoc_entity_type @ImplicitAs.%ImplicitAs.type (%ImplicitAs.type.2), @ImplicitAs.%Convert.type (%Convert.type.1) [symbolic = %.1 (constants.%.8)]
  106. // CHECK:STDOUT: %.2: @ImplicitAs.%.1 (%.8) = assoc_entity element0, imports.%import_ref.6 [symbolic = %.2 (constants.%.9)]
  107. // CHECK:STDOUT:
  108. // CHECK:STDOUT: interface {
  109. // CHECK:STDOUT: !members:
  110. // CHECK:STDOUT: .Self = imports.%import_ref.3
  111. // CHECK:STDOUT: .Convert = imports.%import_ref.4
  112. // CHECK:STDOUT: witness = (imports.%import_ref.5)
  113. // CHECK:STDOUT: }
  114. // CHECK:STDOUT: }
  115. // CHECK:STDOUT:
  116. // CHECK:STDOUT: fn @Int32() -> type = "int.make_type_32";
  117. // CHECK:STDOUT:
  118. // CHECK:STDOUT: generic fn @Convert(constants.%Dest: type, constants.%Self.1: @ImplicitAs.%ImplicitAs.type (%ImplicitAs.type.2)) {
  119. // CHECK:STDOUT: %Dest: type = bind_symbolic_name Dest, 0 [symbolic = %Dest (constants.%Dest)]
  120. // CHECK:STDOUT: %ImplicitAs.type: type = interface_type @ImplicitAs, @ImplicitAs(%Dest) [symbolic = %ImplicitAs.type (constants.%ImplicitAs.type.2)]
  121. // CHECK:STDOUT: %Self: %ImplicitAs.type.2 = bind_symbolic_name Self, 1 [symbolic = %Self (constants.%Self.2)]
  122. // CHECK:STDOUT:
  123. // CHECK:STDOUT: fn[%self.param_patt: @Convert.%Self (%Self.2)]() -> @Convert.%Dest (%Dest);
  124. // CHECK:STDOUT: }
  125. // CHECK:STDOUT:
  126. // CHECK:STDOUT: fn @__global_init() {
  127. // CHECK:STDOUT: !entry:
  128. // CHECK:STDOUT: %.loc11_22: i32 = int_literal 12 [template = constants.%.5]
  129. // CHECK:STDOUT: %.loc11_26: i32 = int_literal 6 [template = constants.%.6]
  130. // CHECK:STDOUT: %.loc11_27.1: %.3 = tuple_literal (%.loc11_22, %.loc11_26)
  131. // CHECK:STDOUT: %.loc11_27.2: ref i32 = tuple_access file.%a.var, element0
  132. // CHECK:STDOUT: %.loc11_27.3: init i32 = initialize_from %.loc11_22 to %.loc11_27.2 [template = constants.%.5]
  133. // CHECK:STDOUT: %.loc11_27.4: ref i32 = tuple_access file.%a.var, element1
  134. // CHECK:STDOUT: %.loc11_27.5: init i32 = initialize_from %.loc11_26 to %.loc11_27.4 [template = constants.%.6]
  135. // CHECK:STDOUT: %.loc11_27.6: init %.3 = tuple_init (%.loc11_27.3, %.loc11_27.5) to file.%a.var [template = constants.%tuple]
  136. // CHECK:STDOUT: %.loc11_28: init %.3 = converted %.loc11_27.1, %.loc11_27.6 [template = constants.%tuple]
  137. // CHECK:STDOUT: assign file.%a.var, %.loc11_28
  138. // CHECK:STDOUT: %a.ref: ref %.3 = name_ref a, file.%a
  139. // CHECK:STDOUT: %.loc18_17.1: f64 = float_literal 2.6000000000000001 [template = constants.%.7]
  140. // CHECK:STDOUT: %ImplicitAs.type: type = interface_type @ImplicitAs, @ImplicitAs(i32) [template = constants.%ImplicitAs.type.3]
  141. // CHECK:STDOUT: %.loc18_17.2: %.10 = specific_constant imports.%import_ref.4, @ImplicitAs(i32) [template = constants.%.11]
  142. // CHECK:STDOUT: %Convert.ref: %.10 = name_ref Convert, %.loc18_17.2 [template = constants.%.11]
  143. // CHECK:STDOUT: %.loc18_17.3: i32 = converted %.loc18_17.1, <error> [template = <error>]
  144. // CHECK:STDOUT: assign file.%b.var, <error>
  145. // CHECK:STDOUT: return
  146. // CHECK:STDOUT: }
  147. // CHECK:STDOUT:
  148. // CHECK:STDOUT: specific @ImplicitAs(constants.%Dest) {
  149. // CHECK:STDOUT: %Dest => constants.%Dest
  150. // CHECK:STDOUT: %Dest.patt => constants.%Dest
  151. // CHECK:STDOUT: }
  152. // CHECK:STDOUT:
  153. // CHECK:STDOUT: specific @ImplicitAs(@ImplicitAs.%Dest) {
  154. // CHECK:STDOUT: %Dest => constants.%Dest
  155. // CHECK:STDOUT: %Dest.patt => constants.%Dest
  156. // CHECK:STDOUT: }
  157. // CHECK:STDOUT:
  158. // CHECK:STDOUT: specific @ImplicitAs(@Convert.%Dest) {
  159. // CHECK:STDOUT: %Dest => constants.%Dest
  160. // CHECK:STDOUT: %Dest.patt => constants.%Dest
  161. // CHECK:STDOUT: }
  162. // CHECK:STDOUT:
  163. // CHECK:STDOUT: specific @Convert(constants.%Dest, constants.%Self.1) {
  164. // CHECK:STDOUT: %Dest => constants.%Dest
  165. // CHECK:STDOUT: %ImplicitAs.type => constants.%ImplicitAs.type.2
  166. // CHECK:STDOUT: %Self => constants.%Self.1
  167. // CHECK:STDOUT: }
  168. // CHECK:STDOUT:
  169. // CHECK:STDOUT: specific @ImplicitAs(i32) {
  170. // CHECK:STDOUT: %Dest => i32
  171. // CHECK:STDOUT: %Dest.patt => i32
  172. // CHECK:STDOUT:
  173. // CHECK:STDOUT: !definition:
  174. // CHECK:STDOUT: %ImplicitAs.type => constants.%ImplicitAs.type.3
  175. // CHECK:STDOUT: %Self => constants.%Self.2
  176. // CHECK:STDOUT: %Convert.type => constants.%Convert.type.2
  177. // CHECK:STDOUT: %Convert => constants.%Convert.2
  178. // CHECK:STDOUT: %.1 => constants.%.10
  179. // CHECK:STDOUT: %.2 => constants.%.11
  180. // CHECK:STDOUT: }
  181. // CHECK:STDOUT: