Skip to content

Commit be64802

Browse files
committed
Use StringPart more.
1 parent 0621cd4 commit be64802

File tree

3 files changed

+38
-43
lines changed

3 files changed

+38
-43
lines changed

compiler/rustc_errors/src/diagnostic.rs

+25-30
Original file line numberDiff line numberDiff line change
@@ -165,10 +165,10 @@ impl DiagnosticStyledString {
165165
DiagnosticStyledString(vec![])
166166
}
167167
pub fn push_normal<S: Into<String>>(&mut self, t: S) {
168-
self.0.push(StringPart::normal(t.into()));
168+
self.0.push(StringPart::normal(t));
169169
}
170170
pub fn push_highlighted<S: Into<String>>(&mut self, t: S) {
171-
self.0.push(StringPart::highlighted(t.into()));
171+
self.0.push(StringPart::highlighted(t));
172172
}
173173
pub fn push<S: Into<String>>(&mut self, t: S, highlight: bool) {
174174
if highlight {
@@ -178,11 +178,11 @@ impl DiagnosticStyledString {
178178
}
179179
}
180180
pub fn normal<S: Into<String>>(t: S) -> DiagnosticStyledString {
181-
DiagnosticStyledString(vec![StringPart::normal(t.into())])
181+
DiagnosticStyledString(vec![StringPart::normal(t)])
182182
}
183183

184184
pub fn highlighted<S: Into<String>>(t: S) -> DiagnosticStyledString {
185-
DiagnosticStyledString(vec![StringPart::highlighted(t.into())])
185+
DiagnosticStyledString(vec![StringPart::highlighted(t)])
186186
}
187187

188188
pub fn content(&self) -> String {
@@ -197,12 +197,12 @@ pub struct StringPart {
197197
}
198198

199199
impl StringPart {
200-
fn normal(content: String) -> StringPart {
201-
StringPart { content, style: Style::NoStyle }
200+
pub fn normal<S: Into<String>>(content: S) -> StringPart {
201+
StringPart { content: content.into(), style: Style::NoStyle }
202202
}
203203

204-
fn highlighted(content: String) -> StringPart {
205-
StringPart { content, style: Style::Highlight }
204+
pub fn highlighted<S: Into<String>>(content: S) -> StringPart {
205+
StringPart { content: content.into(), style: Style::Highlight }
206206
}
207207
}
208208

@@ -391,13 +391,16 @@ impl Diagnostic {
391391
} else {
392392
(0, found_label.len() - expected_label.len())
393393
};
394-
let mut msg: Vec<_> =
395-
vec![(format!("{}{} `", " ".repeat(expected_padding), expected_label), Style::NoStyle)];
396-
msg.extend(expected.0.into_iter().map(|p| (p.content, p.style)));
397-
msg.push((format!("`{expected_extra}\n"), Style::NoStyle));
398-
msg.push((format!("{}{} `", " ".repeat(found_padding), found_label), Style::NoStyle));
399-
msg.extend(found.0.into_iter().map(|p| (p.content, p.style)));
400-
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}")));
401404

402405
// For now, just attach these as notes.
403406
self.highlighted_note(msg);
@@ -406,9 +409,9 @@ impl Diagnostic {
406409

407410
pub fn note_trait_signature(&mut self, name: Symbol, signature: String) -> &mut Self {
408411
self.highlighted_note(vec![
409-
(format!("`{name}` from trait: `"), Style::NoStyle),
410-
(signature, Style::Highlight),
411-
("`".to_string(), Style::NoStyle),
412+
StringPart::normal(format!("`{name}` from trait: `")),
413+
StringPart::highlighted(signature),
414+
StringPart::normal("`"),
412415
]);
413416
self
414417
}
@@ -420,10 +423,7 @@ impl Diagnostic {
420423
self
421424
}
422425

423-
fn highlighted_note<M: Into<SubdiagnosticMessage>>(
424-
&mut self,
425-
msg: Vec<(M, Style)>,
426-
) -> &mut Self {
426+
fn highlighted_note(&mut self, msg: Vec<StringPart>) -> &mut Self {
427427
self.sub_with_highlights(Level::Note, msg, MultiSpan::new());
428428
self
429429
}
@@ -492,7 +492,7 @@ impl Diagnostic {
492492
}
493493

494494
/// Add a help message attached to this diagnostic with a customizable highlighted message.
495-
pub fn highlighted_help(&mut self, msg: Vec<(String, Style)>) -> &mut Self {
495+
pub fn highlighted_help(&mut self, msg: Vec<StringPart>) -> &mut Self {
496496
self.sub_with_highlights(Level::Help, msg, MultiSpan::new());
497497
self
498498
}
@@ -941,15 +941,10 @@ impl Diagnostic {
941941

942942
/// Convenience function for internal use, clients should use one of the
943943
/// public methods above.
944-
fn sub_with_highlights<M: Into<SubdiagnosticMessage>>(
945-
&mut self,
946-
level: Level,
947-
messages: Vec<(M, Style)>,
948-
span: MultiSpan,
949-
) {
944+
fn sub_with_highlights(&mut self, level: Level, messages: Vec<StringPart>, span: MultiSpan) {
950945
let messages = messages
951946
.into_iter()
952-
.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))
953948
.collect();
954949
let sub = SubDiagnostic { level, messages, span };
955950
self.children.push(sub);

compiler/rustc_errors/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ extern crate self as rustc_errors;
3434
pub use codes::*;
3535
pub use diagnostic::{
3636
AddToDiagnostic, DecorateLint, Diagnostic, DiagnosticArg, DiagnosticArgName,
37-
DiagnosticArgValue, DiagnosticStyledString, IntoDiagnosticArg, SubDiagnostic,
37+
DiagnosticArgValue, DiagnosticStyledString, IntoDiagnosticArg, StringPart, SubDiagnostic,
3838
};
3939
pub use diagnostic_builder::{
4040
BugAbort, DiagnosticBuilder, EmissionGuarantee, FatalAbort, IntoDiagnostic,

compiler/rustc_trait_selection/src/traits/error_reporting/type_err_ctxt_ext.rs

+12-12
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use crate::traits::{
2020
use rustc_data_structures::fx::{FxHashMap, FxIndexMap};
2121
use rustc_errors::{
2222
codes::*, pluralize, struct_span_code_err, Applicability, Diagnostic, DiagnosticBuilder,
23-
ErrorGuaranteed, MultiSpan, StashKey, Style,
23+
ErrorGuaranteed, MultiSpan, StashKey, StringPart,
2424
};
2525
use rustc_hir as hir;
2626
use rustc_hir::def::{DefKind, Namespace, Res};
@@ -2059,11 +2059,11 @@ impl<'tcx> InferCtxtPrivExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
20592059
ct_op: |ct| ct.normalize(self.tcx, ty::ParamEnv::empty()),
20602060
});
20612061
err.highlighted_help(vec![
2062-
(format!("the trait `{}` ", cand.print_trait_sugared()), Style::NoStyle),
2063-
("is".to_string(), Style::Highlight),
2064-
(" implemented for `".to_string(), Style::NoStyle),
2065-
(cand.self_ty().to_string(), Style::Highlight),
2066-
("`".to_string(), Style::NoStyle),
2062+
StringPart::normal(format!("the trait `{}` ", cand.print_trait_sugared())),
2063+
StringPart::highlighted("is"),
2064+
StringPart::normal(" implemented for `"),
2065+
StringPart::highlighted(cand.self_ty().to_string()),
2066+
StringPart::normal("`"),
20672067
]);
20682068

20692069
if let [TypeError::Sorts(exp_found)] = &terrs[..] {
@@ -2095,12 +2095,12 @@ impl<'tcx> InferCtxtPrivExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
20952095
_ => (" implemented for `", ""),
20962096
};
20972097
err.highlighted_help(vec![
2098-
(format!("the trait `{}` ", cand.print_trait_sugared()), Style::NoStyle),
2099-
("is".to_string(), Style::Highlight),
2100-
(desc.to_string(), Style::NoStyle),
2101-
(cand.self_ty().to_string(), Style::Highlight),
2102-
("`".to_string(), Style::NoStyle),
2103-
(mention_castable.to_string(), Style::NoStyle),
2098+
StringPart::normal(format!("the trait `{}` ", cand.print_trait_sugared())),
2099+
StringPart::highlighted("is"),
2100+
StringPart::normal(desc),
2101+
StringPart::highlighted(cand.self_ty().to_string()),
2102+
StringPart::normal("`"),
2103+
StringPart::normal(mention_castable),
21042104
]);
21052105
return true;
21062106
}

0 commit comments

Comments
 (0)