constexpr.carbon 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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/full.carbon
  6. // EXTRA-ARGS: --clang-arg=--std=c++20
  7. //
  8. // AUTOUPDATE
  9. // TIP: To test this file alone, run:
  10. // TIP: bazel test //toolchain/testing:file_test --test_arg=--file_tests=toolchain/check/testdata/interop/cpp/constexpr.carbon
  11. // TIP: To dump output, run:
  12. // TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/interop/cpp/constexpr.carbon
  13. // --- bool.carbon
  14. library "[[@TEST_NAME]]";
  15. import Cpp inline '''
  16. constexpr bool b = true;
  17. ''';
  18. class C(B:! bool) {}
  19. fn F() -> C(Cpp.b);
  20. let x: C(true) = F();
  21. // --- int.carbon
  22. library "[[@TEST_NAME]]";
  23. import Cpp inline '''
  24. constexpr int i = 123;
  25. ''';
  26. class C(I:! i32) {}
  27. fn F() -> C(Cpp.i);
  28. let x: C(123) = F();
  29. // --- float.carbon
  30. library "[[@TEST_NAME]]";
  31. import Cpp inline '''
  32. constexpr float flt = 123.5;
  33. ''';
  34. class C(V:! f32) {}
  35. fn F() -> C(Cpp.flt);
  36. let x: C(123.5) = F();
  37. // --- pointer.carbon
  38. library "[[@TEST_NAME]]";
  39. import Cpp inline '''
  40. int i = 123;
  41. constexpr int* _Nonnull ptr = &i;
  42. ''';
  43. class C(V:! i32*) {}
  44. fn F() -> C(Cpp.ptr);
  45. let x: C(&Cpp.i) = F();
  46. // --- function.carbon
  47. library "[[@TEST_NAME]]";
  48. import Cpp inline '''
  49. constexpr int f(int a, int b) { return a + b; }
  50. ''';
  51. let a: array(i32, Cpp.f(1, 2)) = (1, 2, 3);
  52. // --- function_bool_param.carbon
  53. library "[[@TEST_NAME]]";
  54. import Cpp inline '''
  55. constexpr int f(bool b) {
  56. return b ? 3 : 0;
  57. }
  58. ''';
  59. let a: array(i32, Cpp.f(true)) = (1, 2, 3);
  60. // --- function_float_param.carbon
  61. library "[[@TEST_NAME]]";
  62. import Cpp inline '''
  63. constexpr int f(float b) {
  64. return static_cast<int>(b);
  65. }
  66. ''';
  67. let a: array(i32, Cpp.f(3.0)) = (1, 2, 3);
  68. // --- function_return_bool.carbon
  69. library "[[@TEST_NAME]]";
  70. import Cpp inline '''
  71. constexpr bool f() {
  72. return true;
  73. }
  74. ''';
  75. musteval fn F(b: bool) -> i32 {
  76. return if b then 3 else 0;
  77. }
  78. let a: array(i32, F(Cpp.f())) = (1, 2, 3);
  79. // --- fail_invalid_constant_eval.carbon
  80. library "[[@TEST_NAME]]";
  81. import Cpp inline '''
  82. constexpr int arr[1] = {0};
  83. constexpr int f(int a) {
  84. return arr[a];
  85. }
  86. ''';
  87. // CHECK:STDERR: fail_invalid_constant_eval.carbon:[[@LINE+4]]:19: error: array bound is not a constant [InvalidArrayExpr]
  88. // CHECK:STDERR: let a: array(i32, Cpp.f(5)) = (1, 2, 3);
  89. // CHECK:STDERR: ^~~~~~~~
  90. // CHECK:STDERR:
  91. let a: array(i32, Cpp.f(5)) = (1, 2, 3);
  92. // --- nonconstant_constexpr_call.carbon
  93. library "[[@TEST_NAME]]";
  94. import Cpp;
  95. var a: i32 = 0;
  96. inline Cpp '''
  97. constexpr int f(const int &r) { return r; }
  98. ''';
  99. // OK, runtime call.
  100. var b: i32 = Cpp.f(a);
  101. // --- fail_nonconstant_consteval_call.carbon
  102. library "[[@TEST_NAME]]";
  103. import Cpp;
  104. var a: i32 = 0;
  105. // TODO: This is failing when building the thunk, not when calling it.
  106. inline Cpp '''
  107. // CHECK:STDERR: fail_nonconstant_consteval_call.carbon:[[@LINE+9]]:15: error: call to consteval function 'f' is not a constant expression [CppInteropParseError]
  108. // CHECK:STDERR: 19 | consteval int f(const int &r) { return r; }
  109. // CHECK:STDERR: | ^
  110. // CHECK:STDERR: fail_nonconstant_consteval_call.carbon:[[@LINE+6]]:15: note: function parameter 'r' with unknown value cannot be used in a constant expression [CppInteropParseNote]
  111. // CHECK:STDERR: 19 | consteval int f(const int &r) { return r; }
  112. // CHECK:STDERR: | ^
  113. // CHECK:STDERR: fail_nonconstant_consteval_call.carbon:[[@LINE+3]]:15: note: declared here [CppInteropParseNote]
  114. // CHECK:STDERR: 19 | consteval int f(const int &r) { return r; }
  115. // CHECK:STDERR: | ^
  116. consteval int f(const int &r) { return r; }
  117. ''';
  118. // CHECK:STDERR: fail_nonconstant_consteval_call.carbon:[[@LINE+11]]:14: note: in thunk for C++ function used here [InCppThunk]
  119. // CHECK:STDERR: var b: i32 = Cpp.f(a);
  120. // CHECK:STDERR: ^~~~~~~~
  121. // CHECK:STDERR:
  122. // CHECK:STDERR: fail_nonconstant_consteval_call.carbon:[[@LINE+7]]:14: error: non-constant call to compile-time-only function [NonConstantCallToCompTimeOnlyFunction]
  123. // CHECK:STDERR: var b: i32 = Cpp.f(a);
  124. // CHECK:STDERR: ^~~~~~~~
  125. // CHECK:STDERR: fail_nonconstant_consteval_call.carbon:[[@LINE-10]]:15: note: compile-time-only function declared here [CompTimeOnlyFunctionHere]
  126. // CHECK:STDERR: consteval int f(const int &r) { return r; }
  127. // CHECK:STDERR: ^
  128. // CHECK:STDERR:
  129. var b: i32 = Cpp.f(a);