-
Notifications
You must be signed in to change notification settings - Fork 13.7k
Allow borrowing array elements from packed structs with ABI align <= packed align #145419
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
hns1971
wants to merge
15
commits into
rust-lang:master
Choose a base branch
from
hns1971:fix-packed-array-align
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 13 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
301137d
Fix E0793 error for packed structs with const generic parameters
hns1971 19f51f2
Fix code formatting issues
hns1971 a2a94b9
Fix remaining code formatting issues
hns1971 2e84ab8
Fix TypingEnv formatting to match rustfmt requirements
hns1971 d8785b9
Fix TypingEnv formatting to match rustfmt requirements
hns1971 dd1537c
Fix TypingEnv formatting to match rustfmt requirements
hns1971 99dedb0
Fix TypingEnv formatting to match rustfmt requirements
hns1971 4620527
fix: update is_disaligned check and tests
hns1971 2fe4b7f
Rename is_disaligned to is_potentially_disaligned for better semantic…
hns1971 f0c8e9e
Rename is_disaligned to is_potentially_disaligned for better semantic…
hns1971 4a65fdc
Rename is_disaligned to is_potentially_disaligned for better semantic…
hns1971 585af05
Add test cases to verify u8/i8 array fix
hns1971 b37f571
updated the comment to formally justify that align([T]) == align(T)
hns1971 ef2538b
refactor: improve get_element_alignment function for packed arrays
hns1971 8c07e0a
modify commit for check
hns1971 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
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,19 @@ | ||
//@ check-pass | ||
#![allow(dead_code)] | ||
|
||
#[repr(C, packed)] | ||
struct PascalString<const CAP: usize> { | ||
len: u8, | ||
buf: [u8; CAP], | ||
} | ||
|
||
fn bar<const CAP: usize>(s: &PascalString<CAP>) -> &str { | ||
// Goal: this line should not trigger E0793 | ||
std::str::from_utf8(&s.buf[0..s.len as usize]).unwrap() | ||
} | ||
|
||
fn main() { | ||
let p = PascalString::<10> { len: 3, buf: *b"abc\0\0\0\0\0\0\0" }; | ||
let s = bar(&p); | ||
assert_eq!(s, "abc"); | ||
} |
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,19 @@ | ||
//@ check-pass | ||
#![allow(dead_code)] | ||
|
||
#[repr(C, packed)] | ||
struct PascalStringI8<const CAP: usize> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. please merge this into a single test and use |
||
len: u8, | ||
buf: [i8; CAP], | ||
} | ||
|
||
fn bar<const CAP: usize>(s: &PascalStringI8<CAP>) -> &[i8] { | ||
// Goal: this line should not trigger E0793 for i8 arrays | ||
&s.buf[0..s.len as usize] | ||
} | ||
|
||
fn main() { | ||
let p = PascalStringI8::<10> { len: 3, buf: [1, 2, 3, 0, 0, 0, 0, 0, 0, 0] }; | ||
let s = bar(&p); | ||
assert_eq!(s, &[1, 2, 3]); | ||
} |
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.
properly compute the of the
element_ty
here, limiting it to only u8 and i8 feels unnecessary 🤔