constexpr.carbon 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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/function/import/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/function/import/constexpr.carbon
  13. // --- basic.carbon
  14. library "[[@TEST_NAME]]";
  15. import Cpp inline '''
  16. constexpr int f(int a, int b) { return a + b; }
  17. ''';
  18. let a: array(i32, Cpp.f(1, 2)) = (1, 2, 3);
  19. // --- bool_param.carbon
  20. library "[[@TEST_NAME]]";
  21. import Cpp inline '''
  22. constexpr int f(bool b) {
  23. return b ? 3 : 0;
  24. }
  25. ''';
  26. let a: array(i32, Cpp.f(true)) = (1, 2, 3);
  27. // --- float_param.carbon
  28. library "[[@TEST_NAME]]";
  29. import Cpp inline '''
  30. constexpr int f(float b) {
  31. return static_cast<int>(b);
  32. }
  33. ''';
  34. let a: array(i32, Cpp.f(3.0)) = (1, 2, 3);
  35. // --- return_bool.carbon
  36. library "[[@TEST_NAME]]";
  37. import Cpp inline '''
  38. constexpr bool f() {
  39. return true;
  40. }
  41. ''';
  42. musteval fn F(b: bool) -> i32 {
  43. return if b then 3 else 0;
  44. }
  45. let a: array(i32, F(Cpp.f())) = (1, 2, 3);
  46. // --- fail_invalid_constant_eval.carbon
  47. library "[[@TEST_NAME]]";
  48. import Cpp inline '''
  49. constexpr int arr[1] = {0};
  50. constexpr int f(int a) {
  51. return arr[a];
  52. }
  53. ''';
  54. // CHECK:STDERR: fail_invalid_constant_eval.carbon:[[@LINE+4]]:19: error: array bound is not a constant [InvalidArrayExpr]
  55. // CHECK:STDERR: let a: array(i32, Cpp.f(5)) = (1, 2, 3);
  56. // CHECK:STDERR: ^~~~~~~~
  57. // CHECK:STDERR:
  58. let a: array(i32, Cpp.f(5)) = (1, 2, 3);
  59. // --- nonconstant_constexpr_call.carbon
  60. library "[[@TEST_NAME]]";
  61. import Cpp;
  62. var a: i32 = 0;
  63. inline Cpp '''
  64. constexpr int f(const int &r) { return r; }
  65. ''';
  66. // OK, runtime call.
  67. var b: i32 = Cpp.f(a);
  68. // --- fail_nonconstant_consteval_call.carbon
  69. library "[[@TEST_NAME]]";
  70. import Cpp;
  71. var a: i32 = 0;
  72. // TODO: This is failing when building the thunk, not when calling it.
  73. inline Cpp '''
  74. // CHECK:STDERR: fail_nonconstant_consteval_call.carbon:[[@LINE+9]]:15: error: call to consteval function 'f' is not a constant expression [CppInteropParseError]
  75. // CHECK:STDERR: 19 | consteval int f(const int &r) { return r; }
  76. // CHECK:STDERR: | ^
  77. // 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]
  78. // CHECK:STDERR: 19 | consteval int f(const int &r) { return r; }
  79. // CHECK:STDERR: | ^
  80. // CHECK:STDERR: fail_nonconstant_consteval_call.carbon:[[@LINE+3]]:15: note: declared here [CppInteropParseNote]
  81. // CHECK:STDERR: 19 | consteval int f(const int &r) { return r; }
  82. // CHECK:STDERR: | ^
  83. consteval int f(const int &r) { return r; }
  84. ''';
  85. // CHECK:STDERR: fail_nonconstant_consteval_call.carbon:[[@LINE+11]]:14: note: in thunk for C++ function used here [InCppThunk]
  86. // CHECK:STDERR: var b: i32 = Cpp.f(a);
  87. // CHECK:STDERR: ^~~~~~~~
  88. // CHECK:STDERR:
  89. // CHECK:STDERR: fail_nonconstant_consteval_call.carbon:[[@LINE+7]]:14: error: non-constant call to compile-time-only function [NonConstantCallToCompTimeOnlyFunction]
  90. // CHECK:STDERR: var b: i32 = Cpp.f(a);
  91. // CHECK:STDERR: ^~~~~~~~
  92. // CHECK:STDERR: fail_nonconstant_consteval_call.carbon:[[@LINE-10]]:15: note: compile-time-only function declared here [CompTimeOnlyFunctionHere]
  93. // CHECK:STDERR: consteval int f(const int &r) { return r; }
  94. // CHECK:STDERR: ^
  95. // CHECK:STDERR:
  96. var b: i32 = Cpp.f(a);