diff options
| author | bors <bors@rust-lang.org> | 2019-11-21 17:53:19 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2019-11-21 17:53:19 +0000 |
| commit | 53712f8637dbe326df569a90814aae1cc5429710 (patch) | |
| tree | 3ffee9436fa178bcc3d74251c686074cbd8a4c04 /src/librustc_errors | |
| parent | 35ef33a89dfd8ff8c8a7b3c58fa7136bbcb2f1ed (diff) | |
| parent | 468722b33c589ade04a11f05f53d0f3bff6c7a99 (diff) | |
| download | rust-53712f8637dbe326df569a90814aae1cc5429710.tar.gz rust-53712f8637dbe326df569a90814aae1cc5429710.zip | |
Auto merge of #66389 - estebank:type-err-labels, r=petrochenkov
Specific labels when referring to "expected" and "found" types
Diffstat (limited to 'src/librustc_errors')
| -rw-r--r-- | src/librustc_errors/diagnostic.rs | 76 | ||||
| -rw-r--r-- | src/librustc_errors/diagnostic_builder.rs | 59 |
2 files changed, 77 insertions, 58 deletions
diff --git a/src/librustc_errors/diagnostic.rs b/src/librustc_errors/diagnostic.rs index b9af4aae550..41ce2d8c440 100644 --- a/src/librustc_errors/diagnostic.rs +++ b/src/librustc_errors/diagnostic.rs @@ -154,20 +154,21 @@ impl Diagnostic { self } - pub fn note_expected_found(&mut self, - label: &dyn fmt::Display, - expected: DiagnosticStyledString, - found: DiagnosticStyledString) - -> &mut Self - { - self.note_expected_found_extra(label, expected, found, &"", &"") - } - - pub fn note_unsuccessfull_coercion(&mut self, - expected: DiagnosticStyledString, - found: DiagnosticStyledString) - -> &mut Self - { + pub fn note_expected_found( + &mut self, + expected_label: &dyn fmt::Display, + expected: DiagnosticStyledString, + found_label: &dyn fmt::Display, + found: DiagnosticStyledString, + ) -> &mut Self { + self.note_expected_found_extra(expected_label, expected, found_label, found, &"", &"") + } + + pub fn note_unsuccessfull_coercion( + &mut self, + expected: DiagnosticStyledString, + found: DiagnosticStyledString, + ) -> &mut Self { let mut msg: Vec<_> = vec![(format!("required when trying to coerce from type `"), Style::NoStyle)]; @@ -189,27 +190,38 @@ impl Diagnostic { self } - pub fn note_expected_found_extra(&mut self, - label: &dyn fmt::Display, - expected: DiagnosticStyledString, - found: DiagnosticStyledString, - expected_extra: &dyn fmt::Display, - found_extra: &dyn fmt::Display) - -> &mut Self - { - let mut msg: Vec<_> = vec![(format!("expected {} `", label), Style::NoStyle)]; + pub fn note_expected_found_extra( + &mut self, + expected_label: &dyn fmt::Display, + expected: DiagnosticStyledString, + found_label: &dyn fmt::Display, + found: DiagnosticStyledString, + expected_extra: &dyn fmt::Display, + found_extra: &dyn fmt::Display, + ) -> &mut Self { + let expected_label = format!("expected {}", expected_label); + let found_label = format!("found {}", found_label); + let (found_padding, expected_padding) = if expected_label.len() > found_label.len() { + (expected_label.len() - found_label.len(), 0) + } else { + (0, found_label.len() - expected_label.len()) + }; + let mut msg: Vec<_> = vec![( + format!("{}{} `", " ".repeat(expected_padding), expected_label), + Style::NoStyle, + )]; msg.extend(expected.0.iter() - .map(|x| match *x { - StringPart::Normal(ref s) => (s.to_owned(), Style::NoStyle), - StringPart::Highlighted(ref s) => (s.to_owned(), Style::Highlight), - })); + .map(|x| match *x { + StringPart::Normal(ref s) => (s.to_owned(), Style::NoStyle), + StringPart::Highlighted(ref s) => (s.to_owned(), Style::Highlight), + })); msg.push((format!("`{}\n", expected_extra), Style::NoStyle)); - msg.push((format!(" found {} `", label), Style::NoStyle)); + msg.push((format!("{}{} `", " ".repeat(found_padding), found_label), Style::NoStyle)); msg.extend(found.0.iter() - .map(|x| match *x { - StringPart::Normal(ref s) => (s.to_owned(), Style::NoStyle), - StringPart::Highlighted(ref s) => (s.to_owned(), Style::Highlight), - })); + .map(|x| match *x { + StringPart::Normal(ref s) => (s.to_owned(), Style::NoStyle), + StringPart::Highlighted(ref s) => (s.to_owned(), Style::Highlight), + })); msg.push((format!("`{}", found_extra), Style::NoStyle)); // For now, just attach these as notes diff --git a/src/librustc_errors/diagnostic_builder.rs b/src/librustc_errors/diagnostic_builder.rs index 40642dd14b8..a95c29f8c27 100644 --- a/src/librustc_errors/diagnostic_builder.rs +++ b/src/librustc_errors/diagnostic_builder.rs @@ -195,37 +195,44 @@ impl<'a> DiagnosticBuilder<'a> { self } - forward!(pub fn note_expected_found(&mut self, - label: &dyn fmt::Display, - expected: DiagnosticStyledString, - found: DiagnosticStyledString, - ) -> &mut Self); - - forward!(pub fn note_expected_found_extra(&mut self, - label: &dyn fmt::Display, - expected: DiagnosticStyledString, - found: DiagnosticStyledString, - expected_extra: &dyn fmt::Display, - found_extra: &dyn fmt::Display, - ) -> &mut Self); - - forward!(pub fn note_unsuccessfull_coercion(&mut self, - expected: DiagnosticStyledString, - found: DiagnosticStyledString, - ) -> &mut Self); + forward!(pub fn note_expected_found( + &mut self, + expected_label: &dyn fmt::Display, + expected: DiagnosticStyledString, + found_label: &dyn fmt::Display, + found: DiagnosticStyledString, + ) -> &mut Self); + + forward!(pub fn note_expected_found_extra( + &mut self, + expected_label: &dyn fmt::Display, + expected: DiagnosticStyledString, + found_label: &dyn fmt::Display, + found: DiagnosticStyledString, + expected_extra: &dyn fmt::Display, + found_extra: &dyn fmt::Display, + ) -> &mut Self); + + forward!(pub fn note_unsuccessfull_coercion( + &mut self, + expected: DiagnosticStyledString, + found: DiagnosticStyledString, + ) -> &mut Self); forward!(pub fn note(&mut self, msg: &str) -> &mut Self); - forward!(pub fn span_note<S: Into<MultiSpan>>(&mut self, - sp: S, - msg: &str, - ) -> &mut Self); + forward!(pub fn span_note<S: Into<MultiSpan>>( + &mut self, + sp: S, + msg: &str, + ) -> &mut Self); forward!(pub fn warn(&mut self, msg: &str) -> &mut Self); forward!(pub fn span_warn<S: Into<MultiSpan>>(&mut self, sp: S, msg: &str) -> &mut Self); forward!(pub fn help(&mut self, msg: &str) -> &mut Self); - forward!(pub fn span_help<S: Into<MultiSpan>>(&mut self, - sp: S, - msg: &str, - ) -> &mut Self); + forward!(pub fn span_help<S: Into<MultiSpan>>( + &mut self, + sp: S, + msg: &str, + ) -> &mut Self); pub fn multipart_suggestion( &mut self, |
