convert.carbon 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. // --- fail_todo_unspecified.carbon
  32. library "[[@TEST_NAME]]";
  33. interface Z {}
  34. constraint E {
  35. // CHECK:STDERR: fail_todo_unspecified.carbon:[[@LINE+4]]:3: error: semantics TODO: `require` [SemanticsTodo]
  36. // CHECK:STDERR: require impls Z;
  37. // CHECK:STDERR: ^~~~~~~
  38. // CHECK:STDERR:
  39. require impls Z;
  40. }
  41. fn F(T:! E) {}
  42. class C {}
  43. impl C as Z {}
  44. fn G(T:! Z) {
  45. // Anything that impls Z also impls E, since that's all it requires. Nothing
  46. // needs to specify that T impls E.
  47. F(T);
  48. // Same for concrete types.
  49. F(C);
  50. }
  51. // CHECK:STDOUT: --- type_and_with_named_constraint_self.carbon
  52. // CHECK:STDOUT:
  53. // CHECK:STDOUT: constants {
  54. // CHECK:STDOUT: %type: type = facet_type <type> [concrete]
  55. // CHECK:STDOUT: %T: %type = bind_symbolic_name T, 0 [symbolic]
  56. // CHECK:STDOUT: %pattern_type.e25: type = pattern_type %type [concrete]
  57. // CHECK:STDOUT: %F.type: type = fn_type @F [concrete]
  58. // CHECK:STDOUT: %empty_tuple.type: type = tuple_type () [concrete]
  59. // CHECK:STDOUT: %F: %F.type = struct_value () [concrete]
  60. // CHECK:STDOUT: %BitAndWith.type.8a6: type = facet_type <@BitAndWith, @BitAndWith(type)> [concrete]
  61. // CHECK:STDOUT: %BitAndWith.Op.type.9a3: type = fn_type @BitAndWith.Op, @BitAndWith(type) [concrete]
  62. // CHECK:STDOUT: %BitAndWith.impl_witness: <witness> = impl_witness imports.%BitAndWith.impl_witness_table [concrete]
  63. // CHECK:STDOUT: %BitAndWith.facet: %BitAndWith.type.8a6 = facet_value type, (%BitAndWith.impl_witness) [concrete]
  64. // CHECK:STDOUT: %.fa7: type = fn_type_with_self_type %BitAndWith.Op.type.9a3, %BitAndWith.facet [concrete]
  65. // CHECK:STDOUT: %type.as.BitAndWith.impl.Op.type: type = fn_type @type.as.BitAndWith.impl.Op [concrete]
  66. // CHECK:STDOUT: %type.as.BitAndWith.impl.Op: %type.as.BitAndWith.impl.Op.type = struct_value () [concrete]
  67. // CHECK:STDOUT: %type.as.BitAndWith.impl.Op.bound: <bound method> = bound_method %type, %type.as.BitAndWith.impl.Op [concrete]
  68. // CHECK:STDOUT: %G.type: type = fn_type @G [concrete]
  69. // CHECK:STDOUT: %G: %G.type = struct_value () [concrete]
  70. // CHECK:STDOUT: %F.specific_fn: <specific function> = specific_function %F, @F(%T) [symbolic]
  71. // CHECK:STDOUT: }
  72. // CHECK:STDOUT:
  73. // CHECK:STDOUT: imports {
  74. // CHECK:STDOUT: %Core.import_ref.636: %type.as.BitAndWith.impl.Op.type = import_ref Core//prelude/parts/as, loc{{\d+_\d+}}, loaded [concrete = constants.%type.as.BitAndWith.impl.Op]
  75. // CHECK:STDOUT: %BitAndWith.impl_witness_table = impl_witness_table (%Core.import_ref.636), @type.as.BitAndWith.impl [concrete]
  76. // CHECK:STDOUT: }
  77. // CHECK:STDOUT:
  78. // CHECK:STDOUT: file {
  79. // CHECK:STDOUT: %G.decl: %G.type = fn_decl @G [concrete = constants.%G] {
  80. // CHECK:STDOUT: %T.patt: %pattern_type.e25 = symbolic_binding_pattern T, 0 [concrete]
  81. // CHECK:STDOUT: } {
  82. // CHECK:STDOUT: %.loc8_12.1: type = splice_block %.loc8_12.3 [concrete = constants.%type] {
  83. // CHECK:STDOUT: <elided>
  84. // CHECK:STDOUT: %E.ref.loc8_10: type = name_ref E, file.%E.decl [concrete = constants.%type]
  85. // CHECK:STDOUT: %E.ref.loc8_14: type = name_ref E, file.%E.decl [concrete = constants.%type]
  86. // CHECK:STDOUT: %impl.elem0: %.fa7 = impl_witness_access constants.%BitAndWith.impl_witness, element0 [concrete = constants.%type.as.BitAndWith.impl.Op]
  87. // CHECK:STDOUT: %bound_method: <bound method> = bound_method %E.ref.loc8_10, %impl.elem0 [concrete = constants.%type.as.BitAndWith.impl.Op.bound]
  88. // CHECK:STDOUT: %type.as.BitAndWith.impl.Op.call: init type = call %bound_method(%E.ref.loc8_10, %E.ref.loc8_14) [concrete = constants.%type]
  89. // CHECK:STDOUT: %.loc8_12.2: type = value_of_initializer %type.as.BitAndWith.impl.Op.call [concrete = constants.%type]
  90. // CHECK:STDOUT: %.loc8_12.3: type = converted %type.as.BitAndWith.impl.Op.call, %.loc8_12.2 [concrete = constants.%type]
  91. // CHECK:STDOUT: }
  92. // CHECK:STDOUT: %T.loc8_6.2: %type = bind_symbolic_name T, 0 [symbolic = %T.loc8_6.1 (constants.%T)]
  93. // CHECK:STDOUT: }
  94. // CHECK:STDOUT: }
  95. // CHECK:STDOUT:
  96. // CHECK:STDOUT: generic fn @G(%T.loc8_6.2: %type) {
  97. // CHECK:STDOUT: %T.loc8_6.1: %type = bind_symbolic_name T, 0 [symbolic = %T.loc8_6.1 (constants.%T)]
  98. // CHECK:STDOUT:
  99. // CHECK:STDOUT: !definition:
  100. // 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)]
  101. // CHECK:STDOUT:
  102. // CHECK:STDOUT: fn() {
  103. // CHECK:STDOUT: !entry:
  104. // CHECK:STDOUT: %F.ref: %F.type = name_ref F, file.%F.decl [concrete = constants.%F]
  105. // CHECK:STDOUT: %T.ref: %type = name_ref T, %T.loc8_6.2 [symbolic = %T.loc8_6.1 (constants.%T)]
  106. // 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)]
  107. // CHECK:STDOUT: %F.call: init %empty_tuple.type = call %F.specific_fn.loc9_3.1()
  108. // CHECK:STDOUT: return
  109. // CHECK:STDOUT: }
  110. // CHECK:STDOUT: }
  111. // CHECK:STDOUT:
  112. // CHECK:STDOUT: specific @G(constants.%T) {
  113. // CHECK:STDOUT: %T.loc8_6.1 => constants.%T
  114. // CHECK:STDOUT: }
  115. // CHECK:STDOUT:
  116. // CHECK:STDOUT: --- compatible_constraints.carbon
  117. // CHECK:STDOUT:
  118. // CHECK:STDOUT: constants {
  119. // CHECK:STDOUT: %type: type = facet_type <type> [concrete]
  120. // CHECK:STDOUT: %T: %type = bind_symbolic_name T, 0 [symbolic]
  121. // CHECK:STDOUT: %pattern_type: type = pattern_type %type [concrete]
  122. // CHECK:STDOUT: %F.type: type = fn_type @F [concrete]
  123. // CHECK:STDOUT: %empty_tuple.type: type = tuple_type () [concrete]
  124. // CHECK:STDOUT: %F: %F.type = struct_value () [concrete]
  125. // CHECK:STDOUT: %G.type: type = fn_type @G [concrete]
  126. // CHECK:STDOUT: %G: %G.type = struct_value () [concrete]
  127. // CHECK:STDOUT: %F.specific_fn: <specific function> = specific_function %F, @F(%T) [symbolic]
  128. // CHECK:STDOUT: }
  129. // CHECK:STDOUT:
  130. // CHECK:STDOUT: imports {
  131. // CHECK:STDOUT: }
  132. // CHECK:STDOUT:
  133. // CHECK:STDOUT: file {
  134. // CHECK:STDOUT: %G.decl: %G.type = fn_decl @G [concrete = constants.%G] {
  135. // CHECK:STDOUT: %T.patt: %pattern_type = symbolic_binding_pattern T, 0 [concrete]
  136. // CHECK:STDOUT: } {
  137. // CHECK:STDOUT: %.loc9: type = splice_block %E2.ref [concrete = constants.%type] {
  138. // CHECK:STDOUT: <elided>
  139. // CHECK:STDOUT: %E2.ref: type = name_ref E2, file.%E2.decl [concrete = constants.%type]
  140. // CHECK:STDOUT: }
  141. // CHECK:STDOUT: %T.loc9_6.2: %type = bind_symbolic_name T, 0 [symbolic = %T.loc9_6.1 (constants.%T)]
  142. // CHECK:STDOUT: }
  143. // CHECK:STDOUT: }
  144. // CHECK:STDOUT:
  145. // CHECK:STDOUT: generic fn @G(%T.loc9_6.2: %type) {
  146. // CHECK:STDOUT: %T.loc9_6.1: %type = bind_symbolic_name T, 0 [symbolic = %T.loc9_6.1 (constants.%T)]
  147. // CHECK:STDOUT:
  148. // CHECK:STDOUT: !definition:
  149. // CHECK:STDOUT: %F.specific_fn.loc10_3.2: <specific function> = specific_function constants.%F, @F(%T.loc9_6.1) [symbolic = %F.specific_fn.loc10_3.2 (constants.%F.specific_fn)]
  150. // CHECK:STDOUT:
  151. // CHECK:STDOUT: fn() {
  152. // CHECK:STDOUT: !entry:
  153. // CHECK:STDOUT: %F.ref: %F.type = name_ref F, file.%F.decl [concrete = constants.%F]
  154. // CHECK:STDOUT: %T.ref: %type = name_ref T, %T.loc9_6.2 [symbolic = %T.loc9_6.1 (constants.%T)]
  155. // CHECK:STDOUT: %F.specific_fn.loc10_3.1: <specific function> = specific_function %F.ref, @F(constants.%T) [symbolic = %F.specific_fn.loc10_3.2 (constants.%F.specific_fn)]
  156. // CHECK:STDOUT: %F.call: init %empty_tuple.type = call %F.specific_fn.loc10_3.1()
  157. // CHECK:STDOUT: return
  158. // CHECK:STDOUT: }
  159. // CHECK:STDOUT: }
  160. // CHECK:STDOUT:
  161. // CHECK:STDOUT: specific @G(constants.%T) {
  162. // CHECK:STDOUT: %T.loc9_6.1 => constants.%T
  163. // CHECK:STDOUT: }
  164. // CHECK:STDOUT: