-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
Constify more of alloc::Layout #67494
Conversation
(rust_highfive has picked a reviewer for you, use r? to override) |
r? @oli-obk |
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.
Please file a tracking issue for this constification and use the id instead of "0"
I split the consttransmogrification of |
src/libcore/alloc.rs
Outdated
pub fn align_to(&self, align: usize) -> Result<Self, LayoutErr> { | ||
Layout::from_size_align(self.size(), cmp::max(self.align(), align)) | ||
pub const fn align_to(&self, align: usize) -> Result<Self, LayoutErr> { | ||
let align = if self.align() >= align { self.align() } else { align }; |
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.
Please make cmp::max
const fn
, too :)
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.
Should I file a separate tracking issue?
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.
sorry I missed this in the first pass
the PR lgtm after this
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 I just realized: std::cmp::{max; min}
are defined as pub fn max<T>(v1: T, v2: T) -> T where T: Ord
. There is no way to require an impl of Ord
to be const?!
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, no that's not possible. So leave align_to
as not const fn for now, or is it super necessary?
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.
Not to me, no. I actually don't see this one-liner as a problem: The type is always going to be usize, so even if it never gets changed back to cmp::max()
, it's totally obvious what it does.
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.
it's totally obvious what it does.
I agree, but
- the line of what is obvious and what not is very subjective, if at some point we have to start arguing about it, it seems good to start out conservative if there's no strong desire for the main change
- the original author must have thought that
cmp::max
is even more obvious (and I think this, too). So making code less obvious just to make it const without a strong reason to do so seems not like a good strategy.
Thanks for changing it back
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.
Maybe a const fn align_max(align1: usize, align2: usize) -> usize
helper function to replace cmpp::max()
?
(Not in this PR, it's already merged)
@bors r+ |
📌 Commit b56e0e2126eaee3675805485169338d7736dd5bd has been approved by |
🌲 The tree is currently closed for pull requests below priority 100, this pull request will be tested once the tree is reopened |
I've just seen this will conflict with #66254 which does constification for just |
@bors r- |
☔ The latest upstream changes (presumably #67540) made this pull request unmergeable. Please resolve the merge conflicts. |
Constify Result r? @oli-obk This is just the `Result`-part of rust-lang#67494 which I'll resubmit once rust-lang#66254 has landed.
Constify Result r? @oli-obk This is just the `Result`-part of rust-lang#67494 which I'll resubmit once rust-lang#66254 has landed.
b56e0e2
to
cf89869
Compare
Force-pushed a commit that wont conflict with #66254 |
cf89869
to
c5ac45e
Compare
rebased again |
☔ The latest upstream changes (presumably #68126) made this pull request unmergeable. Please resolve the merge conflicts. |
Tracking issue rust-lang#67521, Layout::new in rust-lang#66254
c5ac45e
to
c5a9a14
Compare
rebased again |
@bors r+ |
📌 Commit c5a9a14 has been approved by |
⌛ Testing commit c5a9a14 with merge eb9af48328fc6dee800ed3fe2f55551322a09cd6... |
Constify more of alloc::Layout Making more methods of `alloc::Layout` const would allow us to compute alignment/size information for arbitrary (sized) types at compile-time, including placing the information in associated constants. While `mem::size_of` and `mem::align_of` are already const and `Layout` is solely based on those, there is no guarantee in the implementation that a const derived from these functions will be exactly the same as what `Layout` uses and is subsequently used in a call to `alloc::alloc`. Constifying `Layout` makes this possible. First contribution to core, excuse my ignorance.
@bors retry rolled up. |
Rollup of 6 pull requests Successful merges: - #67494 (Constify more of alloc::Layout) - #67867 (Correctly check for opaque types in `assoc_ty_def`) - #67948 (Galloping search for binary_search_util) - #68045 (Move more of `rustc::lint` into `rustc_lint`) - #68089 (Unstabilize `Vec::remove_item`) - #68108 (Add suggestions when encountering chained comparisons) Failed merges: r? @ghost
Making more methods of
alloc::Layout
const would allow us to compute alignment/size information for arbitrary (sized) types at compile-time, including placing the information in associated constants. Whilemem::size_of
andmem::align_of
are already const andLayout
is solely based on those, there is no guarantee in the implementation that a const derived from these functions will be exactly the same as whatLayout
uses and is subsequently used in a call toalloc::alloc
. ConstifyingLayout
makes this possible.First contribution to core, excuse my ignorance.