Skip to content

Commit 29407d0

Browse files
committed
Only compute failed_scrape_diagnostic when needed
1 parent de34d60 commit 29407d0

File tree

1 file changed

+33
-25
lines changed

1 file changed

+33
-25
lines changed

src/cargo/core/compiler/mod.rs

Lines changed: 33 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ pub mod unit_graph;
2525
use std::collections::{HashMap, HashSet};
2626
use std::env;
2727
use std::ffi::{OsStr, OsString};
28+
use std::fmt::Display;
2829
use std::fs::{self, File};
2930
use std::io::{BufRead, Write};
3031
use std::path::{Path, PathBuf};
@@ -209,12 +210,16 @@ fn compile<'cfg>(
209210

210211
/// Generates the warning message used when fallible doc-scrape units fail,
211212
/// either for rustdoc or rustc.
212-
fn make_failed_scrape_diagnostic(cx: &Context<'_, '_>, unit: &Unit, top_line: String) -> String {
213+
fn make_failed_scrape_diagnostic(
214+
cx: &Context<'_, '_>,
215+
unit: &Unit,
216+
top_line: impl Display,
217+
) -> String {
213218
let manifest_path = unit.pkg.manifest_path();
214219
let relative_manifest_path = manifest_path
215220
.strip_prefix(cx.bcx.ws.root())
216-
.unwrap_or(&manifest_path)
217-
.to_owned();
221+
.unwrap_or(&manifest_path);
222+
218223
format!(
219224
"\
220225
{top_line}
@@ -282,21 +287,22 @@ fn rustc(cx: &mut Context<'_, '_>, unit: &Unit, exec: &Arc<dyn Executor>) -> Car
282287
let is_local = unit.is_local();
283288
let artifact = unit.artifact;
284289

285-
// If this unit is needed for doc-scraping, then we generate a diagnostic that
286-
// describes the set of reverse-dependencies that cause the unit to be needed.
287-
let target_desc = unit.target.description_named();
288-
let mut for_scrape_units = cx
289-
.bcx
290-
.scrape_units_have_dep_on(unit)
291-
.into_iter()
292-
.map(|unit| unit.target.description_named())
293-
.collect::<Vec<_>>();
294-
for_scrape_units.sort();
295-
let for_scrape_units = for_scrape_units.join(", ");
296-
let failed_scrape_diagnostic = make_failed_scrape_diagnostic(cx, unit, format!("failed to check {target_desc} in package `{name}` as a prerequisite for scraping examples from: {for_scrape_units}"));
297-
298290
let hide_diagnostics_for_scrape_unit = cx.bcx.unit_can_fail_for_docscraping(unit)
299291
&& !matches!(cx.bcx.config.shell().verbosity(), Verbosity::Verbose);
292+
let failed_scrape_diagnostic = hide_diagnostics_for_scrape_unit.then(|| {
293+
// If this unit is needed for doc-scraping, then we generate a diagnostic that
294+
// describes the set of reverse-dependencies that cause the unit to be needed.
295+
let target_desc = unit.target.description_named();
296+
let mut for_scrape_units = cx
297+
.bcx
298+
.scrape_units_have_dep_on(unit)
299+
.into_iter()
300+
.map(|unit| unit.target.description_named())
301+
.collect::<Vec<_>>();
302+
for_scrape_units.sort();
303+
let for_scrape_units = for_scrape_units.join(", ");
304+
make_failed_scrape_diagnostic(cx, unit, format_args!("failed to check {target_desc} in package `{name}` as a prerequisite for scraping examples from: {for_scrape_units}"))
305+
});
300306
if hide_diagnostics_for_scrape_unit {
301307
output_options.show_diagnostics = false;
302308
}
@@ -410,8 +416,8 @@ fn rustc(cx: &mut Context<'_, '_>, unit: &Unit, exec: &Arc<dyn Executor>) -> Car
410416
});
411417

412418
if let Err(e) = result {
413-
if hide_diagnostics_for_scrape_unit {
414-
state.warning(failed_scrape_diagnostic)?;
419+
if let Some(diagnostic) = failed_scrape_diagnostic {
420+
state.warning(diagnostic)?;
415421
}
416422

417423
return Err(e);
@@ -766,11 +772,13 @@ fn rustdoc(cx: &mut Context<'_, '_>, unit: &Unit) -> CargoResult<Work> {
766772
let failed_scrape_units = Arc::clone(&cx.failed_scrape_units);
767773
let hide_diagnostics_for_scrape_unit = cx.bcx.unit_can_fail_for_docscraping(unit)
768774
&& !matches!(cx.bcx.config.shell().verbosity(), Verbosity::Verbose);
769-
let failed_scrape_diagnostic = make_failed_scrape_diagnostic(
770-
cx,
771-
unit,
772-
format!("failed to scan {target_desc} in package `{name}` for example code usage"),
773-
);
775+
let failed_scrape_diagnostic = hide_diagnostics_for_scrape_unit.then(|| {
776+
make_failed_scrape_diagnostic(
777+
cx,
778+
unit,
779+
format_args!("failed to scan {target_desc} in package `{name}` for example code usage"),
780+
)
781+
});
774782
if hide_diagnostics_for_scrape_unit {
775783
output_options.show_diagnostics = false;
776784
}
@@ -822,8 +830,8 @@ fn rustdoc(cx: &mut Context<'_, '_>, unit: &Unit) -> CargoResult<Work> {
822830
.with_context(|| format!("could not document `{}`", name));
823831

824832
if let Err(e) = result {
825-
if hide_diagnostics_for_scrape_unit {
826-
state.warning(failed_scrape_diagnostic)?;
833+
if let Some(diagnostic) = failed_scrape_diagnostic {
834+
state.warning(diagnostic)?;
827835
}
828836

829837
return Err(e);

0 commit comments

Comments
 (0)