Skip to content

Commit 5b0b080

Browse files
committed
show span-only suggestions when source is unavailable in ui tests
1 parent a06c1ca commit 5b0b080

8 files changed

Lines changed: 100 additions & 1 deletion

File tree

compiler/rustc_errors/src/annotate_snippet_emitter_writer.rs

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ pub struct AnnotateSnippetEmitter {
4747
track_diagnostics: bool,
4848
terminal_url: TerminalUrl,
4949
theme: OutputTheme,
50+
show_suggestions_with_unavailable_source: bool,
5051
}
5152

5253
impl Debug for AnnotateSnippetEmitter {
@@ -63,6 +64,10 @@ impl Debug for AnnotateSnippetEmitter {
6364
.field("track_diagnostics", &self.track_diagnostics)
6465
.field("terminal_url", &self.terminal_url)
6566
.field("theme", &self.theme)
67+
.field(
68+
"show_suggestions_with_unavailable_source",
69+
&self.show_suggestions_with_unavailable_source,
70+
)
6671
.finish()
6772
}
6873
}
@@ -136,6 +141,7 @@ impl AnnotateSnippetEmitter {
136141
track_diagnostics: false,
137142
terminal_url: TerminalUrl::No,
138143
theme: OutputTheme::Ascii,
144+
show_suggestions_with_unavailable_source: false,
139145
}
140146
}
141147

