Skip to content

Commit a290c68

Browse files
committed
Don't translate constexpr functions to const fn
1 parent 360a2a5 commit a290c68

6 files changed

Lines changed: 167 additions & 7 deletions

File tree

‎cpp2rust/converter/converter.cpp‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -377,8 +377,7 @@ bool Converter::VisitFunctionDecl(clang::FunctionDecl *decl) {
377377
// main_0 should be static
378378
if (!decl->isMain())
379379
ConvertFunctionQualifiers(decl);
380-
StrCat(decl->isConstexpr() ? keyword_const_fn_ : "", keyword_unsafe_,
381-
keyword::kFn, std::move(function_name));
380+
StrCat(keyword_unsafe_, keyword::kFn, std::move(function_name));
382381
{
383382
PushParen paren(*this);
384383
ConvertFunctionParameters(decl);

‎cpp2rust/converter/converter.h‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,9 @@ class Converter : public clang::RecursiveASTVisitor<Converter> {
2929
public:
3030
explicit Converter(std::string &rs_code, clang::ASTContext &ctx,
3131
const char *keyword_unsafe = "unsafe",
32-
const char *keyword_mut = keyword::kMut,
33-
const char *keyword_const_fn = keyword::kConst)
32+
const char *keyword_mut = keyword::kMut)
3433
: rs_code_(&rs_code), ctx_(ctx), keyword_unsafe_(keyword_unsafe),
35-
keyword_mut_(keyword_mut), keyword_const_fn_(keyword_const_fn) {}
34+
keyword_mut_(keyword_mut) {}
3635

3736
virtual ~Converter() = default;
3837

@@ -1011,7 +1010,6 @@ class Converter : public clang::RecursiveASTVisitor<Converter> {
10111010
const clang::QualType *type = nullptr);
10121011
const char *keyword_unsafe_;
10131012
const char *keyword_mut_;
1014-
const char *keyword_const_fn_;
10151013
std::vector<ExprKind> curr_expr_kind_;
10161014
static std::unordered_map<std::string, std::string> inner_structs_;
10171015
static std::unordered_set<std::string> globals_;

‎cpp2rust/converter/models/converter_refcount.cpp‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
namespace cpp2rust {
2020
ConverterRefCount::ConverterRefCount(std::string &rs_code,
2121
clang::ASTContext &ctx)
22-
: Converter(rs_code, ctx, "", "", ""),
22+
: Converter(rs_code, ctx, "", ""),
2323
conversion_kind_({ConversionKind::Unboxed}) {}
2424

2525
void ConverterRefCount::EmitFilePreamble() {

‎tests/unit/constexpr_function.cpp‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#include <cassert>
2+
3+
int runtime_only(int x) { return x * 2; }
4+
5+
constexpr int first(const int *p) { return *p; }
6+
7+
constexpr int scaled(int x) {
8+
if (x < 0) {
9+
return runtime_only(-x);
10+
}
11+
return x;
12+
}
13+
14+
constexpr double half(double x) { return x / 2.0; }
15+
16+
struct P {
17+
int v;
18+
constexpr int get() const { return v; }
19+
};
20+
21+
int main() {
22+
int arr[2] = {7, 8};
23+
assert(first(arr) == 7);
24+
assert(first(arr + 1) == 8);
25+
assert(scaled(3) == 3);
26+
assert(scaled(-3) == 6);
27+
assert(half(5.0) == 2.5);
28+
P p{9};
29+
assert(p.get() == 9);
30+
constexpr int k = scaled(4);
31+
assert(k == 4);
32+
return 0;
33+
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
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 runtime_only_0(x: i32) -> i32 {
10+
let x: Value<i32> = Rc::new(RefCell::new(x));
11+
return ((*x.borrow()) * 2);
12+
}
13+
pub fn first_1(p: Ptr<i32>) -> i32 {
14+
let p: Value<Ptr<i32>> = Rc::new(RefCell::new(p));
15+
return ((*p.borrow()).read());
16+
}
17+
pub fn scaled_2(x: i32) -> i32 {
18+
let x: Value<i32> = Rc::new(RefCell::new(x));
19+
if ((*x.borrow()) < 0) {
20+
return ({ runtime_only_0(-(*x.borrow())) });
21+
}
22+
return (*x.borrow());
23+
}
24+
pub fn half_3(x: f64) -> f64 {
25+
let x: Value<f64> = Rc::new(RefCell::new(x));
26+
return ((*x.borrow()) / 2.0E+0);
27+
}
28+
#[derive(Default)]
29+
pub struct P {
30+
pub v: Value<i32>,
31+
}
32+
impl Clone for P {
33+
fn clone(&self) -> Self {
34+
let __this: Value<P> = Rc::new(RefCell::new(Self {
35+
v: Rc::new(RefCell::new((*self.v.borrow()))),
36+
}));
37+
let this: Ptr<P> = __this.as_pointer();
38+
Rc::try_unwrap(__this).ok().unwrap().into_inner()
39+
}
40+
}
41+
impl ByteRepr for P {
42+
fn byte_size() -> usize {
43+
4
44+
}
45+
fn to_bytes(&self, buf: &mut [u8]) {
46+
(*self.v.borrow()).to_bytes(&mut buf[0..4]);
47+
}
48+
fn from_bytes(buf: &[u8]) -> Self {
49+
Self {
50+
v: Rc::new(RefCell::new(<i32>::from_bytes(&buf[0..4]))),
51+
}
52+
}
53+
}
54+
pub fn main() {
55+
std::process::exit(main_0());
56+
}
57+
fn main_0() -> i32 {
58+
let arr: Value<Box<[i32]>> = Rc::new(RefCell::new(Box::new([7, 8])));
59+
assert!((({ first_1((arr.as_pointer() as Ptr::<i32>),) }) == 7));
60+
assert!((({ first_1((arr.as_pointer() as Ptr::<i32>).offset((1) as isize),) }) == 8));
61+
assert!((({ scaled_2(3,) }) == 3));
62+
assert!((({ scaled_2(-3_i32,) }) == 6));
63+
assert!((({ half_3(5.0E+0,) }) == 2.5E+0));
64+
let p: Value<P> = Rc::new(RefCell::new(P {
65+
v: Rc::new(RefCell::new(9)),
66+
}));
67+
assert!((({ PImpl::get(&p.as_pointer(),) }) == 9));
68+
let k: Value<i32> = Rc::new(RefCell::new(({ scaled_2(4) })));
69+
assert!(((*k.borrow()) == 4));
70+
return 0;
71+
}
72+
pub trait PImpl {
73+
fn get(&self) -> i32;
74+
}
75+
impl PImpl for Ptr<P> {
76+
fn get(&self) -> i32 {
77+
return (*(*(*self).upgrade().deref()).v.borrow());
78+
}
79+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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 runtime_only_0(mut x: i32) -> i32 {
10+
return ((x) * (2));
11+
}
12+
pub unsafe fn first_1(mut p: *const i32) -> i32 {
13+
return (*p);
14+
}
15+
pub unsafe fn scaled_2(mut x: i32) -> i32 {
16+
if ((x) < (0)) {
17+
return (unsafe { runtime_only_0(-x) });
18+
}
19+
return x;
20+
}
21+
pub unsafe fn half_3(mut x: f64) -> f64 {
22+
return ((x) / (2.0E+0));
23+
}
24+
#[repr(C)]
25+
#[derive(Copy, Clone, Default)]
26+
pub struct P {
27+
pub v: i32,
28+
}
29+
impl P {
30+
pub unsafe fn get(&self) -> i32 {
31+
return self.v;
32+
}
33+
}
34+
pub fn main() {
35+
unsafe {
36+
std::process::exit(main_0() as i32);
37+
}
38+
}
39+
unsafe fn main_0() -> i32 {
40+
let mut arr: [i32; 2] = [7, 8];
41+
assert!(((unsafe { first_1((arr.as_mut_ptr()).cast_const(),) }) == (7)));
42+
assert!(((unsafe { first_1((arr.as_mut_ptr().offset((1) as isize)).cast_const(),) }) == (8)));
43+
assert!(((unsafe { scaled_2(3,) }) == (3)));
44+
assert!(((unsafe { scaled_2(-3_i32,) }) == (6)));
45+
assert!(((unsafe { half_3(5.0E+0,) }) == (2.5E+0)));
46+
let mut p: P = P { v: 9 };
47+
assert!(((unsafe { P::get(&p,) }) == (9)));
48+
let k: i32 = (unsafe { scaled_2(4) });
49+
assert!(((k) == (4)));
50+
return 0;
51+
}

0 commit comments

Comments
 (0)