From 3a1847b07d552fff62be4ebf11e74f90b6a43fce Mon Sep 17 00:00:00 2001 From: Mark Mansi Date: Wed, 28 Nov 2018 15:05:36 -0600 Subject: implement outlive suggestions --- src/librustc_errors/diagnostic.rs | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) (limited to 'src/librustc_errors') diff --git a/src/librustc_errors/diagnostic.rs b/src/librustc_errors/diagnostic.rs index 1781f2e1650..3c4539b597b 100644 --- a/src/librustc_errors/diagnostic.rs +++ b/src/librustc_errors/diagnostic.rs @@ -6,7 +6,7 @@ use crate::Applicability; use crate::Level; use crate::snippet::Style; use std::fmt; -use syntax_pos::{MultiSpan, Span}; +use syntax_pos::{MultiSpan, Span, DUMMY_SP}; #[must_use] #[derive(Clone, Debug, PartialEq, Hash, RustcEncodable, RustcDecodable)] @@ -17,6 +17,11 @@ pub struct Diagnostic { pub span: MultiSpan, pub children: Vec, pub suggestions: Vec, + + /// This is not used for highlighting or rendering any error message. Rather, it can be used + /// as a sort key to sort a buffer of diagnostics. By default, it is the primary span of + /// `span` if there is one. Otherwise, it is `DUMMY_SP`. + pub sort_span: Span, } #[derive(Clone, Debug, PartialEq, Eq, Hash, RustcEncodable, RustcDecodable)] @@ -87,6 +92,7 @@ impl Diagnostic { span: MultiSpan::new(), children: vec![], suggestions: vec![], + sort_span: DUMMY_SP, } } @@ -118,6 +124,11 @@ impl Diagnostic { self.level == Level::Cancelled } + /// Set the sorting span. + pub fn set_sort_span(&mut self, sp: Span) { + self.sort_span = sp; + } + /// Adds a span/label to be included in the resulting snippet. /// This label will be shown together with the original span/label used when creating the /// diagnostic, *not* a span added by one of the `span_*` methods. @@ -457,6 +468,9 @@ impl Diagnostic { pub fn set_span>(&mut self, sp: S) -> &mut Self { self.span = sp.into(); + if let Some(span) = self.span.primary_span() { + self.sort_span = span; + } self } -- cgit 1.4.1-3-g733a5 From 19122ab98190053ad19e8baf153870c56c266f29 Mon Sep 17 00:00:00 2001 From: Mark Mansi Date: Thu, 3 Oct 2019 18:14:25 -0500 Subject: add and use struct_help --- src/librustc_errors/lib.rs | 5 +++ .../error_reporting/outlives_suggestion.rs | 37 ++++++++++------------ 2 files changed, 21 insertions(+), 21 deletions(-) (limited to 'src/librustc_errors') diff --git a/src/librustc_errors/lib.rs b/src/librustc_errors/lib.rs index 63df052a225..705c802d90a 100644 --- a/src/librustc_errors/lib.rs +++ b/src/librustc_errors/lib.rs @@ -619,6 +619,11 @@ impl Handler { 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, msg: &str) -> FatalError { self.emit_diag_at_span(Diagnostic::new(Fatal, msg), span); FatalError diff --git a/src/librustc_mir/borrow_check/nll/region_infer/error_reporting/outlives_suggestion.rs b/src/librustc_mir/borrow_check/nll/region_infer/error_reporting/outlives_suggestion.rs index a55a3a40604..57f844f2924 100644 --- a/src/librustc_mir/borrow_check/nll/region_infer/error_reporting/outlives_suggestion.rs +++ b/src/librustc_mir/borrow_check/nll/region_infer/error_reporting/outlives_suggestion.rs @@ -6,7 +6,7 @@ use std::collections::BTreeMap; use log::debug; use rustc::{hir::def_id::DefId, infer::InferCtxt, mir::Body, ty::RegionVid}; use rustc_data_structures::fx::FxHashSet; -use rustc_errors::{Diagnostic, DiagnosticBuilder, Level}; +use rustc_errors::{Diagnostic, DiagnosticBuilder}; use smallvec::SmallVec; @@ -258,29 +258,24 @@ impl OutlivesSuggestionBuilder { // If there is exactly one suggestable constraints, then just suggest it. Otherwise, emit a // list of diagnostics. let mut diag = if suggested.len() == 1 { - DiagnosticBuilder::new( - infcx.tcx.sess.diagnostic(), - Level::Help, - &match suggested.last().unwrap() { - SuggestedConstraint::Outlives(a, bs) => { - let bs: SmallVec<[String; 2]> = - bs.iter().map(|r| format!("{}", r)).collect(); - format!("add bound `{}: {}`", a, bs.join(" + ")) - } + infcx.tcx.sess.diagnostic().struct_help(&match suggested.last().unwrap() { + 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) => { - format!("`{}` and `{}` must be the same: replace one with the other", a, b) - } - SuggestedConstraint::Static(a) => format!("replace `{}` with `'static`", a), - }, - ) + SuggestedConstraint::Equal(a, b) => { + format!("`{}` and `{}` must be the same: replace one with the other", a, b) + } + SuggestedConstraint::Static(a) => format!("replace `{}` with `'static`", a), + }) } else { // Create a new diagnostic. - let mut diag = DiagnosticBuilder::new( - infcx.tcx.sess.diagnostic(), - Level::Help, - "the following changes may resolve your lifetime errors", - ); + let mut diag = infcx + .tcx + .sess + .diagnostic() + .struct_help("the following changes may resolve your lifetime errors"); // Add suggestions. for constraint in suggested { -- cgit 1.4.1-3-g733a5