convert.carbon 9.3 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. // INCLUDE-FILE: toolchain/testing/testdata/min_prelude/convert.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/named_constraint/convert.carbon
  10. // TIP: To dump output, run:
  11. // TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/named_constraint/convert.carbon
  12. // --- type_and_with_named_constraint_self.carbon
  13. library "[[@TEST_NAME]]";
  14. constraint E {}
  15. fn F(T:! E) {}
  16. //@dump-sem-ir-begin
  17. fn G(T:! E & E) {
  18. F(T);
  19. }
  20. //@dump-sem-ir-end
  21. // --- compatible_constraints.carbon
  22. library "[[@TEST_NAME]]";
  23. constraint E1 {}
  24. constraint E2 {}
  25. fn F(T:! E1) {}
  26. //@dump-sem-ir-begin
  27. fn G(T:! E2) {
  28. F(T);
  29. }
  30. //@dump-sem-ir-end
  31. // --- unspecified.carbon
  32. library "[[@TEST_NAME]]";
  33. interface Z {}
  34. constraint E {
  35. require impls Z;
  36. }
  37. fn F(T:! E) {}
  38. class C {}
  39. impl C as Z {}
  40. fn G(T:! Z) {
  41. // Anything that impls Z also impls E, since that's all it requires. Nothing
  42. // needs to specify that T impls E.
  43. F(T);
  44. // Same for concrete types.
  45. F(C);
  46. }
  47. // CHECK:STDOUT: --- type_and_with_named_constraint_self.carbon
  48. // CHECK:STDOUT:
  49. // CHECK:STDOUT: constants {
  50. // CHECK:STDOUT: %E.type: type = facet_type <@E> [concrete]
  51. // CHECK:STDOUT: %T: %E.type = symbolic_binding T, 0 [symbolic]
  52. // CHECK:STDOUT: %pattern_type.9a5: type = pattern_type %E.type [concrete]
  53. // CHECK:STDOUT: %F.type: type = fn_type @F [concrete]
  54. // CHECK:STDOUT: %empty_tuple.type: type = tuple_type () [concrete]
  55. // CHECK:STDOUT: %F: %F.type = struct_value () [concrete]
  56. // CHECK:STDOUT: %BitAndWith.type.b10: type = facet_type <@BitAndWith, @BitAndWith(type)> [concrete]
  57. // CHECK:STDOUT: %BitAndWith.Op.type.9a3: type = fn_type @BitAndWith.Op, @BitAndWith(type) [concrete]
  58. // CHECK:STDOUT: %BitAndWith.impl_witness: <witness> = impl_witness imports.%BitAndWith.impl_witness_table [concrete]
  59. // CHECK:STDOUT: %BitAndWith.facet: %BitAndWith.type.b10 = facet_value type, (%BitAndWith.impl_witness) [concrete]
  60. // CHECK:STDOUT: %.890: type = fn_type_with_self_type %BitAndWith.Op.type.9a3, %BitAndWith.facet [concrete]
  61. // CHECK:STDOUT: %type.as.BitAndWith.impl.Op.type: type = fn_type @type.as.BitAndWith.impl.Op [concrete]
  62. // CHECK:STDOUT: %type.as.BitAndWith.impl.Op: %type.as.BitAndWith.impl.Op.type = struct_value () [concrete]
  63. // CHECK:STDOUT: %type.as.BitAndWith.impl.Op.bound: <bound method> = bound_method %E.type, %type.as.BitAndWith.impl.Op [concrete]
  64. // CHECK:STDOUT: %G.type: type = fn_type @G [concrete]
  65. // CHECK:STDOUT: %G: %G.type = struct_value () [concrete]
  66. // CHECK:STDOUT: %F.specific_fn: <specific function> = specific_function %F, @F(%T) [symbolic]
  67. // CHECK:STDOUT: }
  68. // CHECK:STDOUT:
  69. // CHECK:STDOUT: imports {
  70. // CHECK:STDOUT: %Core.import_ref.8d3: %type.as.BitAndWith.impl.Op.type = import_ref Core//prelude/parts/as, loc{{\d+_\d+}}, loaded [concrete = constants.%type.as.BitAndWith.impl.Op]
  71. // CHECK:STDOUT: %BitAndWith.impl_witness_table = impl_witness_table (%Core.import_ref.8d3), @type.as.BitAndWith.impl [concrete]
  72. // CHECK:STDOUT: }
  73. // CHECK:STDOUT:
  74. // CHECK:STDOUT: file {
  75. // CHECK:STDOUT: %G.decl: %G.type = fn_decl @G [concrete = constants.%G] {
  76. // CHECK:STDOUT: %T.patt: %pattern_type.9a5 = symbolic_binding_pattern T, 0 [concrete]
  77. // CHECK:STDOUT: } {
  78. // CHECK:STDOUT: %.loc8_12.1: type = splice_block %.loc8_12.3 [concrete = constants.%E.type] {
  79. // CHECK:STDOUT: <elided>
  80. // CHECK:STDOUT: %E.ref.loc8_10: type = name_ref E, file.%E.decl [concrete = constants.%E.type]
  81. // CHECK:STDOUT: %E.ref.loc8_14: type = name_ref E, file.%E.decl [concrete = constants.%E.type]
  82. // CHECK:STDOUT: %impl.elem0: %.890 = impl_witness_access constants.%BitAndWith.impl_witness, element0 [concrete = constants.%type.as.BitAndWith.impl.Op]
  83. // CHECK:STDOUT: %bound_method: <bound method> = bound_method %E.ref.loc8_10, %impl.elem0 [concrete = constants.%type.as.BitAndWith.impl.Op.bound]
  84. // CHECK:STDOUT: %type.as.BitAndWith.impl.Op.call: init type = call %bound_method(%E.ref.loc8_10, %E.ref.loc8_14) [concrete = constants.%E.type]
  85. // CHECK:STDOUT: %.loc8_12.2: type = value_of_initializer %type.as.BitAndWith.impl.Op.call [concrete = constants.%E.type]
  86. // CHECK:STDOUT: %.loc8_12.3: type = converted %type.as.BitAndWith.impl.Op.call, %.loc8_12.2 [concrete = constants.%E.type]
  87. // CHECK:STDOUT: }
  88. // CHECK:STDOUT: %T.loc8_6.2: %E.type = symbolic_binding T, 0 [symbolic = %T.loc8_6.1 (constants.%T)]
  89. // CHECK:STDOUT: }
  90. // CHECK:STDOUT: }
  91. // CHECK:STDOUT:
  92. // CHECK:STDOUT: generic fn @G(%T.loc8_6.2: %E.type) {
  93. // CHECK:STDOUT: %T.loc8_6.1: %E.type = symbolic_binding T, 0 [symbolic = %T.loc8_6.1 (constants.%T)]
  94. // CHECK:STDOUT:
  95. // CHECK:STDOUT: !definition:
  96. // CHECK:STDOUT: %F.specific_fn.loc9_3.2: <specific function> = specific_function constants.%F, @F(%T.loc8_6.1) [symbolic = %F.specific_fn.loc9_3.2 (constants.%F.specific_fn)]
  97. // CHECK:STDOUT:
  98. // CHECK:STDOUT: fn() {
  99. // CHECK:STDOUT: !entry:
  100. // CHECK:STDOUT: %F.ref: %F.type = name_ref F, file.%F.decl [concrete = constants.%F]
  101. // CHECK:STDOUT: %T.ref: %E.type = name_ref T, %T.loc8_6.2 [symbolic = %T.loc8_6.1 (constants.%T)]
  102. // CHECK:STDOUT: %F.specific_fn.loc9_3.1: <specific function> = specific_function %F.ref, @F(constants.%T) [symbolic = %F.specific_fn.loc9_3.2 (constants.%F.specific_fn)]
  103. // CHECK:STDOUT: %F.call: init %empty_tuple.type = call %F.specific_fn.loc9_3.1()
  104. // CHECK:STDOUT: return
  105. // CHECK:STDOUT: }
  106. // CHECK:STDOUT: }
  107. // CHECK:STDOUT:
  108. // CHECK:STDOUT: specific @G(constants.%T) {
  109. // CHECK:STDOUT: %T.loc8_6.1 => constants.%T
  110. // CHECK:STDOUT: }
  111. // CHECK:STDOUT:
  112. // CHECK:STDOUT: --- compatible_constraints.carbon
  113. // CHECK:STDOUT:
  114. // CHECK:STDOUT: constants {
  115. // CHECK:STDOUT: %E1.type: type = facet_type <@E1> [concrete]
  116. // CHECK:STDOUT: %E2.type: type = facet_type <@E2> [concrete]
  117. // CHECK:STDOUT: %F.type: type = fn_type @F [concrete]
  118. // CHECK:STDOUT: %empty_tuple.type: type = tuple_type () [concrete]
  119. // CHECK:STDOUT: %F: %F.type = struct_value () [concrete]
  120. // CHECK:STDOUT: %T.f45530.2: %E2.type = symbolic_binding T, 0 [symbolic]
  121. // CHECK:STDOUT: %pattern_type.9a587c.2: type = pattern_type %E2.type [concrete]
  122. // CHECK:STDOUT: %G.type: type = fn_type @G [concrete]
  123. // CHECK:STDOUT: %G: %G.type = struct_value () [concrete]
  124. // CHECK:STDOUT: %T.binding.as_type: type = symbolic_binding_type T, 0, %T.f45530.2 [symbolic]
  125. // CHECK:STDOUT: %E1.facet: %E1.type = facet_value %T.binding.as_type, () [symbolic]
  126. // CHECK:STDOUT: %F.specific_fn: <specific function> = specific_function %F, @F(%E1.facet) [symbolic]
  127. // CHECK:STDOUT: }
  128. // CHECK:STDOUT:
  129. // CHECK:STDOUT: imports {
  130. // CHECK:STDOUT: }
  131. // CHECK:STDOUT:
  132. // CHECK:STDOUT: file {
  133. // CHECK:STDOUT: %G.decl: %G.type = fn_decl @G [concrete = constants.%G] {
  134. // CHECK:STDOUT: %T.patt: %pattern_type.9a587c.2 = symbolic_binding_pattern T, 0 [concrete]
  135. // CHECK:STDOUT: } {
  136. // CHECK:STDOUT: %.loc9: type = splice_block %E2.ref [concrete = constants.%E2.type] {
  137. // CHECK:STDOUT: <elided>
  138. // CHECK:STDOUT: %E2.ref: type = name_ref E2, file.%E2.decl [concrete = constants.%E2.type]
  139. // CHECK:STDOUT: }
  140. // CHECK:STDOUT: %T.loc9_6.2: %E2.type = symbolic_binding T, 0 [symbolic = %T.loc9_6.1 (constants.%T.f45530.2)]
  141. // CHECK:STDOUT: }
  142. // CHECK:STDOUT: }
  143. // CHECK:STDOUT:
  144. // CHECK:STDOUT: generic fn @G(%T.loc9_6.2: %E2.type) {
  145. // CHECK:STDOUT: %T.loc9_6.1: %E2.type = symbolic_binding T, 0 [symbolic = %T.loc9_6.1 (constants.%T.f45530.2)]
  146. // CHECK:STDOUT:
  147. // CHECK:STDOUT: !definition:
  148. // CHECK:STDOUT: %T.binding.as_type: type = symbolic_binding_type T, 0, %T.loc9_6.1 [symbolic = %T.binding.as_type (constants.%T.binding.as_type)]
  149. // CHECK:STDOUT: %E1.facet.loc10_6.2: %E1.type = facet_value %T.binding.as_type, () [symbolic = %E1.facet.loc10_6.2 (constants.%E1.facet)]
  150. // CHECK:STDOUT: %F.specific_fn.loc10_3.2: <specific function> = specific_function constants.%F, @F(%E1.facet.loc10_6.2) [symbolic = %F.specific_fn.loc10_3.2 (constants.%F.specific_fn)]
  151. // CHECK:STDOUT:
  152. // CHECK:STDOUT: fn() {
  153. // CHECK:STDOUT: !entry:
  154. // CHECK:STDOUT: %F.ref: %F.type = name_ref F, file.%F.decl [concrete = constants.%F]
  155. // CHECK:STDOUT: %T.ref: %E2.type = name_ref T, %T.loc9_6.2 [symbolic = %T.loc9_6.1 (constants.%T.f45530.2)]
  156. // CHECK:STDOUT: %T.as_type: type = facet_access_type %T.ref [symbolic = %T.binding.as_type (constants.%T.binding.as_type)]
  157. // CHECK:STDOUT: %E1.facet.loc10_6.1: %E1.type = facet_value %T.as_type, () [symbolic = %E1.facet.loc10_6.2 (constants.%E1.facet)]
  158. // CHECK:STDOUT: %.loc10: %E1.type = converted %T.ref, %E1.facet.loc10_6.1 [symbolic = %E1.facet.loc10_6.2 (constants.%E1.facet)]
  159. // CHECK:STDOUT: %F.specific_fn.loc10_3.1: <specific function> = specific_function %F.ref, @F(constants.%E1.facet) [symbolic = %F.specific_fn.loc10_3.2 (constants.%F.specific_fn)]
  160. // CHECK:STDOUT: %F.call: init %empty_tuple.type = call %F.specific_fn.loc10_3.1()
  161. // CHECK:STDOUT: return
  162. // CHECK:STDOUT: }
  163. // CHECK:STDOUT: }
  164. // CHECK:STDOUT:
  165. // CHECK:STDOUT: specific @G(constants.%T.f45530.2) {
  166. // CHECK:STDOUT: %T.loc9_6.1 => constants.%T.f45530.2
  167. // CHECK:STDOUT: }
  168. // CHECK:STDOUT: