diff options
| author | bors <bors@rust-lang.org> | 2023-06-02 05:11:49 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2023-06-02 05:11:49 +0000 |
| commit | 33c3d101280c8eb3cd8af421bfb56a8afcc3881d (patch) | |
| tree | 2e65db5c7568d3a9220cca76dec79c036a97d749 /compiler/rustc_abi/src/lib.rs | |
| parent | 774a3d1523bde3a16f8a92dbaac01d211ba911c3 (diff) | |
| parent | f6c2bc5c24fd08200c0e4438d981ca58dc71f488 (diff) | |
| download | rust-33c3d101280c8eb3cd8af421bfb56a8afcc3881d.tar.gz rust-33c3d101280c8eb3cd8af421bfb56a8afcc3881d.zip | |
Auto merge of #111677 - fee1-dead-contrib:rustc_const_eval-translatable, r=oli-obk,RalfJung
Use translatable diagnostics in `rustc_const_eval` This PR: * adds a `no_span` parameter to `note` / `help` attributes when using `Subdiagnostic` to allow adding notes/helps without using a span * has minor tweaks and changes to error messages
Diffstat (limited to 'compiler/rustc_abi/src/lib.rs')
| -rw-r--r-- | compiler/rustc_abi/src/lib.rs | 49 |
1 files changed, 42 insertions, 7 deletions
diff --git a/compiler/rustc_abi/src/lib.rs b/compiler/rustc_abi/src/lib.rs index 43db66a3c28..2ee63c286ba 100644 --- a/compiler/rustc_abi/src/lib.rs +++ b/compiler/rustc_abi/src/lib.rs @@ -209,7 +209,7 @@ pub enum TargetDataLayoutErrors<'a> { InvalidAddressSpace { addr_space: &'a str, cause: &'a str, err: ParseIntError }, InvalidBits { kind: &'a str, bit: &'a str, cause: &'a str, err: ParseIntError }, MissingAlignment { cause: &'a str }, - InvalidAlignment { cause: &'a str, err: String }, + InvalidAlignment { cause: &'a str, err: AlignFromBytesError }, InconsistentTargetArchitecture { dl: &'a str, target: &'a str }, InconsistentTargetPointerWidth { pointer_size: u64, target: u32 }, InvalidBitsSize { err: String }, @@ -640,30 +640,65 @@ impl fmt::Debug for Align { } } +#[derive(Clone, Copy)] +pub enum AlignFromBytesError { + NotPowerOfTwo(u64), + TooLarge(u64), +} + +impl AlignFromBytesError { + pub fn diag_ident(self) -> &'static str { + match self { + Self::NotPowerOfTwo(_) => "not_power_of_two", + Self::TooLarge(_) => "too_large", + } + } + + pub fn align(self) -> u64 { + let (Self::NotPowerOfTwo(align) | Self::TooLarge(align)) = self; + align + } +} + +impl fmt::Debug for AlignFromBytesError { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + fmt::Display::fmt(self, f) + } +} + +impl fmt::Display for AlignFromBytesError { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match self { + AlignFromBytesError::NotPowerOfTwo(align) => write!(f, "`{align}` is not a power of 2"), + AlignFromBytesError::TooLarge(align) => write!(f, "`{align}` is too large"), + } + } +} + impl Align { pub const ONE: Align = Align { pow2: 0 }; pub const MAX: Align = Align { pow2: 29 }; #[inline] - pub fn from_bits(bits: u64) -> Result<Align, String> { + pub fn from_bits(bits: u64) -> Result<Align, AlignFromBytesError> { Align::from_bytes(Size::from_bits(bits).bytes()) } #[inline] - pub fn from_bytes(align: u64) -> Result<Align, String> { + pub fn from_bytes(align: u64) -> Result<Align, AlignFromBytesError> { // Treat an alignment of 0 bytes like 1-byte alignment. if align == 0 { return Ok(Align::ONE); } #[cold] - fn not_power_of_2(align: u64) -> String { - format!("`{}` is not a power of 2", align) + fn not_power_of_2(align: u64) -> AlignFromBytesError { + AlignFromBytesError::NotPowerOfTwo(align) } #[cold] - fn too_large(align: u64) -> String { - format!("`{}` is too large", align) + fn too_large(align: u64) -> AlignFromBytesError { + AlignFromBytesError::TooLarge(align) } let tz = align.trailing_zeros(); |
