add and use struct_help

This commit is contained in:
Mark Mansi 2019-10-03 18:14:25 -05:00
parent 89a184a399
commit 19122ab981
2 changed files with 21 additions and 21 deletions

View file

@ -619,6 +619,11 @@ impl Handler {
DiagnosticBuilder::new(self, Level::Fatal, msg) DiagnosticBuilder::new(self, Level::Fatal, msg)
} }
/// Construct a builder at the `Help` level with the `msg`.
pub fn struct_help(&self, msg: &str) -> DiagnosticBuilder<'_> {
DiagnosticBuilder::new(self, Level::Help, msg)
}
pub fn span_fatal(&self, span: impl Into<MultiSpan>, msg: &str) -> FatalError { pub fn span_fatal(&self, span: impl Into<MultiSpan>, msg: &str) -> FatalError {
self.emit_diag_at_span(Diagnostic::new(Fatal, msg), span); self.emit_diag_at_span(Diagnostic::new(Fatal, msg), span);
FatalError FatalError

View file

@ -6,7 +6,7 @@ use std::collections::BTreeMap;
use log::debug; use log::debug;
use rustc::{hir::def_id::DefId, infer::InferCtxt, mir::Body, ty::RegionVid}; use rustc::{hir::def_id::DefId, infer::InferCtxt, mir::Body, ty::RegionVid};
use rustc_data_structures::fx::FxHashSet; use rustc_data_structures::fx::FxHashSet;
use rustc_errors::{Diagnostic, DiagnosticBuilder, Level}; use rustc_errors::{Diagnostic, DiagnosticBuilder};
use smallvec::SmallVec; use smallvec::SmallVec;
@ -258,29 +258,24 @@ impl OutlivesSuggestionBuilder {
// If there is exactly one suggestable constraints, then just suggest it. Otherwise, emit a // If there is exactly one suggestable constraints, then just suggest it. Otherwise, emit a
// list of diagnostics. // list of diagnostics.
let mut diag = if suggested.len() == 1 { let mut diag = if suggested.len() == 1 {
DiagnosticBuilder::new( infcx.tcx.sess.diagnostic().struct_help(&match suggested.last().unwrap() {
infcx.tcx.sess.diagnostic(), SuggestedConstraint::Outlives(a, bs) => {
Level::Help, let bs: SmallVec<[String; 2]> = bs.iter().map(|r| format!("{}", r)).collect();
&match suggested.last().unwrap() { format!("add bound `{}: {}`", a, bs.join(" + "))
SuggestedConstraint::Outlives(a, bs) => { }
let bs: SmallVec<[String; 2]> =
bs.iter().map(|r| format!("{}", r)).collect();
format!("add bound `{}: {}`", a, bs.join(" + "))
}
SuggestedConstraint::Equal(a, b) => { SuggestedConstraint::Equal(a, b) => {
format!("`{}` and `{}` must be the same: replace one with the other", a, b) format!("`{}` and `{}` must be the same: replace one with the other", a, b)
} }
SuggestedConstraint::Static(a) => format!("replace `{}` with `'static`", a), SuggestedConstraint::Static(a) => format!("replace `{}` with `'static`", a),
}, })
)
} else { } else {
// Create a new diagnostic. // Create a new diagnostic.
let mut diag = DiagnosticBuilder::new( let mut diag = infcx
infcx.tcx.sess.diagnostic(), .tcx
Level::Help, .sess
"the following changes may resolve your lifetime errors", .diagnostic()
); .struct_help("the following changes may resolve your lifetime errors");
// Add suggestions. // Add suggestions.
for constraint in suggested { for constraint in suggested {