Skip to content

Commit 58318d6

Browse files
authored
Visit instantiations of generic lambda bodies in boxed var analysis (#632)
1 parent bab7f26 commit 58318d6

4 files changed

Lines changed: 104 additions & 0 deletions

File tree

‎cpp2rust/converter/models/boxed_vars.cpp‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,11 @@ class AddressTakenVisitor
230230
AddressTaken(capture.getCapturedVar());
231231
}
232232
}
233+
if (expr->getLambdaClass()->isGenericLambda()) {
234+
ForEachLambdaCallOperator(
235+
expr->getLambdaClass(),
236+
[&](clang::CXXMethodDecl *op) { TraverseDecl(op); });
237+
}
233238
return true;
234239
}
235240

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// ADDITIONAL_COMPILE_FLAGS: -std=c++23
2+
#include <cassert>
3+
4+
// N4950 [expr.prim.lambda.capture]/12: an entity is captured by reference if
5+
// it is implicitly or explicitly captured but not captured by copy.
6+
namespace p12_parameter_of_generic_lambda_captured_by_reference {
7+
void test() {
8+
auto increment_twice = [](auto n) {
9+
auto increment = [&n] { ++n; };
10+
increment();
11+
increment();
12+
return n;
13+
};
14+
assert(increment_twice(3) == 5);
15+
}
16+
} // namespace p12_parameter_of_generic_lambda_captured_by_reference
17+
18+
int main() {
19+
p12_parameter_of_generic_lambda_captured_by_reference::test();
20+
return 0;
21+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
extern crate libcc2rs;
2+
use libcc2rs::*;
3+
use std::cell::RefCell;
4+
use std::collections::BTreeMap;
5+
use std::io::prelude::*;
6+
use std::io::{Read, Seek, Write};
7+
use std::os::fd::AsFd;
8+
use std::rc::{Rc, Weak};
9+
pub fn test_0() {
10+
let increment_twice: Value<FnPtr<Generic>> =
11+
Rc::new(RefCell::new(lambda!(Generic, {}, |n: i32| -> i32 {
12+
let n: Value<i32> = Rc::new(RefCell::new(n));
13+
let increment: Value<FnPtr<fn()>> = Rc::new(RefCell::new(lambda!(
14+
{
15+
let n: Ptr<i32> = n.as_pointer();
16+
},
17+
|| {
18+
n.with_mut(|__v| __v.prefix_inc());
19+
}
20+
)));
21+
({ (*increment.borrow()).call() });
22+
({ (*increment.borrow()).call() });
23+
return (*n.borrow());
24+
})));
25+
assert!(
26+
({
27+
(({
28+
(*increment_twice.borrow())
29+
.spec::<fn(i32) -> i32>(0)
30+
.call(3)
31+
}) == 5)
32+
})
33+
);
34+
}
35+
pub fn main() {
36+
__cpp2rust_init_globals();
37+
std::process::exit(main_0());
38+
}
39+
fn main_0() -> i32 {
40+
({ test_0() });
41+
return 0;
42+
}
43+
pub fn __cpp2rust_init_globals() {}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
extern crate libc;
2+
use libc::*;
3+
extern crate libcc2rs;
4+
use libcc2rs::*;
5+
use std::collections::BTreeMap;
6+
use std::io::{Read, Seek, Write};
7+
use std::os::fd::{AsFd, FromRawFd, IntoRawFd};
8+
use std::rc::Rc;
9+
pub unsafe fn test_0() {
10+
let mut increment_twice: FnPtr<Generic> = lambda_unsafe!(Generic, {}, |mut n: i32| -> i32 {
11+
let mut increment: FnPtr<fn()> = lambda_unsafe!(
12+
{
13+
let n: *mut i32 = &mut n;
14+
},
15+
|| {
16+
(*n).prefix_inc();
17+
}
18+
);
19+
(unsafe { increment.call() });
20+
(unsafe { increment.call() });
21+
return n;
22+
});
23+
assert!(((unsafe { increment_twice.spec::<fn(i32) -> i32>(0).call(3,) }) == (5)));
24+
}
25+
pub fn main() {
26+
unsafe {
27+
__cpp2rust_init_globals();
28+
std::process::exit(main_0() as i32);
29+
}
30+
}
31+
unsafe fn main_0() -> i32 {
32+
(unsafe { test_0() });
33+
return 0;
34+
}
35+
pub unsafe fn __cpp2rust_init_globals() {}

0 commit comments

Comments
 (0)