Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 33c3d10

Browse files
committedJun 2, 2023
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
2 parents 774a3d1 + f6c2bc5 commit 33c3d10

File tree

93 files changed

+2381
-1124
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

93 files changed

+2381
-1124
lines changed
 

‎compiler/rustc_abi/src/lib.rs

Lines changed: 42 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ pub enum TargetDataLayoutErrors<'a> {
209209
InvalidAddressSpace { addr_space: &'a str, cause: &'a str, err: ParseIntError },
210210
InvalidBits { kind: &'a str, bit: &'a str, cause: &'a str, err: ParseIntError },
211211
MissingAlignment { cause: &'a str },
212-
InvalidAlignment { cause: &'a str, err: String },
212+
InvalidAlignment { cause: &'a str, err: AlignFromBytesError },
213213
InconsistentTargetArchitecture { dl: &'a str, target: &'a str },
214214
InconsistentTargetPointerWidth { pointer_size: u64, target: u32 },
215215
InvalidBitsSize { err: String },
@@ -640,30 +640,65 @@ impl fmt::Debug for Align {
640640
}
641641
}
642642

643+
#[derive(Clone, Copy)]
644+
pub enum AlignFromBytesError {
645+
NotPowerOfTwo(u64),
646+
TooLarge(u64),
647+
}
648+
649+
impl AlignFromBytesError {
650+
pub fn diag_ident(self) -> &'static str {
651+
match self {
652+
Self::NotPowerOfTwo(_) => "not_power_of_two",
653+
Self::TooLarge(_) => "too_large",
654+
}
655+
}
656+
657+
pub fn align(self) -> u64 {
658+
let (Self::NotPowerOfTwo(align) | Self::TooLarge(align)) = self;
659+
align
660+
}
661+
}
662+
663+
impl fmt::Debug for AlignFromBytesError {
664+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
665+
fmt::Display::fmt(self, f)
666+
}
667+
}
668+
669+
impl fmt::Display for AlignFromBytesError {
670+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
671+
match self {
672+
AlignFromBytesError::NotPowerOfTwo(align) => write!(f, "`{align}` is not a power of 2"),
673+
AlignFromBytesError::TooLarge(align) => write!(f, "`{align}` is too large"),
674+
}
675+
}
676+
}
677+
643678
impl Align {
644679
pub const ONE: Align = Align { pow2: 0 };
645680
pub const MAX: Align = Align { pow2: 29 };
646681

647682
#[inline]
648-
pub fn from_bits(bits: u64) -> Result<Align, String> {
683+
pub fn from_bits(bits: u64) -> Result<Align, AlignFromBytesError> {
649684
Align::from_bytes(Size::from_bits(bits).bytes())
650685
}
651686

652687
#[inline]
653-
pub fn from_bytes(align: u64) -> Result<Align, String> {
688+
pub fn from_bytes(align: u64) -> Result<Align, AlignFromBytesError> {
654689
// Treat an alignment of 0 bytes like 1-byte alignment.
655690
if align == 0 {
656691
return Ok(Align::ONE);
657692
}
658693

659694
#[cold]
660-
fn not_power_of_2(align: u64) -> String {
661-
format!("`{}` is not a power of 2", align)
695+
fn not_power_of_2(align: u64) -> AlignFromBytesError {
696+
AlignFromBytesError::NotPowerOfTwo(align)
662697
}
663698

664699
#[cold]
665-
fn too_large(align: u64) -> String {
666-
format!("`{}` is too large", align)
700+
fn too_large(align: u64) -> AlignFromBytesError {
701+
AlignFromBytesError::TooLarge(align)
667702
}
668703

669704
let tz = align.trailing_zeros();

‎compiler/rustc_codegen_cranelift/src/common.rs

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use rustc_index::IndexVec;
66
use rustc_middle::ty::layout::{
77
FnAbiError, FnAbiOfHelpers, FnAbiRequest, LayoutError, LayoutOfHelpers,
88
};
9+
use rustc_span::source_map::Spanned;
910
use rustc_span::SourceFile;
1011
use rustc_target::abi::call::FnAbi;
1112
use rustc_target::abi::{Integer, Primitive};
@@ -495,25 +496,16 @@ impl<'tcx> FnAbiOfHelpers<'tcx> for RevealAllLayoutCx<'tcx> {
495496
fn_abi_request: FnAbiRequest<'tcx>,
496497
) -> ! {
497498
if let FnAbiError::Layout(LayoutError::SizeOverflow(_)) = err {
498-
self.0.sess.span_fatal(span, err.to_string())
499+
self.0.sess.emit_fatal(Spanned { span, node: err })
499500
} else {
500501
match fn_abi_request {
501502
FnAbiRequest::OfFnPtr { sig, extra_args } => {
502-
span_bug!(
503-
span,
504-
"`fn_abi_of_fn_ptr({}, {:?})` failed: {}",
505-
sig,
506-
extra_args,
507-
err
508-
);
503+
span_bug!(span, "`fn_abi_of_fn_ptr({sig}, {extra_args:?})` failed: {err:?}");
509504
}
510505
FnAbiRequest::OfInstance { instance, extra_args } => {
511506
span_bug!(
512507
span,
513-
"`fn_abi_of_instance({}, {:?})` failed: {}",
514-
instance,
515-
extra_args,
516-
err
508+
"`fn_abi_of_instance({instance}, {extra_args:?})` failed: {err:?}"
517509
);
518510
}
519511
}

0 commit comments

Comments
 (0)
Please sign in to comment.