eval_musteval.carbon 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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/eval_musteval.carbon
  10. // TIP: To dump output, run:
  11. // TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/impl/eval_musteval.carbon
  12. // --- interface.carbon
  13. library "[[@TEST_NAME]]";
  14. interface Runtime {
  15. fn F[self: Self]() -> type;
  16. }
  17. interface Eval {
  18. eval fn F[self: Self]() -> type;
  19. }
  20. interface MustEval {
  21. musteval fn F[self: Self]() -> type;
  22. }
  23. // --- impl_runtime.carbon
  24. library "[[@TEST_NAME]]";
  25. import library "interface";
  26. class A { adapt {}; }
  27. impl A as Runtime {
  28. fn F[self: Self]() -> type { return A; }
  29. }
  30. impl A as Eval {
  31. // TODO: Consider rejecting this, as compile-time evaluation would always
  32. // fail.
  33. fn F[self: Self]() -> type { return A; }
  34. }
  35. impl A as MustEval {
  36. // TODO: Consider rejecting this, as compile-time evaluation would always
  37. // fail.
  38. fn F[self: Self]() -> type { return A; }
  39. }
  40. fn Call() {
  41. ({} as A).(Runtime.F)();
  42. ({} as A).(Eval.F)();
  43. ({} as A).(MustEval.F)();
  44. }
  45. // --- fail_impl_runtime_eval.carbon
  46. library "[[@TEST_NAME]]";
  47. import library "interface";
  48. class A { adapt {}; }
  49. impl A as Runtime {
  50. fn F[self: Self]() -> type { return A; }
  51. }
  52. impl A as Eval {
  53. fn F[self: Self]() -> type { return A; }
  54. }
  55. impl A as MustEval {
  56. fn F[self: Self]() -> type { return A; }
  57. }
  58. fn Call() {
  59. // CHECK:STDERR: fail_impl_runtime_eval.carbon:[[@LINE+4]]:10: error: cannot evaluate type expression [TypeExprEvaluationFailure]
  60. // CHECK:STDERR: let t: ({} as A).(Runtime.F)() = {} as A;
  61. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~
  62. // CHECK:STDERR:
  63. let t: ({} as A).(Runtime.F)() = {} as A;
  64. // CHECK:STDERR: fail_impl_runtime_eval.carbon:[[@LINE+4]]:10: error: cannot evaluate type expression [TypeExprEvaluationFailure]
  65. // CHECK:STDERR: let u: ({} as A).(Eval.F)() = {} as A;
  66. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~
  67. // CHECK:STDERR:
  68. let u: ({} as A).(Eval.F)() = {} as A;
  69. // CHECK:STDERR: fail_impl_runtime_eval.carbon:[[@LINE+4]]:10: error: cannot evaluate type expression [TypeExprEvaluationFailure]
  70. // CHECK:STDERR: let v: ({} as A).(MustEval.F)() = {} as A;
  71. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~
  72. // CHECK:STDERR:
  73. let v: ({} as A).(MustEval.F)() = {} as A;
  74. }
  75. // --- impl_eval.carbon
  76. library "[[@TEST_NAME]]";
  77. import library "interface";
  78. class A { adapt {}; }
  79. impl A as Runtime {
  80. eval fn F[self: Self]() -> type { return A; }
  81. }
  82. impl A as Eval {
  83. eval fn F[self: Self]() -> type { return A; }
  84. }
  85. impl A as MustEval {
  86. eval fn F[self: Self]() -> type { return A; }
  87. }
  88. fn Call() {
  89. // TODO: Should we allow calling this at compile time? This is not allowed if
  90. // we generate a thunk; see the next split for an example.
  91. let t: ({} as A).(Runtime.F)() = {} as A;
  92. let u: ({} as A).(Eval.F)() = {} as A;
  93. let v: ({} as A).(MustEval.F)() = {} as A;
  94. }
  95. // --- fail_todo_impl_eval_from_runtime_thunk.carbon
  96. library "[[@TEST_NAME]]";
  97. import library "interface";
  98. class B {}
  99. class BView {}
  100. impl B as Core.ImplicitAs(BView) {
  101. fn Convert[self: Self]() -> BView { return {}; }
  102. }
  103. impl B as Runtime {
  104. eval fn F[self: BView]() -> type { return B; }
  105. }
  106. fn Call() {
  107. // TODO: Should we allow calling this at compile time? This is allowed if we
  108. // don't generate a thunk.
  109. // CHECK:STDERR: fail_todo_impl_eval_from_runtime_thunk.carbon:[[@LINE+4]]:10: error: cannot evaluate type expression [TypeExprEvaluationFailure]
  110. // CHECK:STDERR: let t: ({} as B).(Runtime.F)() = {} as B;
  111. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~
  112. // CHECK:STDERR:
  113. let t: ({} as B).(Runtime.F)() = {} as B;
  114. }
  115. // --- fail_impl_musteval.carbon
  116. library "[[@TEST_NAME]]";
  117. import library "interface";
  118. class B { adapt {}; }
  119. impl B as Runtime {
  120. // CHECK:STDERR: fail_impl_musteval.carbon:[[@LINE+11]]:3: error: non-constant call to compile-time-only function [NonConstantCallToCompTimeOnlyFunction]
  121. // CHECK:STDERR: musteval fn F[self: Self]() -> type { return B; }
  122. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  123. // CHECK:STDERR: fail_impl_musteval.carbon:[[@LINE+8]]:3: note: compile-time-only function declared here [CompTimeOnlyFunctionHere]
  124. // CHECK:STDERR: musteval fn F[self: Self]() -> type { return B; }
  125. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  126. // CHECK:STDERR: fail_impl_musteval.carbon:[[@LINE-11]]:1: in import [InImport]
  127. // CHECK:STDERR: interface.carbon:5:3: note: while building thunk to match the signature of this function [ThunkSignature]
  128. // CHECK:STDERR: fn F[self: Self]() -> type;
  129. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~
  130. // CHECK:STDERR:
  131. musteval fn F[self: Self]() -> type { return B; }
  132. }
  133. impl B as Eval {
  134. // CHECK:STDERR: fail_impl_musteval.carbon:[[@LINE+11]]:3: error: non-constant call to compile-time-only function [NonConstantCallToCompTimeOnlyFunction]
  135. // CHECK:STDERR: musteval fn F[self: Self]() -> type { return B; }
  136. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  137. // CHECK:STDERR: fail_impl_musteval.carbon:[[@LINE+8]]:3: note: compile-time-only function declared here [CompTimeOnlyFunctionHere]
  138. // CHECK:STDERR: musteval fn F[self: Self]() -> type { return B; }
  139. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  140. // CHECK:STDERR: fail_impl_musteval.carbon:[[@LINE-26]]:1: in import [InImport]
  141. // CHECK:STDERR: interface.carbon:9:3: note: while building thunk to match the signature of this function [ThunkSignature]
  142. // CHECK:STDERR: eval fn F[self: Self]() -> type;
  143. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  144. // CHECK:STDERR:
  145. musteval fn F[self: Self]() -> type { return B; }
  146. }
  147. // --- impl_musteval_from_musteval.carbon
  148. library "[[@TEST_NAME]]";
  149. import library "interface";
  150. class B { adapt {}; }
  151. impl B as MustEval {
  152. musteval fn F[self: Self]() -> type { return B; }
  153. }
  154. fn Call() {
  155. let t: ({} as B).(MustEval.F)() = {} as B;
  156. }
  157. // --- fail_todo_impl_musteval_from_musteval_thunk.carbon
  158. library "[[@TEST_NAME]]";
  159. import library "interface";
  160. class B {}
  161. class BView {}
  162. impl B as Core.ImplicitAs(BView) {
  163. fn Convert[self: Self]() -> BView { return {}; }
  164. }
  165. impl B as MustEval {
  166. // CHECK:STDERR: fail_todo_impl_musteval_from_musteval_thunk.carbon:[[@LINE+11]]:3: error: non-constant call to compile-time-only function [NonConstantCallToCompTimeOnlyFunction]
  167. // CHECK:STDERR: musteval fn F[self: BView]() -> type { return B; }
  168. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  169. // CHECK:STDERR: fail_todo_impl_musteval_from_musteval_thunk.carbon:[[@LINE+8]]:3: note: compile-time-only function declared here [CompTimeOnlyFunctionHere]
  170. // CHECK:STDERR: musteval fn F[self: BView]() -> type { return B; }
  171. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  172. // CHECK:STDERR: fail_todo_impl_musteval_from_musteval_thunk.carbon:[[@LINE-16]]:1: in import [InImport]
  173. // CHECK:STDERR: interface.carbon:13:3: note: while building thunk to match the signature of this function [ThunkSignature]
  174. // CHECK:STDERR: musteval fn F[self: Self]() -> type;
  175. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  176. // CHECK:STDERR:
  177. musteval fn F[self: BView]() -> type { return B; }
  178. }