Skip to content

Commit 33c3d10

Browse files
committed
Auto merge of rust-lang#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

+42-7
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

+4-12
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
}

compiler/rustc_codegen_gcc/src/consts.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ fn set_global_alignment<'gcc, 'tcx>(cx: &CodegenCx<'gcc, 'tcx>, gv: LValue<'gcc>
2424
match Align::from_bits(min) {
2525
Ok(min) => align = align.max(min),
2626
Err(err) => {
27-
cx.sess().emit_err(InvalidMinimumAlignment { err });
27+
cx.sess().emit_err(InvalidMinimumAlignment { err: err.to_string() });
2828
}
2929
}
3030
}

compiler/rustc_codegen_gcc/src/context.rs

+3-12
Original file line numberDiff line numberDiff line change
@@ -477,7 +477,7 @@ impl<'gcc, 'tcx> LayoutOfHelpers<'tcx> for CodegenCx<'gcc, 'tcx> {
477477
#[inline]
478478
fn handle_layout_err(&self, err: LayoutError<'tcx>, span: Span, ty: Ty<'tcx>) -> ! {
479479
if let LayoutError::SizeOverflow(_) = err {
480-
self.sess().emit_fatal(respan(span, err))
480+
self.sess().emit_fatal(respan(span, err.into_diagnostic()))
481481
} else {
482482
span_bug!(span, "failed to get layout for `{}`: {}", ty, err)
483483
}
@@ -499,21 +499,12 @@ impl<'gcc, 'tcx> FnAbiOfHelpers<'tcx> for CodegenCx<'gcc, 'tcx> {
499499
} else {
500500
match fn_abi_request {
501501
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-
);
502+
span_bug!(span, "`fn_abi_of_fn_ptr({sig}, {extra_args:?})` failed: {err:?}");
509503
}
510504
FnAbiRequest::OfInstance { instance, extra_args } => {
511505
span_bug!(
512506
span,
513-
"`fn_abi_of_instance({}, {:?})` failed: {}",
514-
instance,
515-
extra_args,
516-
err
507+
"`fn_abi_of_instance({instance}, {extra_args:?})` failed: {err:?}"
517508
);
518509
}
519510
}

compiler/rustc_codegen_llvm/messages.ftl

+6-2
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,12 @@ codegen_llvm_error_writing_def_file =
2020
codegen_llvm_from_llvm_diag = {$message}
2121
2222
codegen_llvm_from_llvm_optimization_diag = {$filename}:{$line}:{$column} {$pass_name} ({$kind}): {$message}
23-
codegen_llvm_invalid_minimum_alignment =
24-
invalid minimum global alignment: {$err}
23+
24+
codegen_llvm_invalid_minimum_alignment_not_power_of_two =
25+
invalid minimum global alignment: {$align} is not power of 2
26+
27+
codegen_llvm_invalid_minimum_alignment_too_large =
28+
invalid minimum global alignment: {$align} is too large
2529
2630
codegen_llvm_load_bitcode = failed to load bitcode of module "{$name}"
2731
codegen_llvm_load_bitcode_with_llvm_err = failed to load bitcode of module "{$name}": {$llvm_err}

compiler/rustc_codegen_llvm/src/consts.rs

+14-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
use crate::base;
22
use crate::common::{self, CodegenCx};
33
use crate::debuginfo;
4-
use crate::errors::{InvalidMinimumAlignment, SymbolAlreadyDefined};
4+
use crate::errors::{
5+
InvalidMinimumAlignmentNotPowerOfTwo, InvalidMinimumAlignmentTooLarge, SymbolAlreadyDefined,
6+
};
57
use crate::llvm::{self, True};
68
use crate::type_::Type;
79
use crate::type_of::LayoutLlvmExt;
@@ -19,7 +21,9 @@ use rustc_middle::ty::layout::LayoutOf;
1921
use rustc_middle::ty::{self, Instance, Ty};
2022
use rustc_middle::{bug, span_bug};
2123
use rustc_session::config::Lto;
22-
use rustc_target::abi::{Align, HasDataLayout, Primitive, Scalar, Size, WrappingRange};
24+
use rustc_target::abi::{
25+
Align, AlignFromBytesError, HasDataLayout, Primitive, Scalar, Size, WrappingRange,
26+
};
2327
use std::ops::Range;
2428

