-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of #13490 - HKalbasi:layout, r=jonas-schievink
Compute data layout of types cc #4091 Things that aren't working: * Closures * Generators (so no support for `Future` I think) * Opaque types * Type alias and associated types which may need normalization Things that show wrong result: * ~Enums with explicit discriminant~ * SIMD types * ~`NonZero*` and similar standard library items which control layout with special attributes~ At the user level, I didn't put much work, since I wasn't confident about what is the best way to present this information. Currently it shows size and align for ADTs, and size, align, offset for struct fields, in the hover, similar to clangd. I used it some days and I feel I liked it, but we may consider it too noisy and move it to an assist or command.
- Loading branch information
Showing
21 changed files
with
1,112 additions
and
164 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,96 @@ | ||
//! Definitions needed for computing data layout of types. | ||
use std::cmp; | ||
|
||
use la_arena::{Idx, RawIdx}; | ||
pub use rustc_abi::{ | ||
Abi, AbiAndPrefAlign, AddressSpace, Align, Endian, FieldsShape, Integer, IntegerType, | ||
LayoutCalculator, Niche, Primitive, ReprFlags, ReprOptions, Scalar, Size, StructKind, | ||
TargetDataLayout, WrappingRange, | ||
}; | ||
|
||
use crate::LocalEnumVariantId; | ||
|
||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] | ||
pub struct RustcEnumVariantIdx(pub LocalEnumVariantId); | ||
|
||
impl rustc_index::vec::Idx for RustcEnumVariantIdx { | ||
fn new(idx: usize) -> Self { | ||
RustcEnumVariantIdx(Idx::from_raw(RawIdx::from(idx as u32))) | ||
} | ||
|
||
fn index(self) -> usize { | ||
u32::from(self.0.into_raw()) as usize | ||
} | ||
} | ||
|
||
pub type Layout = rustc_abi::LayoutS<RustcEnumVariantIdx>; | ||
pub type TagEncoding = rustc_abi::TagEncoding<RustcEnumVariantIdx>; | ||
pub type Variants = rustc_abi::Variants<RustcEnumVariantIdx>; | ||
|
||
pub trait IntegerExt { | ||
fn repr_discr( | ||
dl: &TargetDataLayout, | ||
repr: &ReprOptions, | ||
min: i128, | ||
max: i128, | ||
) -> Result<(Integer, bool), LayoutError>; | ||
} | ||
|
||
impl IntegerExt for Integer { | ||
/// Finds the appropriate Integer type and signedness for the given | ||
/// signed discriminant range and `#[repr]` attribute. | ||
/// N.B.: `u128` values above `i128::MAX` will be treated as signed, but | ||
/// that shouldn't affect anything, other than maybe debuginfo. | ||
fn repr_discr( | ||
dl: &TargetDataLayout, | ||
repr: &ReprOptions, | ||
min: i128, | ||
max: i128, | ||
) -> Result<(Integer, bool), LayoutError> { | ||
// Theoretically, negative values could be larger in unsigned representation | ||
// than the unsigned representation of the signed minimum. However, if there | ||
// are any negative values, the only valid unsigned representation is u128 | ||
// which can fit all i128 values, so the result remains unaffected. | ||
let unsigned_fit = Integer::fit_unsigned(cmp::max(min as u128, max as u128)); | ||
let signed_fit = cmp::max(Integer::fit_signed(min), Integer::fit_signed(max)); | ||
|
||
if let Some(ity) = repr.int { | ||
let discr = Integer::from_attr(dl, ity); | ||
let fit = if ity.is_signed() { signed_fit } else { unsigned_fit }; | ||
if discr < fit { | ||
return Err(LayoutError::UserError( | ||
"Integer::repr_discr: `#[repr]` hint too small for \ | ||
discriminant range of enum " | ||
.to_string(), | ||
)); | ||
} | ||
return Ok((discr, ity.is_signed())); | ||
} | ||
|
||
let at_least = if repr.c() { | ||
// This is usually I32, however it can be different on some platforms, | ||
// notably hexagon and arm-none/thumb-none | ||
dl.c_enum_min_size | ||
} else { | ||
// repr(Rust) enums try to be as small as possible | ||
Integer::I8 | ||
}; | ||
|
||
// If there are no negative values, we can use the unsigned fit. | ||
Ok(if min >= 0 { | ||
(cmp::max(unsigned_fit, at_least), false) | ||
} else { | ||
(cmp::max(signed_fit, at_least), true) | ||
}) | ||
} | ||
} | ||
|
||
#[derive(Debug, PartialEq, Eq, Clone)] | ||
pub enum LayoutError { | ||
UserError(String), | ||
SizeOverflow, | ||
HasPlaceholder, | ||
NotImplemented, | ||
Unknown, | ||
} |
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 |
---|---|---|
|
@@ -419,6 +419,7 @@ pub mod known { | |
shr, | ||
sub_assign, | ||
sub, | ||
unsafe_cell, | ||
va_list | ||
); | ||
|
||
|
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
Oops, something went wrong.