Skip to content

Commit 8827d11

Browse files
authored
Unrolled build for rust-lang#120571
Rollup merge of rust-lang#120571 - nnethercote:misc-diagnostics, r=oli-obk Miscellaneous diagnostics cleanups All found while working on some speculative, invasive changes, but worth doing in their own right. r? `@oli-obk`
2 parents 671eb38 + be64802 commit 8827d11

File tree

25 files changed

+110
-154
lines changed

25 files changed

+110
-154
lines changed

compiler/rustc_borrowck/src/diagnostics/mutability_errors.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -198,12 +198,12 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
198198
{
199199
let span = self.body.local_decls[local].source_info.span;
200200
mut_error = Some(span);
201-
if let Some((buffer, c)) = self.get_buffered_mut_error(span) {
201+
if let Some((buffered_err, c)) = self.get_buffered_mut_error(span) {
202202
// We've encountered a second (or more) attempt to mutably borrow an
203203
// immutable binding, so the likely problem is with the binding
204204
// declaration, not the use. We collect these in a single diagnostic
205205
// and make the binding the primary span of the error.
206-
err = buffer;
206+
err = buffered_err;
207207
count = c + 1;
208208
if count == 2 {
209209
err.replace_span_with(span, false);

compiler/rustc_codegen_llvm/src/errors.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ pub(crate) struct ParseTargetMachineConfig<'a>(pub LlvmError<'a>);
102102
impl<G: EmissionGuarantee> IntoDiagnostic<'_, G> for ParseTargetMachineConfig<'_> {
103103
fn into_diagnostic(self, dcx: &'_ DiagCtxt, level: Level) -> DiagnosticBuilder<'_, G> {
104104
let diag: DiagnosticBuilder<'_, G> = self.0.into_diagnostic(dcx, level);
105-
let (message, _) = diag.messages().first().expect("`LlvmError` with no message");
105+
let (message, _) = diag.messages.first().expect("`LlvmError` with no message");
106106
let message = dcx.eagerly_translate_to_string(message.clone(), diag.args());
107107

108108
DiagnosticBuilder::new(dcx, level, fluent::codegen_llvm_parse_target_machine_config)

compiler/rustc_codegen_ssa/src/back/write.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ use rustc_target::spec::{MergeFunctions, SanitizerSet};
3939

4040
use crate::errors::ErrorCreatingRemarkDir;
4141
use std::any::Any;
42-
use std::borrow::Cow;
4342
use std::fs;
4443
use std::io;
4544
use std::marker::PhantomData;
@@ -1812,12 +1811,12 @@ impl Translate for SharedEmitter {
18121811

18131812
impl Emitter for SharedEmitter {
18141813
fn emit_diagnostic(&mut self, diag: &rustc_errors::Diagnostic) {
1815-
let args: FxHashMap<Cow<'_, str>, DiagnosticArgValue> =
1814+
let args: FxHashMap<DiagnosticArgName, DiagnosticArgValue> =
18161815
diag.args().map(|(name, arg)| (name.clone(), arg.clone())).collect();
18171816
drop(self.sender.send(SharedEmitterMessage::Diagnostic(Diagnostic {
18181817
msgs: diag.messages.clone(),
18191818
args: args.clone(),
1820-
code: diag.code.clone(),
1819+
code: diag.code,
18211820
lvl: diag.level(),
18221821
})));
18231822
for child in &diag.children {

compiler/rustc_const_eval/src/const_eval/error.rs

+4-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
use std::mem;
22

3-
use rustc_errors::{DiagnosticArgValue, DiagnosticMessage, IntoDiagnostic, IntoDiagnosticArg};
3+
use rustc_errors::{
4+
DiagnosticArgName, DiagnosticArgValue, DiagnosticMessage, IntoDiagnostic, IntoDiagnosticArg,
5+
};
46
use rustc_hir::CRATE_HIR_ID;
57
use rustc_middle::mir::AssertKind;
68
use rustc_middle::query::TyCtxtAt;
@@ -32,10 +34,7 @@ impl MachineStopType for ConstEvalErrKind {
3234
AssertFailure(x) => x.diagnostic_message(),
3335
}
3436
}
35-
fn add_args(
36-
self: Box<Self>,
37-
adder: &mut dyn FnMut(std::borrow::Cow<'static, str>, DiagnosticArgValue),
38-
) {
37+
fn add_args(self: Box<Self>, adder: &mut dyn FnMut(DiagnosticArgName, DiagnosticArgValue)) {
3938
use ConstEvalErrKind::*;
4039
match *self {
4140
ConstAccessesStatic | ModifiedGlobal => {}

compiler/rustc_errors/src/diagnostic.rs

+37-59
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,10 @@ pub type DiagnosticArgName = Cow<'static, str>;
3333
#[derive(Clone, Debug, PartialEq, Eq, Hash, Encodable, Decodable)]
3434
pub enum DiagnosticArgValue {
3535
Str(Cow<'static, str>),
36-
Number(i128),
36+
// This gets converted to a `FluentNumber`, which is an `f64`. An `i32`
37+
// safely fits in an `f64`. Any integers bigger than that will be converted
38+
// to strings in `into_diagnostic_arg` and stored using the `Str` variant.
39+
Number(i32),
3740
StrListSepByAnd(Vec<Cow<'static, str>>),
3841
}
3942

@@ -113,7 +116,7 @@ pub struct Diagnostic {
113116

114117
/// With `-Ztrack_diagnostics` enabled,
115118
/// we print where in rustc this error was emitted.
116-
pub emitted_at: DiagnosticLocation,
119+
pub(crate) emitted_at: DiagnosticLocation,
117120
}
118121

119122
#[derive(Clone, Debug, Encodable, Decodable)]
@@ -162,10 +165,10 @@ impl DiagnosticStyledString {
162165
DiagnosticStyledString(vec![])
163166
}
164167
pub fn push_normal<S: Into<String>>(&mut self, t: S) {
165-
self.0.push(StringPart::Normal(t.into()));
168+
self.0.push(StringPart::normal(t));
166169
}
167170
pub fn push_highlighted<S: Into<String>>(&mut self, t: S) {
168-
self.0.push(StringPart::Highlighted(t.into()));
171+
self.0.push(StringPart::highlighted(t));
169172
}
170173
pub fn push<S: Into<String>>(&mut self, t: S, highlight: bool) {
171174
if highlight {
@@ -175,35 +178,34 @@ impl DiagnosticStyledString {
175178
}
176179
}
177180
pub fn normal<S: Into<String>>(t: S) -> DiagnosticStyledString {
178-
DiagnosticStyledString(vec![StringPart::Normal(t.into())])
181+
DiagnosticStyledString(vec![StringPart::normal(t)])
179182
}
180183

181184
pub fn highlighted<S: Into<String>>(t: S) -> DiagnosticStyledString {
182-
DiagnosticStyledString(vec![StringPart::Highlighted(t.into())])
185+
DiagnosticStyledString(vec![StringPart::highlighted(t)])
183186
}
184187

185188
pub fn content(&self) -> String {
186-
self.0.iter().map(|x| x.content()).collect::<String>()
189+
self.0.iter().map(|x| x.content.as_str()).collect::<String>()
187190
}
188191
}
189192

190193
#[derive(Debug, PartialEq, Eq)]
191-
pub enum StringPart {
192-
Normal(String),
193-
Highlighted(String),
194+
pub struct StringPart {
195+
content: String,
196+
style: Style,
194197
}
195198

196199
impl StringPart {
197-
pub fn content(&self) -> &str {
198-
match self {
199-
&StringPart::Normal(ref s) | &StringPart::Highlighted(ref s) => s,
200-
}
200+
pub fn normal<S: Into<String>>(content: S) -> StringPart {
201+
StringPart { content: content.into(), style: Style::NoStyle }
202+
}
203+
204+
pub fn highlighted<S: Into<String>>(content: S) -> StringPart {
205+
StringPart { content: content.into(), style: Style::Highlight }
201206
}
202207
}
203208

204-
// Note: most of these methods are setters that return `&mut Self`. The small
205-
// number of simple getter functions all have `get_` prefixes to distinguish
206-
// them from the setters.
207209
impl Diagnostic {
208210
#[track_caller]
209211
pub fn new<M: Into<DiagnosticMessage>>(level: Level, message: M) -> Self {
@@ -389,19 +391,16 @@ impl Diagnostic {
389391
} else {
390392
(0, found_label.len() - expected_label.len())
391393
};
392-
let mut msg: Vec<_> =
393-
vec![(format!("{}{} `", " ".repeat(expected_padding), expected_label), Style::NoStyle)];
394-
msg.extend(expected.0.iter().map(|x| match *x {
395-
StringPart::Normal(ref s) => (s.to_owned(), Style::NoStyle),
396-
StringPart::Highlighted(ref s) => (s.to_owned(), Style::Highlight),
397-
}));
398-
msg.push((format!("`{expected_extra}\n"), Style::NoStyle));
399-
msg.push((format!("{}{} `", " ".repeat(found_padding), found_label), Style::NoStyle));
400-
msg.extend(found.0.iter().map(|x| match *x {
401-
StringPart::Normal(ref s) => (s.to_owned(), Style::NoStyle),
402-
StringPart::Highlighted(ref s) => (s.to_owned(), Style::Highlight),
403-
}));
404-
msg.push((format!("`{found_extra}"), Style::NoStyle));
394+
let mut msg = vec![StringPart::normal(format!(
395+
"{}{} `",
396+
" ".repeat(expected_padding),
397+
expected_label
398+
))];
399+
msg.extend(expected.0.into_iter());
400+
msg.push(StringPart::normal(format!("`{expected_extra}\n")));
401+
msg.push(StringPart::normal(format!("{}{} `", " ".repeat(found_padding), found_label)));
402+
msg.extend(found.0.into_iter());
403+
msg.push(StringPart::normal(format!("`{found_extra}")));
405404

406405
// For now, just attach these as notes.
407406
self.highlighted_note(msg);
@@ -410,9 +409,9 @@ impl Diagnostic {
410409

411410
pub fn note_trait_signature(&mut self, name: Symbol, signature: String) -> &mut Self {
412411
self.highlighted_note(vec![
413-
(format!("`{name}` from trait: `"), Style::NoStyle),
414-
(signature, Style::Highlight),
415-
("`".to_string(), Style::NoStyle),
412+
StringPart::normal(format!("`{name}` from trait: `")),
413+
StringPart::highlighted(signature),
414+
StringPart::normal("`"),
416415
]);
417416
self
418417
}
@@ -424,10 +423,7 @@ impl Diagnostic {
424423
self
425424
}
426425

427-
fn highlighted_note<M: Into<SubdiagnosticMessage>>(
428-
&mut self,
429-
msg: Vec<(M, Style)>,
430-
) -> &mut Self {
426+
fn highlighted_note(&mut self, msg: Vec<StringPart>) -> &mut Self {
431427
self.sub_with_highlights(Level::Note, msg, MultiSpan::new());
432428
self
433429
}
@@ -496,7 +492,7 @@ impl Diagnostic {
496492
}
497493

498494
/// Add a help message attached to this diagnostic with a customizable highlighted message.
499-
pub fn highlighted_help(&mut self, msg: Vec<(String, Style)>) -> &mut Self {
495+
pub fn highlighted_help(&mut self, msg: Vec<StringPart>) -> &mut Self {
500496
self.sub_with_highlights(Level::Help, msg, MultiSpan::new());
501497
self
502498
}
@@ -890,15 +886,6 @@ impl Diagnostic {
890886
self
891887
}
892888

893-
pub fn clear_code(&mut self) -> &mut Self {
894-
self.code = None;
895-
self
896-
}
897-
898-
pub fn get_code(&self) -> Option<ErrCode> {
899-
self.code
900-
}
901-
902889
pub fn primary_message(&mut self, msg: impl Into<DiagnosticMessage>) -> &mut Self {
903890
self.messages[0] = (msg.into(), Style::NoStyle);
904891
self
@@ -913,7 +900,7 @@ impl Diagnostic {
913900

914901
pub fn arg(
915902
&mut self,
916-
name: impl Into<Cow<'static, str>>,
903+
name: impl Into<DiagnosticArgName>,
917904
arg: impl IntoDiagnosticArg,
918905
) -> &mut Self {
919906
self.args.insert(name.into(), arg.into_diagnostic_arg());
@@ -924,10 +911,6 @@ impl Diagnostic {
924911
self.args = args;
925912
}
926913

927-
pub fn messages(&self) -> &[(DiagnosticMessage, Style)] {
928-
&self.messages
929-
}
930-
931914
/// Helper function that takes a `SubdiagnosticMessage` and returns a `DiagnosticMessage` by
932915
/// combining it with the primary message of the diagnostic (if translatable, otherwise it just
933916
/// passes the user's string along).
@@ -958,15 +941,10 @@ impl Diagnostic {
958941

959942
/// Convenience function for internal use, clients should use one of the
960943
/// public methods above.
961-
fn sub_with_highlights<M: Into<SubdiagnosticMessage>>(
962-
&mut self,
963-
level: Level,
964-
messages: Vec<(M, Style)>,
965-
span: MultiSpan,
966-
) {
944+
fn sub_with_highlights(&mut self, level: Level, messages: Vec<StringPart>, span: MultiSpan) {
967945
let messages = messages
968946
.into_iter()
969-
.map(|m| (self.subdiagnostic_message_to_diagnostic_message(m.0), m.1))
947+
.map(|m| (self.subdiagnostic_message_to_diagnostic_message(m.content), m.style))
970948
.collect();
971949
let sub = SubDiagnostic { level, messages, span };
972950
self.children.push(sub);

compiler/rustc_errors/src/diagnostic_impls.rs

+2-6
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,8 @@ macro_rules! into_diagnostic_arg_for_number {
6363
$(
6464
impl IntoDiagnosticArg for $ty {
6565
fn into_diagnostic_arg(self) -> DiagnosticArgValue {
66-
// HACK: `FluentNumber` the underline backing struct represent
67-
// numbers using a f64 which can't represent all the i128 numbers
68-
// So in order to be able to use fluent selectors and still
69-
// have all the numbers representable we only convert numbers
70-
// below a certain threshold.
71-
if let Ok(n) = TryInto::<i128>::try_into(self) && n >= -100 && n <= 100 {
66+
// Convert to a string if it won't fit into `Number`.
67+
if let Ok(n) = TryInto::<i32>::try_into(self) {
7268
DiagnosticArgValue::Number(n)
7369
} else {
7470
self.to_string().into_diagnostic_arg()

compiler/rustc_errors/src/emitter.rs

+6-8
Original file line numberDiff line numberDiff line change
@@ -558,7 +558,7 @@ impl Emitter for HumanEmitter {
558558
/// failures of rustc, as witnessed e.g. in issue #89358.
559559
pub struct SilentEmitter {
560560
pub fatal_dcx: DiagCtxt,
561-
pub fatal_note: Option<String>,
561+
pub fatal_note: String,
562562
}
563563

564564
impl Translate for SilentEmitter {
@@ -576,13 +576,11 @@ impl Emitter for SilentEmitter {
576576
None
577577
}
578578

579-
fn emit_diagnostic(&mut self, d: &Diagnostic) {
580-
if d.level == Level::Fatal {
581-
let mut d = d.clone();
582-
if let Some(ref note) = self.fatal_note {
583-
d.note(note.clone());
584-
}
585-
self.fatal_dcx.emit_diagnostic(d);
579+
fn emit_diagnostic(&mut self, diag: &Diagnostic) {
580+
if diag.level == Level::Fatal {
581+
let mut diag = diag.clone();
582+
diag.note(self.fatal_note.clone());
583+
self.fatal_dcx.emit_diagnostic(diag);
586584
}
587585
}
588586
}

0 commit comments

Comments
 (0)