mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-10-05 22:02:55 +01:00
Support calling constexpr functions at compile time (#6878)
Example:
```carbon
import Cpp inline '''
constexpr int f(int a, int b) { return a + b; }
''';
let a: array(i32, Cpp.f(1, 2)) = (1, 2, 3);
```
This commit is contained in:
@@ -45,3 +45,30 @@ constexpr float flt = 123.5;
|
||||
class C(V:! f32) {}
|
||||
fn F() -> C(Cpp.flt);
|
||||
let x: C(123.5) = F();
|
||||
|
||||
// --- function.carbon
|
||||
|
||||
library "[[@TEST_NAME]]";
|
||||
|
||||
import Cpp inline '''
|
||||
constexpr int f(int a, int b) { return a + b; }
|
||||
''';
|
||||
|
||||
let a: array(i32, Cpp.f(1, 2)) = (1, 2, 3);
|
||||
|
||||
// --- fail_invalid_constant_eval.carbon
|
||||
|
||||
library "[[@TEST_NAME]]";
|
||||
|
||||
import Cpp inline '''
|
||||
constexpr int arr[1] = {0};
|
||||
constexpr int f(int a) {
|
||||
return arr[a];
|
||||
}
|
||||
''';
|
||||
|
||||
// CHECK:STDERR: fail_invalid_constant_eval.carbon:[[@LINE+4]]:19: error: failed to evaluate constexpr function call as a constant [CppConstexprEval]
|
||||
// CHECK:STDERR: let a: array(i32, Cpp.f(5)) = (1, 2, 3);
|
||||
// CHECK:STDERR: ^~~~~~~~
|
||||
// CHECK:STDERR:
|
||||
let a: array(i32, Cpp.f(5)) = (1, 2, 3);
|
||||
|
||||
Reference in New Issue
Block a user