-
Notifications
You must be signed in to change notification settings - Fork 283
transpile: use a repeat expression for arrays with a single zero initializer #1218
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
18f6766
transpile: copy `arrays.c` test as a snapshot test, too
kkysen 6474985
transpile: emit the shorter `[0; N]` for arrays initialized with `{ 0 }`
folkertdev 84a4c35
transpile: fix up comments in `fn convert_init_list`'s `ConstantArray…
kkysen 102770d
transpile: remove `buffer` from `arrays.c` snapshot
kkysen 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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| #include <stdlib.h> | ||
|
|
||
| static char simple[] = "mystring"; | ||
| static char *foo = "mystring"; | ||
|
|
||
| void entry(void) { | ||
| int arr[1][1] = { 1 }; | ||
| arr[0][0] += 9; | ||
|
|
||
| int arr2[16] = {}; | ||
| arr2[15] += 9; | ||
|
|
||
| struct {char* x; int y;} arr3[1] = {}; | ||
| arr3[0].y += 9; | ||
|
|
||
| int arr4[16] = {0}; | ||
| arr4[15] += 9; | ||
|
|
||
| struct {short; int y;} arr5[1] = { { 1, 2 } }; | ||
| arr5[0].y += 9; | ||
|
|
||
| // excess elements | ||
| int arr6[2] = { 1, 2, 3 }; | ||
| int arr7[0] = { 1234 }; | ||
|
|
||
| char abc[] = "abc"; | ||
|
|
||
| char def[] = {'d','e','f'}; | ||
|
|
||
| char part[2] = {1}; | ||
|
|
||
| char *abcptr = "abc"; | ||
|
|
||
| char init[] = {"abcd"}; | ||
|
|
||
| char too_long[3] = "abcde"; | ||
|
|
||
| char too_short[20] = "abc"; | ||
|
|
||
| // TODO re-enable after #1266 adds portable support for translating `wchar_t`. | ||
| #if 0 | ||
| wchar_t wide1[] = L"x"; | ||
|
|
||
| wchar_t wide2[3] = L"x"; | ||
|
|
||
| wchar_t wide3[1] = L"xy"; | ||
| #endif | ||
|
|
||
| // Test that we can get the address of the element past the end of the array | ||
| char *past_end = &simple[sizeof(simple)]; | ||
| past_end = &foo[8]; | ||
| } | ||
|
|
||
| void short_initializer() { | ||
| int empty_brackets[16] = {}; | ||
| int brackets_with_zero[16] = {0}; | ||
| int brackets_with_one[4] = {1}; | ||
|
|
||
| // excess elements | ||
| int excess_elements_1[2] = { 1, 2, 3 }; | ||
| int excess_elements_2[0] = { 1234 }; | ||
|
|
||
| struct {short x; int y;} single_struct[1] = { { 1, 2 } }; | ||
| struct {short x; int y;} many_struct[3] = { { 1, 2 } }; | ||
| } |
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,129 @@ | ||
| --- | ||
| source: c2rust-transpile/tests/snapshots.rs | ||
| expression: cat tests/snapshots/arrays.rs | ||
| input_file: c2rust-transpile/tests/snapshots/arrays.c | ||
| --- | ||
| #![allow( | ||
| dead_code, | ||
| mutable_transmutes, | ||
| non_camel_case_types, | ||
| non_snake_case, | ||
| non_upper_case_globals, | ||
| unused_assignments, | ||
| unused_mut | ||
| )] | ||
| #[derive(Copy, Clone)] | ||
| #[repr(C)] | ||
| pub struct C2RustUnnamed { | ||
| pub y: std::ffi::c_int, | ||
| } | ||
| #[derive(Copy, Clone)] | ||
| #[repr(C)] | ||
| pub struct C2RustUnnamed_0 { | ||
| pub x: *mut std::ffi::c_char, | ||
| pub y: std::ffi::c_int, | ||
| } | ||
| #[derive(Copy, Clone)] | ||
| #[repr(C)] | ||
| pub struct C2RustUnnamed_1 { | ||
| pub x: std::ffi::c_short, | ||
| pub y: std::ffi::c_int, | ||
| } | ||
| #[derive(Copy, Clone)] | ||
| #[repr(C)] | ||
| pub struct C2RustUnnamed_2 { | ||
| pub x: std::ffi::c_short, | ||
| pub y: std::ffi::c_int, | ||
| } | ||
| static mut simple: [std::ffi::c_char; 9] = unsafe { | ||
| *::core::mem::transmute::<&[u8; 9], &mut [std::ffi::c_char; 9]>(b"mystring\0") | ||
| }; | ||
| static mut foo: *mut std::ffi::c_char = b"mystring\0" as *const u8 | ||
| as *const std::ffi::c_char as *mut std::ffi::c_char; | ||
| #[no_mangle] | ||
| pub unsafe extern "C" fn entry() { | ||
| let mut arr: [[std::ffi::c_int; 1]; 1] = [[1 as std::ffi::c_int]]; | ||
| arr[0 as std::ffi::c_int as usize][0 as std::ffi::c_int as usize] | ||
| += 9 as std::ffi::c_int; | ||
| let mut arr2: [std::ffi::c_int; 16] = [0; 16]; | ||
| arr2[15 as std::ffi::c_int as usize] += 9 as std::ffi::c_int; | ||
| let mut arr3: [C2RustUnnamed_0; 1] = [C2RustUnnamed_0 { | ||
| x: 0 as *mut std::ffi::c_char, | ||
| y: 0, | ||
| }; 1]; | ||
| arr3[0 as std::ffi::c_int as usize].y += 9 as std::ffi::c_int; | ||
| let mut arr4: [std::ffi::c_int; 16] = [0 as std::ffi::c_int; 16]; | ||
| arr4[15 as std::ffi::c_int as usize] += 9 as std::ffi::c_int; | ||
| let mut arr5: [C2RustUnnamed; 1] = [ | ||
| { | ||
| let mut init = C2RustUnnamed { | ||
| y: 1 as std::ffi::c_int, | ||
| }; | ||
| init | ||
| }, | ||
| ]; | ||
| arr5[0 as std::ffi::c_int as usize].y += 9 as std::ffi::c_int; | ||
| let mut arr6: [std::ffi::c_int; 2] = [1 as std::ffi::c_int, 2 as std::ffi::c_int]; | ||
| let mut arr7: [std::ffi::c_int; 0] = [0; 0]; | ||
| let mut abc: [std::ffi::c_char; 4] = *::core::mem::transmute::< | ||
| &[u8; 4], | ||
| &mut [std::ffi::c_char; 4], | ||
| >(b"abc\0"); | ||
| let mut def: [std::ffi::c_char; 3] = [ | ||
| 'd' as i32 as std::ffi::c_char, | ||
| 'e' as i32 as std::ffi::c_char, | ||
| 'f' as i32 as std::ffi::c_char, | ||
| ]; | ||
| let mut part: [std::ffi::c_char; 2] = [1 as std::ffi::c_int as std::ffi::c_char, 0]; | ||
| let mut abcptr: *mut std::ffi::c_char = b"abc\0" as *const u8 | ||
| as *const std::ffi::c_char as *mut std::ffi::c_char; | ||
| let mut init: [std::ffi::c_char; 5] = *::core::mem::transmute::< | ||
| &[u8; 5], | ||
| &mut [std::ffi::c_char; 5], | ||
| >(b"abcd\0"); | ||
| let mut too_long: [std::ffi::c_char; 3] = *::core::mem::transmute::< | ||
| &[u8; 3], | ||
| &mut [std::ffi::c_char; 3], | ||
| >(b"abc"); | ||
| let mut too_short: [std::ffi::c_char; 20] = *::core::mem::transmute::< | ||
| &[u8; 20], | ||
| &mut [std::ffi::c_char; 20], | ||
| >(b"abc\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"); | ||
| let mut past_end: *mut std::ffi::c_char = &mut *simple | ||
| .as_mut_ptr() | ||
| .offset( | ||
| ::core::mem::size_of::<[std::ffi::c_char; 9]>() as std::ffi::c_ulong as isize, | ||
| ) as *mut std::ffi::c_char; | ||
| past_end = &mut *foo.offset(8 as std::ffi::c_int as isize) as *mut std::ffi::c_char; | ||
| } | ||
| #[no_mangle] | ||
| pub unsafe extern "C" fn short_initializer() { | ||
| let mut empty_brackets: [std::ffi::c_int; 16] = [0; 16]; | ||
| let mut brackets_with_zero: [std::ffi::c_int; 16] = [0 as std::ffi::c_int; 16]; | ||
| let mut brackets_with_one: [std::ffi::c_int; 4] = [1 as std::ffi::c_int, 0, 0, 0]; | ||
| let mut excess_elements_1: [std::ffi::c_int; 2] = [ | ||
| 1 as std::ffi::c_int, | ||
| 2 as std::ffi::c_int, | ||
| ]; | ||
| let mut excess_elements_2: [std::ffi::c_int; 0] = [0; 0]; | ||
| let mut single_struct: [C2RustUnnamed_2; 1] = [ | ||
| { | ||
| let mut init = C2RustUnnamed_2 { | ||
| x: 1 as std::ffi::c_int as std::ffi::c_short, | ||
| y: 2 as std::ffi::c_int, | ||
| }; | ||
| init | ||
| }, | ||
| ]; | ||
| let mut many_struct: [C2RustUnnamed_1; 3] = [ | ||
| { | ||
| let mut init = C2RustUnnamed_1 { | ||
| x: 1 as std::ffi::c_int as std::ffi::c_short, | ||
| y: 2 as std::ffi::c_int, | ||
| }; | ||
| init | ||
| }, | ||
| C2RustUnnamed_1 { x: 0, y: 0 }, | ||
| C2RustUnnamed_1 { x: 0, y: 0 }, | ||
| ]; | ||
| } | ||
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.
in more recent versions this could be e.g.
but idk, it's extra work and may not actually come up in practice.
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.
Yeah, this is definitely cleaner (of course, like you, I'm not sure how worth it it is). How recently was support for this added, though? I know the
const {}blocks are new, but you don't need that for this, right?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.
True, you just have to name the
constitem, but I guess if you nest it in a block the name can never conflict?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.
Here in normal scope, nothing should need to be
const. Inconstscope (e.g. aconstorstaticitem), then it'll need to beconst, but it shouldn't need the explicitconst {, just a normal{, since it already knows to evaluate it inconstmode.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.
Oh yeah, if the value is
Copy, which it would be here, thenconstisn't even needed.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.
Ah, true, although everything from C should be
Copy.