From 927e104ec3ffa6735fc109183f27dca6eb4a6459 Mon Sep 17 00:00:00 2001 From: Lucian Popescu Date: Wed, 18 Nov 2026 12:42:38 +0000 Subject: [PATCH 1/4] Evaluate default arguments at the call site --- cpp2rust/converter/converter.cpp | 133 ++++-------------- cpp2rust/converter/converter.h | 9 +- cpp2rust/converter/converter_lib.cpp | 9 -- cpp2rust/converter/converter_lib.h | 3 - .../converter/models/converter_refcount.cpp | 40 +----- libcc2rs-macros/src/cpp_from.rs | 32 +---- .../dcl.decl/dcl.meaning/dcl.fct.default.cpp | 102 ++++++++++++++ 7 files changed, 145 insertions(+), 183 deletions(-) diff --git a/cpp2rust/converter/converter.cpp b/cpp2rust/converter/converter.cpp index 944bda3c47..b1c9691adf 100644 --- a/cpp2rust/converter/converter.cpp +++ b/cpp2rust/converter/converter.cpp @@ -588,22 +588,10 @@ bool Converter::ConvertVarDeclSkipInit(clang::VarDecl *decl) { StrCat(keyword::kMut); } StrCat(name, token::kColon); - - bool is_parm_with_default_value = false; - if (auto parm = clang::dyn_cast(decl)) { - is_parm_with_default_value = HasUsableDefaultArg(parm); - } - - if (is_parm_with_default_value) { - StrCat("Option<"); - } { PushLazyType lazy(*this, IsGlobalVar(decl) && LazyStaticInit()); Convert(qual_type); } - if (is_parm_with_default_value) { - StrCat('>'); - } return true; } @@ -1024,9 +1012,9 @@ void Converter::EmitConversionAttrs(clang::CXXRecordDecl *decl) { !type->isReferenceType() ? "value" : is_move || type.getNonReferenceType().isConstQualified() ? "ptr" : "mut_ptr"; - StrCat(std::format( - "#[{}({}, {}, {}{})]", is_move ? "cpp_from_move" : "cpp_from", source, - GetCtorName(ctor), param->hasDefaultArg() ? "some_" : "", kind)); + StrCat(std::format("#[{}({}, {}, {})]", + is_move ? "cpp_from_move" : "cpp_from", source, + GetCtorName(ctor), kind)); }); std::unordered_set into_types; ForEachMethod(decl, [&](clang::CXXMethodDecl *method) { @@ -1306,41 +1294,6 @@ bool Converter::VisitFieldDecl(clang::FieldDecl *decl) { return false; } -std::string -Converter::EmitDefaultArgStorage(const clang::ParmVarDecl *param, - const clang::MaterializeTemporaryExpr *temp) { - auto storage = std::format("__{}_default", GetNamedDeclAsString(param)); - StrCat(std::format("let mut {} : Option<{}> = None", storage, - ToString(temp->getType().getUnqualifiedType())), - token::kSemiColon); - return storage; -} - -void Converter::EmitFunctionPreamble(clang::FunctionDecl *decl) { - for (auto *param : DefinitionParameters(decl)) { - if (!HasUsableDefaultArg(param)) { - continue; - } - auto name = GetNamedDeclAsString(param); - auto type = ToString(param->getType()); - std::string value; - if (auto *temp = GetDefaultArgTemporary(param)) { - auto storage = EmitDefaultArgStorage(param, temp); - value = std::format("{}.insert({}) as {}", storage, - ToString(temp->getSubExpr()), type); - } else if (param->getType()->isReferenceType()) { - Buffer buf(*this); - ConvertVarInit(param->getType(), param->getDefaultArg()); - value = std::move(buf).str(); - } else { - value = ToString(param->getDefaultArg()); - } - StrCat(std::format("let mut {} : {} = {}.unwrap_or_else(|| {} {{ {} }})", - name, type, name, keyword_unsafe_, value), - token::kSemiColon); - } -} - void Converter::ConvertDeclContext(clang::DeclContext *decl) { for (auto *child : decl->decls()) { if (IsInMainFile(child) || !decl_ids_.contains(GetID(child))) { @@ -2124,6 +2077,9 @@ Converter::CallInfo Converter::CollectCallInfo(clang::CallExpr *expr) { for (unsigned i = 0; i < num_named_params && i < num_args; ++i) { auto *arg = expr->getArg(i + arg_begin); + if (auto *default_arg = clang::dyn_cast(arg)) { + arg = default_arg->getExpr(); + } CallArg ca{ .param_name = function && !function->getParamDecl(i)->getName().empty() @@ -2132,8 +2088,6 @@ Converter::CallInfo Converter::CollectCallInfo(clang::CallExpr *expr) { .param_type = function ? function->getParamDecl(i)->getType() : proto->getParamType(i), .expr = arg, - .has_default = - function && HasUsableDefaultArg(function->getParamDecl(i)), .kind = (IsLiteral(arg) || info.is_libc_passthrough) ? Kind::Inline : Kind::Hoisted, }; @@ -2237,33 +2191,19 @@ void Converter::EmitArgList(const CallInfo &info) { for (unsigned i = 0; i < info.args.size(); i++) { const auto &ca = info.args[i]; - - if (ca.has_default && clang::isa(ca.expr)) { - StrCat("None", token::kComma); - continue; - } - - if (ca.has_default) { - StrCat("Some"); - } - - { - PushParen push(*this, ca.has_default); - switch (ca.kind) { - case Kind::Hoisted: - StrCat(ca.param_name); - break; - case Kind::Materialized: - StrCat(ca.ref_temp_name); - break; - case Kind::Inline: - ConvertParamTy(ca.param_type, ca.expr); - if (info.is_libc_passthrough) { - StrCat( - std::format("as {}", Mapper::GetParamType(*this, info.expr, i))); - } - break; + switch (ca.kind) { + case Kind::Hoisted: + StrCat(ca.param_name); + break; + case Kind::Materialized: + StrCat(ca.ref_temp_name); + break; + case Kind::Inline: + ConvertParamTy(ca.param_type, ca.expr); + if (info.is_libc_passthrough) { + StrCat(std::format("as {}", Mapper::GetParamType(*this, info.expr, i))); } + break; } StrCat(token::kComma); @@ -4078,31 +4018,15 @@ void Converter::ConvertCXXConstructExprArgs(clang::CXXConstructExpr *expr) { GetCtorName(ctor)); PushParen paren(*this); - unsigned arg_idx = 0; - for (unsigned param_idx = 0; param_idx < ctor->getNumParams(); ++param_idx) { - auto param = ctor->getParamDecl(param_idx); - auto param_type = param->getType(); - bool has_default = HasUsableDefaultArg(param); - - if (arg_idx < expr->getNumArgs() && - clang::isa(expr->getArg(arg_idx))) { - assert(has_default); - ++arg_idx; - StrCat("None", token::kComma); - continue; + for (unsigned i = 0; i < ctor->getNumParams(); ++i) { + assert(i < expr->getNumArgs()); + clang::Expr *arg = expr->getArg(i); + if (auto *default_arg = clang::dyn_cast(arg)) { + arg = default_arg->getExpr(); } - - if (arg_idx < expr->getNumArgs()) { - clang::Expr *arg = expr->getArg(arg_idx++); + { PushBrace brace(*this); - if (has_default) { - StrCat("Some"); - } - PushParen some(*this, has_default); - ConvertVarInit(param_type, arg); - } else { - assert(has_default); - StrCat("None"); + ConvertVarInit(ctor->getParamDecl(i)->getType(), arg); } StrCat(token::kComma); } @@ -5180,8 +5104,13 @@ void Converter::AddDefaultTrait(const clang::RecordDecl *decl) { auto *cxx = clang::dyn_cast(decl); if (auto *default_ctor = cxx ? GetUserDefinedDefaultConstructor(cxx) : nullptr) { + std::vector args; + for (auto *param : default_ctor->parameters()) { + args.push_back(clang::CXXDefaultArgExpr::Create( + ctx_, param->getLocation(), param, nullptr, default_ctor)); + } Convert(MakeConstructExpr(ctx_, ctx_.getCanonicalTagType(decl), - default_ctor, {})); + default_ctor, args)); } else { EmitDefaultStructLiteral(decl); } diff --git a/cpp2rust/converter/converter.h b/cpp2rust/converter/converter.h index d2d92e8482..eee4367137 100644 --- a/cpp2rust/converter/converter.h +++ b/cpp2rust/converter/converter.h @@ -136,13 +136,7 @@ class Converter : public clang::RecursiveASTVisitor { virtual bool VisitFunctionDecl(clang::FunctionDecl *decl); - virtual void EmitFunctionPreamble(clang::FunctionDecl *decl); - - // Declares the storage of the temporary temp that the default argument of - // param is bound to, and returns its name. - std::string - EmitDefaultArgStorage(const clang::ParmVarDecl *param, - const clang::MaterializeTemporaryExpr *temp); + virtual void EmitFunctionPreamble(clang::FunctionDecl *) {} virtual void ConvertFunctionBody(clang::FunctionDecl *decl); @@ -404,7 +398,6 @@ class Converter : public clang::RecursiveASTVisitor { std::string ref_temp_name; clang::QualType param_type; clang::Expr *expr; - bool has_default; Kind kind; }; diff --git a/cpp2rust/converter/converter_lib.cpp b/cpp2rust/converter/converter_lib.cpp index 74f4520448..da9b2f3533 100644 --- a/cpp2rust/converter/converter_lib.cpp +++ b/cpp2rust/converter/converter_lib.cpp @@ -990,15 +990,6 @@ bool HasUsableDefaultArg(const clang::ParmVarDecl *param) { return param->hasDefaultArg() && !param->hasUninstantiatedDefaultArg(); } -const clang::MaterializeTemporaryExpr * -GetDefaultArgTemporary(const clang::ParmVarDecl *param) { - if (!param->getType()->isReferenceType()) { - return nullptr; - } - return clang::dyn_cast( - param->getDefaultArg()->IgnoreParens()); -} - std::string GetFileName(const clang::Decl *decl) { const auto &ctx = decl->getASTContext(); const auto full_location = ctx.getFullLoc(decl->getBeginLoc()); diff --git a/cpp2rust/converter/converter_lib.h b/cpp2rust/converter/converter_lib.h index 75be0f5fd7..78138b46f1 100644 --- a/cpp2rust/converter/converter_lib.h +++ b/cpp2rust/converter/converter_lib.h @@ -200,9 +200,6 @@ GetUserDefinedDefaultConstructor(const clang::CXXRecordDecl *decl); bool HasUsableDefaultArg(const clang::ParmVarDecl *param); -const clang::MaterializeTemporaryExpr * -GetDefaultArgTemporary(const clang::ParmVarDecl *param); - std::string GetFileName(const clang::Decl *decl); unsigned GetLineNumber(const clang::Decl *decl); diff --git a/cpp2rust/converter/models/converter_refcount.cpp b/cpp2rust/converter/models/converter_refcount.cpp index 01324f30cb..7ddb4c2b7c 100644 --- a/cpp2rust/converter/models/converter_refcount.cpp +++ b/cpp2rust/converter/models/converter_refcount.cpp @@ -154,12 +154,11 @@ bool ConverterRefCount::NeedsMut(const clang::VarDecl *decl, if (!in_function_formals_) { return true; } - // Parameters are modified in place, except for those with a default value, - // which are unwrapped into a variable, and those of a function declared + // Parameters are modified in place, except for those of a function declared // without a body, which can't be mutable. auto *param = clang::cast(decl); auto *function = clang::cast(param->getDeclContext()); - return !HasUsableDefaultArg(param) && !function->isPureVirtual() && + return !function->isPureVirtual() && method_target_ != MethodTarget::TraitDecl; } @@ -818,40 +817,13 @@ void ConverterRefCount::EmitFunctionPreamble(clang::FunctionDecl *decl) { "Rc::new(RefCell::new(__args.into()));"); } for (auto *param : DefinitionParameters(decl)) { - auto name = GetNamedDeclAsString(param); - if (param->getType()->isReferenceType()) { - if (!HasUsableDefaultArg(param)) { - continue; - } - auto type = ToString(param->getType()); - std::string value; - if (auto *temp = GetDefaultArgTemporary(param)) { - auto storage = EmitDefaultArgStorage(param, temp); - value = std::format("{}.insert(Rc::new(RefCell::new({}))).as_pointer()", - storage, ToString(temp->getSubExpr())); - } else { - Buffer buf(*this); - ConvertVarInit(param->getType(), param->getDefaultArg()); - value = std::move(buf).str(); - } - StrCat(std::format("let {} : {} = {}.unwrap_or_else(|| {})", name, type, - name, value), - token::kSemiColon); - continue; - } - // Unboxed parameters are used in place. - bool unboxed = IsUnboxedVar(param); - if (unboxed && !HasUsableDefaultArg(param)) { + if (param->getType()->isReferenceType() || IsUnboxedVar(param)) { continue; } - auto init = HasUsableDefaultArg(param) - ? std::format("{}.unwrap_or_else(|| {})", name, - ToString(param->getDefaultArg())) - : name; - PushConversionKind push(*this, ConversionKind::Unboxed, unboxed); - StrCat(std::format("let {}{} : {} = {}", unboxed ? "mut " : "", name, - ToString(param->getType()), BoxValue(std::move(init))), + auto name = GetNamedDeclAsString(param); + StrCat(std::format("let {} : {} = {}", name, ToString(param->getType()), + BoxValue(std::string(name))), token::kSemiColon); } } diff --git a/libcc2rs-macros/src/cpp_from.rs b/libcc2rs-macros/src/cpp_from.rs index 36b30db1a3..bea36f5492 100644 --- a/libcc2rs-macros/src/cpp_from.rs +++ b/libcc2rs-macros/src/cpp_from.rs @@ -9,7 +9,6 @@ use syn::{DeriveInput, Ident, Token, Type, parse_macro_input}; // #[derive(CppFrom)] // #[cpp_from(i32, new, value)] // S(int) -// #[cpp_from(i32, new_1, some_value)] // S(int = 0) // #[cpp_from(T, new_2, ptr)] // S(const T &) // #[cpp_from(T, new_3, mut_ptr)] // S(T &) // #[cpp_from_move(T, new_4, ptr)] // S(T &&) @@ -21,9 +20,8 @@ use syn::{DeriveInput, Ident, Token, Type, parse_macro_input}; // of S, and libcc2rs::CppFrom for each type S converts to with a // conversion operator, such that the rules convert values as C++ does // implicitly. `value` passes the pointee to the constructor, `ptr` the -// pointer, `mut_ptr` a mutable pointer, and `some_` wraps the argument in -// Some for a defaulted parameter. CppFromUnsafe is the same for the unsafe -// model. +// pointer and `mut_ptr` a mutable pointer. CppFromUnsafe is the same for the +// unsafe model. #[derive(Clone, Copy)] enum Kind { @@ -36,7 +34,6 @@ struct Conversion { ty: Type, method: Ident, kind: Kind, - some: bool, } impl Parse for Conversion { @@ -45,36 +42,22 @@ impl Parse for Conversion { input.parse::()?; let method = input.parse()?; let mut kind = Kind::Ptr; - let mut some = false; if input.peek(Token![,]) { input.parse::()?; let spelled: Ident = input.parse()?; - let name = spelled.to_string(); - let name = match name.strip_prefix("some_") { - Some(rest) => { - some = true; - rest - } - None => name.as_str(), - }; - kind = match name { + kind = match spelled.to_string().as_str() { "value" => Kind::Value, "ptr" => Kind::Ptr, "mut_ptr" => Kind::MutPtr, _ => { return Err(syn::Error::new( spelled.span(), - "expected `value`, `ptr` or `mut_ptr`, optionally prefixed by `some_`", + "expected `value`, `ptr` or `mut_ptr`", )); } }; } - Ok(Conversion { - ty, - method, - kind, - some, - }) + Ok(Conversion { ty, method, kind }) } } @@ -97,11 +80,6 @@ fn ctor_call( (Kind::MutPtr, true) if !is_move => quote!(src.cast_mut()), (Kind::Ptr | Kind::MutPtr, _) => quote!(src), }; - let arg = if conversion.some { - quote!(Some(#arg)) - } else { - arg - }; quote!(#name::#method(#arg)) } diff --git a/tests/std/dcl.dcl/dcl.decl/dcl.meaning/dcl.fct.default.cpp b/tests/std/dcl.dcl/dcl.decl/dcl.meaning/dcl.fct.default.cpp index 763593b915..15a6c46cf5 100644 --- a/tests/std/dcl.dcl/dcl.decl/dcl.meaning/dcl.fct.default.cpp +++ b/tests/std/dcl.dcl/dcl.decl/dcl.meaning/dcl.fct.default.cpp @@ -1,5 +1,7 @@ // ADDITIONAL_COMPILE_FLAGS: -std=c++23 +#include #include +#include // N4950 [dcl.fct.default]/5: the names in the default argument are looked up // at the point where the default argument appears. @@ -78,9 +80,109 @@ void test() { } } // namespace p9_static_member_hides_namespace_variable +// N4950 [dcl.fct.default]/9: "A default argument is evaluated each time the +// function is called with no argument for the corresponding parameter." +namespace p9_evaluated_each_time_called { +static int defaults_evaluated; + +int count_default() { return ++defaults_evaluated; } + +int combine(int first, int second = count_default()) { + return first * 100 + second; +} + +struct Widget { + int id; + + Widget(int id = count_default()) : id(id) {} +}; + +class Account { + static inline int base = 10; + + enum { kBonus = 5 }; + + static int twice(int value) { return 2 * value; } + +public: + int balance = 0; + + int deposit(int amount = twice(base) + kBonus) { + balance += amount; + return balance; + } + + static void set_base(int value) { base = value; } +}; + +void test() { + defaults_evaluated = 0; + assert(combine(1) == 101); + assert(combine(2) == 202); + assert(combine(3, 9) == 309); + assert(defaults_evaluated == 2); + + int first = 0; + assert(combine(++first) == 103); + assert(first == 1); + assert(defaults_evaluated == 3); + + Widget a; + Widget b(40); + Widget c; + assert(a.id == 4); + assert(b.id == 40); + assert(c.id == 5); + assert(defaults_evaluated == 5); + + Account account; + assert(account.deposit() == 25); + Account::set_base(1); + assert(account.deposit() == 32); + assert(account.deposit(3) == 35); +} +} // namespace p9_evaluated_each_time_called + +// N4950 [dcl.fct.default]/9: "A default argument is not part of the type of a +// function." +namespace p9_not_part_of_the_type { +int add(int a, int b = 2) { return a + b; } + +struct Scale { + static int times(int a, int b = 3) { return a * b; } +}; + +bool descending(const int &a, const int &b = 0) { return a > b; } + +void test() { + int (*p)(int, int) = add; + int (*q)(int, int) = &add; + assert(p == q); + assert(p(1, 5) == 6); + assert(q(2, 5) == 7); + assert(add(1) == 3); + + int (*s)(int, int) = &Scale::times; + assert(s(2, 5) == 10); + assert(Scale::times(2) == 6); + + std::vector values = {3, -1, 4, 1, 5}; + std::stable_sort(values.begin(), values.end(), descending); + assert(values[0] == 5); + assert(values[1] == 4); + assert(values[2] == 3); + assert(values[3] == 1); + assert(values[4] == -1); + assert(descending(1)); + assert(!descending(-1)); +} +} // namespace p9_not_part_of_the_type + int main() { p5_names_looked_up_at_declaration::test(); p9_evaluated_on_each_call::test(); p9_static_member_hides_namespace_variable::test(); + p9_evaluated_each_time_called::test(); + p9_not_part_of_the_type::test(); return 0; } From 2a2f4a1f146b7075ca51d0e78f6b261b8f93bdf5 Mon Sep 17 00:00:00 2001 From: Lucian Popescu Date: Wed, 18 Nov 2026 12:42:38 +0000 Subject: [PATCH 2/4] Update tests --- .../p16-default-argument-lambda-in-class.rs | 39 +++- .../p16-default-argument-lambda-in-class.rs | 53 +++-- .../out/refcount/class.mem.general.rs | 9 +- .../class.mem/out/unsafe/class.mem.general.rs | 14 +- .../out/refcount/dcl.fct.default.rs | 205 ++++++++++++++-- .../dcl.meaning/out/unsafe/dcl.fct.default.rs | 219 ++++++++++++++++-- .../refcount/default_function_arguments.rs | 71 +++--- tests/unit/out/refcount/destructor_params.rs | 6 +- tests/unit/out/refcount/fn_ptr_default_arg.rs | 11 +- .../out/refcount/lambda_immediate_init.rs | 29 +-- .../refcount/pair_converting_construction.rs | 9 +- tests/unit/out/refcount/size_t_pointer.rs | 10 +- .../unit/out/refcount/template_default_arg.rs | 19 +- tests/unit/out/refcount/unboxed_locals.rs | 7 +- .../out/unsafe/default_function_arguments.rs | 77 +++--- tests/unit/out/unsafe/destructor_params.rs | 5 +- tests/unit/out/unsafe/fn_ptr_default_arg.rs | 9 +- .../unit/out/unsafe/lambda_immediate_init.rs | 29 +-- .../unsafe/pair_converting_construction.rs | 9 +- tests/unit/out/unsafe/size_t_pointer.rs | 10 +- tests/unit/out/unsafe/template_default_arg.rs | 19 +- tests/unit/out/unsafe/unboxed_locals.rs | 7 +- 22 files changed, 625 insertions(+), 241 deletions(-) diff --git a/tests/std/basic/basic.def.odr/p16-default-argument-lambda-in-class/out/refcount/p16-default-argument-lambda-in-class.rs b/tests/std/basic/basic.def.odr/p16-default-argument-lambda-in-class/out/refcount/p16-default-argument-lambda-in-class.rs index a7c1b2d88a..0788ec32d4 100644 --- a/tests/std/basic/basic.def.odr/p16-default-argument-lambda-in-class/out/refcount/p16-default-argument-lambda-in-class.rs +++ b/tests/std/basic/basic.def.odr/p16-default-argument-lambda-in-class/out/refcount/p16-default-argument-lambda-in-class.rs @@ -90,7 +90,15 @@ pub fn main() { fn main_0() -> i32 { assert!((ctorCount_0.with(|rc| *rc.borrow()) == 0)); let x: Value = Rc::new(RefCell::new(::default())); - ({ XImpl::h(&x.as_pointer(), true, None) }); + ({ + XImpl::h( + &x.as_pointer(), + true, + FnPtr::::new(|| { + ({ LifetimeTrackedImpl::use_(&local_4.with(|v| v.as_pointer())) }); + }), + ) + }); assert!((ctorCount_0.with(|rc| *rc.borrow()) == 1)); assert!((useCount_3.with(|rc| *rc.borrow()) == 1)); ({ b_call_6() }); @@ -100,7 +108,15 @@ fn main_0() -> i32 { } pub fn b_call_6() { let x: Value = Rc::new(RefCell::new(::default())); - ({ XImpl::h(&x.as_pointer(), true, None) }); + ({ + XImpl::h( + &x.as_pointer(), + true, + FnPtr::::new(|| { + ({ LifetimeTrackedImpl::use_(&local_4.with(|v| v.as_pointer())) }); + }), + ) + }); } pub trait AssertsDtorCountAtExitImpl { fn destructor(&self); @@ -131,17 +147,20 @@ impl LifetimeTrackedImpl for Ptr { } } pub trait XImpl { - fn h(&self, cond: bool, p: Option>); + fn h(&self, cond: bool, p: FnPtr); } impl XImpl for Ptr { - fn h(&self, mut cond: bool, p: Option>) { - let mut p: FnPtr = p.unwrap_or_else(|| { - FnPtr::::new(|| { - ({ LifetimeTrackedImpl::use_(&local_4.with(|v| v.as_pointer())) }); - }) - }); + fn h(&self, mut cond: bool, mut p: FnPtr) { if cond { - ({ XImpl::h(self, false, None) }); + ({ + XImpl::h( + self, + false, + FnPtr::::new(|| { + ({ LifetimeTrackedImpl::use_(&local_4.with(|v| v.as_pointer())) }); + }), + ) + }); } else { ({ p.call() }); } diff --git a/tests/std/basic/basic.def.odr/p16-default-argument-lambda-in-class/out/unsafe/p16-default-argument-lambda-in-class.rs b/tests/std/basic/basic.def.odr/p16-default-argument-lambda-in-class/out/unsafe/p16-default-argument-lambda-in-class.rs index e0a0cdc47c..8402cf20a6 100644 --- a/tests/std/basic/basic.def.odr/p16-default-argument-lambda-in-class/out/unsafe/p16-default-argument-lambda-in-class.rs +++ b/tests/std/basic/basic.def.odr/p16-default-argument-lambda-in-class/out/unsafe/p16-default-argument-lambda-in-class.rs @@ -74,18 +74,21 @@ impl AssertsDtorCountAtExit { #[derive(Copy, Clone, VaArg, FnPtrArg, Default)] pub struct X {} impl X { - pub unsafe fn h(&mut self, mut cond: bool, mut p: Option>) { - let mut p: Option = p.unwrap_or_else(|| unsafe { - Some(|| unsafe { - (unsafe { - LifetimeTracked::use_( - &(*std::cell::LazyCell::force_mut(&mut *&raw mut local_4)), - ) - }); - }) - }); + pub unsafe fn h(&mut self, mut cond: bool, mut p: Option) { if cond { - (unsafe { X::h(self, false, None) }); + (unsafe { + X::h( + self, + false, + Some(|| unsafe { + (unsafe { + LifetimeTracked::use_( + &(*std::cell::LazyCell::force_mut(&mut *&raw mut local_4)), + ) + }); + }), + ) + }); } else { (unsafe { (p).unwrap()() }); } @@ -104,7 +107,19 @@ pub fn main() { unsafe fn main_0() -> i32 { assert!(((*std::cell::LazyCell::force_mut(&mut *&raw mut ctorCount_0)) == (0))); let mut x: X = ::default(); - (unsafe { X::h(&mut x, true, None) }); + (unsafe { + X::h( + &mut x, + true, + Some(|| unsafe { + (unsafe { + LifetimeTracked::use_( + &(*std::cell::LazyCell::force_mut(&mut *&raw mut local_4)), + ) + }); + }), + ) + }); assert!(((*std::cell::LazyCell::force_mut(&mut *&raw mut ctorCount_0)) == (1))); assert!(((*std::cell::LazyCell::force_mut(&mut *&raw mut useCount_3)) == (1))); (unsafe { b_call_6() }); @@ -114,7 +129,19 @@ unsafe fn main_0() -> i32 { } pub unsafe fn b_call_6() { let mut x: X = ::default(); - (unsafe { X::h(&mut x, true, None) }); + (unsafe { + X::h( + &mut x, + true, + Some(|| unsafe { + (unsafe { + LifetimeTracked::use_( + &(*std::cell::LazyCell::force_mut(&mut *&raw mut local_4)), + ) + }); + }), + ) + }); } pub unsafe fn __cpp2rust_init_globals() { std::cell::LazyCell::force(&*&raw const ctorCount_0); diff --git a/tests/std/class/class.mem/out/refcount/class.mem.general.rs b/tests/std/class/class.mem/out/refcount/class.mem.general.rs index 5dac9829c8..9f10275a2f 100644 --- a/tests/std/class/class.mem/out/refcount/class.mem.general.rs +++ b/tests/std/class/class.mem/out/refcount/class.mem.general.rs @@ -62,7 +62,7 @@ pub fn test_6() { (({ p7_2_default_argument_names_later_member_HasLaterStaticStepImpl::advance( &object.as_pointer(), - None, + step_5.with(|rc| *rc.borrow()), ) }) == 5) ); @@ -70,7 +70,7 @@ pub fn test_6() { (({ p7_2_default_argument_names_later_member_HasLaterStaticStepImpl::advance( &object.as_pointer(), - Some(2), + 2, ) }) == 7) ); @@ -106,13 +106,12 @@ impl p7_1_function_body_names_later_member_HasLaterMembersImpl } } pub trait p7_2_default_argument_names_later_member_HasLaterStaticStepImpl { - fn advance(&self, amount: Option) -> i32; + fn advance(&self, amount: i32) -> i32; } impl p7_2_default_argument_names_later_member_HasLaterStaticStepImpl for Ptr { - fn advance(&self, amount: Option) -> i32 { - let mut amount: i32 = amount.unwrap_or_else(|| step_5.with(|rc| *rc.borrow())); + fn advance(&self, mut amount: i32) -> i32 { { let __rhs = amount; field!((*self), total).with_mut(|__v| *__v = *__v + __rhs) diff --git a/tests/std/class/class.mem/out/unsafe/class.mem.general.rs b/tests/std/class/class.mem/out/unsafe/class.mem.general.rs index 74992ae3dc..451ff7f946 100644 --- a/tests/std/class/class.mem/out/unsafe/class.mem.general.rs +++ b/tests/std/class/class.mem/out/unsafe/class.mem.general.rs @@ -51,9 +51,7 @@ pub struct p7_2_default_argument_names_later_member_HasLaterStaticStep { pub total: i32, } impl p7_2_default_argument_names_later_member_HasLaterStaticStep { - pub unsafe fn advance(&mut self, mut amount: Option) -> i32 { - let mut amount: i32 = amount - .unwrap_or_else(|| unsafe { (*std::cell::LazyCell::force_mut(&mut *&raw mut step_5)) }); + pub unsafe fn advance(&mut self, mut amount: i32) -> i32 { self.total += amount; return self.total; } @@ -68,15 +66,15 @@ pub unsafe fn test_6() { ::default(); assert!( ((unsafe { - p7_2_default_argument_names_later_member_HasLaterStaticStep::advance(&mut object, None) + p7_2_default_argument_names_later_member_HasLaterStaticStep::advance( + &mut object, + (*std::cell::LazyCell::force_mut(&mut *&raw mut step_5)), + ) }) == (5)) ); assert!( ((unsafe { - p7_2_default_argument_names_later_member_HasLaterStaticStep::advance( - &mut object, - Some(2), - ) + p7_2_default_argument_names_later_member_HasLaterStaticStep::advance(&mut object, 2) }) == (7)) ); assert!(((*std::cell::LazyCell::force_mut(&mut *&raw mut step_4)) == (100))); diff --git a/tests/std/dcl.dcl/dcl.decl/dcl.meaning/out/refcount/dcl.fct.default.rs b/tests/std/dcl.dcl/dcl.decl/dcl.meaning/out/refcount/dcl.fct.default.rs index 4c38f879d9..89de77e3f5 100644 --- a/tests/std/dcl.dcl/dcl.decl/dcl.meaning/out/refcount/dcl.fct.default.rs +++ b/tests/std/dcl.dcl/dcl.decl/dcl.meaning/out/refcount/dcl.fct.default.rs @@ -16,19 +16,18 @@ pub fn scaled_2(mut amount: i32) -> i32 { (*scaled_calls_0.with(Value::clone).borrow_mut()).prefix_inc(); return (amount * 10); } -pub fn total_3(base: Option) -> i32 { - let mut base: i32 = base.unwrap_or_else(|| ({ scaled_2(rate_1.with(|rc| *rc.borrow())) })); +pub fn total_3(mut base: i32) -> i32 { return base; } pub fn test_4() { rate_1.with(|rc| *rc.borrow_mut() = 2); { let mut rate: i32 = 3; - assert!((({ total_3(None,) }) == 20)); + assert!((({ total_3(({ scaled_2(rate_1.with(|rc| *rc.borrow()),) }),) }) == 20)); assert!((scaled_calls_0.with(|rc| *rc.borrow()) == 1)); assert!((rate == 3)); } - assert!((({ total_3(Some(5),) }) == 5)); + assert!((({ total_3(5,) }) == 5)); assert!((scaled_calls_0.with(|rc| *rc.borrow()) == 1)); } thread_local!( @@ -37,17 +36,16 @@ thread_local!( pub fn next_ticket_6() -> i32 { return ((*(*next_ticket_calls_5.with(Value::clone).borrow_mut()).prefix_inc()) * 100); } -pub fn issue_7(ticket: Option) -> i32 { - let mut ticket: i32 = ticket.unwrap_or_else(|| ({ next_ticket_6() })); +pub fn issue_7(mut ticket: i32) -> i32 { return ticket; } pub fn test_8() { assert!((next_ticket_calls_5.with(|rc| *rc.borrow()) == 0)); - assert!((({ issue_7(None,) }) == 100)); + assert!((({ issue_7(({ next_ticket_6() }),) }) == 100)); assert!((next_ticket_calls_5.with(|rc| *rc.borrow()) == 1)); - assert!((({ issue_7(Some(7),) }) == 7)); + assert!((({ issue_7(7,) }) == 7)); assert!((next_ticket_calls_5.with(|rc| *rc.borrow()) == 1)); - assert!((({ issue_7(None,) }) == 200)); + assert!((({ issue_7(({ next_ticket_6() }),) }) == 200)); assert!((next_ticket_calls_5.with(|rc| *rc.borrow()) == 2)); } thread_local!( @@ -75,7 +73,7 @@ pub fn test_11() { (({ p9_static_member_hides_namespace_variable_HasStaticStepImpl::advance( &object.as_pointer(), - None, + step_10.with(|rc| *rc.borrow()), ) }) == 5) ); @@ -84,7 +82,7 @@ pub fn test_11() { (({ p9_static_member_hides_namespace_variable_HasStaticStepImpl::advance( &object.as_pointer(), - None, + step_10.with(|rc| *rc.borrow()), ) }) == 11) ); @@ -92,12 +90,174 @@ pub fn test_11() { (({ p9_static_member_hides_namespace_variable_HasStaticStepImpl::advance( &object.as_pointer(), - Some(2), + 2, ) }) == 13) ); assert!((step_9.with(|rc| *rc.borrow()) == 100)); } +thread_local!( + pub static defaults_evaluated_12: Value = Rc::new(RefCell::new(0_i32)); +); +pub fn count_default_13() -> i32 { + return (*(*defaults_evaluated_12.with(Value::clone).borrow_mut()).prefix_inc()); +} +pub fn combine_14(mut first: i32, mut second: i32) -> i32 { + return ((first * 100) + second); +} +#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg, CppFrom)] +#[cpp_from(i32, new, value)] +#[byte_size(4)] +pub struct p9_evaluated_each_time_called_Widget { + #[offset(0)] + pub id: i32, +} +impl p9_evaluated_each_time_called_Widget { + pub fn new(mut id: i32) -> Self { + Self { id: id } + } +} +impl Default for p9_evaluated_each_time_called_Widget { + fn default() -> Self { + { p9_evaluated_each_time_called_Widget::new({ ({ count_default_13() }) }) } + } +} +thread_local!( + static base_15: Value = Rc::new(RefCell::new(10)); +); +pub type anon_16 = u32; +pub const anon_16_kBonus: anon_16 = 5; +#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg)] +#[byte_size(4)] +pub struct p9_evaluated_each_time_called_Account { + #[offset(0)] + pub balance: i32, +} +impl p9_evaluated_each_time_called_Account { + fn twice(mut value: i32) -> i32 { + return (2 * value); + } + pub fn set_base(mut value: i32) { + base_15.with(|rc| *rc.borrow_mut() = value); + } +} +impl Default for p9_evaluated_each_time_called_Account { + fn default() -> Self { + { p9_evaluated_each_time_called_Account { balance: 0 } } + } +} +pub fn test_17() { + defaults_evaluated_12.with(|rc| *rc.borrow_mut() = 0); + assert!((({ combine_14(1, ({ count_default_13() }),) }) == 101)); + assert!((({ combine_14(2, ({ count_default_13() }),) }) == 202)); + assert!((({ combine_14(3, 9,) }) == 309)); + assert!((defaults_evaluated_12.with(|rc| *rc.borrow()) == 2)); + let mut first: i32 = 0; + assert!((({ combine_14((*first.prefix_inc()), ({ count_default_13() }),) }) == 103)); + assert!((first == 1)); + assert!((defaults_evaluated_12.with(|rc| *rc.borrow()) == 3)); + let mut a: p9_evaluated_each_time_called_Widget = + p9_evaluated_each_time_called_Widget::new({ ({ count_default_13() }) }); + let mut b: p9_evaluated_each_time_called_Widget = + p9_evaluated_each_time_called_Widget::new({ 40 }); + let mut c: p9_evaluated_each_time_called_Widget = + p9_evaluated_each_time_called_Widget::new({ ({ count_default_13() }) }); + assert!((a.id == 4)); + assert!((b.id == 40)); + assert!((c.id == 5)); + assert!((defaults_evaluated_12.with(|rc| *rc.borrow()) == 5)); + let account: Value = Rc::new(RefCell::new( + ::default(), + )); + assert!( + (({ + p9_evaluated_each_time_called_AccountImpl::deposit( + &account.as_pointer(), + (({ + p9_evaluated_each_time_called_Account::twice(base_15.with(|rc| *rc.borrow())) + }) + (anon_16_kBonus as i32)), + ) + }) == 25) + ); + ({ p9_evaluated_each_time_called_Account::set_base(1) }); + assert!( + (({ + p9_evaluated_each_time_called_AccountImpl::deposit( + &account.as_pointer(), + (({ + p9_evaluated_each_time_called_Account::twice(base_15.with(|rc| *rc.borrow())) + }) + (anon_16_kBonus as i32)), + ) + }) == 32) + ); + assert!( + (({ p9_evaluated_each_time_called_AccountImpl::deposit(&account.as_pointer(), 3,) }) == 35) + ); +} +pub fn add_18(mut a: i32, mut b: i32) -> i32 { + return (a + b); +} +#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[byte_size(1)] +pub struct p9_not_part_of_the_type_Scale {} +impl p9_not_part_of_the_type_Scale { + pub fn times(mut a: i32, mut b: i32) -> i32 { + return (a * b); + } +} +pub fn descending_19(a: Ptr, b: Ptr) -> bool { + return ({ (a.read()) } > { (b.read()) }); +} +pub fn test_20() { + let mut p: FnPtr i32> = FnPtr:: i32>::new(add_18); + let mut q: FnPtr i32> = (FnPtr:: i32>::new(add_18)); + assert!(({ (p).clone() } == { (q).clone() })); + assert!((({ p.call(1, 5,) }) == 6)); + assert!((({ q.call(2, 5,) }) == 7)); + assert!((({ add_18(1, 2,) }) == 3)); + let mut s: FnPtr i32> = + (FnPtr:: i32>::new(p9_not_part_of_the_type_Scale::times)); + assert!((({ s.call(2, 5,) }) == 10)); + assert!((({ p9_not_part_of_the_type_Scale::times(2, 3,) }) == 6)); + let values: Value> = Rc::new(RefCell::new(Vec::from([3, -1_i32, 4, 1, 5]))); + match ({ values.decay().begin() },) { + (mut __tmp_0,) => { + let __n = values.decay().end().iter_distance(&__tmp_0); + let mut __order: Vec = (0..__n).collect(); + __order.sort_by(|&__x, &__y| { + let __a = __tmp_0.iter_at(__x); + let __b = __tmp_0.iter_at(__y); + if descending_19.call(__a.clone(), __b.clone()) { + std::cmp::Ordering::Less + } else if descending_19.call(__b, __a) { + std::cmp::Ordering::Greater + } else { + std::cmp::Ordering::Equal + } + }); + reorder(__order, |__i| __tmp_0.iter_at(__i)); + } + }; + assert!(({ ({ (*values.borrow())[0_usize] } == 5) })); + assert!(({ ({ (*values.borrow())[1_usize] } == 4) })); + assert!(({ ({ (*values.borrow())[2_usize] } == 3) })); + assert!(({ ({ (*values.borrow())[3_usize] } == 1) })); + assert!(({ ({ (*values.borrow())[4_usize] } == -1_i32) })); + assert!( + ({ + let _a: Value = Rc::new(RefCell::new(1)); + let _b: Value = Rc::new(RefCell::new(0)); + descending_19(_a.as_pointer(), _b.as_pointer()) + }) + ); + assert!( + !({ + let _a: Value = Rc::new(RefCell::new(-1_i32)); + let _b: Value = Rc::new(RefCell::new(0)); + descending_19(_a.as_pointer(), _b.as_pointer()) + }) + ); +} pub fn main() { __cpp2rust_init_globals(); std::process::exit(main_0()); @@ -106,16 +266,29 @@ fn main_0() -> i32 { ({ test_4() }); ({ test_8() }); ({ test_11() }); + ({ test_17() }); + ({ test_20() }); return 0; } +pub trait p9_evaluated_each_time_called_AccountImpl { + fn deposit(&self, amount: i32) -> i32; +} +impl p9_evaluated_each_time_called_AccountImpl for Ptr { + fn deposit(&self, mut amount: i32) -> i32 { + { + let __rhs = amount; + field!((*self), balance).with_mut(|__v| *__v = *__v + __rhs) + }; + return (*self).with(|__s| __s.balance); + } +} pub trait p9_static_member_hides_namespace_variable_HasStaticStepImpl { - fn advance(&self, amount: Option) -> i32; + fn advance(&self, amount: i32) -> i32; } impl p9_static_member_hides_namespace_variable_HasStaticStepImpl for Ptr { - fn advance(&self, amount: Option) -> i32 { - let mut amount: i32 = amount.unwrap_or_else(|| step_10.with(|rc| *rc.borrow())); + fn advance(&self, mut amount: i32) -> i32 { { let __rhs = amount; field!((*self), total).with_mut(|__v| *__v = *__v + __rhs) @@ -129,4 +302,6 @@ pub fn __cpp2rust_init_globals() { let _ = next_ticket_calls_5.with(|_| ()); let _ = step_9.with(|_| ()); let _ = step_10.with(|_| ()); + let _ = defaults_evaluated_12.with(|_| ()); + let _ = base_15.with(|_| ()); } diff --git a/tests/std/dcl.dcl/dcl.decl/dcl.meaning/out/unsafe/dcl.fct.default.rs b/tests/std/dcl.dcl/dcl.decl/dcl.meaning/out/unsafe/dcl.fct.default.rs index 2638dcbb5c..d805ae66b0 100644 --- a/tests/std/dcl.dcl/dcl.decl/dcl.meaning/out/unsafe/dcl.fct.default.rs +++ b/tests/std/dcl.dcl/dcl.decl/dcl.meaning/out/unsafe/dcl.fct.default.rs @@ -13,21 +13,24 @@ pub unsafe fn scaled_2(mut amount: i32) -> i32 { (*std::cell::LazyCell::force_mut(&mut *&raw mut scaled_calls_0)).prefix_inc(); return ((amount) * (10)); } -pub unsafe fn total_3(mut base: Option) -> i32 { - let mut base: i32 = base.unwrap_or_else(|| unsafe { - (unsafe { scaled_2((*std::cell::LazyCell::force_mut(&mut *&raw mut rate_1))) }) - }); +pub unsafe fn total_3(mut base: i32) -> i32 { return base; } pub unsafe fn test_4() { (*std::cell::LazyCell::force_mut(&mut *&raw mut rate_1)) = 2; { let mut rate: i32 = 3; - assert!(((unsafe { total_3(None,) }) == (20))); + assert!( + ((unsafe { + total_3( + (unsafe { scaled_2((*std::cell::LazyCell::force_mut(&mut *&raw mut rate_1))) }), + ) + }) == (20)) + ); assert!(((*std::cell::LazyCell::force_mut(&mut *&raw mut scaled_calls_0)) == (1))); assert!(((rate) == (3))); } - assert!(((unsafe { total_3(Some(5),) }) == (5))); + assert!(((unsafe { total_3(5,) }) == (5))); assert!(((*std::cell::LazyCell::force_mut(&mut *&raw mut scaled_calls_0)) == (1))); } pub static mut next_ticket_calls_5: std::cell::LazyCell = @@ -36,17 +39,16 @@ pub unsafe fn next_ticket_6() -> i32 { return ((*(*std::cell::LazyCell::force_mut(&mut *&raw mut next_ticket_calls_5)).prefix_inc()) * (100)); } -pub unsafe fn issue_7(mut ticket: Option) -> i32 { - let mut ticket: i32 = ticket.unwrap_or_else(|| unsafe { (unsafe { next_ticket_6() }) }); +pub unsafe fn issue_7(mut ticket: i32) -> i32 { return ticket; } pub unsafe fn test_8() { assert!(((*std::cell::LazyCell::force_mut(&mut *&raw mut next_ticket_calls_5)) == (0))); - assert!(((unsafe { issue_7(None,) }) == (100))); + assert!(((unsafe { issue_7((unsafe { next_ticket_6() }),) }) == (100))); assert!(((*std::cell::LazyCell::force_mut(&mut *&raw mut next_ticket_calls_5)) == (1))); - assert!(((unsafe { issue_7(Some(7),) }) == (7))); + assert!(((unsafe { issue_7(7,) }) == (7))); assert!(((*std::cell::LazyCell::force_mut(&mut *&raw mut next_ticket_calls_5)) == (1))); - assert!(((unsafe { issue_7(None,) }) == (200))); + assert!(((unsafe { issue_7((unsafe { next_ticket_6() }),) }) == (200))); assert!(((*std::cell::LazyCell::force_mut(&mut *&raw mut next_ticket_calls_5)) == (2))); } pub static mut step_9: std::cell::LazyCell = std::cell::LazyCell::new(|| unsafe { 100 }); @@ -57,10 +59,7 @@ pub struct p9_static_member_hides_namespace_variable_HasStaticStep { pub total: i32, } impl p9_static_member_hides_namespace_variable_HasStaticStep { - pub unsafe fn advance(&mut self, mut amount: Option) -> i32 { - let mut amount: i32 = amount.unwrap_or_else(|| unsafe { - (*std::cell::LazyCell::force_mut(&mut *&raw mut step_10)) - }); + pub unsafe fn advance(&mut self, mut amount: i32) -> i32 { self.total += amount; return self.total; } @@ -75,22 +74,202 @@ pub unsafe fn test_11() { ::default(); assert!( ((unsafe { - p9_static_member_hides_namespace_variable_HasStaticStep::advance(&mut object, None) + p9_static_member_hides_namespace_variable_HasStaticStep::advance( + &mut object, + (*std::cell::LazyCell::force_mut(&mut *&raw mut step_10)), + ) }) == (5)) ); (*std::cell::LazyCell::force_mut(&mut *&raw mut step_10)) = 6; assert!( ((unsafe { - p9_static_member_hides_namespace_variable_HasStaticStep::advance(&mut object, None) + p9_static_member_hides_namespace_variable_HasStaticStep::advance( + &mut object, + (*std::cell::LazyCell::force_mut(&mut *&raw mut step_10)), + ) }) == (11)) ); assert!( ((unsafe { - p9_static_member_hides_namespace_variable_HasStaticStep::advance(&mut object, Some(2)) + p9_static_member_hides_namespace_variable_HasStaticStep::advance(&mut object, 2) }) == (13)) ); assert!(((*std::cell::LazyCell::force_mut(&mut *&raw mut step_9)) == (100))); } +pub static mut defaults_evaluated_12: std::cell::LazyCell = + std::cell::LazyCell::new(|| unsafe { 0_i32 }); +pub unsafe fn count_default_13() -> i32 { + return (*(*std::cell::LazyCell::force_mut(&mut *&raw mut defaults_evaluated_12)).prefix_inc()); +} +pub unsafe fn combine_14(mut first: i32, mut second: i32) -> i32 { + return (((first) * (100)) + (second)); +} +#[repr(C)] +#[derive(Copy, Clone, VaArg, FnPtrArg, CppFromUnsafe)] +#[cpp_from(i32, new, value)] +pub struct p9_evaluated_each_time_called_Widget { + pub id: i32, +} +impl p9_evaluated_each_time_called_Widget { + pub unsafe fn new(mut id: i32) -> Self { + let mut this = Self { id: id }; + this + } +} +impl Default for p9_evaluated_each_time_called_Widget { + fn default() -> Self { + unsafe { p9_evaluated_each_time_called_Widget::new({ (unsafe { count_default_13() }) }) } + } +} +static mut base_15: std::cell::LazyCell = std::cell::LazyCell::new(|| unsafe { 10 }); +pub type anon_16 = u32; +pub const anon_16_kBonus: anon_16 = 5; +#[repr(C)] +#[derive(Copy, Clone, VaArg, FnPtrArg)] +pub struct p9_evaluated_each_time_called_Account { + pub balance: i32, +} +impl p9_evaluated_each_time_called_Account { + unsafe fn twice(mut value: i32) -> i32 { + return ((2) * (value)); + } + pub unsafe fn deposit(&mut self, mut amount: i32) -> i32 { + self.balance += amount; + return self.balance; + } + pub unsafe fn set_base(mut value: i32) { + (*std::cell::LazyCell::force_mut(&mut *&raw mut base_15)) = value; + } +} +impl Default for p9_evaluated_each_time_called_Account { + fn default() -> Self { + unsafe { p9_evaluated_each_time_called_Account { balance: 0 } } + } +} +pub unsafe fn test_17() { + (*std::cell::LazyCell::force_mut(&mut *&raw mut defaults_evaluated_12)) = 0; + assert!(((unsafe { combine_14(1, (unsafe { count_default_13() }),) }) == (101))); + assert!(((unsafe { combine_14(2, (unsafe { count_default_13() }),) }) == (202))); + assert!(((unsafe { combine_14(3, 9,) }) == (309))); + assert!(((*std::cell::LazyCell::force_mut(&mut *&raw mut defaults_evaluated_12)) == (2))); + let mut first: i32 = 0; + assert!( + ((unsafe { combine_14((*first.prefix_inc()), (unsafe { count_default_13() }),) }) == (103)) + ); + assert!(((first) == (1))); + assert!(((*std::cell::LazyCell::force_mut(&mut *&raw mut defaults_evaluated_12)) == (3))); + let mut a: p9_evaluated_each_time_called_Widget = + p9_evaluated_each_time_called_Widget::new({ (unsafe { count_default_13() }) }); + let mut b: p9_evaluated_each_time_called_Widget = + p9_evaluated_each_time_called_Widget::new({ 40 }); + let mut c: p9_evaluated_each_time_called_Widget = + p9_evaluated_each_time_called_Widget::new({ (unsafe { count_default_13() }) }); + assert!(((a.id) == (4))); + assert!(((b.id) == (40))); + assert!(((c.id) == (5))); + assert!(((*std::cell::LazyCell::force_mut(&mut *&raw mut defaults_evaluated_12)) == (5))); + let mut account: p9_evaluated_each_time_called_Account = + ::default(); + assert!( + ((unsafe { + p9_evaluated_each_time_called_Account::deposit( + &mut account, + ((unsafe { + p9_evaluated_each_time_called_Account::twice( + (*std::cell::LazyCell::force_mut(&mut *&raw mut base_15)), + ) + }) + (anon_16_kBonus as i32)), + ) + }) == (25)) + ); + (unsafe { p9_evaluated_each_time_called_Account::set_base(1) }); + assert!( + ((unsafe { + p9_evaluated_each_time_called_Account::deposit( + &mut account, + ((unsafe { + p9_evaluated_each_time_called_Account::twice( + (*std::cell::LazyCell::force_mut(&mut *&raw mut base_15)), + ) + }) + (anon_16_kBonus as i32)), + ) + }) == (32)) + ); + assert!( + ((unsafe { p9_evaluated_each_time_called_Account::deposit(&mut account, 3,) }) == (35)) + ); +} +pub unsafe fn add_18(mut a: i32, mut b: i32) -> i32 { + return ((a) + (b)); +} +#[repr(C)] +#[derive(Copy, Clone, VaArg, FnPtrArg, Default)] +pub struct p9_not_part_of_the_type_Scale {} +impl p9_not_part_of_the_type_Scale { + pub unsafe fn times(mut a: i32, mut b: i32) -> i32 { + return ((a) * (b)); + } +} +pub unsafe fn descending_19(a: *const i32, b: *const i32) -> bool { + return ((*a) > (*b)); +} +pub unsafe fn test_20() { + let mut p: Option i32> = Some(add_18); + let mut q: Option i32> = (Some(add_18)); + assert!(((p) == (q))); + assert!(((unsafe { (p).unwrap()(1, 5,) }) == (6))); + assert!(((unsafe { (q).unwrap()(2, 5,) }) == (7))); + assert!(((unsafe { add_18(1, 2,) }) == (3))); + let mut s: Option i32> = (Some(p9_not_part_of_the_type_Scale::times)); + assert!(((unsafe { (s).unwrap()(2, 5,) }) == (10))); + assert!(((unsafe { p9_not_part_of_the_type_Scale::times(2, 3,) }) == (6))); + let mut values: Vec = Vec::from([3, -1_i32, 4, 1, 5]); + match ({ values.begin() },) { + (mut __tmp_0,) => { + let __n = values.end().iter_distance(&__tmp_0); + let mut __order: Vec = (0..__n).collect(); + __order.sort_by(|&__x, &__y| { + let __a = __tmp_0.iter_at(__x) as *const _; + let __b = __tmp_0.iter_at(__y) as *const _; + if (descending_19 as unsafe fn(*const i32, *const i32) -> bool).call(__a, __b) { + std::cmp::Ordering::Less + } else if (descending_19 as unsafe fn(*const i32, *const i32) -> bool) + .call(__b, __a) + { + std::cmp::Ordering::Greater + } else { + std::cmp::Ordering::Equal + } + }); + let __values: Vec<_> = __order + .iter() + .map(|&__i| std::ptr::read(__tmp_0.iter_at(__i))) + .collect(); + for (__i, __v) in (0..__n).zip(__values) { + std::ptr::write(__tmp_0.iter_at(__i), __v); + } + } + }; + assert!(((values[(0_usize)]) == (5))); + assert!(((values[(1_usize)]) == (4))); + assert!(((values[(2_usize)]) == (3))); + assert!(((values[(3_usize)]) == (1))); + assert!(((values[(4_usize)]) == (-1_i32))); + assert!( + (unsafe { + let mut _a: i32 = 1; + let mut _b: i32 = 0; + descending_19(&mut _a, &mut _b) + }) + ); + assert!( + !(unsafe { + let mut _a: i32 = -1_i32; + let mut _b: i32 = 0; + descending_19(&mut _a, &mut _b) + }) + ); +} pub fn main() { unsafe { __cpp2rust_init_globals(); @@ -101,6 +280,8 @@ unsafe fn main_0() -> i32 { (unsafe { test_4() }); (unsafe { test_8() }); (unsafe { test_11() }); + (unsafe { test_17() }); + (unsafe { test_20() }); return 0; } pub unsafe fn __cpp2rust_init_globals() { @@ -109,4 +290,6 @@ pub unsafe fn __cpp2rust_init_globals() { std::cell::LazyCell::force(&*&raw const next_ticket_calls_5); std::cell::LazyCell::force(&*&raw const step_9); std::cell::LazyCell::force(&*&raw const step_10); + std::cell::LazyCell::force(&*&raw const defaults_evaluated_12); + std::cell::LazyCell::force(&*&raw const base_15); } diff --git a/tests/unit/out/refcount/default_function_arguments.rs b/tests/unit/out/refcount/default_function_arguments.rs index eeced5778f..c95ce92148 100644 --- a/tests/unit/out/refcount/default_function_arguments.rs +++ b/tests/unit/out/refcount/default_function_arguments.rs @@ -6,30 +6,27 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -pub fn foo_0(mut a: i32, b: Option) -> i32 { - let mut b: i32 = b.unwrap_or_else(|| 10); +pub fn foo_0(mut a: i32, mut b: i32) -> i32 { return (a + b); } -pub fn baz_1(mut a: Ptr, b: Option>) -> bool { - let mut b: Ptr = b.unwrap_or_else(|| Ptr::::null()); +pub fn baz_1(mut a: Ptr, mut b: Ptr) -> bool { return ({ a } == { b }); } #[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg, CppFrom)] -#[cpp_from(i32, new, some_value)] +#[cpp_from(i32, new, value)] #[byte_size(4)] pub struct Bar { #[offset(0)] pub v: i32, } impl Bar { - pub fn new(v: Option) -> Self { - let mut v: i32 = v.unwrap_or_else(|| 1); + pub fn new(mut v: i32) -> Self { Self { v: v } } } impl Default for Bar { fn default() -> Self { - { Bar::new(None) } + { Bar::new({ 1 }) } } } thread_local!( @@ -38,28 +35,19 @@ thread_local!( pub fn next_3() -> i32 { return (*(*counter_2.with(Value::clone).borrow_mut()).prefix_inc()); } -pub fn lazy_4(x: Option) -> i32 { - let mut x: i32 = x.unwrap_or_else(|| ({ next_3() })); +pub fn lazy_4(mut x: i32) -> i32 { return x; } -pub fn by_ref_5(b: Option>) -> i32 { - let mut __b_default: Option> = None; - let b: Ptr = b.unwrap_or_else(|| { - __b_default - .insert(Rc::new(RefCell::new(Bar::new({ Some(7) })))) - .as_pointer() - }); +pub fn by_ref_5(b: Ptr) -> i32 { return b.with(|__s| __s.v); } thread_local!( - pub static global_bar_6: Value = Rc::new(RefCell::new(Bar::new({ Some(9) }))); + pub static global_bar_6: Value = Rc::new(RefCell::new(Bar::new({ 9 }))); ); -pub fn by_global_ref_7(b: Option>) -> i32 { - let b: Ptr = b.unwrap_or_else(|| global_bar_6.with(|v| v.as_pointer())); +pub fn by_global_ref_7(b: Ptr) -> i32 { return b.with(|__s| __s.v); } -pub fn shifted_8(mut x: i32, y: Option) -> i32 { - let mut y: i32 = y.unwrap_or_else(|| -1_i32); +pub fn shifted_8(mut x: i32, mut y: i32) -> i32 { return (x + y); } pub fn first_9(r: Ptr) -> i32 { @@ -81,46 +69,51 @@ pub fn main() { std::process::exit(main_0()); } fn main_0() -> i32 { - assert!((({ foo_0(1, None,) }) == 11)); - assert!((({ foo_0(1, Some(2),) }) == 3)); + assert!((({ foo_0(1, 10,) }) == 11)); + assert!((({ foo_0(1, 2,) }) == 3)); let a: Value = Rc::new(RefCell::new(0)); - assert!(((({ baz_1((a.as_pointer()), None,) }) as i32) == (false as i32))); + assert!(((({ baz_1((a.as_pointer()), Ptr::::null(),) }) as i32) == (false as i32))); assert!( ((({ let _a: Ptr = (a.as_pointer()); let _b: Ptr = (a.as_pointer()); - baz_1(_a, Some(_b)) + baz_1(_a, _b) }) as i32) == (true as i32)) ); - let mut b: Bar = Bar::new(None); + let mut b: Bar = Bar::new({ 1 }); assert!((b.v == 1)); - assert!(({ Bar::new({ Some(2) },).v } == 2)); - let mut arr: [Bar; 3] = [Bar::new(None), Bar::new(None), Bar::new(None)]; + assert!(({ Bar::new({ 2 },).v } == 2)); + let mut arr: [Bar; 3] = [Bar::new({ 1 }), Bar::new({ 1 }), Bar::new({ 1 })]; assert!((arr[(0) as usize].v == 1)); assert!((arr[(2) as usize].v == 1)); - assert!((({ lazy_4(Some(5),) }) == 5)); + assert!((({ lazy_4(5,) }) == 5)); assert!((counter_2.with(|rc| *rc.borrow()) == 0)); - assert!((({ lazy_4(None,) }) == 1)); + assert!((({ lazy_4(({ next_3() }),) }) == 1)); assert!((counter_2.with(|rc| *rc.borrow()) == 1)); - assert!((({ by_ref_5(None,) }) == 7)); assert!( (({ - let _b: Value = Rc::new(RefCell::new(Bar::new({ Some(3) }))); - by_ref_5(Some(_b.as_pointer())) + let _b: Value = Rc::new(RefCell::new(Bar::new({ 7 }))); + by_ref_5(_b.as_pointer()) + }) == 7) + ); + assert!( + (({ + let _b: Value = Rc::new(RefCell::new(Bar::new({ 3 }))); + by_ref_5(_b.as_pointer()) }) == 3) ); - assert!((({ by_global_ref_7(None,) }) == 9)); + assert!((({ by_global_ref_7(global_bar_6.with(|v| v.as_pointer()),) }) == 9)); assert!( (({ - let _b: Value = Rc::new(RefCell::new(Bar::new({ Some(4) }))); - by_global_ref_7(Some(_b.as_pointer())) + let _b: Value = Rc::new(RefCell::new(Bar::new({ 4 }))); + by_global_ref_7(_b.as_pointer()) }) == 4) ); let mut h: Holder_int_ = Holder_int_::new({ 4 }); assert!((h.v == 4)); - assert!((({ shifted_8(3, None,) }) == 2)); - assert!((({ shifted_8(3, Some(4),) }) == 7)); + assert!((({ shifted_8(3, -1_i32,) }) == 2)); + assert!((({ shifted_8(3, 4,) }) == 7)); assert!( (({ let _r: Value> = Rc::new(RefCell::new(Box::new([4, 5, 6]))); diff --git a/tests/unit/out/refcount/destructor_params.rs b/tests/unit/out/refcount/destructor_params.rs index eeb6c33957..c3af0b595d 100644 --- a/tests/unit/out/refcount/destructor_params.rs +++ b/tests/unit/out/refcount/destructor_params.rs @@ -23,8 +23,8 @@ pub fn by_value_2(s: S) -> i32 { assert!((destroyed_0.with(|rc| *rc.borrow()) == 0)); return { (*s.borrow()).v }; } -pub fn with_default_3(s: Option) -> i32 { - let s: Value = Rc::new(RefCell::new(s.unwrap_or_else(|| S { v: 5 }))); +pub fn with_default_3(s: S) -> i32 { + let s: Value = Rc::new(RefCell::new(s)); return { (*s.borrow()).v }; } #[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] @@ -59,7 +59,7 @@ fn main_0() -> i32 { assert!((({ by_value_2(S { v: 2 },) }) == 2)); assert!((destroyed_0.with(|rc| *rc.borrow()) == 1) && (last_1.with(|rc| *rc.borrow()) == 2)); destroyed_0.with(|rc| *rc.borrow_mut() = 0); - assert!((({ with_default_3(None,) }) == 5)); + assert!((({ with_default_3(S { v: 5 },) }) == 5)); assert!((destroyed_0.with(|rc| *rc.borrow()) == 1) && (last_1.with(|rc| *rc.borrow()) == 5)); destroyed_0.with(|rc| *rc.borrow_mut() = 0); { diff --git a/tests/unit/out/refcount/fn_ptr_default_arg.rs b/tests/unit/out/refcount/fn_ptr_default_arg.rs index 4f5afdbd14..fff358363d 100644 --- a/tests/unit/out/refcount/fn_ptr_default_arg.rs +++ b/tests/unit/out/refcount/fn_ptr_default_arg.rs @@ -9,8 +9,7 @@ use std::rc::{Rc, Weak}; pub fn identity_0(mut x: i32) -> i32 { return x; } -pub fn apply_1(mut x: i32, fn_: Option i32>>) -> i32 { - let mut fn_: FnPtr i32> = fn_.unwrap_or_else(|| FnPtr:: i32>::null()); +pub fn apply_1(mut x: i32, mut fn_: FnPtr i32>) -> i32 { if !(fn_).is_null() { return ({ fn_.call(x) }); } @@ -21,15 +20,15 @@ pub fn main() { std::process::exit(main_0()); } fn main_0() -> i32 { - assert!((({ apply_1(5, None,) }) == 5)); - assert!((({ apply_1(5, Some(FnPtr:: i32>::null()),) }) == 5)); - assert!((({ apply_1(5, Some(FnPtr:: i32>::new(identity_0)),) }) == 5)); + assert!((({ apply_1(5, FnPtr:: i32>::null(),) }) == 5)); + assert!((({ apply_1(5, FnPtr:: i32>::null(),) }) == 5)); + assert!((({ apply_1(5, FnPtr:: i32>::new(identity_0),) }) == 5)); let mut negate: FnPtr i32> = FnPtr:: i32>::new(|mut x: i32| -> i32 { { return -x; } }); - assert!((({ apply_1(5, Some((negate).clone()),) }) == -5_i32)); + assert!((({ apply_1(5, (negate).clone(),) }) == -5_i32)); return 0; } pub fn __cpp2rust_init_globals() {} diff --git a/tests/unit/out/refcount/lambda_immediate_init.rs b/tests/unit/out/refcount/lambda_immediate_init.rs index cea412eacc..d0a013da2d 100644 --- a/tests/unit/out/refcount/lambda_immediate_init.rs +++ b/tests/unit/out/refcount/lambda_immediate_init.rs @@ -6,17 +6,7 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -pub fn pick_0(x: Option) -> i32 { - let mut x: i32 = x.unwrap_or_else(|| { - ({ - FnPtr:: i32>::new(|| -> i32 { - { - return 237; - } - }) - .call() - }) - }); +pub fn pick_0(mut x: i32) -> i32 { return x; } #[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg)] @@ -69,8 +59,21 @@ pub fn main() { std::process::exit(main_0()); } fn main_0() -> i32 { - assert!((({ pick_0(None,) }) == 237)); - assert!((({ pick_0(Some(1),) }) == 1)); + assert!( + (({ + pick_0( + ({ + FnPtr:: i32>::new(|| -> i32 { + { + return 237; + } + }) + .call() + }), + ) + }) == 237) + ); + assert!((({ pick_0(1,) }) == 1)); let mut s: S = ::default(); assert!((s.k == 4)); assert!((g_1.with(|rc| *rc.borrow()) == 10)); diff --git a/tests/unit/out/refcount/pair_converting_construction.rs b/tests/unit/out/refcount/pair_converting_construction.rs index de3c9114fb..e8e5f45564 100644 --- a/tests/unit/out/refcount/pair_converting_construction.rs +++ b/tests/unit/out/refcount/pair_converting_construction.rs @@ -76,21 +76,20 @@ impl Holder { } } #[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg, CppFrom)] -#[cpp_from(i32, new, some_value)] +#[cpp_from(i32, new, value)] #[byte_size(4)] pub struct Fallback { #[offset(0)] pub value: i32, } impl Fallback { - pub fn new(v: Option) -> Self { - let mut v: i32 = v.unwrap_or_else(|| -1_i32); + pub fn new(mut v: i32) -> Self { Self { value: v } } } impl Default for Fallback { fn default() -> Self { - { Fallback::new(None) } + { Fallback::new({ -1_i32 }) } } } #[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] @@ -285,7 +284,7 @@ fn main_0() -> i32 { )); assert!(({ ({ (*(*from_moved_by_value_pair.borrow()).0.borrow()).value } == 14) })); assert!(({ ({ (*(*from_moved_by_value_pair.borrow()).1.borrow()).value } == 15) })); - let mut defaulted: Fallback = Fallback::new(None); + let mut defaulted: Fallback = Fallback::new({ -1_i32 }); assert!((defaulted.value == -1_i32)); let from_defaulted_ctor: Value<(Value, Value)> = Rc::new(RefCell::new(( Rc::new(RefCell::new(>::cpp_from( diff --git a/tests/unit/out/refcount/size_t_pointer.rs b/tests/unit/out/refcount/size_t_pointer.rs index 9e1e9899cc..f0115aa56d 100644 --- a/tests/unit/out/refcount/size_t_pointer.rs +++ b/tests/unit/out/refcount/size_t_pointer.rs @@ -6,14 +6,12 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -pub fn out_param_0(out: Option>) { - let mut out: Ptr = out.unwrap_or_else(|| Ptr::::null()); +pub fn out_param_0(mut out: Ptr) { if !(out).is_null() { out.write(4_usize); } } -pub fn parse_1(mut v: i32, idx: Option>) -> i32 { - let mut idx: Ptr = idx.unwrap_or_else(|| Ptr::::null()); +pub fn parse_1(mut v: i32, mut idx: Ptr) -> i32 { if !(idx).is_null() { idx.write(3_usize); } @@ -25,10 +23,10 @@ pub fn main() { } fn main_0() -> i32 { let v2: Value = Rc::new(RefCell::new(0_usize)); - ({ out_param_0(Some((v2.as_pointer()))) }); + ({ out_param_0((v2.as_pointer())) }); assert!(({ ((*v2.borrow()) == 4_usize) })); let pidx: Value = Rc::new(RefCell::new(0_usize)); - assert!((({ parse_1(1, Some((pidx.as_pointer())),) }) == 1)); + assert!((({ parse_1(1, (pidx.as_pointer()),) }) == 1)); assert!(({ ((*pidx.borrow()) == 3_usize) })); let sv: Value = Rc::new(RefCell::new(7_usize)); let sp: Value> = Rc::new(RefCell::new((sv.as_pointer()))); diff --git a/tests/unit/out/refcount/template_default_arg.rs b/tests/unit/out/refcount/template_default_arg.rs index 239584b9e5..ec30af58e1 100644 --- a/tests/unit/out/refcount/template_default_arg.rs +++ b/tests/unit/out/refcount/template_default_arg.rs @@ -54,16 +54,13 @@ impl NoDefault { Self { v: v } } } -pub fn used_0(x: Option) -> i32 { - let mut x: A = x.unwrap_or_else(|| A::new_2()); +pub fn used_0(mut x: A) -> i32 { return x.v; } -pub fn used_1(x: Option) -> i32 { - let mut x: B = x.unwrap_or_else(|| B::new()); +pub fn used_1(mut x: B) -> i32 { return x.v; } -pub fn scaled_2(mut x: A, n: Option) -> i32 { - let mut n: i32 = n.unwrap_or_else(|| (4usize as i32)); +pub fn scaled_2(mut x: A, mut n: i32) -> i32 { return (x.v * n); } pub fn always_given_3(mut x: NoDefault) -> i32 { @@ -86,11 +83,11 @@ pub fn main() { std::process::exit(main_0()); } fn main_0() -> i32 { - assert!((({ used_0(None,) }) == 1)); - assert!((({ used_0(Some(A::new_1({ 5 },)),) }) == 5)); - assert!((({ used_1(None,) }) == 2)); - assert!((({ scaled_2(A::new_1({ 3 },), None,) }) == (3 * (4usize as i32)))); - assert!((({ scaled_2(A::new_1({ 3 },), Some(2),) }) == 6)); + assert!((({ used_0(A::new_2(),) }) == 1)); + assert!((({ used_0(A::new_1({ 5 },),) }) == 5)); + assert!((({ used_1(B::new(),) }) == 2)); + assert!((({ scaled_2(A::new_1({ 3 },), (4usize as i32),) }) == (3 * (4usize as i32)))); + assert!((({ scaled_2(A::new_1({ 3 },), 2,) }) == 6)); assert!((({ always_given_3(NoDefault::new({ 3 },),) }) == 3)); let s: Value = Rc::new(RefCell::new(S_NoDefault_::new({ 1 }))); assert!((({ S_NoDefault_Impl::get(&s.as_pointer(), NoDefault::new({ 4 },),) }) == 5)); diff --git a/tests/unit/out/refcount/unboxed_locals.rs b/tests/unit/out/refcount/unboxed_locals.rs index b5e8f0f148..c13c7052d6 100644 --- a/tests/unit/out/refcount/unboxed_locals.rs +++ b/tests/unit/out/refcount/unboxed_locals.rs @@ -41,8 +41,7 @@ pub fn sum_3(mut arr: Ptr, mut n: i32) -> i32 { } return s; } -pub fn countdown_4(mut n: i32, step: Option) -> i32 { - let mut step: i32 = step.unwrap_or_else(|| 1); +pub fn countdown_4(mut n: i32, mut step: i32) -> i32 { let mut steps: i32 = 0; 'loop_: while (n > 0) { n -= step; @@ -119,8 +118,8 @@ fn main_0() -> i32 { let mut zeros: [f64; 16] = [0_f64; 16]; zeros[(15) as usize] = 1.5_f64; assert!((zeros[(0) as usize] == 0_f64) && (zeros[(15) as usize] == 1.5_f64)); - assert!((({ countdown_4(10, None,) }) == 10)); - assert!((({ countdown_4(10, Some(3),) }) == 4)); + assert!((({ countdown_4(10, 1,) }) == 10)); + assert!((({ countdown_4(10, 3,) }) == 4)); let mut init: i32 = 3; let mut pair: Pair = Pair { first: init, diff --git a/tests/unit/out/unsafe/default_function_arguments.rs b/tests/unit/out/unsafe/default_function_arguments.rs index 8e38f82b2f..bd5540d4a6 100644 --- a/tests/unit/out/unsafe/default_function_arguments.rs +++ b/tests/unit/out/unsafe/default_function_arguments.rs @@ -6,56 +6,45 @@ use std::collections::BTreeMap; use std::io::{Read, Seek, Write}; use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; use std::rc::Rc; -pub unsafe fn foo_0(mut a: i32, mut b: Option) -> i32 { - let mut b: i32 = b.unwrap_or_else(|| unsafe { 10 }); +pub unsafe fn foo_0(mut a: i32, mut b: i32) -> i32 { return ((a) + (b)); } -pub unsafe fn baz_1(mut a: *mut i32, mut b: Option<*mut i32>) -> bool { - let mut b: *mut i32 = b.unwrap_or_else(|| unsafe { std::ptr::null_mut() }); +pub unsafe fn baz_1(mut a: *mut i32, mut b: *mut i32) -> bool { return ((a) == (b)); } #[repr(C)] #[derive(Copy, Clone, VaArg, FnPtrArg, CppFromUnsafe)] -#[cpp_from(i32, new, some_value)] +#[cpp_from(i32, new, value)] pub struct Bar { pub v: i32, } impl Bar { - pub unsafe fn new(mut v: Option) -> Self { - let mut v: i32 = v.unwrap_or_else(|| unsafe { 1 }); + pub unsafe fn new(mut v: i32) -> Self { let mut this = Self { v: v }; this } } impl Default for Bar { fn default() -> Self { - unsafe { Bar::new(None) } + unsafe { Bar::new({ 1 }) } } } pub static mut counter_2: std::cell::LazyCell = std::cell::LazyCell::new(|| unsafe { 0 }); pub unsafe fn next_3() -> i32 { return (*(*std::cell::LazyCell::force_mut(&mut *&raw mut counter_2)).prefix_inc()); } -pub unsafe fn lazy_4(mut x: Option) -> i32 { - let mut x: i32 = x.unwrap_or_else(|| unsafe { (unsafe { next_3() }) }); +pub unsafe fn lazy_4(mut x: i32) -> i32 { return x; } -pub unsafe fn by_ref_5(b: Option<*const Bar>) -> i32 { - let mut __b_default: Option = None; - let mut b: *const Bar = - b.unwrap_or_else(|| unsafe { __b_default.insert(Bar::new({ Some(7) })) as *const Bar }); +pub unsafe fn by_ref_5(b: *const Bar) -> i32 { return (*b).v; } pub static mut global_bar_6: std::cell::LazyCell = - std::cell::LazyCell::new(|| unsafe { Bar::new({ Some(9) }) }); -pub unsafe fn by_global_ref_7(b: Option<*const Bar>) -> i32 { - let mut b: *const Bar = b.unwrap_or_else(|| unsafe { - &(*std::cell::LazyCell::force_mut(&mut *&raw mut global_bar_6)) - }); + std::cell::LazyCell::new(|| unsafe { Bar::new({ 9 }) }); +pub unsafe fn by_global_ref_7(b: *const Bar) -> i32 { return (*b).v; } -pub unsafe fn shifted_8(mut x: i32, mut y: Option) -> i32 { - let mut y: i32 = y.unwrap_or_else(|| unsafe { -1_i32 }); +pub unsafe fn shifted_8(mut x: i32, mut y: i32) -> i32 { return ((x) + (y)); } pub unsafe fn first_9(r: *mut [i32; 3]) -> i32 { @@ -79,46 +68,58 @@ pub fn main() { } } unsafe fn main_0() -> i32 { - assert!(((unsafe { foo_0(1, None,) }) == (11))); - assert!(((unsafe { foo_0(1, Some(2),) }) == (3))); + assert!(((unsafe { foo_0(1, 10,) }) == (11))); + assert!(((unsafe { foo_0(1, 2,) }) == (3))); let mut a: i32 = 0; - assert!((((unsafe { baz_1((&mut a as *mut i32), None,) }) as i32) == (false as i32))); + assert!( + (((unsafe { baz_1((&mut a as *mut i32), std::ptr::null_mut(),) }) as i32) + == (false as i32)) + ); assert!( (((unsafe { let _a: *mut i32 = (&mut a as *mut i32); let _b: *mut i32 = (&mut a as *mut i32); - baz_1(_a, Some(_b)) + baz_1(_a, _b) }) as i32) == (true as i32)) ); - let mut b: Bar = Bar::new(None); + let mut b: Bar = Bar::new({ 1 }); assert!(((b.v) == (1))); - assert!(((Bar::new({ Some(2) },).v) == (2))); - let mut arr: [Bar; 3] = [Bar::new(None), Bar::new(None), Bar::new(None)]; + assert!(((Bar::new({ 2 },).v) == (2))); + let mut arr: [Bar; 3] = [Bar::new({ 1 }), Bar::new({ 1 }), Bar::new({ 1 })]; assert!(((arr[(0) as usize].v) == (1))); assert!(((arr[(2) as usize].v) == (1))); - assert!(((unsafe { lazy_4(Some(5),) }) == (5))); + assert!(((unsafe { lazy_4(5,) }) == (5))); assert!(((*std::cell::LazyCell::force_mut(&mut *&raw mut counter_2)) == (0))); - assert!(((unsafe { lazy_4(None,) }) == (1))); + assert!(((unsafe { lazy_4((unsafe { next_3() }),) }) == (1))); assert!(((*std::cell::LazyCell::force_mut(&mut *&raw mut counter_2)) == (1))); - assert!(((unsafe { by_ref_5(None,) }) == (7))); assert!( ((unsafe { - let mut _b: Bar = Bar::new({ Some(3) }); - by_ref_5(Some(&mut _b)) + let mut _b: Bar = Bar::new({ 7 }); + by_ref_5(&mut _b) + }) == (7)) + ); + assert!( + ((unsafe { + let mut _b: Bar = Bar::new({ 3 }); + by_ref_5(&mut _b) }) == (3)) ); - assert!(((unsafe { by_global_ref_7(None,) }) == (9))); assert!( ((unsafe { - let mut _b: Bar = Bar::new({ Some(4) }); - by_global_ref_7(Some(&mut _b)) + by_global_ref_7(&(*std::cell::LazyCell::force_mut(&mut *&raw mut global_bar_6))) + }) == (9)) + ); + assert!( + ((unsafe { + let mut _b: Bar = Bar::new({ 4 }); + by_global_ref_7(&mut _b) }) == (4)) ); let mut h: Holder_int_ = Holder_int_::new({ 4 }); assert!(((h.v) == (4))); - assert!(((unsafe { shifted_8(3, None,) }) == (2))); - assert!(((unsafe { shifted_8(3, Some(4),) }) == (7))); + assert!(((unsafe { shifted_8(3, -1_i32,) }) == (2))); + assert!(((unsafe { shifted_8(3, 4,) }) == (7))); assert!( ((unsafe { let mut _r: [i32; 3] = [4, 5, 6]; diff --git a/tests/unit/out/unsafe/destructor_params.rs b/tests/unit/out/unsafe/destructor_params.rs index 75234c5edf..44913ede1b 100644 --- a/tests/unit/out/unsafe/destructor_params.rs +++ b/tests/unit/out/unsafe/destructor_params.rs @@ -23,8 +23,7 @@ pub unsafe fn by_value_2(mut s: S) -> i32 { assert!(((*std::cell::LazyCell::force_mut(&mut *&raw mut destroyed_0)) == (0))); return s.v; } -pub unsafe fn with_default_3(mut s: Option) -> i32 { - let mut s: S = s.unwrap_or_else(|| unsafe { S { v: 5 } }); +pub unsafe fn with_default_3(mut s: S) -> i32 { return s.v; } #[repr(C)] @@ -65,7 +64,7 @@ unsafe fn main_0() -> i32 { && ((*std::cell::LazyCell::force_mut(&mut *&raw mut last_1)) == (2)) ); (*std::cell::LazyCell::force_mut(&mut *&raw mut destroyed_0)) = 0; - assert!(((unsafe { with_default_3(None,) }) == (5))); + assert!(((unsafe { with_default_3(S { v: 5 },) }) == (5))); assert!( ((*std::cell::LazyCell::force_mut(&mut *&raw mut destroyed_0)) == (1)) && ((*std::cell::LazyCell::force_mut(&mut *&raw mut last_1)) == (5)) diff --git a/tests/unit/out/unsafe/fn_ptr_default_arg.rs b/tests/unit/out/unsafe/fn_ptr_default_arg.rs index f10f9084ee..890ef4c5d2 100644 --- a/tests/unit/out/unsafe/fn_ptr_default_arg.rs +++ b/tests/unit/out/unsafe/fn_ptr_default_arg.rs @@ -9,8 +9,7 @@ use std::rc::Rc; pub unsafe fn identity_0(mut x: i32) -> i32 { return x; } -pub unsafe fn apply_1(mut x: i32, mut fn_: Option i32>>) -> i32 { - let mut fn_: Option i32> = fn_.unwrap_or_else(|| unsafe { None }); +pub unsafe fn apply_1(mut x: i32, mut fn_: Option i32>) -> i32 { if !(fn_).is_none() { return (unsafe { (fn_).unwrap()(x) }); } @@ -24,14 +23,14 @@ pub fn main() { } unsafe fn main_0() -> i32 { assert!(((unsafe { apply_1(5, None,) }) == (5))); - assert!(((unsafe { apply_1(5, Some(None),) }) == (5))); - assert!(((unsafe { apply_1(5, Some(Some(identity_0)),) }) == (5))); + assert!(((unsafe { apply_1(5, None,) }) == (5))); + assert!(((unsafe { apply_1(5, Some(identity_0),) }) == (5))); let mut negate: Option i32> = Some(|mut x: i32| -> i32 { unsafe { return -x; } }); - assert!(((unsafe { apply_1(5, Some(negate),) }) == (-5_i32))); + assert!(((unsafe { apply_1(5, negate,) }) == (-5_i32))); return 0; } pub unsafe fn __cpp2rust_init_globals() {} diff --git a/tests/unit/out/unsafe/lambda_immediate_init.rs b/tests/unit/out/unsafe/lambda_immediate_init.rs index 3292bb1d37..747a873f9c 100644 --- a/tests/unit/out/unsafe/lambda_immediate_init.rs +++ b/tests/unit/out/unsafe/lambda_immediate_init.rs @@ -6,17 +6,7 @@ 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(mut x: Option) -> i32 { - let mut x: i32 = x.unwrap_or_else(|| unsafe { - (unsafe { - FnPtr:: i32>::new(|| -> i32 { - unsafe { - return 237; - } - }) - .call() - }) - }); +pub unsafe fn pick_0(mut x: i32) -> i32 { return x; } #[repr(C)] @@ -67,8 +57,21 @@ pub fn main() { } } unsafe fn main_0() -> i32 { - assert!(((unsafe { pick_0(None,) }) == (237))); - assert!(((unsafe { pick_0(Some(1),) }) == (1))); + assert!( + ((unsafe { + pick_0( + (unsafe { + FnPtr:: i32>::new(|| -> i32 { + unsafe { + return 237; + } + }) + .call() + }), + ) + }) == (237)) + ); + assert!(((unsafe { pick_0(1,) }) == (1))); let mut s: S = ::default(); assert!(((s.k) == (4))); assert!(((*std::cell::LazyCell::force_mut(&mut *&raw mut g_1)) == (10))); diff --git a/tests/unit/out/unsafe/pair_converting_construction.rs b/tests/unit/out/unsafe/pair_converting_construction.rs index 806bbee6ea..817ae11786 100644 --- a/tests/unit/out/unsafe/pair_converting_construction.rs +++ b/tests/unit/out/unsafe/pair_converting_construction.rs @@ -79,20 +79,19 @@ impl Holder { } #[repr(C)] #[derive(Copy, Clone, VaArg, FnPtrArg, CppFromUnsafe)] -#[cpp_from(i32, new, some_value)] +#[cpp_from(i32, new, value)] pub struct Fallback { pub value: i32, } impl Fallback { - pub unsafe fn new(mut v: Option) -> Self { - let mut v: i32 = v.unwrap_or_else(|| unsafe { -1_i32 }); + pub unsafe fn new(mut v: i32) -> Self { let mut this = Self { value: v }; this } } impl Default for Fallback { fn default() -> Self { - unsafe { Fallback::new(None) } + unsafe { Fallback::new({ -1_i32 }) } } } #[repr(C)] @@ -214,7 +213,7 @@ unsafe fn main_0() -> i32 { }; assert!(((from_moved_by_value_pair.0.value) == (14))); assert!(((from_moved_by_value_pair.1.value) == (15))); - let mut defaulted: Fallback = Fallback::new(None); + let mut defaulted: Fallback = Fallback::new({ -1_i32 }); assert!(((defaulted.value) == (-1_i32))); let mut from_defaulted_ctor: (Fallback, i64) = ( >::cpp_from(&ints.0), diff --git a/tests/unit/out/unsafe/size_t_pointer.rs b/tests/unit/out/unsafe/size_t_pointer.rs index d853ce3d8e..e23ad05738 100644 --- a/tests/unit/out/unsafe/size_t_pointer.rs +++ b/tests/unit/out/unsafe/size_t_pointer.rs @@ -6,14 +6,12 @@ use std::collections::BTreeMap; use std::io::{Read, Seek, Write}; use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; use std::rc::Rc; -pub unsafe fn out_param_0(mut out: Option<*mut usize>) { - let mut out: *mut usize = out.unwrap_or_else(|| unsafe { std::ptr::null_mut() }); +pub unsafe fn out_param_0(mut out: *mut usize) { if !(out).is_null() { (*out) = 4_usize; } } -pub unsafe fn parse_1(mut v: i32, mut idx: Option<*mut usize>) -> i32 { - let mut idx: *mut usize = idx.unwrap_or_else(|| unsafe { std::ptr::null_mut() }); +pub unsafe fn parse_1(mut v: i32, mut idx: *mut usize) -> i32 { if !(idx).is_null() { (*idx) = 3_usize; } @@ -27,10 +25,10 @@ pub fn main() { } unsafe fn main_0() -> i32 { let mut v2: usize = 0_usize; - (unsafe { out_param_0(Some((&mut v2 as *mut usize))) }); + (unsafe { out_param_0((&mut v2 as *mut usize)) }); assert!(((v2) == (4_usize))); let mut pidx: usize = 0_usize; - assert!(((unsafe { parse_1(1, Some((&mut pidx as *mut usize)),) }) == (1))); + assert!(((unsafe { parse_1(1, (&mut pidx as *mut usize),) }) == (1))); assert!(((pidx) == (3_usize))); let mut sv: usize = 7_usize; let mut sp: *mut usize = (&mut sv as *mut usize); diff --git a/tests/unit/out/unsafe/template_default_arg.rs b/tests/unit/out/unsafe/template_default_arg.rs index 9808d05df4..b3796d0e8d 100644 --- a/tests/unit/out/unsafe/template_default_arg.rs +++ b/tests/unit/out/unsafe/template_default_arg.rs @@ -55,16 +55,13 @@ impl NoDefault { this } } -pub unsafe fn used_0(mut x: Option) -> i32 { - let mut x: A = x.unwrap_or_else(|| unsafe { A::new_2() }); +pub unsafe fn used_0(mut x: A) -> i32 { return x.v; } -pub unsafe fn used_1(mut x: Option) -> i32 { - let mut x: B = x.unwrap_or_else(|| unsafe { B::new() }); +pub unsafe fn used_1(mut x: B) -> i32 { return x.v; } -pub unsafe fn scaled_2(mut x: A, mut n: Option) -> i32 { - let mut n: i32 = n.unwrap_or_else(|| unsafe { (::std::mem::size_of::() as i32) }); +pub unsafe fn scaled_2(mut x: A, mut n: i32) -> i32 { return ((x.v) * (n)); } pub unsafe fn always_given_3(mut x: NoDefault) -> i32 { @@ -92,14 +89,14 @@ pub fn main() { } } unsafe fn main_0() -> i32 { - assert!(((unsafe { used_0(None,) }) == (1))); - assert!(((unsafe { used_0(Some(A::new_1({ 5 },)),) }) == (5))); - assert!(((unsafe { used_1(None,) }) == (2))); + assert!(((unsafe { used_0(A::new_2(),) }) == (1))); + assert!(((unsafe { used_0(A::new_1({ 5 },),) }) == (5))); + assert!(((unsafe { used_1(B::new(),) }) == (2))); assert!( - ((unsafe { scaled_2(A::new_1({ 3 },), None,) }) + ((unsafe { scaled_2(A::new_1({ 3 },), (::std::mem::size_of::() as i32),) }) == ((3) * (::std::mem::size_of::() as i32))) ); - assert!(((unsafe { scaled_2(A::new_1({ 3 },), Some(2),) }) == (6))); + assert!(((unsafe { scaled_2(A::new_1({ 3 },), 2,) }) == (6))); assert!(((unsafe { always_given_3(NoDefault::new({ 3 },),) }) == (3))); let mut s: S_NoDefault_ = S_NoDefault_::new({ 1 }); assert!(((unsafe { S_NoDefault_::get(&s, NoDefault::new({ 4 },),) }) == (5))); diff --git a/tests/unit/out/unsafe/unboxed_locals.rs b/tests/unit/out/unsafe/unboxed_locals.rs index c978d105d4..e7a8a7ae32 100644 --- a/tests/unit/out/unsafe/unboxed_locals.rs +++ b/tests/unit/out/unsafe/unboxed_locals.rs @@ -37,8 +37,7 @@ pub unsafe fn sum_3(mut arr: *const i32, mut n: i32) -> i32 { } return s; } -pub unsafe fn countdown_4(mut n: i32, mut step: Option) -> i32 { - let mut step: i32 = step.unwrap_or_else(|| unsafe { 1 }); +pub unsafe fn countdown_4(mut n: i32, mut step: i32) -> i32 { let mut steps: i32 = 0; 'loop_: while ((n) > (0)) { n -= step; @@ -116,8 +115,8 @@ unsafe fn main_0() -> i32 { let mut zeros: [f64; 16] = [0_f64; 16]; zeros[(15) as usize] = 1.5_f64; assert!(((zeros[(0) as usize]) == (0_f64)) && ((zeros[(15) as usize]) == (1.5_f64))); - assert!(((unsafe { countdown_4(10, None,) }) == (10))); - assert!(((unsafe { countdown_4(10, Some(3),) }) == (4))); + assert!(((unsafe { countdown_4(10, 1,) }) == (10))); + assert!(((unsafe { countdown_4(10, 3,) }) == (4))); let mut init: i32 = 3; let mut pair: Pair = Pair { first: init, From b4ca432ef34ecbf03d4223c0845bb136a5a12abc Mon Sep 17 00:00:00 2001 From: Lucian Popescu Date: Wed, 18 Nov 2026 12:46:19 +0000 Subject: [PATCH 3/4] Update tests --- .../out/refcount/dcl.fct.default.rs | 6 +- .../refcount/default_function_arguments.rs | 71 +++++++++---------- tests/unit/out/refcount/destructor_params.rs | 6 +- .../out/refcount/lambda_immediate_init.rs | 29 ++++---- .../refcount/pair_converting_construction.rs | 9 ++- .../unit/out/refcount/template_default_arg.rs | 19 +++-- tests/unit/out/refcount/unboxed_locals.rs | 7 +- 7 files changed, 69 insertions(+), 78 deletions(-) diff --git a/tests/std/dcl.dcl/dcl.decl/dcl.meaning/out/refcount/dcl.fct.default.rs b/tests/std/dcl.dcl/dcl.decl/dcl.meaning/out/refcount/dcl.fct.default.rs index 14872ef282..e3a99aceae 100644 --- a/tests/std/dcl.dcl/dcl.decl/dcl.meaning/out/refcount/dcl.fct.default.rs +++ b/tests/std/dcl.dcl/dcl.decl/dcl.meaning/out/refcount/dcl.fct.default.rs @@ -105,7 +105,7 @@ pub fn count_default_13() -> i32 { pub fn combine_14(mut first: i32, mut second: i32) -> i32 { return ((first * 100) + second); } -#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg, CppFrom)] +#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, CppFrom)] #[cpp_from(i32, new, value)] #[byte_size(4)] pub struct p9_evaluated_each_time_called_Widget { @@ -127,7 +127,7 @@ thread_local!( ); pub type anon_16 = u32; pub const anon_16_kBonus: anon_16 = 5; -#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg)] +#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg)] #[byte_size(4)] pub struct p9_evaluated_each_time_called_Account { #[offset(0)] @@ -197,7 +197,7 @@ pub fn test_17() { pub fn add_18(mut a: i32, mut b: i32) -> i32 { return (a + b); } -#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] #[byte_size(1)] pub struct p9_not_part_of_the_type_Scale {} impl p9_not_part_of_the_type_Scale { diff --git a/tests/unit/out/refcount/default_function_arguments.rs b/tests/unit/out/refcount/default_function_arguments.rs index 79ae47e7ad..204411c4f1 100644 --- a/tests/unit/out/refcount/default_function_arguments.rs +++ b/tests/unit/out/refcount/default_function_arguments.rs @@ -6,30 +6,27 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -pub fn foo_0(mut a: i32, b: Option) -> i32 { - let mut b: i32 = b.unwrap_or_else(|| 10); +pub fn foo_0(mut a: i32, mut b: i32) -> i32 { return (a + b); } -pub fn baz_1(mut a: Ptr, b: Option>) -> bool { - let mut b: Ptr = b.unwrap_or_else(|| Ptr::::null()); +pub fn baz_1(mut a: Ptr, mut b: Ptr) -> bool { return ({ a } == { b }); } #[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, CppFrom)] -#[cpp_from(i32, new, some_value)] +#[cpp_from(i32, new, value)] #[byte_size(4)] pub struct Bar { #[offset(0)] pub v: i32, } impl Bar { - pub fn new(v: Option) -> Self { - let mut v: i32 = v.unwrap_or_else(|| 1); + pub fn new(mut v: i32) -> Self { Self { v: v } } } impl Default for Bar { fn default() -> Self { - { Bar::new(None) } + { Bar::new({ 1 }) } } } thread_local!( @@ -38,28 +35,19 @@ thread_local!( pub fn next_3() -> i32 { return (*(*counter_2.with(Value::clone).borrow_mut()).prefix_inc()); } -pub fn lazy_4(x: Option) -> i32 { - let mut x: i32 = x.unwrap_or_else(|| ({ next_3() })); +pub fn lazy_4(mut x: i32) -> i32 { return x; } -pub fn by_ref_5(b: Option>) -> i32 { - let mut __b_default: Option> = None; - let b: Ptr = b.unwrap_or_else(|| { - __b_default - .insert(Rc::new(RefCell::new(Bar::new({ Some(7) })))) - .as_pointer() - }); +pub fn by_ref_5(b: Ptr) -> i32 { return b.with(|__s| __s.v); } thread_local!( - pub static global_bar_6: Value = Rc::new(RefCell::new(Bar::new({ Some(9) }))); + pub static global_bar_6: Value = Rc::new(RefCell::new(Bar::new({ 9 }))); ); -pub fn by_global_ref_7(b: Option>) -> i32 { - let b: Ptr = b.unwrap_or_else(|| global_bar_6.with(|v| v.as_pointer())); +pub fn by_global_ref_7(b: Ptr) -> i32 { return b.with(|__s| __s.v); } -pub fn shifted_8(mut x: i32, y: Option) -> i32 { - let mut y: i32 = y.unwrap_or_else(|| -1_i32); +pub fn shifted_8(mut x: i32, mut y: i32) -> i32 { return (x + y); } pub fn first_9(r: Ptr) -> i32 { @@ -81,46 +69,51 @@ pub fn main() { std::process::exit(main_0()); } fn main_0() -> i32 { - assert!((({ foo_0(1, None,) }) == 11)); - assert!((({ foo_0(1, Some(2),) }) == 3)); + assert!((({ foo_0(1, 10,) }) == 11)); + assert!((({ foo_0(1, 2,) }) == 3)); let a: Value = Rc::new(RefCell::new(0)); - assert!(((({ baz_1((a.as_pointer()), None,) }) as i32) == (false as i32))); + assert!(((({ baz_1((a.as_pointer()), Ptr::::null(),) }) as i32) == (false as i32))); assert!( ((({ let _a: Ptr = (a.as_pointer()); let _b: Ptr = (a.as_pointer()); - baz_1(_a, Some(_b)) + baz_1(_a, _b) }) as i32) == (true as i32)) ); - let mut b: Bar = Bar::new(None); + let mut b: Bar = Bar::new({ 1 }); assert!((b.v == 1)); - assert!(({ Bar::new({ Some(2) },).v } == 2)); - let mut arr: [Bar; 3] = [Bar::new(None), Bar::new(None), Bar::new(None)]; + assert!(({ Bar::new({ 2 },).v } == 2)); + let mut arr: [Bar; 3] = [Bar::new({ 1 }), Bar::new({ 1 }), Bar::new({ 1 })]; assert!((arr[(0) as usize].v == 1)); assert!((arr[(2) as usize].v == 1)); - assert!((({ lazy_4(Some(5),) }) == 5)); + assert!((({ lazy_4(5,) }) == 5)); assert!((counter_2.with(|rc| *rc.borrow()) == 0)); - assert!((({ lazy_4(None,) }) == 1)); + assert!((({ lazy_4(({ next_3() }),) }) == 1)); assert!((counter_2.with(|rc| *rc.borrow()) == 1)); - assert!((({ by_ref_5(None,) }) == 7)); assert!( (({ - let _b: Value = Rc::new(RefCell::new(Bar::new({ Some(3) }))); - by_ref_5(Some(_b.as_pointer())) + let _b: Value = Rc::new(RefCell::new(Bar::new({ 7 }))); + by_ref_5(_b.as_pointer()) + }) == 7) + ); + assert!( + (({ + let _b: Value = Rc::new(RefCell::new(Bar::new({ 3 }))); + by_ref_5(_b.as_pointer()) }) == 3) ); - assert!((({ by_global_ref_7(None,) }) == 9)); + assert!((({ by_global_ref_7(global_bar_6.with(|v| v.as_pointer()),) }) == 9)); assert!( (({ - let _b: Value = Rc::new(RefCell::new(Bar::new({ Some(4) }))); - by_global_ref_7(Some(_b.as_pointer())) + let _b: Value = Rc::new(RefCell::new(Bar::new({ 4 }))); + by_global_ref_7(_b.as_pointer()) }) == 4) ); let mut h: Holder_int_ = Holder_int_::new({ 4 }); assert!((h.v == 4)); - assert!((({ shifted_8(3, None,) }) == 2)); - assert!((({ shifted_8(3, Some(4),) }) == 7)); + assert!((({ shifted_8(3, -1_i32,) }) == 2)); + assert!((({ shifted_8(3, 4,) }) == 7)); assert!( (({ let _r: Value> = Rc::new(RefCell::new(Box::new([4, 5, 6]))); diff --git a/tests/unit/out/refcount/destructor_params.rs b/tests/unit/out/refcount/destructor_params.rs index 160a3760e6..522f851a93 100644 --- a/tests/unit/out/refcount/destructor_params.rs +++ b/tests/unit/out/refcount/destructor_params.rs @@ -23,8 +23,8 @@ pub fn by_value_2(s: S) -> i32 { assert!((destroyed_0.with(|rc| *rc.borrow()) == 0)); return { (*s.borrow()).v }; } -pub fn with_default_3(s: Option) -> i32 { - let s: Value = Rc::new(RefCell::new(s.unwrap_or_else(|| S { v: 5 }))); +pub fn with_default_3(s: S) -> i32 { + let s: Value = Rc::new(RefCell::new(s)); return { (*s.borrow()).v }; } #[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] @@ -59,7 +59,7 @@ fn main_0() -> i32 { assert!((({ by_value_2(S { v: 2 },) }) == 2)); assert!((destroyed_0.with(|rc| *rc.borrow()) == 1) && (last_1.with(|rc| *rc.borrow()) == 2)); destroyed_0.with(|rc| *rc.borrow_mut() = 0); - assert!((({ with_default_3(None,) }) == 5)); + assert!((({ with_default_3(S { v: 5 },) }) == 5)); assert!((destroyed_0.with(|rc| *rc.borrow()) == 1) && (last_1.with(|rc| *rc.borrow()) == 5)); destroyed_0.with(|rc| *rc.borrow_mut() = 0); { diff --git a/tests/unit/out/refcount/lambda_immediate_init.rs b/tests/unit/out/refcount/lambda_immediate_init.rs index 881897abaa..6dc23d3a21 100644 --- a/tests/unit/out/refcount/lambda_immediate_init.rs +++ b/tests/unit/out/refcount/lambda_immediate_init.rs @@ -6,17 +6,7 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -pub fn pick_0(x: Option) -> i32 { - let mut x: i32 = x.unwrap_or_else(|| { - ({ - FnPtr:: i32>::new(|| -> i32 { - { - return 237; - } - }) - .call() - }) - }); +pub fn pick_0(mut x: i32) -> i32 { return x; } #[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg)] @@ -69,8 +59,21 @@ pub fn main() { std::process::exit(main_0()); } fn main_0() -> i32 { - assert!((({ pick_0(None,) }) == 237)); - assert!((({ pick_0(Some(1),) }) == 1)); + assert!( + (({ + pick_0( + ({ + FnPtr:: i32>::new(|| -> i32 { + { + return 237; + } + }) + .call() + }), + ) + }) == 237) + ); + assert!((({ pick_0(1,) }) == 1)); let mut s: S = ::default(); assert!((s.k == 4)); assert!((g_1.with(|rc| *rc.borrow()) == 10)); diff --git a/tests/unit/out/refcount/pair_converting_construction.rs b/tests/unit/out/refcount/pair_converting_construction.rs index e4dae48bcb..f726f9358f 100644 --- a/tests/unit/out/refcount/pair_converting_construction.rs +++ b/tests/unit/out/refcount/pair_converting_construction.rs @@ -76,21 +76,20 @@ impl Holder { } } #[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, CppFrom)] -#[cpp_from(i32, new, some_value)] +#[cpp_from(i32, new, value)] #[byte_size(4)] pub struct Fallback { #[offset(0)] pub value: i32, } impl Fallback { - pub fn new(v: Option) -> Self { - let mut v: i32 = v.unwrap_or_else(|| -1_i32); + pub fn new(mut v: i32) -> Self { Self { value: v } } } impl Default for Fallback { fn default() -> Self { - { Fallback::new(None) } + { Fallback::new({ -1_i32 }) } } } #[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] @@ -285,7 +284,7 @@ fn main_0() -> i32 { )); assert!(({ ({ (*(*from_moved_by_value_pair.borrow()).0.borrow()).value } == 14) })); assert!(({ ({ (*(*from_moved_by_value_pair.borrow()).1.borrow()).value } == 15) })); - let mut defaulted: Fallback = Fallback::new(None); + let mut defaulted: Fallback = Fallback::new({ -1_i32 }); assert!((defaulted.value == -1_i32)); let from_defaulted_ctor: Value<(Value, Value)> = Rc::new(RefCell::new(( Rc::new(RefCell::new(>::cpp_from( diff --git a/tests/unit/out/refcount/template_default_arg.rs b/tests/unit/out/refcount/template_default_arg.rs index bf5e605e79..ac44df6288 100644 --- a/tests/unit/out/refcount/template_default_arg.rs +++ b/tests/unit/out/refcount/template_default_arg.rs @@ -54,16 +54,13 @@ impl NoDefault { Self { v: v } } } -pub fn used_0(x: Option) -> i32 { - let mut x: A = x.unwrap_or_else(|| A::new_2()); +pub fn used_0(mut x: A) -> i32 { return x.v; } -pub fn used_1(x: Option) -> i32 { - let mut x: B = x.unwrap_or_else(|| B::new()); +pub fn used_1(mut x: B) -> i32 { return x.v; } -pub fn scaled_2(mut x: A, n: Option) -> i32 { - let mut n: i32 = n.unwrap_or_else(|| (4usize as i32)); +pub fn scaled_2(mut x: A, mut n: i32) -> i32 { return (x.v * n); } pub fn always_given_3(mut x: NoDefault) -> i32 { @@ -86,11 +83,11 @@ pub fn main() { std::process::exit(main_0()); } fn main_0() -> i32 { - assert!((({ used_0(None,) }) == 1)); - assert!((({ used_0(Some(A::new_1({ 5 },)),) }) == 5)); - assert!((({ used_1(None,) }) == 2)); - assert!((({ scaled_2(A::new_1({ 3 },), None,) }) == (3 * (4usize as i32)))); - assert!((({ scaled_2(A::new_1({ 3 },), Some(2),) }) == 6)); + assert!((({ used_0(A::new_2(),) }) == 1)); + assert!((({ used_0(A::new_1({ 5 },),) }) == 5)); + assert!((({ used_1(B::new(),) }) == 2)); + assert!((({ scaled_2(A::new_1({ 3 },), (4usize as i32),) }) == (3 * (4usize as i32)))); + assert!((({ scaled_2(A::new_1({ 3 },), 2,) }) == 6)); assert!((({ always_given_3(NoDefault::new({ 3 },),) }) == 3)); let s: Value = Rc::new(RefCell::new(S_NoDefault_::new({ 1 }))); assert!((({ S_NoDefault_Impl::get(&s.as_pointer(), NoDefault::new({ 4 },),) }) == 5)); diff --git a/tests/unit/out/refcount/unboxed_locals.rs b/tests/unit/out/refcount/unboxed_locals.rs index 171d2aa5c8..24128f3375 100644 --- a/tests/unit/out/refcount/unboxed_locals.rs +++ b/tests/unit/out/refcount/unboxed_locals.rs @@ -41,8 +41,7 @@ pub fn sum_3(mut arr: Ptr, mut n: i32) -> i32 { } return s; } -pub fn countdown_4(mut n: i32, step: Option) -> i32 { - let mut step: i32 = step.unwrap_or_else(|| 1); +pub fn countdown_4(mut n: i32, mut step: i32) -> i32 { let mut steps: i32 = 0; 'loop_: while (n > 0) { n -= step; @@ -119,8 +118,8 @@ fn main_0() -> i32 { let mut zeros: [f64; 16] = [0_f64; 16]; zeros[(15) as usize] = 1.5_f64; assert!((zeros[(0) as usize] == 0_f64) && (zeros[(15) as usize] == 1.5_f64)); - assert!((({ countdown_4(10, None,) }) == 10)); - assert!((({ countdown_4(10, Some(3),) }) == 4)); + assert!((({ countdown_4(10, 1,) }) == 10)); + assert!((({ countdown_4(10, 3,) }) == 4)); let mut init: i32 = 3; let mut pair: Pair = Pair { first: init, From e823739d6326326bdbbed9563ef8669d4c03849d Mon Sep 17 00:00:00 2001 From: Lucian Popescu Date: Wed, 18 Nov 2026 13:27:30 +0000 Subject: [PATCH 4/4] Trigger CI