2529
pub fn const_alloc_to_llvm<'ll>(cx: &CodegenCx<'ll, '_>, alloc: ConstAllocation<'_>) -> &'ll Value {
@@ -129,9 +133,14 @@ fn set_global_alignment<'ll>(cx: &CodegenCx<'ll, '_>, gv: &'ll Value, mut align:
129133
if let Some(min) = cx.sess().target.min_global_align {
130134
match Align::from_bits(min) {
131135
Ok(min) => align = align.max(min),
132-
Err(err) => {
133-
cx.sess().emit_err(InvalidMinimumAlignment { err });
134-
}
136+
Err(err) => match err {
137+
AlignFromBytesError::NotPowerOfTwo(align) => {
138+
cx.sess().emit_err(InvalidMinimumAlignmentNotPowerOfTwo { align });
139+
}
140+
AlignFromBytesError::TooLarge(align) => {
141+
cx.sess().emit_err(InvalidMinimumAlignmentTooLarge { align });
142+
}
143+
},
135144
}
136145
}
137146
unsafe {

compiler/rustc_codegen_llvm/src/context.rs

+4-13
Original file line numberDiff line numberDiff line change
@@ -969,9 +969,9 @@ impl<'tcx> LayoutOfHelpers<'tcx> for CodegenCx<'_, 'tcx> {
969969
#[inline]
970970
fn handle_layout_err(&self, err: LayoutError<'tcx>, span: Span, ty: Ty<'tcx>) -> ! {
971971
if let LayoutError::SizeOverflow(_) = err {
972-
self.sess().emit_fatal(Spanned { span, node: err })
972+
self.sess().emit_fatal(Spanned { span, node: err.into_diagnostic() })
973973
} else {
974-
span_bug!(span, "failed to get layout for `{}`: {}", ty, err)
974+
span_bug!(span, "failed to get layout for `{ty}`: {err:?}")
975975
}
976976
}
977977
}
@@ -991,21 +991,12 @@ impl<'tcx> FnAbiOfHelpers<'tcx> for CodegenCx<'_, 'tcx> {
991991
} else {
992992
match fn_abi_request {
993993
FnAbiRequest::OfFnPtr { sig, extra_args } => {
994-
span_bug!(
995-
span,
996-
"`fn_abi_of_fn_ptr({}, {:?})` failed: {}",
997-
sig,
998-
extra_args,
999-
err
1000-
);
994+
span_bug!(span, "`fn_abi_of_fn_ptr({sig}, {extra_args:?})` failed: {err:?}",);
1001995
}
1002996
FnAbiRequest::OfInstance { instance, extra_args } => {
1003997
span_bug!(
1004998
span,
1005-
"`fn_abi_of_instance({}, {:?})` failed: {}",
1006-
instance,
1007-
extra_args,
1008-
err
999+
"`fn_abi_of_instance({instance}, {extra_args:?})` failed: {err:?}",
10091000
);
10101001
}
10111002
}

compiler/rustc_codegen_llvm/src/errors.rs

+9-3
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,15 @@ pub(crate) struct SymbolAlreadyDefined<'a> {
5050
}
5151

5252
#[derive(Diagnostic)]
53-
#[diag(codegen_llvm_invalid_minimum_alignment)]
54-
pub(crate) struct InvalidMinimumAlignment {
55-
pub err: String,
53+
#[diag(codegen_llvm_invalid_minimum_alignment_not_power_of_two)]
54+
pub(crate) struct InvalidMinimumAlignmentNotPowerOfTwo {
55+
pub align: u64,
56+
}
57+
58+
#[derive(Diagnostic)]
59+
#[diag(codegen_llvm_invalid_minimum_alignment_too_large)]
60+
pub(crate) struct InvalidMinimumAlignmentTooLarge {
61+
pub align: u64,
5662
}
5763

5864
#[derive(Diagnostic)]

compiler/rustc_codegen_ssa/src/debuginfo/type_names.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,7 @@ fn push_debuginfo_type_name<'tcx>(
9393
Err(e) => {
9494
// Computing the layout can still fail here, e.g. if the target architecture
9595
// cannot represent the type. See https://github.com/rust-lang/rust/issues/94961.
96-
// FIXME: migrate once `rustc_middle::mir::interpret::InterpError` is translatable.
97-
tcx.sess.fatal(format!("{}", e));
96+
tcx.sess.emit_fatal(e.into_diagnostic());
9897
}
9998
}
10099
} else {

0 commit comments

Comments
 (0)