impl_thunk_min_prelude.carbon 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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/impl/impl_thunk_min_prelude.carbon
  10. // TIP: To dump output, run:
  11. // TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/impl/impl_thunk_min_prelude.carbon
  12. // --- convert_in_class.carbon
  13. library "[[@TEST_NAME]]";
  14. interface X(T:! type, U:! type) {
  15. fn F(t: T) -> U;
  16. }
  17. class ConvertsToA {}
  18. class ConvertsToB {}
  19. // Check that we don't try to define a thunk for `A.B.(as X).F` until we reach
  20. // the end of `A`. If we tried earlier, we wouldn't find a conversion from
  21. // `ConvertsToA` to `A` or from `ConvertsToB` to `B`.
  22. class A {
  23. class B {
  24. impl as X(ConvertsToA, B) {
  25. fn F(a: A) -> ConvertsToB;
  26. }
  27. impl ConvertsToB as Core.ImplicitAs(B) {
  28. fn Convert[self: Self]() -> B { return {}; }
  29. }
  30. }
  31. impl ConvertsToA as Core.ImplicitAs(A) {
  32. fn Convert[self: Self]() -> A { return {}; }
  33. }
  34. }
  35. // --- fail_todo_out_of_line_thunk.carbon
  36. library "[[@TEST_NAME]]";
  37. class Wrap(T:! type) {}
  38. interface OpWith(U:! type) {
  39. fn Op[self: Self](u: U);
  40. }
  41. impl forall [T:! type, U:! Core.ImplicitAs(Wrap(T))] Wrap(T) as OpWith(U) {
  42. // CHECK:STDERR: fail_todo_out_of_line_thunk.carbon:[[@LINE+7]]:3: error: use of undefined generic function [MissingGenericFunctionDefinition]
  43. // CHECK:STDERR: fn Op[self: Self](other: Self);
  44. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  45. // CHECK:STDERR: fail_todo_out_of_line_thunk.carbon:[[@LINE+4]]:3: note: generic function declared here [MissingGenericFunctionDefinitionHere]
  46. // CHECK:STDERR: fn Op[self: Self](other: Self);
  47. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  48. // CHECK:STDERR:
  49. fn Op[self: Self](other: Self);
  50. }
  51. // TODO: Once we support the syntax for defining impl members out of line,
  52. // define the above function here.
  53. // fn (forall [T:! type, U:! Core.ImplicitAs(Wrap(T))] Wrap(T) as OpWith(U)).Op[self: Self](other: Self) {}
  54. // --- builtin_thunk.carbon
  55. library "[[@TEST_NAME]]";
  56. class Wrap(T:! type) {}
  57. interface OpWith(U:! type) {
  58. fn Op[self: Self](u: U);
  59. }
  60. impl forall [T:! type, U:! Core.ImplicitAs(Wrap(T))] Wrap(T) as OpWith(U) {
  61. fn Op[self: Self](other: Self) = "no_op";
  62. }
  63. // --- thunk_literal_convert.carbon
  64. library "[[@TEST_NAME]]";
  65. fn IntLiteral() -> type = "int_literal.make_type";
  66. fn Int(size: IntLiteral()) -> type = "int.make_type_signed";
  67. impl IntLiteral() as Core.ImplicitAs(Int(32)) {
  68. fn Convert[self: Self]() -> Int(32) = "int.convert_checked";
  69. }
  70. interface Add(T:! type) {
  71. fn Op(a: Self, b: T) -> Self;
  72. }
  73. impl forall [T:! Core.ImplicitAs(Int(32))] Int(32) as Add(T) {
  74. fn Op(a: Self, b: Self) -> Self = "int.sadd";
  75. }
  76. fn Call() -> Int(32) {
  77. let a: Int(32) = 1;
  78. // The conversion from 2 to IntLiteral here relies on having the
  79. // constant value available, so is only possible if the thunk is
  80. // inlined.
  81. return Int(32).(Add(IntLiteral()).Op)
  82. //@dump-sem-ir-begin
  83. (a, 2)
  84. //@dump-sem-ir-end
  85. ;
  86. }
  87. // CHECK:STDOUT: --- thunk_literal_convert.carbon
  88. // CHECK:STDOUT:
  89. // CHECK:STDOUT: constants {
  90. // CHECK:STDOUT: %int_32: Core.IntLiteral = int_value 32 [concrete]
  91. // CHECK:STDOUT: %i32.builtin: type = int_type signed, %int_32 [concrete]
  92. // CHECK:STDOUT: %ImplicitAs.type.873: type = facet_type <@ImplicitAs, @ImplicitAs(%i32.builtin)> [concrete]
  93. // CHECK:STDOUT: %ImplicitAs.Convert.type.059: type = fn_type @ImplicitAs.Convert, @ImplicitAs(%i32.builtin) [concrete]
  94. // CHECK:STDOUT: %ImplicitAs.impl_witness: <witness> = impl_witness file.%ImplicitAs.impl_witness_table [concrete]
  95. // CHECK:STDOUT: %Core.IntLiteral.as.ImplicitAs.impl.Convert.type: type = fn_type @Core.IntLiteral.as.ImplicitAs.impl.Convert [concrete]
  96. // CHECK:STDOUT: %Core.IntLiteral.as.ImplicitAs.impl.Convert: %Core.IntLiteral.as.ImplicitAs.impl.Convert.type = struct_value () [concrete]
  97. // CHECK:STDOUT: %ImplicitAs.facet: %ImplicitAs.type.873 = facet_value Core.IntLiteral, (%ImplicitAs.impl_witness) [concrete]
  98. // CHECK:STDOUT: %.0f5: type = fn_type_with_self_type %ImplicitAs.Convert.type.059, %ImplicitAs.facet [concrete]
  99. // CHECK:STDOUT: %i32.builtin.as.Add.impl.Op.type.5284bd.1: type = fn_type @i32.builtin.as.Add.impl.Op.loc15_35.1, @i32.builtin.as.Add.impl(%ImplicitAs.facet) [concrete]
  100. // CHECK:STDOUT: %i32.builtin.as.Add.impl.Op.1d2eda.1: %i32.builtin.as.Add.impl.Op.type.5284bd.1 = struct_value () [concrete]
  101. // CHECK:STDOUT: %int_2.ecc: Core.IntLiteral = int_value 2 [concrete]
  102. // CHECK:STDOUT: %Core.IntLiteral.as.ImplicitAs.impl.Convert.bound.e54: <bound method> = bound_method %int_2.ecc, %Core.IntLiteral.as.ImplicitAs.impl.Convert [concrete]
  103. // CHECK:STDOUT: %int_2.5a1: %i32.builtin = int_value 2 [concrete]
  104. // CHECK:STDOUT: %i32.builtin.as.Add.impl.Op.specific_fn.ffd5e9.2: <specific function> = specific_function %i32.builtin.as.Add.impl.Op.1d2eda.1, @i32.builtin.as.Add.impl.Op.loc15_35.1(%ImplicitAs.facet) [concrete]
  105. // CHECK:STDOUT: }
  106. // CHECK:STDOUT:
  107. // CHECK:STDOUT: imports {
  108. // CHECK:STDOUT: }
  109. // CHECK:STDOUT:
  110. // CHECK:STDOUT: fn @Call() -> %i32.builtin {
  111. // CHECK:STDOUT: !entry:
  112. // CHECK:STDOUT: <elided>
  113. // CHECK:STDOUT: %a.ref: %i32.builtin = name_ref a, %a
  114. // CHECK:STDOUT: %int_2: Core.IntLiteral = int_value 2 [concrete = constants.%int_2.ecc]
  115. // CHECK:STDOUT: %ImplicitAs.facet.loc25_14.1: %ImplicitAs.type.873 = facet_value Core.IntLiteral, (constants.%ImplicitAs.impl_witness) [concrete = constants.%ImplicitAs.facet]
  116. // CHECK:STDOUT: %.loc25_14.1: %ImplicitAs.type.873 = converted Core.IntLiteral, %ImplicitAs.facet.loc25_14.1 [concrete = constants.%ImplicitAs.facet]
  117. // CHECK:STDOUT: %ImplicitAs.facet.loc25_14.2: %ImplicitAs.type.873 = facet_value Core.IntLiteral, (constants.%ImplicitAs.impl_witness) [concrete = constants.%ImplicitAs.facet]
  118. // CHECK:STDOUT: %.loc25_14.2: %ImplicitAs.type.873 = converted Core.IntLiteral, %ImplicitAs.facet.loc25_14.2 [concrete = constants.%ImplicitAs.facet]
  119. // CHECK:STDOUT: <elided>
  120. // CHECK:STDOUT: %.loc25_14.3: %i32.builtin.as.Add.impl.Op.type.5284bd.1 = specific_constant @i32.builtin.as.Add.impl.%i32.builtin.as.Add.impl.Op.decl.loc15_35.1, @i32.builtin.as.Add.impl(constants.%ImplicitAs.facet) [concrete = constants.%i32.builtin.as.Add.impl.Op.1d2eda.1]
  121. // CHECK:STDOUT: %Op.ref.loc25: %i32.builtin.as.Add.impl.Op.type.5284bd.1 = name_ref Op, %.loc25_14.3 [concrete = constants.%i32.builtin.as.Add.impl.Op.1d2eda.1]
  122. // CHECK:STDOUT: %impl.elem0.loc25_14: %.0f5 = impl_witness_access constants.%ImplicitAs.impl_witness, element0 [concrete = constants.%Core.IntLiteral.as.ImplicitAs.impl.Convert]
  123. // CHECK:STDOUT: %bound_method.loc25_14: <bound method> = bound_method %int_2, %impl.elem0.loc25_14 [concrete = constants.%Core.IntLiteral.as.ImplicitAs.impl.Convert.bound.e54]
  124. // CHECK:STDOUT: %Core.IntLiteral.as.ImplicitAs.impl.Convert.call.loc25_14: init %i32.builtin = call %bound_method.loc25_14(%int_2) [concrete = constants.%int_2.5a1]
  125. // CHECK:STDOUT: %.loc25_14.4: %i32.builtin = value_of_initializer %Core.IntLiteral.as.ImplicitAs.impl.Convert.call.loc25_14 [concrete = constants.%int_2.5a1]
  126. // CHECK:STDOUT: %.loc25_14.5: %i32.builtin = converted %int_2, %.loc25_14.4 [concrete = constants.%int_2.5a1]
  127. // CHECK:STDOUT: %i32.builtin.as.Add.impl.Op.specific_fn: <specific function> = specific_function %Op.ref.loc25, @i32.builtin.as.Add.impl.Op.loc15_35.1(constants.%ImplicitAs.facet) [concrete = constants.%i32.builtin.as.Add.impl.Op.specific_fn.ffd5e9.2]
  128. // CHECK:STDOUT: %impl.elem0.loc25_13: %.0f5 = impl_witness_access constants.%ImplicitAs.impl_witness, element0 [concrete = constants.%Core.IntLiteral.as.ImplicitAs.impl.Convert]
  129. // CHECK:STDOUT: %bound_method.loc25_13: <bound method> = bound_method %int_2, %impl.elem0.loc25_13 [concrete = constants.%Core.IntLiteral.as.ImplicitAs.impl.Convert.bound.e54]
  130. // CHECK:STDOUT: %Core.IntLiteral.as.ImplicitAs.impl.Convert.call.loc25_13: init %i32.builtin = call %bound_method.loc25_13(%int_2) [concrete = constants.%int_2.5a1]
  131. // CHECK:STDOUT: %.loc25_13.1: %i32.builtin = value_of_initializer %Core.IntLiteral.as.ImplicitAs.impl.Convert.call.loc25_13 [concrete = constants.%int_2.5a1]
  132. // CHECK:STDOUT: %.loc25_13.2: %i32.builtin = converted %int_2, %.loc25_13.1 [concrete = constants.%int_2.5a1]
  133. // CHECK:STDOUT: %i32.builtin.as.Add.impl.Op.call: init %i32.builtin = call %i32.builtin.as.Add.impl.Op.specific_fn(%a.ref, %.loc25_13.2)
  134. // CHECK:STDOUT: <elided>
  135. // CHECK:STDOUT: }
  136. // CHECK:STDOUT: