Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Distinguish guesses from suggestions #39458

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions src/librustc_borrowck/borrowck/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1008,12 +1008,12 @@ impl<'a, 'tcx> BorrowckCtxt<'a, 'tcx> {
.span_label(err.span,
&format!("may outlive borrowed value {}",
cmt_path_or_string))
.span_suggestion(err.span,
&format!("to force the closure to take ownership of {} \
(and any other referenced variables), \
use the `move` keyword, as shown:",
cmt_path_or_string),
suggestion)
.guess(err.span,
&format!("to force the closure to take ownership of {} \
(and any other referenced variables), \
use the `move` keyword, as shown:",
cmt_path_or_string),
suggestion)
.emit();
}

Expand Down
101 changes: 66 additions & 35 deletions src/librustc_errors/diagnostic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@

use CodeSuggestion;
use Level;
use RenderSpan;
use RenderSpan::Suggestion;
use std::fmt;
use std::{fmt, iter};
use syntax_pos::{MultiSpan, Span};
use snippet::Style;

Expand All @@ -24,6 +22,19 @@ pub struct Diagnostic {
pub code: Option<String>,
pub span: MultiSpan,
pub children: Vec<SubDiagnostic>,
pub code_hints: Option<DiagnosticCodeHint>,
}

#[derive(Clone, Debug, PartialEq)]
pub enum DiagnosticCodeHint {
Suggestion {
msg: String,
sugg: CodeSuggestion,
},
Guesses {
msg: String,
guesses: Vec<CodeSuggestion>,
},
}

/// For example a note attached to an error.
Expand All @@ -32,7 +43,7 @@ pub struct SubDiagnostic {
pub level: Level,
pub message: Vec<(String, Style)>,
pub span: MultiSpan,
pub render_span: Option<RenderSpan>,
pub render_span: Option<MultiSpan>,
}

impl Diagnostic {
Expand All @@ -47,6 +58,7 @@ impl Diagnostic {
code: code,
span: MultiSpan::new(),
children: vec![],
code_hints: None,
}
}

Expand Down Expand Up @@ -109,67 +121,90 @@ impl Diagnostic {
}

pub fn note(&mut self, msg: &str) -> &mut Self {
self.sub(Level::Note, msg, MultiSpan::new(), None);
self.sub(Level::Note, msg, MultiSpan::new());
self
}

pub fn highlighted_note(&mut self, msg: Vec<(String, Style)>) -> &mut Self {
self.sub_with_highlights(Level::Note, msg, MultiSpan::new(), None);
self.sub_with_highlights(Level::Note, msg, MultiSpan::new());
self
}

pub fn span_note<S: Into<MultiSpan>>(&mut self,
sp: S,
msg: &str)
-> &mut Self {
self.sub(Level::Note, msg, sp.into(), None);
self.sub(Level::Note, msg, sp.into());
self
}

pub fn warn(&mut self, msg: &str) -> &mut Self {
self.sub(Level::Warning, msg, MultiSpan::new(), None);
self.sub(Level::Warning, msg, MultiSpan::new());
self
}

pub fn span_warn<S: Into<MultiSpan>>(&mut self,
sp: S,
msg: &str)
-> &mut Self {
self.sub(Level::Warning, msg, sp.into(), None);
self.sub(Level::Warning, msg, sp.into());
self
}

pub fn help(&mut self , msg: &str) -> &mut Self {
self.sub(Level::Help, msg, MultiSpan::new(), None);
self.sub(Level::Help, msg, MultiSpan::new());
self
}

pub fn span_help<S: Into<MultiSpan>>(&mut self,
sp: S,
msg: &str)
-> &mut Self {
self.sub(Level::Help, msg, sp.into(), None);
self.sub(Level::Help, msg, sp.into());
self
}

/// Prints out a message with a suggested edit of the code.
///
/// See `diagnostic::RenderSpan::Suggestion` for more information.
pub fn span_suggestion<S: Into<MultiSpan>>(&mut self,
sp: S,
msg: &str,
suggestion: String)
-> &mut Self {
self.sub(Level::Help,
msg,
MultiSpan::new(),
Some(Suggestion(CodeSuggestion {
msp: sp.into(),
substitutes: vec![suggestion],
})));
pub fn span_suggestion(&mut self, sp: Span, msg: &str, suggestion: String) -> &mut Self {
assert!(self.code_hints.is_none(),
"use guesses to assign multiple suggestions to an error");
self.code_hints = Some(DiagnosticCodeHint::Suggestion {
sugg: CodeSuggestion {
msp: sp.into(),
substitutes: vec![suggestion],
},
msg: msg.to_owned(),
});
self
}

/// Prints out a message with one or multiple suggested edits of the
/// code, which may break the code, require manual intervention
/// or be plain out wrong, but might possibly be the correct solution.
///
/// See `diagnostic::RenderSpan::Guesses` for more information.
pub fn guesses<I>(&mut self, sp: Span, msg: &str, guesses: I) -> &mut Self
where I: IntoIterator<Item = String>
{
assert!(self.code_hints.is_none(),
"cannot attach multiple guesses to the same error");
let guesses = guesses.into_iter().map(|guess| CodeSuggestion {
msp: sp.into(),
substitutes: vec![guess],
}).collect();
self.code_hints = Some(DiagnosticCodeHint::Guesses {
guesses: guesses,
msg: msg.to_owned(),
});
self
}

pub fn guess(&mut self, sp: Span, msg: &str, guess: String) -> &mut Self {
self.guesses(sp, msg, iter::once(guess))
}

pub fn set_span<S: Into<MultiSpan>>(&mut self, sp: S) -> &mut Self {
self.span = sp.into();
self
Expand Down Expand Up @@ -205,29 +240,25 @@ impl Diagnostic {
fn sub(&mut self,
level: Level,
message: &str,
span: MultiSpan,
render_span: Option<RenderSpan>) {
let sub = SubDiagnostic {
level: level,
message: vec![(message.to_owned(), Style::NoStyle)],
span: span,
render_span: render_span,
};
self.children.push(sub);
span: MultiSpan) {
self.sub_with_highlights(
level,
vec![(message.to_owned(), Style::NoStyle)],
span,
);
}

/// Convenience function for internal use, clients should use one of the
/// public methods above.
fn sub_with_highlights(&mut self,
level: Level,
message: Vec<(String, Style)>,
span: MultiSpan,
render_span: Option<RenderSpan>) {
span: MultiSpan) {
let sub = SubDiagnostic {
level: level,
message: message,
span: span,
render_span: render_span,
render_span: None,
};
self.children.push(sub);
}
Expand Down
18 changes: 13 additions & 5 deletions src/librustc_errors/diagnostic_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,20 +139,28 @@ impl<'a> DiagnosticBuilder<'a> {
sp: S,
msg: &str)
-> &mut Self);
forward!(pub fn span_suggestion<S: Into<MultiSpan>>(&mut self,
sp: S,
msg: &str,
suggestion: String)
-> &mut Self);
forward!(pub fn span_suggestion(&mut self,
sp: Span,
msg: &str,
suggestion: String)
-> &mut Self);
forward!(pub fn set_span<S: Into<MultiSpan>>(&mut self, sp: S) -> &mut Self);
forward!(pub fn code(&mut self, s: String) -> &mut Self);
forward!(pub fn guess(&mut self, sp: Span, msg: &str, guess: String) -> &mut Self);

/// Convenience function for internal use, clients should use one of the
/// struct_* methods on Handler.
pub fn new(handler: &'a Handler, level: Level, message: &str) -> DiagnosticBuilder<'a> {
DiagnosticBuilder::new_with_code(handler, level, None, message)
}

pub fn guesses<I>(&mut self, sp: Span, msg: &str, guesses: I) -> &mut Self
where I: IntoIterator<Item = String>
{
self.diagnostic.guesses(sp, msg, guesses);
self
}

/// Convenience function for internal use, clients should use one of the
/// struct_* methods on Handler.
pub fn new_with_code(handler: &'a Handler,
Expand Down
Loading