Skip to content
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

Add semver guidelines for changing the repr of structs/enums to ref… #10276

Closed
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Fix rustfmt errors
  • Loading branch information
Noratrieb committed Jan 14, 2022
commit d3dac5ffd15c526e2381a68c07cf70a4e320b09f
20 changes: 11 additions & 9 deletions src/doc/src/reference/semver.md
Original file line number Diff line number Diff line change
Expand Up @@ -305,20 +305,22 @@ pub struct Foo {

///////////////////////////////////////////////////////////
// Example usage that will break.
use updated_crate::Foo;

fn main() {
let foo = Foo {
f1: 1,
f2: 1000,
f3: 5,
};

let foo_ptr = &foo as *const Foo as *const u8;

// this is now unsound because of the change
// SAFETY: Foo is repr(C), so we are guaranteed that there will be `3` at this offset (u8, 8 pad, u16)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is what was meant?

Suggested change
// SAFETY: Foo is repr(C), so we are guaranteed that there will be `3` at this offset (u8, 8 pad, u16)
// SAFETY: Foo is repr(C), so we are guaranteed that there will be `5` at this offset (u8, 8 pad, u16)

let f2 = unsafe { foo_ptr.offset(4).read() };
println!("{}", f2);
let f3 = unsafe { foo_ptr.offset(4).read() };

assert_eq!(5, f3);
}
```

Expand Down Expand Up @@ -534,12 +536,12 @@ pub enum Number {
///////////////////////////////////////////////////////////
// Example usage that will break.
fn main() {
let num_three = Number::Three;
let num_three = updated_crate::Number::Three;

// SAFETY: `Number` is `#[repr(i32)]`
let three: i32 = unsafe { std::mem::transmute(num_three) }; // Error: cannot transmute between types of different sizes
println!("{}", three)

assert_eq!(3, three)
}
```

Expand Down