fail_no_conversion.carbon 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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/as/fail_no_conversion.carbon
  8. // TIP: To dump output, run:
  9. // TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/as/fail_no_conversion.carbon
  10. // CHECK:STDERR: fail_no_conversion.carbon:[[@LINE+6]]:21: error: cannot convert from `i32` to `(i32, i32)` with `as`
  11. // CHECK:STDERR: let n: (i32, i32) = 1 as (i32, i32);
  12. // CHECK:STDERR: ^~~~~~~~~~~~~~~
  13. // CHECK:STDERR: fail_no_conversion.carbon:[[@LINE+3]]:21: note: type `i32` does not implement interface `As`
  14. // CHECK:STDERR: let n: (i32, i32) = 1 as (i32, i32);
  15. // CHECK:STDERR: ^~~~~~~~~~~~~~~
  16. let n: (i32, i32) = 1 as (i32, i32);
  17. // CHECK:STDOUT: --- fail_no_conversion.carbon
  18. // CHECK:STDOUT:
  19. // CHECK:STDOUT: constants {
  20. // CHECK:STDOUT: %Int32.type: type = fn_type @Int32 [template]
  21. // CHECK:STDOUT: %.1: type = tuple_type () [template]
  22. // CHECK:STDOUT: %Int32: %Int32.type = struct_value () [template]
  23. // CHECK:STDOUT: %.2: type = tuple_type (type, type) [template]
  24. // CHECK:STDOUT: %.3: type = tuple_type (i32, i32) [template]
  25. // CHECK:STDOUT: %.4: type = ptr_type %.3 [template]
  26. // CHECK:STDOUT: %.5: i32 = int_literal 1 [template]
  27. // CHECK:STDOUT: %As.type: type = generic_interface_type @As [template]
  28. // CHECK:STDOUT: %As: %As.type = struct_value () [template]
  29. // CHECK:STDOUT: %Dest: type = bind_symbolic_name Dest 0 [symbolic]
  30. // CHECK:STDOUT: %.6: type = interface_type @As, @As(%Dest) [symbolic]
  31. // CHECK:STDOUT: %Self.1: @As.%.1 (%.6) = bind_symbolic_name Self 1 [symbolic]
  32. // CHECK:STDOUT: %Self.2: %.6 = bind_symbolic_name Self 1 [symbolic]
  33. // CHECK:STDOUT: %Convert.type.1: type = fn_type @Convert, @As(%Dest) [symbolic]
  34. // CHECK:STDOUT: %Convert.1: %Convert.type.1 = struct_value () [symbolic]
  35. // CHECK:STDOUT: %.7: type = assoc_entity_type %.6, %Convert.type.1 [symbolic]
  36. // CHECK:STDOUT: %.8: %.7 = assoc_entity element0, imports.%import_ref.6 [symbolic]
  37. // CHECK:STDOUT: %.9: type = interface_type @As, @As(%.3) [template]
  38. // CHECK:STDOUT: %Convert.type.2: type = fn_type @Convert, @As(%.3) [template]
  39. // CHECK:STDOUT: %Convert.2: %Convert.type.2 = struct_value () [template]
  40. // CHECK:STDOUT: %.10: type = assoc_entity_type %.9, %Convert.type.2 [template]
  41. // CHECK:STDOUT: %.11: %.10 = assoc_entity element0, imports.%import_ref.6 [template]
  42. // CHECK:STDOUT: %.12: %.7 = assoc_entity element0, imports.%import_ref.7 [symbolic]
  43. // CHECK:STDOUT: }
  44. // CHECK:STDOUT:
  45. // CHECK:STDOUT: imports {
  46. // CHECK:STDOUT: %Core: <namespace> = namespace file.%Core.import, [template] {
  47. // CHECK:STDOUT: .Int32 = %import_ref.1
  48. // CHECK:STDOUT: .As = %import_ref.2
  49. // CHECK:STDOUT: import Core//prelude
  50. // CHECK:STDOUT: import Core//prelude/operators
  51. // CHECK:STDOUT: import Core//prelude/types
  52. // CHECK:STDOUT: import Core//prelude/operators/arithmetic
  53. // CHECK:STDOUT: import Core//prelude/operators/as
  54. // CHECK:STDOUT: import Core//prelude/operators/bitwise
  55. // CHECK:STDOUT: import Core//prelude/operators/comparison
  56. // CHECK:STDOUT: import Core//prelude/types/bool
  57. // CHECK:STDOUT: }
  58. // CHECK:STDOUT: %import_ref.1: %Int32.type = import_ref Core//prelude/types, inst+4, loaded [template = constants.%Int32]
  59. // CHECK:STDOUT: %import_ref.2: %As.type = import_ref Core//prelude/operators/as, inst+5, loaded [template = constants.%As]
  60. // CHECK:STDOUT: %import_ref.3 = import_ref Core//prelude/operators/as, inst+11, unloaded
  61. // CHECK:STDOUT: %import_ref.4: @As.%.2 (%.7) = import_ref Core//prelude/operators/as, inst+29, loaded [symbolic = @As.%.3 (constants.%.12)]
  62. // CHECK:STDOUT: %import_ref.5 = import_ref Core//prelude/operators/as, inst+22, unloaded
  63. // CHECK:STDOUT: %import_ref.6 = import_ref Core//prelude/operators/as, inst+22, unloaded
  64. // CHECK:STDOUT: %import_ref.7 = import_ref Core//prelude/operators/as, inst+22, unloaded
  65. // CHECK:STDOUT: }
  66. // CHECK:STDOUT:
  67. // CHECK:STDOUT: file {
  68. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  69. // CHECK:STDOUT: .Core = imports.%Core
  70. // CHECK:STDOUT: .n = @__global_init.%n
  71. // CHECK:STDOUT: }
  72. // CHECK:STDOUT: %Core.import = import Core
  73. // CHECK:STDOUT: %int.make_type_32.loc17_9: init type = call constants.%Int32() [template = i32]
  74. // CHECK:STDOUT: %int.make_type_32.loc17_14: init type = call constants.%Int32() [template = i32]
  75. // CHECK:STDOUT: %.loc17_17.1: %.2 = tuple_literal (%int.make_type_32.loc17_9, %int.make_type_32.loc17_14)
  76. // CHECK:STDOUT: %.loc17_17.2: type = value_of_initializer %int.make_type_32.loc17_9 [template = i32]
  77. // CHECK:STDOUT: %.loc17_17.3: type = converted %int.make_type_32.loc17_9, %.loc17_17.2 [template = i32]
  78. // CHECK:STDOUT: %.loc17_17.4: type = value_of_initializer %int.make_type_32.loc17_14 [template = i32]
  79. // CHECK:STDOUT: %.loc17_17.5: type = converted %int.make_type_32.loc17_14, %.loc17_17.4 [template = i32]
  80. // CHECK:STDOUT: %.loc17_17.6: type = converted %.loc17_17.1, constants.%.3 [template = constants.%.3]
  81. // CHECK:STDOUT: }
  82. // CHECK:STDOUT:
  83. // CHECK:STDOUT: generic interface @As(constants.%Dest: type) {
  84. // CHECK:STDOUT: %Dest: type = bind_symbolic_name Dest 0 [symbolic = %Dest (constants.%Dest)]
  85. // CHECK:STDOUT:
  86. // CHECK:STDOUT: !definition:
  87. // CHECK:STDOUT: %.1: type = interface_type @As, @As(%Dest) [symbolic = %.1 (constants.%.6)]
  88. // CHECK:STDOUT: %Self: %.6 = bind_symbolic_name Self 1 [symbolic = %Self (constants.%Self.2)]
  89. // CHECK:STDOUT: %Convert.type: type = fn_type @Convert, @As(%Dest) [symbolic = %Convert.type (constants.%Convert.type.1)]
  90. // CHECK:STDOUT: %Convert: @As.%Convert.type (%Convert.type.1) = struct_value () [symbolic = %Convert (constants.%Convert.1)]
  91. // CHECK:STDOUT: %.2: type = assoc_entity_type @As.%.1 (%.6), @As.%Convert.type (%Convert.type.1) [symbolic = %.2 (constants.%.7)]
  92. // CHECK:STDOUT: %.3: @As.%.2 (%.7) = assoc_entity element0, imports.%import_ref.6 [symbolic = %.3 (constants.%.8)]
  93. // CHECK:STDOUT:
  94. // CHECK:STDOUT: interface {
  95. // CHECK:STDOUT: !members:
  96. // CHECK:STDOUT: .Self = imports.%import_ref.3
  97. // CHECK:STDOUT: .Convert = imports.%import_ref.4
  98. // CHECK:STDOUT: witness = (imports.%import_ref.5)
  99. // CHECK:STDOUT: }
  100. // CHECK:STDOUT: }
  101. // CHECK:STDOUT:
  102. // CHECK:STDOUT: fn @Int32() -> type = "int.make_type_32";
  103. // CHECK:STDOUT:
  104. // CHECK:STDOUT: generic fn @Convert(constants.%Dest: type, constants.%Self.1: @As.%.1 (%.6)) {
  105. // CHECK:STDOUT: %Dest: type = bind_symbolic_name Dest 0 [symbolic = %Dest (constants.%Dest)]
  106. // CHECK:STDOUT: %.1: type = interface_type @As, @As(%Dest) [symbolic = %.1 (constants.%.6)]
  107. // CHECK:STDOUT: %Self: %.6 = bind_symbolic_name Self 1 [symbolic = %Self (constants.%Self.2)]
  108. // CHECK:STDOUT:
  109. // CHECK:STDOUT: fn[%self: @Convert.%Self (%Self.2)]() -> @Convert.%Dest (%Dest);
  110. // CHECK:STDOUT: }
  111. // CHECK:STDOUT:
  112. // CHECK:STDOUT: fn @__global_init() {
  113. // CHECK:STDOUT: !entry:
  114. // CHECK:STDOUT: %.loc17_21: i32 = int_literal 1 [template = constants.%.5]
  115. // CHECK:STDOUT: %int.make_type_32.loc17_27: init type = call constants.%Int32() [template = i32]
  116. // CHECK:STDOUT: %int.make_type_32.loc17_32: init type = call constants.%Int32() [template = i32]
  117. // CHECK:STDOUT: %.loc17_35.1: %.2 = tuple_literal (%int.make_type_32.loc17_27, %int.make_type_32.loc17_32)
  118. // CHECK:STDOUT: %.loc17_35.2: type = value_of_initializer %int.make_type_32.loc17_27 [template = i32]
  119. // CHECK:STDOUT: %.loc17_35.3: type = converted %int.make_type_32.loc17_27, %.loc17_35.2 [template = i32]
  120. // CHECK:STDOUT: %.loc17_35.4: type = value_of_initializer %int.make_type_32.loc17_32 [template = i32]
  121. // CHECK:STDOUT: %.loc17_35.5: type = converted %int.make_type_32.loc17_32, %.loc17_35.4 [template = i32]
  122. // CHECK:STDOUT: %.loc17_35.6: type = converted %.loc17_35.1, constants.%.3 [template = constants.%.3]
  123. // CHECK:STDOUT: %.loc17_23.1: type = interface_type @As, @As(constants.%.3) [template = constants.%.9]
  124. // CHECK:STDOUT: %.loc17_23.2: %.10 = specific_constant imports.%import_ref.4, @As(constants.%.3) [template = constants.%.11]
  125. // CHECK:STDOUT: %Convert.ref: %.10 = name_ref Convert, %.loc17_23.2 [template = constants.%.11]
  126. // CHECK:STDOUT: %.loc17_23.3: %.3 = converted %.loc17_21, <error> [template = <error>]
  127. // CHECK:STDOUT: %.loc17_23.4: i32 = tuple_access %.loc17_23.3, element0 [template = <error>]
  128. // CHECK:STDOUT: %n: %.3 = bind_name n, <error>
  129. // CHECK:STDOUT: return
  130. // CHECK:STDOUT: }
  131. // CHECK:STDOUT:
  132. // CHECK:STDOUT: specific @As(constants.%Dest) {
  133. // CHECK:STDOUT: %Dest => constants.%Dest
  134. // CHECK:STDOUT: }
  135. // CHECK:STDOUT:
  136. // CHECK:STDOUT: specific @As(@As.%Dest) {
  137. // CHECK:STDOUT: %Dest => constants.%Dest
  138. // CHECK:STDOUT: }
  139. // CHECK:STDOUT:
  140. // CHECK:STDOUT: specific @As(@Convert.%Dest) {
  141. // CHECK:STDOUT: %Dest => constants.%Dest
  142. // CHECK:STDOUT: }
  143. // CHECK:STDOUT:
  144. // CHECK:STDOUT: specific @Convert(constants.%Dest, constants.%Self.1) {
  145. // CHECK:STDOUT: %Dest => constants.%Dest
  146. // CHECK:STDOUT: %.1 => constants.%.6
  147. // CHECK:STDOUT: %Self => constants.%Self.1
  148. // CHECK:STDOUT: }
  149. // CHECK:STDOUT:
  150. // CHECK:STDOUT: specific @As(constants.%.3) {
  151. // CHECK:STDOUT: %Dest => constants.%.3
  152. // CHECK:STDOUT:
  153. // CHECK:STDOUT: !definition:
  154. // CHECK:STDOUT: %.1 => constants.%.9
  155. // CHECK:STDOUT: %Self => constants.%Self.2
  156. // CHECK:STDOUT: %Convert.type => constants.%Convert.type.2
  157. // CHECK:STDOUT: %Convert => constants.%Convert.2
  158. // CHECK:STDOUT: %.2 => constants.%.10
  159. // CHECK:STDOUT: %.3 => constants.%.11
  160. // CHECK:STDOUT: }
  161. // CHECK:STDOUT: