diff --git a/cpp2rust/converter/models/boxed_vars.cpp b/cpp2rust/converter/models/boxed_vars.cpp index 3934c5a434..8d100e9981 100644 --- a/cpp2rust/converter/models/boxed_vars.cpp +++ b/cpp2rust/converter/models/boxed_vars.cpp @@ -228,6 +228,11 @@ class AddressTakenVisitor AddressTaken(capture.getCapturedVar()); } } + if (expr->getLambdaClass()->isGenericLambda()) { + ForEachLambdaCallOperator( + expr->getLambdaClass(), + [&](clang::CXXMethodDecl *op) { TraverseDecl(op); }); + } return true; } diff --git a/tests/std/expr/expr.prim/expr.prim.lambda/expr.prim.lambda.capture.cpp b/tests/std/expr/expr.prim/expr.prim.lambda/expr.prim.lambda.capture.cpp new file mode 100644 index 0000000000..87d997ea63 --- /dev/null +++ b/tests/std/expr/expr.prim/expr.prim.lambda/expr.prim.lambda.capture.cpp @@ -0,0 +1,21 @@ +// ADDITIONAL_COMPILE_FLAGS: -std=c++23 +#include + +// N4950 [expr.prim.lambda.capture]/12: an entity is captured by reference if +// it is implicitly or explicitly captured but not captured by copy. +namespace p12_parameter_of_generic_lambda_captured_by_reference { +void test() { + auto increment_twice = [](auto n) { + auto increment = [&n] { ++n; }; + increment(); + increment(); + return n; + }; + assert(increment_twice(3) == 5); +} +} // namespace p12_parameter_of_generic_lambda_captured_by_reference + +int main() { + p12_parameter_of_generic_lambda_captured_by_reference::test(); + return 0; +} diff --git a/tests/std/expr/expr.prim/expr.prim.lambda/out/refcount/expr.prim.lambda.capture.rs b/tests/std/expr/expr.prim/expr.prim.lambda/out/refcount/expr.prim.lambda.capture.rs new file mode 100644 index 0000000000..2bd8372e55 --- /dev/null +++ b/tests/std/expr/expr.prim/expr.prim.lambda/out/refcount/expr.prim.lambda.capture.rs @@ -0,0 +1,43 @@ +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 test_0() { + let increment_twice: Value> = + Rc::new(RefCell::new(lambda!(Generic, {}, |n: i32| -> i32 { + let n: Value = Rc::new(RefCell::new(n)); + let increment: Value> = Rc::new(RefCell::new(lambda!( + { + let n: Ptr = n.as_pointer(); + }, + || { + n.with_mut(|__v| __v.prefix_inc()); + } + ))); + ({ (*increment.borrow()).call() }); + ({ (*increment.borrow()).call() }); + return (*n.borrow()); + }))); + assert!( + ({ + (({ + (*increment_twice.borrow()) + .spec:: i32>(0) + .call(3) + }) == 5) + }) + ); +} +pub fn main() { + __cpp2rust_init_globals(); + std::process::exit(main_0()); +} +fn main_0() -> i32 { + ({ test_0() }); + return 0; +} +pub fn __cpp2rust_init_globals() {} diff --git a/tests/std/expr/expr.prim/expr.prim.lambda/out/unsafe/expr.prim.lambda.capture.rs b/tests/std/expr/expr.prim/expr.prim.lambda/out/unsafe/expr.prim.lambda.capture.rs new file mode 100644 index 0000000000..01165a1203 --- /dev/null +++ b/tests/std/expr/expr.prim/expr.prim.lambda/out/unsafe/expr.prim.lambda.capture.rs @@ -0,0 +1,35 @@ +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 test_0() { + let mut increment_twice: FnPtr = lambda_unsafe!(Generic, {}, |mut n: i32| -> i32 { + let mut increment: FnPtr = lambda_unsafe!( + { + let n: *mut i32 = &mut n; + }, + || { + (*n).prefix_inc(); + } + ); + (unsafe { increment.call() }); + (unsafe { increment.call() }); + return n; + }); + assert!(((unsafe { increment_twice.spec:: i32>(0).call(3,) }) == (5))); +} +pub fn main() { + unsafe { + __cpp2rust_init_globals(); + std::process::exit(main_0() as i32); + } +} +unsafe fn main_0() -> i32 { + (unsafe { test_0() }); + return 0; +} +pub unsafe fn __cpp2rust_init_globals() {}