Skip to content

Commit 6c0f20c

Browse files
authored
Fix usage of lambda in unevaluated context (#627)
`decltype(twice(2.5)) n = 4;` was forcing a translation of the generic lambda `twice`, but for `double` there is no body, only this usage has a body: `assert(twice(3) == 6)`. Skip lambda instantiations that don't have a body.
1 parent f0f2471 commit 6c0f20c

4 files changed

Lines changed: 75 additions & 1 deletion

File tree

‎cpp2rust/converter/converter_lib.cpp‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1452,7 +1452,9 @@ void ForEachLambdaCallOperator(
14521452
return;
14531453
}
14541454
for (auto spec : decl->getDependentLambdaCallOperator()->specializations()) {
1455-
fn(clang::cast<clang::CXXMethodDecl>(spec));
1455+
if (spec->hasBody()) {
1456+
fn(clang::cast<clang::CXXMethodDecl>(spec));
1457+
}
14561458
}
14571459
}
14581460

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// ADDITIONAL_COMPILE_FLAGS: -std=c++23
2+
#include <cassert>
3+
4+
// N4950 [expr.prim.lambda.closure]/3: the closure type of a generic lambda has
5+
// a function call operator template whose return type is described by the
6+
// trailing-return-type.
7+
namespace p3_call_operator_template_named_in_decltype {
8+
void test() {
9+
auto twice = [](auto x) -> int { return x * 2; };
10+
decltype(twice(2.5)) n = 4;
11+
assert(n == 4);
12+
assert(twice(3) == 6);
13+
}
14+
} // namespace p3_call_operator_template_named_in_decltype
15+
16+
int main() {
17+
p3_call_operator_template_named_in_decltype::test();
18+
return 0;
19+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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 twice: Value<FnPtr<Generic>> =
11+
Rc::new(RefCell::new(lambda!(Generic, {}, |mut x: i32| -> i32 {
12+
return (x * 2);
13+
})));
14+
let mut n: i32 = 4;
15+
assert!((n == 4));
16+
assert!(({ (({ (*twice.borrow()).spec::<fn(i32) -> i32>(0).call(3,) }) == 6) }));
17+
}
18+
pub fn main() {
19+
__cpp2rust_init_globals();
20+
std::process::exit(main_0());
21+
}
22+
fn main_0() -> i32 {
23+
({ test_0() });
24+
return 0;
25+
}
26+
pub fn __cpp2rust_init_globals() {}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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 twice: FnPtr<Generic> = lambda_unsafe!(Generic, {}, |mut x: i32| -> i32 {
11+
return ((x) * (2));
12+
});
13+
let mut n: i32 = 4;
14+
assert!(((n) == (4)));
15+
assert!(((unsafe { twice.spec::<fn(i32) -> i32>(0).call(3,) }) == (6)));
16+
}
17+
pub fn main() {
18+
unsafe {
19+
__cpp2rust_init_globals();
20+
std::process::exit(main_0() as i32);
21+
}
22+
}
23+
unsafe fn main_0() -> i32 {
24+
(unsafe { test_0() });
25+
return 0;
26+
}
27+
pub unsafe fn __cpp2rust_init_globals() {}

0 commit comments

Comments
 (0)