@@ -307,6 +313,12 @@ impl AnnotateSnippetEmitter {
307313
SuggestionStyle::HideCodeInline
308314
| SuggestionStyle::ShowCode
309315
| SuggestionStyle::ShowAlways => {
316+
// Get the original unavailable spans before `suggestion` is consumed.
317+
let unavailable_source_span = if self.show_suggestions_with_unavailable_source {
318+
self.suggestion_span_with_unavailable_source(sm, &suggestion)
319+
} else {
320+
None
321+
};
310322
let substitutions = suggestion
311323
.substitutions
312324
.into_iter()
@@ -356,6 +368,30 @@ impl AnnotateSnippetEmitter {
356368
.collect::<Vec<_>>();
357369

358370
if substitutions.is_empty() {
371+
if let Some(span) = unavailable_source_span {
372+
let msg = format_diag_message(&suggestion.msg, args).to_string();
373+
report.push(std::mem::replace(
374+
&mut group,
375+
Group::with_title(
376+
annotate_snippets::Level::HELP.secondary_title(msg),
377+
),
378+
));
379+
380+
let file_ann = collect_annotations(args, &span, sm);
381+
let level = annotate_snippets::Level::HELP;
382+
for (file_idx, (file, annotations)) in file_ann.into_iter().enumerate()
383+
{
384+
group = self.unannotated_messages(
385+
annotations,
386+
&file.name,
387+
sm,
388+
file_idx,
389+
&mut report,
390+
group,
391+
&level,
392+
);
393+
}
394+
}
359395
continue;
360396
}
361397
let mut msg = format_diag_message(&suggestion.msg, args).to_string();
@@ -645,6 +681,34 @@ impl AnnotateSnippetEmitter {
645681
}
646682
group
647683
}
684+
685+
fn suggestion_span_with_unavailable_source(
686+
&self,
687+
sm: &Arc<SourceMap>,
688+
suggestion: &CodeSuggestion,
689+
) -> Option<MultiSpan> {
690+
// These spans cannot be rendered as source patches because their source
691+
// files are unavailable, but can still be shown as locations.
692+
let spans = suggestion
693+
.substitutions
694+
.iter()
695+
.flat_map(|subst| &subst.parts)
696+
.filter_map(|part| {
697+
if sm.is_valid_span(part.span).is_err() {
698+
debug!("suggestion contains an invalid span: {:?}", part);
699+
return None;
700+
}
701+
let lines = sm.span_to_lines(part.span).ok()?;
702+
if sm.ensure_source_file_source_present(&lines.file) {
703+
None
704+
} else {
705+
Some(part.span)
706+
}
707+
})
708+
.collect::<Vec<_>>();
709+
710+
if spans.is_empty() { None } else { Some(MultiSpan::from_spans(spans)) }
711+
}
648712
}
649713

650714
fn emit_to_destination(

compiler/rustc_errors/src/json.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ pub struct JsonEmitter {
5454
macro_backtrace: bool,
5555
track_diagnostics: bool,
5656
terminal_url: TerminalUrl,
57+
show_suggestions_with_unavailable_source: bool,
5758
}
5859

5960
impl JsonEmitter {
@@ -76,6 +77,7 @@ impl JsonEmitter {
7677
macro_backtrace: false,
7778
track_diagnostics: false,
7879
terminal_url: TerminalUrl::No,
80+
show_suggestions_with_unavailable_source: false,
7981
}
8082
}
8183

@@ -373,6 +375,7 @@ impl Diagnostic {
373375
.terminal_url(je.terminal_url)
374376
.ui_testing(je.ui_testing)
375377
.ignored_directories_in_source_blocks(je.ignored_directories_in_source_blocks.clone())
378+
.show_suggestions_with_unavailable_source(je.show_suggestions_with_unavailable_source)
376379
.theme(if je.json_rendered.unicode { OutputTheme::Unicode } else { OutputTheme::Ascii })
377380
.emit_diagnostic(diag);
378381

compiler/rustc_session/src/options.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2629,6 +2629,8 @@ written to standard error output)"),
26292629
"make the current crate share its generic instantiations"),
26302630
shell_argfiles: bool = (false, parse_bool, [UNTRACKED],
26312631
"allow argument files to be specified with POSIX \"shell-style\" argument quoting"),
2632+
show_suggestions_with_unavailable_source: bool = (false, parse_bool, [UNTRACKED],
2633+
"show span-only suggestions when the source code is unavailable"),
26322634
simulate_remapped_rust_src_base: Option<PathBuf> = (None, parse_opt_pathbuf, [TRACKED],
26332635
"simulate the effect of remap-debuginfo = true at bootstrapping by remapping path \
26342636
to rust's source base directory. only meant for testing purposes"),

compiler/rustc_session/src/session.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -947,6 +947,8 @@ impl Session {
947947
fn default_emitter(sopts: &config::Options, source_map: Arc<SourceMap>) -> Box<DynEmitter> {
948948
let macro_backtrace = sopts.unstable_opts.macro_backtrace;
949949
let track_diagnostics = sopts.unstable_opts.track_diagnostics;
950+
let show_suggestions_with_unavailable_source =
951+
sopts.unstable_opts.show_suggestions_with_unavailable_source;
950952
let terminal_url = match sopts.unstable_opts.terminal_urls {
951953
TerminalUrl::Auto => {
952954
match (std::env::var("COLORTERM").as_deref(), std::env::var("TERM").as_deref()) {
@@ -974,6 +976,9 @@ fn default_emitter(sopts: &config::Options, source_map: Arc<SourceMap>) -> Box<D
974976
.track_diagnostics(track_diagnostics)
975977
.terminal_url(terminal_url)
976978
.theme(if unicode { OutputTheme::Unicode } else { OutputTheme::Ascii })
979+
.show_suggestions_with_unavailable_source(
980+
show_suggestions_with_unavailable_source,
981+
)
977982
.ignored_directories_in_source_blocks(
978983
sopts.unstable_opts.ignore_directory_in_diagnostics_source_blocks.clone(),
979984
);
@@ -995,7 +1000,8 @@ fn default_emitter(sopts: &config::Options, source_map: Arc<SourceMap>) -> Box<D
9951000
.diagnostic_width(sopts.diagnostic_width)
9961001
.macro_backtrace(macro_backtrace)
9971002
.track_diagnostics(track_diagnostics)
998-
.terminal_url(terminal_url),
1003+
.terminal_url(terminal_url)
1004+
.show_suggestions_with_unavailable_source(show_suggestions_with_unavailable_source),
9991005
),
10001006
}
10011007
}

src/tools/compiletest/src/runtest.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1704,6 +1704,7 @@ impl<'test> TestCx<'test> {
17041704
compiler.arg("-Ccodegen-units=1");
17051705
// Hide line numbers to reduce churn
17061706
compiler.arg("-Zui-testing");
1707+
compiler.arg("-Zshow-suggestions-with-unavailable-source");
17071708
compiler.arg("-Zdeduplicate-diagnostics=no");
17081709
compiler.arg("-Zwrite-long-types-to-disk=no");
17091710
// FIXME: use this for other modes too, for perf?

tests/ui/consts/const_in_pattern/suggest_equality_comparison_instead_of_pattern_matching.stderr

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,8 @@ LL | if let V = vec![] {}
147147
= note: `Vec<()>` is not usable in patterns
148148
|
149149
= note: see https://doc.rust-lang.org/stable/std/marker/trait.StructuralPartialEq.html for details
150+
help: check for equality instead of pattern matching
151+
--> $SRC_DIR/alloc/src/macros.rs:LL:COL
150152

151153
error: constant of non-structural type `Vec<()>` in a pattern
152154
--> $DIR/suggest_equality_comparison_instead_of_pattern_matching.rs:28:13

tests/ui/pin-ergonomics/pinned-drop-check.stderr

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,36 +283,54 @@ error[E0040]: explicit use of destructor method
283283
|
284284
LL | Drop::pin_drop(todo!());
285285
| ^^^^^^^^^^^^^^ explicit destructor calls not allowed
286+
|
287+
help: consider using `drop` function
288+
--> $SRC_DIR/core/src/macros/mod.rs:LL:COL
286289

287290
error[E0040]: explicit use of destructor method
288291
--> $DIR/pinned-drop-check.rs:156:13
289292
|
290293
LL | Drop::pin_drop(todo!());
291294
| ^^^^^^^^^^^^^^ explicit destructor calls not allowed
295+
|
296+
help: consider using `drop` function
297+
--> $SRC_DIR/core/src/macros/mod.rs:LL:COL
292298

293299
error[E0040]: explicit use of destructor method
294300
--> $DIR/pinned-drop-check.rs:168:13
295301
|
296302
LL | Drop::drop(todo!());
297303
| ^^^^^^^^^^ explicit destructor calls not allowed
304+
|
305+
help: consider using `drop` function
306+
--> $SRC_DIR/core/src/macros/mod.rs:LL:COL
298307

299308
error[E0040]: explicit use of destructor method
300309
--> $DIR/pinned-drop-check.rs:173:13
301310
|
302311
LL | Drop::drop(todo!());
303312
| ^^^^^^^^^^ explicit destructor calls not allowed
313+
|
314+
help: consider using `drop` function
315+
--> $SRC_DIR/core/src/macros/mod.rs:LL:COL
304316

305317
error[E0040]: explicit use of destructor method
306318
--> $DIR/pinned-drop-check.rs:183:13
307319
|
308320
LL | Drop::drop(todo!());
309321
| ^^^^^^^^^^ explicit destructor calls not allowed
322+
|
323+
help: consider using `drop` function
324+
--> $SRC_DIR/core/src/macros/mod.rs:LL:COL
310325

311326
error[E0040]: explicit use of destructor method
312327
--> $DIR/pinned-drop-check.rs:184:13
313328
|
314329
LL | Drop::pin_drop(todo!());
315330
| ^^^^^^^^^^^^^^ explicit destructor calls not allowed
331+
|
332+
help: consider using `drop` function
333+
--> $SRC_DIR/core/src/macros/mod.rs:LL:COL
316334

317335
error: aborting due to 25 previous errors
318336

tests/ui/structs/ice-line-bounds-issue-148684.stderr

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ LL | | }
99
...
1010
LL | A(2, vec![])
1111
| ^^^^^^^^^^^^
12+
|
13+
help: use struct literal syntax instead of calling
14+
--> $SRC_DIR/alloc/src/macros.rs:LL:COL
1215

1316
error: aborting due to 1 previous error
1417

0 commit comments

Comments
 (0)