-
Notifications
You must be signed in to change notification settings - Fork 14
Translate in-class initializers in Default trait #332
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
lucic71
wants to merge
7
commits into
Cpp2Rust:master
Choose a base branch
from
lucic71:in-class-initializer
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
a3fc9a3
Translate in-class initializers
lucic71 0f9ed7a
Add in class initializer test
lucic71 950e146
Add implicitely initialized Inner
lucic71 8452469
Use default for template member with null in-class initializer
lucic71 9a00e2d
Add template specialization with in-class initializer test
lucic71 77a11f1
Add explaining comment
lucic71 96eedc9
Merge branch 'master' into in-class-initializer
lucic71 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| #include <cassert> | ||
| #include <string> | ||
|
|
||
| struct Inner { | ||
| int x = 3; | ||
| int y = 4; | ||
| }; | ||
|
|
||
| struct S { | ||
| int a = 1; | ||
| char b = 2; | ||
| Inner c = {}; | ||
| Inner d; | ||
| }; | ||
|
|
||
| // Boxed::tag is in-class initialized. However the default constructor is never | ||
| // instantiated, only the explicit one is used. | ||
| // | ||
| // Because no default constructor is instantiated, the specialization does not | ||
| // contain the in-class initializer. In Rust, the Default trait initializes | ||
| // Boxed::tag with 0. This is correct because the C++ program never reads the | ||
| // in-class initializer of Boxed::tag, hence Rust also does not read it. | ||
| template <typename T> struct Boxed { | ||
| T v = T(); | ||
| int tag = 7; | ||
| Boxed(T x, int t) : v(x), tag(t) {} | ||
| }; | ||
|
|
||
| int main() { | ||
| S s; | ||
| assert(s.a == 1); | ||
| assert(s.b == 2); | ||
| assert(s.c.x == 3); | ||
| assert(s.c.y == 4); | ||
| assert(s.d.x == 3); | ||
| assert(s.d.y == 4); | ||
| Boxed<int> boxed(5, 9); | ||
| assert(boxed.v == 5); | ||
| assert(boxed.tag == 9); | ||
| return 0; | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,163 @@ | ||
| 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}; | ||
| #[derive()] | ||
| pub struct Inner { | ||
| pub x: Value<i32>, | ||
| pub y: Value<i32>, | ||
| } | ||
| impl Clone for Inner { | ||
| fn clone(&self) -> Self { | ||
| let __this: Value<Inner> = Rc::new(RefCell::new(Self { | ||
| x: Rc::new(RefCell::new((*self.x.borrow()))), | ||
| y: Rc::new(RefCell::new((*self.y.borrow()))), | ||
| })); | ||
| let this: Ptr<Inner> = __this.as_pointer(); | ||
| Rc::try_unwrap(__this).ok().unwrap().into_inner() | ||
| } | ||
| } | ||
| impl Default for Inner { | ||
| fn default() -> Self { | ||
| Inner { | ||
| x: Rc::new(RefCell::new(3)), | ||
| y: Rc::new(RefCell::new(4)), | ||
| } | ||
| } | ||
| } | ||
| impl ByteRepr for Inner { | ||
| fn byte_size() -> usize { | ||
| 8 | ||
| } | ||
| fn to_bytes(&self, buf: &mut [u8]) { | ||
| (*self.x.borrow()).to_bytes(&mut buf[0..4]); | ||
| (*self.y.borrow()).to_bytes(&mut buf[4..8]); | ||
| } | ||
| fn from_bytes(buf: &[u8]) -> Self { | ||
| Self { | ||
| x: Rc::new(RefCell::new(<i32>::from_bytes(&buf[0..4]))), | ||
| y: Rc::new(RefCell::new(<i32>::from_bytes(&buf[4..8]))), | ||
| } | ||
| } | ||
| } | ||
| #[derive()] | ||
| pub struct S { | ||
| pub a: Value<i32>, | ||
| pub b: Value<u8>, | ||
| pub c: Value<Inner>, | ||
| pub d: Value<Inner>, | ||
| } | ||
| impl Clone for S { | ||
| fn clone(&self) -> Self { | ||
| let __this: Value<S> = Rc::new(RefCell::new(Self { | ||
| a: Rc::new(RefCell::new((*self.a.borrow()))), | ||
| b: Rc::new(RefCell::new((*self.b.borrow()))), | ||
| c: Rc::new(RefCell::new((*self.c.borrow()).clone())), | ||
| d: Rc::new(RefCell::new((*self.d.borrow()).clone())), | ||
| })); | ||
| let this: Ptr<S> = __this.as_pointer(); | ||
| Rc::try_unwrap(__this).ok().unwrap().into_inner() | ||
| } | ||
| } | ||
| impl Default for S { | ||
| fn default() -> Self { | ||
| S { | ||
| a: Rc::new(RefCell::new(1)), | ||
| b: Rc::new(RefCell::new(2_u8)), | ||
| c: Rc::new(RefCell::new(Inner { | ||
| x: Rc::new(RefCell::new(3)), | ||
| y: Rc::new(RefCell::new(4)), | ||
| })), | ||
| d: <Value<Inner>>::default(), | ||
| } | ||
| } | ||
| } | ||
| impl ByteRepr for S { | ||
| fn byte_size() -> usize { | ||
| 24 | ||
| } | ||
| fn to_bytes(&self, buf: &mut [u8]) { | ||
| (*self.a.borrow()).to_bytes(&mut buf[0..4]); | ||
| (*self.b.borrow()).to_bytes(&mut buf[4..5]); | ||
| (*self.c.borrow()).to_bytes(&mut buf[8..16]); | ||
| (*self.d.borrow()).to_bytes(&mut buf[16..24]); | ||
| } | ||
| fn from_bytes(buf: &[u8]) -> Self { | ||
| Self { | ||
| a: Rc::new(RefCell::new(<i32>::from_bytes(&buf[0..4]))), | ||
| b: Rc::new(RefCell::new(<u8>::from_bytes(&buf[4..5]))), | ||
| c: Rc::new(RefCell::new(<Inner>::from_bytes(&buf[8..16]))), | ||
| d: Rc::new(RefCell::new(<Inner>::from_bytes(&buf[16..24]))), | ||
| } | ||
| } | ||
| } | ||
| #[derive()] | ||
| pub struct Boxed_int_ { | ||
| pub v: Value<i32>, | ||
| pub tag: Value<i32>, | ||
| } | ||
| impl Boxed_int_ { | ||
| pub fn Boxed_int_(x: i32, t: i32) -> Self { | ||
| let x: Value<i32> = Rc::new(RefCell::new(x)); | ||
| let t: Value<i32> = Rc::new(RefCell::new(t)); | ||
| let __this: Value<Boxed_int_> = Rc::new(RefCell::new(Self { | ||
| v: Rc::new(RefCell::new((*x.borrow()))), | ||
| tag: Rc::new(RefCell::new((*t.borrow()))), | ||
| })); | ||
| let this: Ptr<Boxed_int_> = __this.as_pointer(); | ||
| Rc::try_unwrap(__this).ok().unwrap().into_inner() | ||
| } | ||
| } | ||
| impl Clone for Boxed_int_ { | ||
| fn clone(&self) -> Self { | ||
| let __this: Value<Boxed_int_> = Rc::new(RefCell::new(Self { | ||
| v: Rc::new(RefCell::new((*self.v.borrow()))), | ||
| tag: Rc::new(RefCell::new((*self.tag.borrow()))), | ||
| })); | ||
| let this: Ptr<Boxed_int_> = __this.as_pointer(); | ||
| Rc::try_unwrap(__this).ok().unwrap().into_inner() | ||
| } | ||
| } | ||
| impl Default for Boxed_int_ { | ||
| fn default() -> Self { | ||
| Boxed_int_ { | ||
| v: <Value<i32>>::default(), | ||
| tag: <Value<i32>>::default(), | ||
| } | ||
| } | ||
| } | ||
| impl ByteRepr for Boxed_int_ { | ||
| fn byte_size() -> usize { | ||
| 8 | ||
| } | ||
| fn to_bytes(&self, buf: &mut [u8]) { | ||
| (*self.v.borrow()).to_bytes(&mut buf[0..4]); | ||
| (*self.tag.borrow()).to_bytes(&mut buf[4..8]); | ||
| } | ||
| fn from_bytes(buf: &[u8]) -> Self { | ||
| Self { | ||
| v: Rc::new(RefCell::new(<i32>::from_bytes(&buf[0..4]))), | ||
| tag: Rc::new(RefCell::new(<i32>::from_bytes(&buf[4..8]))), | ||
| } | ||
| } | ||
| } | ||
| pub fn main() { | ||
| std::process::exit(main_0()); | ||
| } | ||
| fn main_0() -> i32 { | ||
| let s: Value<S> = Rc::new(RefCell::new(<S>::default())); | ||
| assert!(((*(*s.borrow()).a.borrow()) == 1)); | ||
| assert!((((*(*s.borrow()).b.borrow()) as i32) == 2)); | ||
| assert!(((*(*(*s.borrow()).c.borrow()).x.borrow()) == 3)); | ||
| assert!(((*(*(*s.borrow()).c.borrow()).y.borrow()) == 4)); | ||
| assert!(((*(*(*s.borrow()).d.borrow()).x.borrow()) == 3)); | ||
| assert!(((*(*(*s.borrow()).d.borrow()).y.borrow()) == 4)); | ||
| let boxed: Value<Boxed_int_> = Rc::new(RefCell::new(Boxed_int_::Boxed_int_({ 5 }, { 9 }))); | ||
| assert!(((*(*boxed.borrow()).v.borrow()) == 5)); | ||
| assert!(((*(*boxed.borrow()).tag.borrow()) == 9)); | ||
| return 0; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| 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; | ||
| #[repr(C)] | ||
| #[derive(Copy, Clone)] | ||
| pub struct Inner { | ||
| pub x: i32, | ||
| pub y: i32, | ||
| } | ||
| impl Default for Inner { | ||
| fn default() -> Self { | ||
| Inner { x: 3, y: 4 } | ||
| } | ||
| } | ||
| #[repr(C)] | ||
| #[derive(Copy, Clone)] | ||
| pub struct S { | ||
| pub a: i32, | ||
| pub b: libc::c_char, | ||
| pub c: Inner, | ||
| pub d: Inner, | ||
| } | ||
| impl Default for S { | ||
| fn default() -> Self { | ||
| S { | ||
| a: 1, | ||
| b: (2 as libc::c_char), | ||
| c: Inner { x: 3, y: 4 }, | ||
| d: <Inner>::default(), | ||
| } | ||
| } | ||
| } | ||
| #[repr(C)] | ||
| #[derive(Copy, Clone)] | ||
| pub struct Boxed_int_ { | ||
| pub v: i32, | ||
| pub tag: i32, | ||
| } | ||
| impl Boxed_int_ { | ||
| pub unsafe fn Boxed_int_(mut x: i32, mut t: i32) -> Self { | ||
| let mut this = Self { v: x, tag: t }; | ||
| this | ||
| } | ||
| } | ||
| impl Default for Boxed_int_ { | ||
| fn default() -> Self { | ||
| Boxed_int_ { | ||
| v: 0_i32, | ||
| tag: 0_i32, | ||
| } | ||
| } | ||
| } | ||
| pub fn main() { | ||
| unsafe { | ||
| std::process::exit(main_0() as i32); | ||
| } | ||
| } | ||
| unsafe fn main_0() -> i32 { | ||
| let mut s: S = <S>::default(); | ||
| assert!(((s.a) == (1))); | ||
| assert!(((s.b as i32) == (2))); | ||
| assert!(((s.c.x) == (3))); | ||
| assert!(((s.c.y) == (4))); | ||
| assert!(((s.d.x) == (3))); | ||
| assert!(((s.d.y) == (4))); | ||
| let mut boxed: Boxed_int_ = Boxed_int_::Boxed_int_({ 5 }, { 9 }); | ||
| assert!(((boxed.v) == (5))); | ||
| assert!(((boxed.tag) == (9))); | ||
| return 0; | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
c should be initialized like d
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Even if Inner is defined as:
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Inner x{}would initialize 'x' to zero, While 'Inner x' doesn't. But we probably need to default initialize everything anyway.