diff --git a/cpp2rust/converter/converter.cpp b/cpp2rust/converter/converter.cpp index 8a42f8627c..9dc1c8932b 100644 --- a/cpp2rust/converter/converter.cpp +++ b/cpp2rust/converter/converter.cpp @@ -609,6 +609,8 @@ bool Converter::ConvertVarDeclSkipInit(clang::VarDecl *decl) { void Converter::ConvertVarDeclInitializer(clang::VarDecl *decl) { if (decl->hasInit()) { + ScopedValue constant(constant_evaluated_, + HasConstantEvaluatedInit(ctx_, decl)); ConvertVarInit(decl->getType(), decl->getInit()); } else if (!clang::isa(decl)) { StrCat(ConvertVarDefaultInit(decl)); @@ -620,6 +622,8 @@ void Converter::EmitHoistedInArmAssignment(clang::VarDecl *decl) { return; } StrCat(GetNamedDeclAsString(decl), token::kAssign); + ScopedValue constant(constant_evaluated_, + HasConstantEvaluatedInit(ctx_, decl)); ConvertVarInit(decl->getType(), decl->getInit()); StrCat(token::kSemiColon); } @@ -1925,6 +1929,11 @@ bool Converter::VisitCallExpr(clang::CallExpr *expr) { } bool Converter::ConvertSpecialCall(clang::CallExpr *expr) { + if (IsConstantEvaluatedQuery(expr)) { + EmitBoolLiteral(constant_evaluated_); + return true; + } + if (IsBuiltinVaStart(expr) || IsBuiltinVaEnd(expr) || IsBuiltinVaCopy(expr)) { ConvertVAArgCall(expr); MarkFresh(); @@ -3397,6 +3406,10 @@ bool Converter::VisitDeclRefExpr(clang::DeclRefExpr *expr) { } if (auto init = AsLambdaUncapturedConstant(curr_function_, expr)) { + ScopedValue constant( + constant_evaluated_, + HasConstantEvaluatedInit(ctx_, + clang::cast(expr->getDecl()))); Convert(init); return false; } @@ -4264,6 +4277,8 @@ bool Converter::VisitCXXDefaultArgExpr(clang::CXXDefaultArgExpr *expr) { } bool Converter::VisitConstantExpr(clang::ConstantExpr *expr) { + ScopedValue constant(constant_evaluated_, + constant_evaluated_ || expr->isImmediateInvocation()); Convert(expr->getSubExpr()); MarkFresh(); return false; @@ -4329,6 +4344,7 @@ void Converter::ConvertLambdaClosure(const clang::CXXRecordDecl *decl, EmitFunctionPreamble(call_operator); ScopedValue push_fn(curr_function_, call_operator); ScopedValue function_body(in_const_initializer_, false); + ScopedValue runtime_body(constant_evaluated_, false); ConvertFunctionBody(curr_function_); } diff --git a/cpp2rust/converter/converter.h b/cpp2rust/converter/converter.h index d2d92e8482..254707ac46 100644 --- a/cpp2rust/converter/converter.h +++ b/cpp2rust/converter/converter.h @@ -1011,6 +1011,7 @@ class Converter : public clang::RecursiveASTVisitor { std::string ufcs_receiver_; bool in_const_initializer_ = false; + bool constant_evaluated_ = false; std::optional autoref_mut_; std::vector curr_for_inc_; diff --git a/cpp2rust/converter/converter_lib.cpp b/cpp2rust/converter/converter_lib.cpp index 74f4520448..750a8d0a81 100644 --- a/cpp2rust/converter/converter_lib.cpp +++ b/cpp2rust/converter/converter_lib.cpp @@ -2299,6 +2299,18 @@ bool IsBuiltinVaCopy(const clang::CallExpr *expr) { return IsBuiltinCall(expr, clang::Builtin::BI__builtin_va_copy); } +bool IsConstantEvaluatedQuery(const clang::CallExpr *expr) { + return expr->getBuiltinCallee() == + clang::Builtin::BI__builtin_is_constant_evaluated || + IsStdDecl(expr->getDirectCallee(), "is_constant_evaluated"); +} + +bool HasConstantEvaluatedInit(const clang::ASTContext &ctx, + const clang::VarDecl *decl) { + return decl->isUsableInConstantExpressions(ctx) || + decl->hasConstantInitialization(); +} + bool NeedsImplicitScalarCast(clang::ASTContext &ctx, clang::QualType from, clang::QualType to) { return !from.isNull() && !to.isNull() && from->isIntegerType() && diff --git a/cpp2rust/converter/converter_lib.h b/cpp2rust/converter/converter_lib.h index 75be0f5fd7..270068d40e 100644 --- a/cpp2rust/converter/converter_lib.h +++ b/cpp2rust/converter/converter_lib.h @@ -401,6 +401,11 @@ bool IsBuiltinVaEnd(const clang::CallExpr *expr); bool IsBuiltinVaCopy(const clang::CallExpr *expr); +bool IsConstantEvaluatedQuery(const clang::CallExpr *expr); + +bool HasConstantEvaluatedInit(const clang::ASTContext &ctx, + const clang::VarDecl *decl); + bool IsTransparentStdCall(const clang::CallExpr *expr); const clang::Expr *IgnoreTransparentStdCall(const clang::Expr *expr); diff --git a/cpp2rust/converter/models/converter_refcount.cpp b/cpp2rust/converter/models/converter_refcount.cpp index 7ea46fdacf..b31e3f8a7e 100644 --- a/cpp2rust/converter/models/converter_refcount.cpp +++ b/cpp2rust/converter/models/converter_refcount.cpp @@ -891,6 +891,8 @@ void ConverterRefCount::EmitHoistedInArmAssignment(clang::VarDecl *decl) { PushConversionKind push(*this, VarConversionKind(decl)); StrCat(token::kAssign); + ScopedValue constant(constant_evaluated_, + HasConstantEvaluatedInit(ctx_, decl)); StrCat(ConvertVarInitValue(type, decl->getInit())); StrCat(token::kSemiColon); } diff --git a/rules/builtin/src.cpp b/rules/builtin/src.cpp index 2d132b3e13..34c1a92627 100644 --- a/rules/builtin/src.cpp +++ b/rules/builtin/src.cpp @@ -6,5 +6,3 @@ bool f9(long a, long b, long *r) { return __builtin_mul_overflow(a, b, r); } bool f10(long long a, long long b, long long *r) { return __builtin_mul_overflow(a, b, r); } - -bool f19() { return __builtin_is_constant_evaluated(); } diff --git a/rules/builtin/tgt_unsafe.rs b/rules/builtin/tgt_unsafe.rs index 40b4b87320..1a3cede5b6 100644 --- a/rules/builtin/tgt_unsafe.rs +++ b/rules/builtin/tgt_unsafe.rs @@ -71,7 +71,3 @@ unsafe fn f17() -> f32 { unsafe fn f18(a0: *const ::core::ffi::c_char) -> f32 { f32::NAN } - -unsafe fn f19() -> bool { - false -} diff --git a/tests/std/meta/type.traits/meta.const.eval.cpp b/tests/std/meta/type.traits/meta.const.eval.cpp new file mode 100644 index 0000000000..c863a4d04a --- /dev/null +++ b/tests/std/meta/type.traits/meta.const.eval.cpp @@ -0,0 +1,26 @@ +// ADDITIONAL_COMPILE_FLAGS: -std=c++23 +#include +#include + +// N4950 [meta.const.eval]/1: Effects: Equivalent to: +// if consteval { +// return true; +// } else { +// return false; +// } +namespace p1_is_constant_evaluated { +int pick() { return std::is_constant_evaluated() ? 1 : 2; } + +void test() { + constexpr bool compile_time = std::is_constant_evaluated(); + assert(compile_time); + bool run_time = std::is_constant_evaluated(); + assert(!run_time); + assert(pick() == 2); +} +} // namespace p1_is_constant_evaluated + +int main() { + p1_is_constant_evaluated::test(); + return 0; +} diff --git a/tests/std/meta/type.traits/out/refcount/meta.const.eval.rs b/tests/std/meta/type.traits/out/refcount/meta.const.eval.rs new file mode 100644 index 0000000000..412bb731cf --- /dev/null +++ b/tests/std/meta/type.traits/out/refcount/meta.const.eval.rs @@ -0,0 +1,27 @@ +extern crate libcc2rs; +use libcc2rs::*; +use std::cell::RefCell; +use std::collections::BTreeMap; +use std::io::prelude::*; +use std::io::{Read, Seek, Write}; +use std::os::fd::AsFd; +use std::rc::{Rc, Weak}; +pub fn pick_0() -> i32 { + return if false { 1 } else { 2 }; +} +pub fn test_1() { + let mut compile_time: bool = true; + assert!(compile_time); + let mut run_time: bool = false; + assert!(!(run_time)); + assert!((({ pick_0() }) == 2)); +} +pub fn main() { + __cpp2rust_init_globals(); + std::process::exit(main_0()); +} +fn main_0() -> i32 { + ({ test_1() }); + return 0; +} +pub fn __cpp2rust_init_globals() {} diff --git a/tests/std/meta/type.traits/out/unsafe/meta.const.eval.rs b/tests/std/meta/type.traits/out/unsafe/meta.const.eval.rs new file mode 100644 index 0000000000..69d3ca68ec --- /dev/null +++ b/tests/std/meta/type.traits/out/unsafe/meta.const.eval.rs @@ -0,0 +1,29 @@ +extern crate libc; +use libc::*; +extern crate libcc2rs; +use libcc2rs::*; +use std::collections::BTreeMap; +use std::io::{Read, Seek, Write}; +use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; +use std::rc::Rc; +pub unsafe fn pick_0() -> i32 { + return if false { 1 } else { 2 }; +} +pub unsafe fn test_1() { + let compile_time: bool = true; + assert!(compile_time); + let mut run_time: bool = false; + assert!(!(run_time)); + assert!(((unsafe { pick_0() }) == (2))); +} +pub fn main() { + unsafe { + __cpp2rust_init_globals(); + std::process::exit(main_0() as i32); + } +} +unsafe fn main_0() -> i32 { + (unsafe { test_1() }); + return 0; +} +pub unsafe fn __cpp2rust_init_globals() {} diff --git a/tests/unit/constexpr_function.cpp b/tests/unit/constexpr_function.cpp index 70d8ac1da6..7c5613b440 100644 --- a/tests/unit/constexpr_function.cpp +++ b/tests/unit/constexpr_function.cpp @@ -1,3 +1,4 @@ +// ADDITIONAL_COMPILE_FLAGS: -std=c++20 #include int runtime_only(int x) { return x * 2; } @@ -37,6 +38,8 @@ constexpr bool in_constant_context() { static_assert(in_constant_context()); +consteval bool imm(bool b) { return b; } + struct P { int v; constexpr int get() const { return v; } @@ -63,5 +66,15 @@ int main() { assert(p.get() == 9); constexpr int k = scaled(4); assert(k == 4); + constexpr bool ce = __builtin_is_constant_evaluated(); + assert(ce); + const int n = __builtin_is_constant_evaluated() ? 13 : 17; + assert(n == 13); + static bool s = __builtin_is_constant_evaluated(); + assert(s); + bool rt = __builtin_is_constant_evaluated(); + assert(!rt); + bool b = imm(__builtin_is_constant_evaluated()); + assert(b); return 0; } diff --git a/tests/unit/out/refcount/constexpr_function.rs b/tests/unit/out/refcount/constexpr_function.rs index f554f845e8..4d215aa417 100644 --- a/tests/unit/out/refcount/constexpr_function.rs +++ b/tests/unit/out/refcount/constexpr_function.rs @@ -41,6 +41,9 @@ pub fn in_constant_context_6() -> bool { return (({ runtime_only_0(1) }) == 0); } const _: () = assert!(true, "in_constant_context()"); +pub fn imm_7(mut b: bool) -> bool { + return b; +} #[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] #[byte_size(4)] pub struct P { @@ -72,8 +75,20 @@ fn main_0() -> i32 { assert!((({ PImpl::get(&p.as_pointer(),) }) == 9)); let mut k: i32 = ({ scaled_2(4) }); assert!((k == 4)); + let mut ce: bool = true; + assert!(ce); + let mut n: i32 = if true { 13 } else { 17 }; + assert!((n == 13)); + assert!(s_8.with(|rc| *rc.borrow())); + let mut rt: bool = false; + assert!(!(rt)); + let mut b: bool = ({ imm_7(true) }); + assert!(b); return 0; } +thread_local!( + static s_8: Value = Rc::new(RefCell::new(true)); +); pub trait FlagImpl { fn to_bool(&self) -> bool; } diff --git a/tests/unit/out/unsafe/constexpr_function.rs b/tests/unit/out/unsafe/constexpr_function.rs index e6c6abdcfe..57d97a5028 100644 --- a/tests/unit/out/unsafe/constexpr_function.rs +++ b/tests/unit/out/unsafe/constexpr_function.rs @@ -44,6 +44,9 @@ pub unsafe fn in_constant_context_6() -> bool { return ((unsafe { runtime_only_0(1) }) == (0)); } const _: () = assert!(true, "in_constant_context()"); +pub unsafe fn imm_7(mut b: bool) -> bool { + return b; +} #[repr(C)] #[derive(Copy, Clone, VaArg, FnPtrArg, Default)] pub struct P { @@ -81,6 +84,16 @@ unsafe fn main_0() -> i32 { assert!(((unsafe { P::get(&p,) }) == (9))); let k: i32 = (unsafe { scaled_2(4) }); assert!(((k) == (4))); + let ce: bool = true; + assert!(ce); + let n: i32 = if true { 13 } else { 17 }; + assert!(((n) == (13))); + assert!((*std::cell::LazyCell::force_mut(&mut *&raw mut s_8))); + let mut rt: bool = false; + assert!(!(rt)); + let mut b: bool = (unsafe { imm_7(true) }); + assert!(b); return 0; } +static mut s_8: std::cell::LazyCell = std::cell::LazyCell::new(|| unsafe { true }); pub unsafe fn __cpp2rust_init_globals() {}