impl_thunk_min_prelude.carbon 10 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. // 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/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[unused self: Self]() -> B { return {}; }
  29. }
  30. }
  31. impl ConvertsToA as Core.ImplicitAs(A) {
  32. fn Convert[unused 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. interface Add(T:! type) {
  66. fn Op(a: Self, b: T) -> Self;
  67. }
  68. impl forall [T:! Core.ImplicitAs(i32)] i32 as Add(T) {
  69. fn Op(a: Self, b: Self) -> Self = "int.sadd";
  70. }
  71. fn Call() -> i32 {
  72. let a: i32 = 1;
  73. // The conversion from 2 to IntLiteral here relies on having the
  74. // constant value available, so is only possible if the thunk is
  75. // inlined.
  76. return i32.(Add(Core.IntLiteral()).Op)
  77. //@dump-sem-ir-begin
  78. (a, 2)
  79. //@dump-sem-ir-end
  80. ;
  81. }
  82. // CHECK:STDOUT: --- thunk_literal_convert.carbon
  83. // CHECK:STDOUT:
  84. // CHECK:STDOUT: constants {
  85. // CHECK:STDOUT: %int_32: Core.IntLiteral = int_value 32 [concrete]
  86. // CHECK:STDOUT: %i32: type = class_type @Int, @Int(%int_32) [concrete]
  87. // CHECK:STDOUT: %ImplicitAs.type.e8c: type = facet_type <@ImplicitAs, @ImplicitAs(%i32)> [concrete]
  88. // CHECK:STDOUT: %To: Core.IntLiteral = symbolic_binding To, 0 [symbolic]
  89. // CHECK:STDOUT: %Core.IntLiteral.as.ImplicitAs.impl.Convert.type.4e6: type = fn_type @Core.IntLiteral.as.ImplicitAs.impl.Convert, @Core.IntLiteral.as.ImplicitAs.impl(%To) [symbolic]
  90. // CHECK:STDOUT: %Core.IntLiteral.as.ImplicitAs.impl.Convert.3c2: %Core.IntLiteral.as.ImplicitAs.impl.Convert.type.4e6 = struct_value () [symbolic]
  91. // CHECK:STDOUT: %ImplicitAs.impl_witness.6bc: <witness> = impl_witness imports.%ImplicitAs.impl_witness_table.74f, @Core.IntLiteral.as.ImplicitAs.impl(%int_32) [concrete]
  92. // CHECK:STDOUT: %Core.IntLiteral.as.ImplicitAs.impl.Convert.type.e0d: type = fn_type @Core.IntLiteral.as.ImplicitAs.impl.Convert, @Core.IntLiteral.as.ImplicitAs.impl(%int_32) [concrete]
  93. // CHECK:STDOUT: %Core.IntLiteral.as.ImplicitAs.impl.Convert.0b5: %Core.IntLiteral.as.ImplicitAs.impl.Convert.type.e0d = struct_value () [concrete]
  94. // CHECK:STDOUT: %ImplicitAs.facet: %ImplicitAs.type.e8c = facet_value Core.IntLiteral, (%ImplicitAs.impl_witness.6bc) [concrete]
  95. // CHECK:STDOUT: %ImplicitAs.WithSelf.Convert.type.b37: type = fn_type @ImplicitAs.WithSelf.Convert, @ImplicitAs.WithSelf(%i32, %ImplicitAs.facet) [concrete]
  96. // CHECK:STDOUT: %.545: type = fn_type_with_self_type %ImplicitAs.WithSelf.Convert.type.b37, %ImplicitAs.facet [concrete]
  97. // CHECK:STDOUT: %Core.IntLiteral.as.ImplicitAs.impl.Convert.specific_fn: <specific function> = specific_function %Core.IntLiteral.as.ImplicitAs.impl.Convert.0b5, @Core.IntLiteral.as.ImplicitAs.impl.Convert(%int_32) [concrete]
  98. // CHECK:STDOUT: %i32.as.Add.impl.Op.type.73828b.1: type = fn_type @i32.as.Add.impl.Op.loc8_35.1, @i32.as.Add.impl(%ImplicitAs.facet) [concrete]
  99. // CHECK:STDOUT: %i32.as.Add.impl.Op.0aa775.1: %i32.as.Add.impl.Op.type.73828b.1 = struct_value () [concrete]
  100. // CHECK:STDOUT: %int_2.ecc: Core.IntLiteral = int_value 2 [concrete]
  101. // CHECK:STDOUT: %Core.IntLiteral.as.ImplicitAs.impl.Convert.bound.4e5: <bound method> = bound_method %int_2.ecc, %Core.IntLiteral.as.ImplicitAs.impl.Convert.0b5 [concrete]
  102. // CHECK:STDOUT: %bound_method.646: <bound method> = bound_method %int_2.ecc, %Core.IntLiteral.as.ImplicitAs.impl.Convert.specific_fn [concrete]
  103. // CHECK:STDOUT: %int_2.ef8: %i32 = int_value 2 [concrete]
  104. // CHECK:STDOUT: %i32.as.Add.impl.Op.specific_fn.0f6a75.2: <specific function> = specific_function %i32.as.Add.impl.Op.0aa775.1, @i32.as.Add.impl.Op.loc8_35.1(%ImplicitAs.facet) [concrete]
  105. // CHECK:STDOUT: }
  106. // CHECK:STDOUT:
  107. // CHECK:STDOUT: imports {
  108. // CHECK:STDOUT: %Core.import_ref.42d: @Core.IntLiteral.as.ImplicitAs.impl.%Core.IntLiteral.as.ImplicitAs.impl.Convert.type (%Core.IntLiteral.as.ImplicitAs.impl.Convert.type.4e6) = import_ref Core//prelude/parts/int, loc{{\d+_\d+}}, loaded [symbolic = @Core.IntLiteral.as.ImplicitAs.impl.%Core.IntLiteral.as.ImplicitAs.impl.Convert (constants.%Core.IntLiteral.as.ImplicitAs.impl.Convert.3c2)]
  109. // CHECK:STDOUT: %ImplicitAs.impl_witness_table.74f = impl_witness_table (%Core.import_ref.42d), @Core.IntLiteral.as.ImplicitAs.impl [concrete]
  110. // CHECK:STDOUT: }
  111. // CHECK:STDOUT:
  112. // CHECK:STDOUT: fn @Call() -> out %return.param: %i32 {
  113. // CHECK:STDOUT: !entry:
  114. // CHECK:STDOUT: <elided>
  115. // CHECK:STDOUT: %a.ref: %i32 = name_ref a, %a
  116. // CHECK:STDOUT: %int_2: Core.IntLiteral = int_value 2 [concrete = constants.%int_2.ecc]
  117. // CHECK:STDOUT: %ImplicitAs.facet: %ImplicitAs.type.e8c = facet_value Core.IntLiteral, (constants.%ImplicitAs.impl_witness.6bc) [concrete = constants.%ImplicitAs.facet]
  118. // CHECK:STDOUT: %.loc18_14.1: %ImplicitAs.type.e8c = converted Core.IntLiteral, %ImplicitAs.facet [concrete = constants.%ImplicitAs.facet]
  119. // CHECK:STDOUT: <elided>
  120. // CHECK:STDOUT: %.loc18_14.2: %i32.as.Add.impl.Op.type.73828b.1 = specific_constant @i32.as.Add.impl.%i32.as.Add.impl.Op.decl.loc8_35.1, @i32.as.Add.impl(constants.%ImplicitAs.facet) [concrete = constants.%i32.as.Add.impl.Op.0aa775.1]
  121. // CHECK:STDOUT: %Op.ref.loc18: %i32.as.Add.impl.Op.type.73828b.1 = name_ref Op, %.loc18_14.2 [concrete = constants.%i32.as.Add.impl.Op.0aa775.1]
  122. // CHECK:STDOUT: %impl.elem0.loc18_14: %.545 = impl_witness_access constants.%ImplicitAs.impl_witness.6bc, element0 [concrete = constants.%Core.IntLiteral.as.ImplicitAs.impl.Convert.0b5]
  123. // CHECK:STDOUT: %bound_method.loc18_14.1: <bound method> = bound_method %int_2, %impl.elem0.loc18_14 [concrete = constants.%Core.IntLiteral.as.ImplicitAs.impl.Convert.bound.4e5]
  124. // CHECK:STDOUT: %specific_fn.loc18_14: <specific function> = specific_function %impl.elem0.loc18_14, @Core.IntLiteral.as.ImplicitAs.impl.Convert(constants.%int_32) [concrete = constants.%Core.IntLiteral.as.ImplicitAs.impl.Convert.specific_fn]
  125. // CHECK:STDOUT: %bound_method.loc18_14.2: <bound method> = bound_method %int_2, %specific_fn.loc18_14 [concrete = constants.%bound_method.646]
  126. // CHECK:STDOUT: %Core.IntLiteral.as.ImplicitAs.impl.Convert.call.loc18_14: init %i32 = call %bound_method.loc18_14.2(%int_2) [concrete = constants.%int_2.ef8]
  127. // CHECK:STDOUT: %.loc18_14.3: %i32 = value_of_initializer %Core.IntLiteral.as.ImplicitAs.impl.Convert.call.loc18_14 [concrete = constants.%int_2.ef8]
  128. // CHECK:STDOUT: %.loc18_14.4: %i32 = converted %int_2, %.loc18_14.3 [concrete = constants.%int_2.ef8]
  129. // CHECK:STDOUT: %i32.as.Add.impl.Op.specific_fn: <specific function> = specific_function %Op.ref.loc18, @i32.as.Add.impl.Op.loc8_35.1(constants.%ImplicitAs.facet) [concrete = constants.%i32.as.Add.impl.Op.specific_fn.0f6a75.2]
  130. // CHECK:STDOUT: %impl.elem0.loc18_13: %.545 = impl_witness_access constants.%ImplicitAs.impl_witness.6bc, element0 [concrete = constants.%Core.IntLiteral.as.ImplicitAs.impl.Convert.0b5]
  131. // CHECK:STDOUT: %bound_method.loc18_13.1: <bound method> = bound_method %int_2, %impl.elem0.loc18_13 [concrete = constants.%Core.IntLiteral.as.ImplicitAs.impl.Convert.bound.4e5]
  132. // CHECK:STDOUT: %specific_fn.loc18_13: <specific function> = specific_function %impl.elem0.loc18_13, @Core.IntLiteral.as.ImplicitAs.impl.Convert(constants.%int_32) [concrete = constants.%Core.IntLiteral.as.ImplicitAs.impl.Convert.specific_fn]
  133. // CHECK:STDOUT: %bound_method.loc18_13.2: <bound method> = bound_method %int_2, %specific_fn.loc18_13 [concrete = constants.%bound_method.646]
  134. // CHECK:STDOUT: %Core.IntLiteral.as.ImplicitAs.impl.Convert.call.loc18_13: init %i32 = call %bound_method.loc18_13.2(%int_2) [concrete = constants.%int_2.ef8]
  135. // CHECK:STDOUT: %.loc18_13.1: %i32 = value_of_initializer %Core.IntLiteral.as.ImplicitAs.impl.Convert.call.loc18_13 [concrete = constants.%int_2.ef8]
  136. // CHECK:STDOUT: %.loc18_13.2: %i32 = converted %int_2, %.loc18_13.1 [concrete = constants.%int_2.ef8]
  137. // CHECK:STDOUT: %i32.as.Add.impl.Op.call: init %i32 = call %i32.as.Add.impl.Op.specific_fn(%a.ref, %.loc18_13.2)
  138. // CHECK:STDOUT: <elided>
  139. // CHECK:STDOUT: }
  140. // CHECK:STDOUT: