Skip to content

Commit 84ccbe2

Browse files
committed
librustc_errors: Extract sugg/subst handling into method
An initial refactoring before working on rust-lang#61809. This moves the whole block into a method so that it can be reused in the annotate-snippet output. It's already used in the new emitter, but there's no UI tests with suggestions included in this PR. A first look at some UI tests with suggestions showed that there's some more work to do in [annotate-snippet-rs][annotate-snippet-rs] before the new output is closer to the current one.
1 parent 2d851b3 commit 84ccbe2

File tree

2 files changed

+32
-12
lines changed

2 files changed

+32
-12
lines changed

src/librustc_errors/annotate_snippet_emitter_writer.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,8 @@ pub struct AnnotateSnippetEmitterWriter {
3030
impl Emitter for AnnotateSnippetEmitterWriter {
3131
/// The entry point for the diagnostics generation
3232
fn emit_diagnostic(&mut self, db: &DiagnosticBuilder<'_>) {
33-
let primary_span = db.span.clone();
3433
let children = db.children.clone();
35-
// FIXME(#59346): Collect suggestions (see emitter.rs)
36-
let suggestions: &[_] = &[];
34+
let (primary_span, suggestions) = self.primary_span_formatted(&db);
3735

3836
// FIXME(#59346): Add `fix_multispans_in_std_macros` function from emitter.rs
3937

src/librustc_errors/emitter.rs

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -191,16 +191,25 @@ pub trait Emitter {
191191
fn should_show_explain(&self) -> bool {
192192
true
193193
}
194-
}
195194

196-
impl Emitter for EmitterWriter {
197-
fn emit_diagnostic(&mut self, db: &DiagnosticBuilder<'_>) {
195+
/// Formats the substitutions of the primary_span
196+
///
197+
/// The are a lot of conditions to this method, but in short:
198+
///
199+
/// * If the current `Diagnostic` has only one visible `CodeSuggestion`,
200+
/// we format the `help` suggestion depending on the content of the
201+
/// substitutions. In that case, we return the modified span only.
202+
///
203+
/// * If the current `Diagnostic` has multiple suggestions,
204+
/// we return the original `primary_span` and the original suggestions.
205+
fn primary_span_formatted<'a>(
206+
&mut self,
207+
db: &'a DiagnosticBuilder<'_>
208+
) -> (MultiSpan, &'a [CodeSuggestion]) {
198209
let mut primary_span = db.span.clone();
199-
let mut children = db.children.clone();
200-
let mut suggestions: &[_] = &[];
201-
202210
if let Some((sugg, rest)) = db.suggestions.split_first() {
203211
if rest.is_empty() &&
212+
// ^ if there is only one suggestion
204213
// don't display multi-suggestions as labels
205214
sugg.substitutions.len() == 1 &&
206215
// don't display multipart suggestions as labels
@@ -216,21 +225,34 @@ impl Emitter for EmitterWriter {
216225
{
217226
let substitution = &sugg.substitutions[0].parts[0].snippet.trim();
218227
let msg = if substitution.len() == 0 || sugg.style.hide_inline() {
219-
// This substitution is only removal or we explicitly don't want to show the
220-
// code inline, don't show it
228+
// This substitution is only removal OR we explicitly don't want to show the
229+
// code inline (`hide_inline`). Therefore, we don't show the substitution.
221230
format!("help: {}", sugg.msg)
222231
} else {
232+
// Show the default suggestion text with the substitution
223233
format!("help: {}: `{}`", sugg.msg, substitution)
224234
};
225235
primary_span.push_span_label(sugg.substitutions[0].parts[0].span, msg);
236+
237+
// We return only the modified primary_span
238+
(primary_span, &[])
226239
} else {
227240
// if there are multiple suggestions, print them all in full
228241
// to be consistent. We could try to figure out if we can
229242
// make one (or the first one) inline, but that would give
230243
// undue importance to a semi-random suggestion
231-
suggestions = &db.suggestions;
244+
(primary_span, &db.suggestions)
232245
}
246+
} else {
247+
(primary_span, &db.suggestions)
233248
}
249+
}
250+
}
251+
252+
impl Emitter for EmitterWriter {
253+
fn emit_diagnostic(&mut self, db: &DiagnosticBuilder<'_>) {
254+
let mut children = db.children.clone();
255+
let (mut primary_span, suggestions) = self.primary_span_formatted(&db);
234256

235257
self.fix_multispans_in_std_macros(&mut primary_span,
236258
&mut children,

0 commit comments

Comments
 (0)