convert.carbon 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. // INCLUDE-FILE: toolchain/testing/testdata/min_prelude/int.carbon
  6. //
  7. // AUTOUPDATE
  8. // TIP: To test this file alone, run:
  9. // TIP: bazel test //toolchain/testing:file_test --test_arg=--file_tests=toolchain/check/testdata/interop/cpp/enum/convert.carbon
  10. // TIP: To dump output, run:
  11. // TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/interop/cpp/enum/convert.carbon
  12. // --- enum.h
  13. enum Enum : short { a, b, c };
  14. enum Other : short {};
  15. enum DifferentSize : long {};
  16. // --- convert_enum.carbon
  17. library "[[@TEST_NAME]]";
  18. import Cpp library "enum.h";
  19. //@dump-sem-ir-begin
  20. fn F() {
  21. let unused a: i16 = Cpp.Enum.a as i16;
  22. let unused b: Cpp.Enum = (42 as i16) as Cpp.Enum;
  23. let unused c: Cpp.Other = Cpp.a as Cpp.Other;
  24. }
  25. //@dump-sem-ir-end
  26. // --- fail_wrong_type.carbon
  27. library "[[@TEST_NAME]]";
  28. import Cpp library "enum.h";
  29. fn F() {
  30. // CHECK:STDERR: fail_wrong_type.carbon:[[@LINE+7]]:3: error: cannot convert expression of type `Cpp.Enum` to `i8` with `as` [ConversionFailure]
  31. // CHECK:STDERR: Cpp.Enum.a as i8;
  32. // CHECK:STDERR: ^~~~~~~~~~~~~~~~
  33. // CHECK:STDERR: fail_wrong_type.carbon:[[@LINE+4]]:3: note: type `Cpp.Enum` does not implement interface `Core.As(i8)` [MissingImplInMemberAccessNote]
  34. // CHECK:STDERR: Cpp.Enum.a as i8;
  35. // CHECK:STDERR: ^~~~~~~~~~~~~~~~
  36. // CHECK:STDERR:
  37. Cpp.Enum.a as i8;
  38. // CHECK:STDERR: fail_wrong_type.carbon:[[@LINE+7]]:3: error: cannot convert expression of type `Cpp.Enum` to `i32` with `as` [ConversionFailure]
  39. // CHECK:STDERR: Cpp.Enum.a as i32;
  40. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~
  41. // CHECK:STDERR: fail_wrong_type.carbon:[[@LINE+4]]:3: note: type `Cpp.Enum` does not implement interface `Core.As(i32)` [MissingImplInMemberAccessNote]
  42. // CHECK:STDERR: Cpp.Enum.a as i32;
  43. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~
  44. // CHECK:STDERR:
  45. Cpp.Enum.a as i32;
  46. // CHECK:STDERR: fail_wrong_type.carbon:[[@LINE+7]]:3: error: cannot convert expression of type `i8` to `Cpp.Enum` with `as` [ConversionFailure]
  47. // CHECK:STDERR: (42 as i8) as Cpp.Enum;
  48. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~
  49. // CHECK:STDERR: fail_wrong_type.carbon:[[@LINE+4]]:3: note: type `i8` does not implement interface `Core.As(Cpp.Enum)` [MissingImplInMemberAccessNote]
  50. // CHECK:STDERR: (42 as i8) as Cpp.Enum;
  51. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~
  52. // CHECK:STDERR:
  53. (42 as i8) as Cpp.Enum;
  54. // CHECK:STDERR: fail_wrong_type.carbon:[[@LINE+7]]:3: error: cannot convert expression of type `i32` to `Cpp.Enum` with `as` [ConversionFailure]
  55. // CHECK:STDERR: (42 as i32) as Cpp.Enum;
  56. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~
  57. // CHECK:STDERR: fail_wrong_type.carbon:[[@LINE+4]]:3: note: type `i32` does not implement interface `Core.As(Cpp.Enum)` [MissingImplInMemberAccessNote]
  58. // CHECK:STDERR: (42 as i32) as Cpp.Enum;
  59. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~
  60. // CHECK:STDERR:
  61. (42 as i32) as Cpp.Enum;
  62. // CHECK:STDERR: fail_wrong_type.carbon:[[@LINE+7]]:3: error: cannot convert expression of type `Cpp.Enum` to `Cpp.DifferentSize` with `as` [ConversionFailure]
  63. // CHECK:STDERR: Cpp.a as Cpp.DifferentSize;
  64. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~
  65. // CHECK:STDERR: fail_wrong_type.carbon:[[@LINE+4]]:3: note: type `Cpp.Enum` does not implement interface `Core.As(Cpp.DifferentSize)` [MissingImplInMemberAccessNote]
  66. // CHECK:STDERR: Cpp.a as Cpp.DifferentSize;
  67. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~
  68. // CHECK:STDERR:
  69. Cpp.a as Cpp.DifferentSize;
  70. }
  71. // CHECK:STDOUT: --- convert_enum.carbon
  72. // CHECK:STDOUT:
  73. // CHECK:STDOUT: constants {
  74. // CHECK:STDOUT: %F.type: type = fn_type @F [concrete]
  75. // CHECK:STDOUT: %F: %F.type = struct_value () [concrete]
  76. // CHECK:STDOUT: %int_16: Core.IntLiteral = int_value 16 [concrete]
  77. // CHECK:STDOUT: %i16: type = class_type @Int, @Int(%int_16) [concrete]
  78. // CHECK:STDOUT: %pattern_type.2f8: type = pattern_type %i16 [concrete]
  79. // CHECK:STDOUT: %Enum: type = class_type @Enum [concrete]
  80. // CHECK:STDOUT: %int_0.420: %Enum = int_value 0 [concrete]
  81. // CHECK:STDOUT: %int_0.320: %i16 = int_value 0 [concrete]
  82. // CHECK:STDOUT: %pattern_type.ebf: type = pattern_type %Enum [concrete]
  83. // CHECK:STDOUT: %int_42.20e: Core.IntLiteral = int_value 42 [concrete]
  84. // CHECK:STDOUT: %As.type.359: type = facet_type <@As, @As(%i16)> [concrete]
  85. // CHECK:STDOUT: %To: Core.IntLiteral = symbolic_binding To, 0 [symbolic]
  86. // CHECK:STDOUT: %Core.IntLiteral.as.As.impl.Convert.type.09e: type = fn_type @Core.IntLiteral.as.As.impl.Convert, @Core.IntLiteral.as.As.impl(%To) [symbolic]
  87. // CHECK:STDOUT: %Core.IntLiteral.as.As.impl.Convert.dbe: %Core.IntLiteral.as.As.impl.Convert.type.09e = struct_value () [symbolic]
  88. // CHECK:STDOUT: %As.impl_witness.b61: <witness> = impl_witness imports.%As.impl_witness_table.9fc, @Core.IntLiteral.as.As.impl(%int_16) [concrete]
  89. // CHECK:STDOUT: %Core.IntLiteral.as.As.impl.Convert.type.c60: type = fn_type @Core.IntLiteral.as.As.impl.Convert, @Core.IntLiteral.as.As.impl(%int_16) [concrete]
  90. // CHECK:STDOUT: %Core.IntLiteral.as.As.impl.Convert.a42: %Core.IntLiteral.as.As.impl.Convert.type.c60 = struct_value () [concrete]
  91. // CHECK:STDOUT: %As.facet: %As.type.359 = facet_value Core.IntLiteral, (%As.impl_witness.b61) [concrete]
  92. // CHECK:STDOUT: %As.WithSelf.Convert.type.2c0: type = fn_type @As.WithSelf.Convert, @As(%i16, %As.facet) [concrete]
  93. // CHECK:STDOUT: %.70c: type = fn_type_with_self_type %As.WithSelf.Convert.type.2c0, %As.facet [concrete]
  94. // CHECK:STDOUT: %Core.IntLiteral.as.As.impl.Convert.bound: <bound method> = bound_method %int_42.20e, %Core.IntLiteral.as.As.impl.Convert.a42 [concrete]
  95. // CHECK:STDOUT: %Core.IntLiteral.as.As.impl.Convert.specific_fn: <specific function> = specific_function %Core.IntLiteral.as.As.impl.Convert.a42, @Core.IntLiteral.as.As.impl.Convert(%int_16) [concrete]
  96. // CHECK:STDOUT: %bound_method: <bound method> = bound_method %int_42.20e, %Core.IntLiteral.as.As.impl.Convert.specific_fn [concrete]
  97. // CHECK:STDOUT: %int_42.a0e: %i16 = int_value 42 [concrete]
  98. // CHECK:STDOUT: %int_42.c3a: %Enum = int_value 42 [concrete]
  99. // CHECK:STDOUT: %Other: type = class_type @Other [concrete]
  100. // CHECK:STDOUT: %pattern_type.c06: type = pattern_type %Other [concrete]
  101. // CHECK:STDOUT: %int_0.fa2: %Other = int_value 0 [concrete]
  102. // CHECK:STDOUT: }
  103. // CHECK:STDOUT:
  104. // CHECK:STDOUT: imports {
  105. // CHECK:STDOUT: %Cpp: <namespace> = namespace file.%Cpp.import_cpp, [concrete] {
  106. // CHECK:STDOUT: .Enum = %Enum.decl
  107. // CHECK:STDOUT: .Other = %Other.decl
  108. // CHECK:STDOUT: .a = %int_0
  109. // CHECK:STDOUT: import Cpp//...
  110. // CHECK:STDOUT: }
  111. // CHECK:STDOUT: %Enum.decl: type = class_decl @Enum [concrete = constants.%Enum] {} {}
  112. // CHECK:STDOUT: %int_0: %Enum = int_value 0 [concrete = constants.%int_0.420]
  113. // CHECK:STDOUT: %Core.import_ref.ca0: @Core.IntLiteral.as.As.impl.%Core.IntLiteral.as.As.impl.Convert.type (%Core.IntLiteral.as.As.impl.Convert.type.09e) = import_ref Core//prelude/parts/int, loc{{\d+_\d+}}, loaded [symbolic = @Core.IntLiteral.as.As.impl.%Core.IntLiteral.as.As.impl.Convert (constants.%Core.IntLiteral.as.As.impl.Convert.dbe)]
  114. // CHECK:STDOUT: %As.impl_witness_table.9fc = impl_witness_table (%Core.import_ref.ca0), @Core.IntLiteral.as.As.impl [concrete]
  115. // CHECK:STDOUT: %Other.decl: type = class_decl @Other [concrete = constants.%Other] {} {}
  116. // CHECK:STDOUT: }
  117. // CHECK:STDOUT:
  118. // CHECK:STDOUT: file {
  119. // CHECK:STDOUT: %F.decl: %F.type = fn_decl @F [concrete = constants.%F] {} {}
  120. // CHECK:STDOUT: }
  121. // CHECK:STDOUT:
  122. // CHECK:STDOUT: fn @F() {
  123. // CHECK:STDOUT: !entry:
  124. // CHECK:STDOUT: name_binding_decl {
  125. // CHECK:STDOUT: %a.patt: %pattern_type.2f8 = value_binding_pattern a [concrete]
  126. // CHECK:STDOUT: }
  127. // CHECK:STDOUT: %Cpp.ref.loc8: <namespace> = name_ref Cpp, imports.%Cpp [concrete = imports.%Cpp]
  128. // CHECK:STDOUT: %Enum.ref.loc8: type = name_ref Enum, imports.%Enum.decl [concrete = constants.%Enum]
  129. // CHECK:STDOUT: %a.ref.loc8: %Enum = name_ref a, imports.%int_0 [concrete = constants.%int_0.420]
  130. // CHECK:STDOUT: %int_16.loc8_37: Core.IntLiteral = int_value 16 [concrete = constants.%int_16]
  131. // CHECK:STDOUT: %i16.loc8_37: type = class_type @Int, @Int(constants.%int_16) [concrete = constants.%i16]
  132. // CHECK:STDOUT: %.loc8_34.1: %i16 = as_compatible %a.ref.loc8 [concrete = constants.%int_0.320]
  133. // CHECK:STDOUT: %.loc8_34.2: %i16 = converted %a.ref.loc8, %.loc8_34.1 [concrete = constants.%int_0.320]
  134. // CHECK:STDOUT: %.loc8_17: type = splice_block %i16.loc8_17 [concrete = constants.%i16] {
  135. // CHECK:STDOUT: %int_16.loc8_17: Core.IntLiteral = int_value 16 [concrete = constants.%int_16]
  136. // CHECK:STDOUT: %i16.loc8_17: type = class_type @Int, @Int(constants.%int_16) [concrete = constants.%i16]
  137. // CHECK:STDOUT: }
  138. // CHECK:STDOUT: %a: %i16 = value_binding a, %.loc8_34.2
  139. // CHECK:STDOUT: name_binding_decl {
  140. // CHECK:STDOUT: %b.patt: %pattern_type.ebf = value_binding_pattern b [concrete]
  141. // CHECK:STDOUT: }
  142. // CHECK:STDOUT: %int_42: Core.IntLiteral = int_value 42 [concrete = constants.%int_42.20e]
  143. // CHECK:STDOUT: %int_16.loc9: Core.IntLiteral = int_value 16 [concrete = constants.%int_16]
  144. // CHECK:STDOUT: %i16.loc9: type = class_type @Int, @Int(constants.%int_16) [concrete = constants.%i16]
  145. // CHECK:STDOUT: %impl.elem0: %.70c = impl_witness_access constants.%As.impl_witness.b61, element0 [concrete = constants.%Core.IntLiteral.as.As.impl.Convert.a42]
  146. // CHECK:STDOUT: %bound_method.loc9_32.1: <bound method> = bound_method %int_42, %impl.elem0 [concrete = constants.%Core.IntLiteral.as.As.impl.Convert.bound]
  147. // CHECK:STDOUT: %specific_fn: <specific function> = specific_function %impl.elem0, @Core.IntLiteral.as.As.impl.Convert(constants.%int_16) [concrete = constants.%Core.IntLiteral.as.As.impl.Convert.specific_fn]
  148. // CHECK:STDOUT: %bound_method.loc9_32.2: <bound method> = bound_method %int_42, %specific_fn [concrete = constants.%bound_method]
  149. // CHECK:STDOUT: %Core.IntLiteral.as.As.impl.Convert.call: init %i16 = call %bound_method.loc9_32.2(%int_42) [concrete = constants.%int_42.a0e]
  150. // CHECK:STDOUT: %.loc9_32.1: %i16 = value_of_initializer %Core.IntLiteral.as.As.impl.Convert.call [concrete = constants.%int_42.a0e]
  151. // CHECK:STDOUT: %.loc9_32.2: %i16 = converted %int_42, %.loc9_32.1 [concrete = constants.%int_42.a0e]
  152. // CHECK:STDOUT: %Cpp.ref.loc9_43: <namespace> = name_ref Cpp, imports.%Cpp [concrete = imports.%Cpp]
  153. // CHECK:STDOUT: %Enum.ref.loc9_46: type = name_ref Enum, imports.%Enum.decl [concrete = constants.%Enum]
  154. // CHECK:STDOUT: %.loc9_40.1: %Enum = as_compatible %.loc9_32.2 [concrete = constants.%int_42.c3a]
  155. // CHECK:STDOUT: %.loc9_40.2: %Enum = converted %.loc9_32.2, %.loc9_40.1 [concrete = constants.%int_42.c3a]
  156. // CHECK:STDOUT: %.loc9_20: type = splice_block %Enum.ref.loc9_20 [concrete = constants.%Enum] {
  157. // CHECK:STDOUT: %Cpp.ref.loc9_17: <namespace> = name_ref Cpp, imports.%Cpp [concrete = imports.%Cpp]
  158. // CHECK:STDOUT: %Enum.ref.loc9_20: type = name_ref Enum, imports.%Enum.decl [concrete = constants.%Enum]
  159. // CHECK:STDOUT: }
  160. // CHECK:STDOUT: %b: %Enum = value_binding b, %.loc9_40.2
  161. // CHECK:STDOUT: name_binding_decl {
  162. // CHECK:STDOUT: %c.patt: %pattern_type.c06 = value_binding_pattern c [concrete]
  163. // CHECK:STDOUT: }
  164. // CHECK:STDOUT: %Cpp.ref.loc10_29: <namespace> = name_ref Cpp, imports.%Cpp [concrete = imports.%Cpp]
  165. // CHECK:STDOUT: %a.ref.loc10: %Enum = name_ref a, imports.%int_0 [concrete = constants.%int_0.420]
  166. // CHECK:STDOUT: %Cpp.ref.loc10_38: <namespace> = name_ref Cpp, imports.%Cpp [concrete = imports.%Cpp]
  167. // CHECK:STDOUT: %Other.ref.loc10_41: type = name_ref Other, imports.%Other.decl [concrete = constants.%Other]
  168. // CHECK:STDOUT: %.loc10_35.1: %Other = as_compatible %a.ref.loc10 [concrete = constants.%int_0.fa2]
  169. // CHECK:STDOUT: %.loc10_35.2: %Other = converted %a.ref.loc10, %.loc10_35.1 [concrete = constants.%int_0.fa2]
  170. // CHECK:STDOUT: %.loc10_20: type = splice_block %Other.ref.loc10_20 [concrete = constants.%Other] {
  171. // CHECK:STDOUT: %Cpp.ref.loc10_17: <namespace> = name_ref Cpp, imports.%Cpp [concrete = imports.%Cpp]
  172. // CHECK:STDOUT: %Other.ref.loc10_20: type = name_ref Other, imports.%Other.decl [concrete = constants.%Other]
  173. // CHECK:STDOUT: }
  174. // CHECK:STDOUT: %c: %Other = value_binding c, %.loc10_35.2
  175. // CHECK:STDOUT: return
  176. // CHECK:STDOUT: }
  177. // CHECK:STDOUT: