1
Fork 0

Use StringPart more.

This commit is contained in:
Nicholas Nethercote 2024-02-03 09:02:36 +11:00
parent 0621cd46f2
commit be64802854
3 changed files with 38 additions and 43 deletions

View file

@ -165,10 +165,10 @@ impl DiagnosticStyledString {
DiagnosticStyledString(vec![]) DiagnosticStyledString(vec![])
} }
pub fn push_normal<S: Into<String>>(&mut self, t: S) { pub fn push_normal<S: Into<String>>(&mut self, t: S) {
self.0.push(StringPart::normal(t.into())); self.0.push(StringPart::normal(t));
} }
pub fn push_highlighted<S: Into<String>>(&mut self, t: S) { pub fn push_highlighted<S: Into<String>>(&mut self, t: S) {
self.0.push(StringPart::highlighted(t.into())); self.0.push(StringPart::highlighted(t));
} }
pub fn push<S: Into<String>>(&mut self, t: S, highlight: bool) { pub fn push<S: Into<String>>(&mut self, t: S, highlight: bool) {
if highlight { if highlight {
@ -178,11 +178,11 @@ impl DiagnosticStyledString {
} }
} }
pub fn normal<S: Into<String>>(t: S) -> DiagnosticStyledString { pub fn normal<S: Into<String>>(t: S) -> DiagnosticStyledString {
DiagnosticStyledString(vec![StringPart::normal(t.into())]) DiagnosticStyledString(vec![StringPart::normal(t)])
} }
pub fn highlighted<S: Into<String>>(t: S) -> DiagnosticStyledString { pub fn highlighted<S: Into<String>>(t: S) -> DiagnosticStyledString {
DiagnosticStyledString(vec![StringPart::highlighted(t.into())]) DiagnosticStyledString(vec![StringPart::highlighted(t)])
} }
pub fn content(&self) -> String { pub fn content(&self) -> String {
@ -197,12 +197,12 @@ pub struct StringPart {
} }
impl StringPart { impl StringPart {
fn normal(content: String) -> StringPart { pub fn normal<S: Into<String>>(content: S) -> StringPart {
StringPart { content, style: Style::NoStyle } StringPart { content: content.into(), style: Style::NoStyle }
} }
fn highlighted(content: String) -> StringPart { pub fn highlighted<S: Into<String>>(content: S) -> StringPart {
StringPart { content, style: Style::Highlight } StringPart { content: content.into(), style: Style::Highlight }
} }
} }
@ -391,13 +391,16 @@ impl Diagnostic {
} else { } else {
(0, found_label.len() - expected_label.len()) (0, found_label.len() - expected_label.len())
}; };
let mut msg: Vec<_> = let mut msg = vec![StringPart::normal(format!(
vec![(format!("{}{} `", " ".repeat(expected_padding), expected_label), Style::NoStyle)]; "{}{} `",
msg.extend(expected.0.into_iter().map(|p| (p.content, p.style))); " ".repeat(expected_padding),
msg.push((format!("`{expected_extra}\n"), Style::NoStyle)); expected_label
msg.push((format!("{}{} `", " ".repeat(found_padding), found_label), Style::NoStyle)); ))];
msg.extend(found.0.into_iter().map(|p| (p.content, p.style))); msg.extend(expected.0.into_iter());
msg.push((format!("`{found_extra}"), Style::NoStyle)); msg.push(StringPart::normal(format!("`{expected_extra}\n")));
msg.push(StringPart::normal(format!("{}{} `", " ".repeat(found_padding), found_label)));
msg.extend(found.0.into_iter());
msg.push(StringPart::normal(format!("`{found_extra}")));
// For now, just attach these as notes. // For now, just attach these as notes.
self.highlighted_note(msg); self.highlighted_note(msg);
@ -406,9 +409,9 @@ impl Diagnostic {
pub fn note_trait_signature(&mut self, name: Symbol, signature: String) -> &mut Self { pub fn note_trait_signature(&mut self, name: Symbol, signature: String) -> &mut Self {
self.highlighted_note(vec![ self.highlighted_note(vec![
(format!("`{name}` from trait: `"), Style::NoStyle), StringPart::normal(format!("`{name}` from trait: `")),
(signature, Style::Highlight), StringPart::highlighted(signature),
("`".to_string(), Style::NoStyle), StringPart::normal("`"),
]); ]);
self self
} }
@ -420,10 +423,7 @@ impl Diagnostic {
self self
} }
fn highlighted_note<M: Into<SubdiagnosticMessage>>( fn highlighted_note(&mut self, msg: Vec<StringPart>) -> &mut Self {
&mut self,
msg: Vec<(M, Style)>,
) -> &mut Self {
self.sub_with_highlights(Level::Note, msg, MultiSpan::new()); self.sub_with_highlights(Level::Note, msg, MultiSpan::new());
self self
} }
@ -492,7 +492,7 @@ impl Diagnostic {
} }
/// Add a help message attached to this diagnostic with a customizable highlighted message. /// Add a help message attached to this diagnostic with a customizable highlighted message.
pub fn highlighted_help(&mut self, msg: Vec<(String, Style)>) -> &mut Self { pub fn highlighted_help(&mut self, msg: Vec<StringPart>) -> &mut Self {
self.sub_with_highlights(Level::Help, msg, MultiSpan::new()); self.sub_with_highlights(Level::Help, msg, MultiSpan::new());
self self
} }
@ -941,15 +941,10 @@ impl Diagnostic {
/// Convenience function for internal use, clients should use one of the /// Convenience function for internal use, clients should use one of the
/// public methods above. /// public methods above.
fn sub_with_highlights<M: Into<SubdiagnosticMessage>>( fn sub_with_highlights(&mut self, level: Level, messages: Vec<StringPart>, span: MultiSpan) {
&mut self,
level: Level,
messages: Vec<(M, Style)>,
span: MultiSpan,
) {
let messages = messages let messages = messages
.into_iter() .into_iter()
.map(|m| (self.subdiagnostic_message_to_diagnostic_message(m.0), m.1)) .map(|m| (self.subdiagnostic_message_to_diagnostic_message(m.content), m.style))
.collect(); .collect();
let sub = SubDiagnostic { level, messages, span }; let sub = SubDiagnostic { level, messages, span };
self.children.push(sub); self.children.push(sub);

View file

@ -34,7 +34,7 @@ extern crate self as rustc_errors;
pub use codes::*; pub use codes::*;
pub use diagnostic::{ pub use diagnostic::{
AddToDiagnostic, DecorateLint, Diagnostic, DiagnosticArg, DiagnosticArgName, AddToDiagnostic, DecorateLint, Diagnostic, DiagnosticArg, DiagnosticArgName,
DiagnosticArgValue, DiagnosticStyledString, IntoDiagnosticArg, SubDiagnostic, DiagnosticArgValue, DiagnosticStyledString, IntoDiagnosticArg, StringPart, SubDiagnostic,
}; };
pub use diagnostic_builder::{ pub use diagnostic_builder::{
BugAbort, DiagnosticBuilder, EmissionGuarantee, FatalAbort, IntoDiagnostic, BugAbort, DiagnosticBuilder, EmissionGuarantee, FatalAbort, IntoDiagnostic,

View file

@ -20,7 +20,7 @@ use crate::traits::{
use rustc_data_structures::fx::{FxHashMap, FxIndexMap}; use rustc_data_structures::fx::{FxHashMap, FxIndexMap};
use rustc_errors::{ use rustc_errors::{
codes::*, pluralize, struct_span_code_err, Applicability, Diagnostic, DiagnosticBuilder, codes::*, pluralize, struct_span_code_err, Applicability, Diagnostic, DiagnosticBuilder,
ErrorGuaranteed, MultiSpan, StashKey, Style, ErrorGuaranteed, MultiSpan, StashKey, StringPart,
}; };
use rustc_hir as hir; use rustc_hir as hir;
use rustc_hir::def::{DefKind, Namespace, Res}; use rustc_hir::def::{DefKind, Namespace, Res};
@ -2059,11 +2059,11 @@ impl<'tcx> InferCtxtPrivExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
ct_op: |ct| ct.normalize(self.tcx, ty::ParamEnv::empty()), ct_op: |ct| ct.normalize(self.tcx, ty::ParamEnv::empty()),
}); });
err.highlighted_help(vec![ err.highlighted_help(vec![
(format!("the trait `{}` ", cand.print_trait_sugared()), Style::NoStyle), StringPart::normal(format!("the trait `{}` ", cand.print_trait_sugared())),
("is".to_string(), Style::Highlight), StringPart::highlighted("is"),
(" implemented for `".to_string(), Style::NoStyle), StringPart::normal(" implemented for `"),
(cand.self_ty().to_string(), Style::Highlight), StringPart::highlighted(cand.self_ty().to_string()),
("`".to_string(), Style::NoStyle), StringPart::normal("`"),
]); ]);
if let [TypeError::Sorts(exp_found)] = &terrs[..] { if let [TypeError::Sorts(exp_found)] = &terrs[..] {
@ -2095,12 +2095,12 @@ impl<'tcx> InferCtxtPrivExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
_ => (" implemented for `", ""), _ => (" implemented for `", ""),
}; };
err.highlighted_help(vec![ err.highlighted_help(vec![
(format!("the trait `{}` ", cand.print_trait_sugared()), Style::NoStyle), StringPart::normal(format!("the trait `{}` ", cand.print_trait_sugared())),
("is".to_string(), Style::Highlight), StringPart::highlighted("is"),
(desc.to_string(), Style::NoStyle), StringPart::normal(desc),
(cand.self_ty().to_string(), Style::Highlight), StringPart::highlighted(cand.self_ty().to_string()),
("`".to_string(), Style::NoStyle), StringPart::normal("`"),
(mention_castable.to_string(), Style::NoStyle), StringPart::normal(mention_castable),
]); ]);
return true; return true;
} }