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

Mark core::alloc::Layout::from_size_align_unchecked const #60370

Merged
merged 3 commits into from May 19, 2019

Conversation

Projects
None yet
8 participants
@Richard-W
Copy link
Contributor

commented Apr 29, 2019

Makes it possible (pending stabilization of #57563 (const_fn)) to rewrite code like

const BUFFER_SIZE: usize = 0x2000;
const BUFFER_ALIGN: usize = 0x1000;

fn foo() {
  let layout = std::alloc::Layout::from_size_align(BUFFER_SIZE, BUFFER_ALIGN)
    .unwrap();
  let buffer = std::alloc::alloc(layout);
}

to

const BUFFER_LAYOUT: std::alloc::Layout = unsafe {
  std::alloc::Layout::from_size_align_unchecked(0x2000, 0x1000)
};

fn foo() {
  let buffer = std::alloc::alloc(BUFFER_LAYOUT);
}

which (although unsafe is used) looks somewhat cleaner and is easier to read.

@rust-highfive

This comment has been minimized.

Copy link
Collaborator

commented Apr 29, 2019

Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @rkruppe (or someone else) soon.

If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes.

Please see the contribution instructions for more information.

@Centril Centril added this to the 1.36 milestone Apr 29, 2019

@oli-obk

This comment has been minimized.

Copy link
Contributor

commented Apr 29, 2019

Please add the example as a test and also add a test that passing 0 as the alignment will cause a const eval error (since it's a type-layout violation)

@Richard-W

This comment has been minimized.

Copy link
Contributor Author

commented Apr 30, 2019

@oli-obk, I added a test similar to the example, but I was not able to figure out how I'd go about the eval error test.

I looked for a similar test for nonzero integers as an example (grep -Hrn "new_unchecked(0)") but I was not able to find one.

@rasendubi

This comment has been minimized.

Copy link
Contributor

commented Apr 30, 2019

Check rustc-guide chapter on testing (especially, adding new tests). You need to add a ui test.

This boils down to adding a single file to src/test/ui directory (something like src/test/ui/consts/std/alloc.rs should be fine). You could use src/test/ui/consts/std/cell.rs as an inspiration.

@Richard-W

This comment has been minimized.

Copy link
Contributor Author

commented Apr 30, 2019

Thanks for your help @rasendubi. I added the test.

@bors

This comment has been minimized.

Copy link
Contributor

commented May 14, 2019

☔️ The latest upstream changes (presumably #60794) made this pull request unmergeable. Please resolve the merge conflicts.

@Richard-W Richard-W force-pushed the Richard-W:const-layout-construction branch from b3562d4 to c0b6d3c May 14, 2019

@Centril

This comment has been minimized.

Copy link
Member

commented May 18, 2019

@rust-highfive rust-highfive assigned sfackler and unassigned rkruppe May 18, 2019

@sfackler

This comment has been minimized.

Copy link
Member

commented May 18, 2019

@bors r+

@bors

This comment has been minimized.

Copy link
Contributor

commented May 18, 2019

📌 Commit c0b6d3c has been approved by sfackler

Centril added a commit to Centril/rust that referenced this pull request May 19, 2019

Rollup merge of rust-lang#60370 - Richard-W:const-layout-construction…
…, r=sfackler

Mark core::alloc::Layout::from_size_align_unchecked const

Makes it possible (pending stabilization of rust-lang#57563 (`const_fn`)) to rewrite code like

```rust
const BUFFER_SIZE: usize = 0x2000;
const BUFFER_ALIGN: usize = 0x1000;

fn foo() {
  let layout = std::alloc::Layout::from_size_align(BUFFER_SIZE, BUFFER_ALIGN)
    .unwrap();
  let buffer = std::alloc::alloc(layout);
}
```
to
```rust
const BUFFER_LAYOUT: std::alloc::Layout = unsafe {
  std::alloc::Layout::from_size_align_unchecked(0x2000, 0x1000)
};

fn foo() {
  let buffer = std::alloc::alloc(BUFFER_LAYOUT);
}
```

which (although `unsafe` is used) looks somewhat cleaner and is easier to read.

bors added a commit that referenced this pull request May 19, 2019

Auto merge of #60949 - Centril:rollup-f918e1v, r=Centril
Rollup of 8 pull requests

Successful merges:

 - #60370 (Mark core::alloc::Layout::from_size_align_unchecked const)
 - #60678 (Stabilize vecdeque_rotate)
 - #60924 (Explain that ? converts the error type using From)
 - #60931 (Use iter() for iterating arrays by slice)
 - #60934 (Declare DefIndex with the newtype_index macro)
 - #60943 (fix copy-paste typo in docs for ptr::read_volatile)
 - #60945 (Simplify BufRead::fill_buf doc example using NLL)
 - #60947 (Fix typos in docs of GlobalAlloc)

Failed merges:

r? @ghost
@bors

This comment has been minimized.

Copy link
Contributor

commented May 19, 2019

⌛️ Testing commit c0b6d3c with merge 26ab324...

@bors bors merged commit c0b6d3c into rust-lang:master May 19, 2019

1 of 2 checks passed

homu Testing commit c0b6d3c975915e740548f0ec7bcf5963e7a3b218 with merge 26ab32499c0114ae6e01e76374ae06bcd7a973bd...
Details
Travis CI - Pull Request Build Passed
Details

@Richard-W Richard-W deleted the Richard-W:const-layout-construction branch May 19, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.