-
Notifications
You must be signed in to change notification settings - Fork 13.1k
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
Implement const fn {size,align}_of. #42859
Merged
Merged
Changes from all commits
Commits
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 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 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 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 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 |
---|---|---|
|
@@ -749,14 +749,27 @@ impl<'a, 'tcx> Visitor<'tcx> for Qualifier<'a, 'tcx, 'tcx> { | |
self.visit_operand(func, location); | ||
|
||
let fn_ty = func.ty(self.mir, self.tcx); | ||
let (is_shuffle, is_const_fn) = match fn_ty.sty { | ||
ty::TyFnDef(def_id, _) => { | ||
(self.tcx.fn_sig(def_id).abi() == Abi::PlatformIntrinsic && | ||
self.tcx.item_name(def_id).as_str().starts_with("simd_shuffle"), | ||
self.tcx.is_const_fn(def_id)) | ||
let (mut is_shuffle, mut is_const_fn) = (false, false); | ||
if let ty::TyFnDef(def_id, _) = fn_ty.sty { | ||
match self.tcx.fn_sig(def_id).abi() { | ||
Abi::RustIntrinsic | | ||
Abi::PlatformIntrinsic => { | ||
assert!(!self.tcx.is_const_fn(def_id)); | ||
match &self.tcx.item_name(def_id).as_str()[..] { | ||
"size_of" | "min_align_of" => is_const_fn = true, | ||
|
||
name if name.starts_with("simd_shuffle") => { | ||
is_shuffle = true; | ||
} | ||
|
||
_ => {} | ||
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. maybe add |
||
} | ||
} | ||
_ => { | ||
is_const_fn = self.tcx.is_const_fn(def_id); | ||
} | ||
} | ||
_ => (false, false) | ||
}; | ||
} | ||
|
||
for (i, arg) in args.iter().enumerate() { | ||
self.nest(|this| { | ||
|
This file contains 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 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 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,18 @@ | ||
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT | ||
// file at the top-level directory of this distribution and at | ||
// http://rust-lang.org/COPYRIGHT. | ||
// | ||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or | ||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license | ||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your | ||
// option. This file may not be copied, modified, or distributed | ||
// except according to those terms. | ||
|
||
#![feature(const_fn)] | ||
|
||
struct Foo { | ||
bytes: [u8; std::mem::size_of::<Foo>()] | ||
//~^ ERROR unsupported cyclic reference between types/traits detected | ||
} | ||
|
||
fn main() {} |
This file contains 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,60 @@ | ||
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT | ||
// file at the top-level directory of this distribution and at | ||
// http://rust-lang.org/COPYRIGHT. | ||
// | ||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or | ||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license | ||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your | ||
// option. This file may not be copied, modified, or distributed | ||
// except according to those terms. | ||
|
||
#![feature(const_fn)] | ||
|
||
use std::mem; | ||
|
||
// Get around the limitations of CTFE in today's Rust. | ||
const fn choice_u64(c: bool, a: u64, b: u64) -> u64 { | ||
(-(c as i64) as u64) & a | (-(!c as i64) as u64) & b | ||
} | ||
|
||
const fn max_usize(a: usize, b: usize) -> usize { | ||
choice_u64(a > b, a as u64, b as u64) as usize | ||
} | ||
|
||
const fn align_to(size: usize, align: usize) -> usize { | ||
(size + (align - 1)) & !(align - 1) | ||
} | ||
|
||
const fn packed_union_size_of<A, B>() -> usize { | ||
max_usize(mem::size_of::<A>(), mem::size_of::<B>()) | ||
} | ||
|
||
const fn union_align_of<A, B>() -> usize { | ||
max_usize(mem::align_of::<A>(), mem::align_of::<B>()) | ||
} | ||
|
||
const fn union_size_of<A, B>() -> usize { | ||
align_to(packed_union_size_of::<A, B>(), union_align_of::<A, B>()) | ||
} | ||
|
||
macro_rules! fake_union { | ||
($name:ident { $a:ty, $b:ty }) => ( | ||
struct $name { | ||
_align: ([$a; 0], [$b; 0]), | ||
_bytes: [u8; union_size_of::<$a, $b>()] | ||
} | ||
) | ||
} | ||
|
||
// Check that we can (poorly) emulate unions by | ||
// calling size_of and align_of at compile-time. | ||
fake_union!(U { u16, [u8; 3] }); | ||
|
||
fn test(u: U) { | ||
assert_eq!(mem::size_of_val(&u._bytes), 4); | ||
} | ||
|
||
fn main() { | ||
assert_eq!(mem::size_of::<U>(), 4); | ||
assert_eq!(mem::align_of::<U>(), 2); | ||
} |
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.
Not that I necessarily object to this, but this would be an "insta-stable" change, 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.
This change itself yes, but calling any function in a const context still requires
#[feature(const_fn)]
.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, does it? I thought we enabled calls, but not declarations, of const fns.
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.
Or at least I couldn't remember which it is.
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.
There’s discussion of doing this, but it hasn’t happened yet as far as #24111 says.
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.
#43017