Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 0fe5266

Browse files
committed
Auto merge of rust-lang#15087 - matklad:error-handling, r=matklad
internal: use consistent style for error handling
2 parents 93a788c + 6303551 commit 0fe5266

22 files changed

+128
-134
lines changed

crates/paths/src/lib.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
use std::{
77
borrow::Borrow,
88
ffi::OsStr,
9-
ops,
9+
fmt, ops,
1010
path::{Component, Path, PathBuf},
1111
};
1212

@@ -95,6 +95,12 @@ impl AbsPathBuf {
9595
}
9696
}
9797

98+
impl fmt::Display for AbsPathBuf {
99+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
100+
fmt::Display::fmt(&self.0.display(), f)
101+
}
102+
}
103+
98104
/// Wrapper around an absolute [`Path`].
99105
#[derive(Debug, Ord, PartialOrd, Eq, PartialEq, Hash)]
100106
#[repr(transparent)]
@@ -217,6 +223,7 @@ impl AbsPath {
217223
pub fn as_os_str(&self) -> &OsStr {
218224
self.0.as_os_str()
219225
}
226+
#[deprecated(note = "use Display instead")]
220227
pub fn display(&self) -> std::path::Display<'_> {
221228
self.0.display()
222229
}
@@ -227,6 +234,12 @@ impl AbsPath {
227234
// endregion:delegate-methods
228235
}
229236

237+
impl fmt::Display for AbsPath {
238+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
239+
fmt::Display::fmt(&self.0.display(), f)
240+
}
241+
}
242+
230243
/// Wrapper around a relative [`PathBuf`].
231244
#[derive(Debug, Clone, Ord, PartialOrd, Eq, PartialEq, Hash)]
232245
pub struct RelPathBuf(PathBuf);

crates/project-model/src/build_scripts.rs

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -225,9 +225,8 @@ impl WorkspaceBuildScripts {
225225
let package_build_data = &mut res[idx].outputs[package];
226226
if !package_build_data.is_unchanged() {
227227
tracing::info!(
228-
"{}: {:?}",
229-
workspace[package].manifest.parent().display(),
230-
package_build_data,
228+
"{}: {package_build_data:?}",
229+
workspace[package].manifest.parent(),
231230
);
232231
}
233232
}
@@ -270,9 +269,8 @@ impl WorkspaceBuildScripts {
270269
let package_build_data = &outputs[package];
271270
if !package_build_data.is_unchanged() {
272271
tracing::info!(
273-
"{}: {:?}",
274-
workspace[package].manifest.parent().display(),
275-
package_build_data,
272+
"{}: {package_build_data:?}",
273+
workspace[package].manifest.parent(),
276274
);
277275
}
278276
}
@@ -424,7 +422,7 @@ impl WorkspaceBuildScripts {
424422

425423
let target_libdir = AbsPathBuf::try_from(PathBuf::from(target_libdir))
426424
.map_err(|_| anyhow::format_err!("target-libdir was not an absolute path"))?;
427-
tracing::info!("Loading rustc proc-macro paths from {}", target_libdir.display());
425+
tracing::info!("Loading rustc proc-macro paths from {target_libdir}");
428426

429427
let proc_macro_dylibs: Vec<(String, AbsPathBuf)> = std::fs::read_dir(target_libdir)?
430428
.filter_map(|entry| {
@@ -458,9 +456,8 @@ impl WorkspaceBuildScripts {
458456
let package_build_data = &bs.outputs[package];
459457
if !package_build_data.is_unchanged() {
460458
tracing::info!(
461-
"{}: {:?}",
462-
rustc[package].manifest.parent().display(),
463-
package_build_data,
459+
"{}: {package_build_data:?}",
460+
rustc[package].manifest.parent(),
464461
);
465462
}
466463
}

crates/project-model/src/cargo_workspace.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use std::path::PathBuf;
44
use std::str::from_utf8;
55
use std::{ops, process::Command};
66

7-
use anyhow::{Context, Result};
7+
use anyhow::Context;
88
use base_db::Edition;
99
use cargo_metadata::{CargoOpt, MetadataCommand};
1010
use la_arena::{Arena, Idx};
@@ -236,7 +236,7 @@ impl CargoWorkspace {
236236
current_dir: &AbsPath,
237237
config: &CargoConfig,
238238
progress: &dyn Fn(String),
239-
) -> Result<cargo_metadata::Metadata> {
239+
) -> anyhow::Result<cargo_metadata::Metadata> {
240240
let targets = find_list_of_build_targets(config, cargo_toml);
241241

242242
let mut meta = MetadataCommand::new();

crates/project-model/src/lib.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ use std::{
3737
process::Command,
3838
};
3939

40-
use anyhow::{bail, format_err, Context, Result};
40+
use anyhow::{bail, format_err, Context};
4141
use paths::{AbsPath, AbsPathBuf};
4242
use rustc_hash::FxHashSet;
4343

@@ -60,19 +60,19 @@ pub enum ProjectManifest {
6060
}
6161

6262
impl ProjectManifest {
63-
pub fn from_manifest_file(path: AbsPathBuf) -> Result<ProjectManifest> {
63+
pub fn from_manifest_file(path: AbsPathBuf) -> anyhow::Result<ProjectManifest> {
6464
let path = ManifestPath::try_from(path)
65-
.map_err(|path| format_err!("bad manifest path: {}", path.display()))?;
65+
.map_err(|path| format_err!("bad manifest path: {path}"))?;
6666
if path.file_name().unwrap_or_default() == "rust-project.json" {
6767
return Ok(ProjectManifest::ProjectJson(path));
6868
}
6969
if path.file_name().unwrap_or_default() == "Cargo.toml" {
7070
return Ok(ProjectManifest::CargoToml(path));
7171
}
72-
bail!("project root must point to Cargo.toml or rust-project.json: {}", path.display());
72+
bail!("project root must point to Cargo.toml or rust-project.json: {path}");
7373
}
7474

75-
pub fn discover_single(path: &AbsPath) -> Result<ProjectManifest> {
75+
pub fn discover_single(path: &AbsPath) -> anyhow::Result<ProjectManifest> {
7676
let mut candidates = ProjectManifest::discover(path)?;
7777
let res = match candidates.pop() {
7878
None => bail!("no projects"),
@@ -156,7 +156,7 @@ impl fmt::Display for ProjectManifest {
156156
}
157157
}
158158

159-
fn utf8_stdout(mut cmd: Command) -> Result<String> {
159+
fn utf8_stdout(mut cmd: Command) -> anyhow::Result<String> {
160160
let output = cmd.output().with_context(|| format!("{cmd:?} failed"))?;
161161
if !output.status.success() {
162162
match String::from_utf8(output.stderr) {

crates/project-model/src/manifest_path.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ impl ManifestPath {
4242

4343
impl fmt::Display for ManifestPath {
4444
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
45-
fmt::Display::fmt(&self.file.display(), f)
45+
fmt::Display::fmt(&self.file, f)
4646
}
4747
}
4848

crates/project-model/src/rustc_cfg.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
33
use std::process::Command;
44

5-
use anyhow::Result;
65
use rustc_hash::FxHashMap;
76

87
use crate::{cfg_flag::CfgFlag, utf8_stdout, ManifestPath};
@@ -44,7 +43,7 @@ fn get_rust_cfgs(
4443
cargo_toml: Option<&ManifestPath>,
4544
target: Option<&str>,
4645
extra_env: &FxHashMap<String, String>,
47-
) -> Result<String> {
46+
) -> anyhow::Result<String> {
4847
if let Some(cargo_toml) = cargo_toml {
4948
let mut cargo_config = Command::new(toolchain::cargo());
5049
cargo_config.envs(extra_env);

crates/project-model/src/sysroot.rs

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,8 @@ impl Sysroot {
8585
" try running `rustup component add rust-src` to possible fix this"
8686
};
8787
Some(format!(
88-
"could not find libcore in loaded sysroot at `{}`{}",
89-
self.src_root.as_path().display(),
90-
var_note,
88+
"could not find libcore in loaded sysroot at `{}`{var_note}",
89+
self.src_root.as_path(),
9190
))
9291
} else {
9392
None
@@ -99,7 +98,7 @@ impl Sysroot {
9998
impl Sysroot {
10099
/// Attempts to discover the toolchain's sysroot from the given `dir`.
101100
pub fn discover(dir: &AbsPath, extra_env: &FxHashMap<String, String>) -> Result<Sysroot> {
102-
tracing::debug!("discovering sysroot for {}", dir.display());
101+
tracing::debug!("discovering sysroot for {dir}");
103102
let sysroot_dir = discover_sysroot_dir(dir, extra_env)?;
104103
let sysroot_src_dir =
105104
discover_sysroot_src_dir_or_add_component(&sysroot_dir, dir, extra_env)?;
@@ -111,7 +110,7 @@ impl Sysroot {
111110
extra_env: &FxHashMap<String, String>,
112111
src: AbsPathBuf,
113112
) -> Result<Sysroot> {
114-
tracing::debug!("discovering sysroot for {}", current_dir.display());
113+
tracing::debug!("discovering sysroot for {current_dir}");
115114
let sysroot_dir = discover_sysroot_dir(current_dir, extra_env)?;
116115
Ok(Sysroot::load(sysroot_dir, src))
117116
}
@@ -122,7 +121,7 @@ impl Sysroot {
122121

123122
pub fn with_sysroot_dir(sysroot_dir: AbsPathBuf) -> Result<Sysroot> {
124123
let sysroot_src_dir = discover_sysroot_src_dir(&sysroot_dir).ok_or_else(|| {
125-
format_err!("can't load standard library from sysroot path {}", sysroot_dir.display())
124+
format_err!("can't load standard library from sysroot path {sysroot_dir}")
126125
})?;
127126
Ok(Sysroot::load(sysroot_dir, sysroot_src_dir))
128127
}
@@ -220,10 +219,10 @@ fn discover_sysroot_src_dir(sysroot_path: &AbsPathBuf) -> Option<AbsPathBuf> {
220219
if let Ok(path) = AbsPathBuf::try_from(path.as_str()) {
221220
let core = path.join("core");
222221
if fs::metadata(&core).is_ok() {
223-
tracing::debug!("Discovered sysroot by RUST_SRC_PATH: {}", path.display());
222+
tracing::debug!("Discovered sysroot by RUST_SRC_PATH: {path}");
224223
return Some(path);
225224
}
226-
tracing::debug!("RUST_SRC_PATH is set, but is invalid (no core: {:?}), ignoring", core);
225+
tracing::debug!("RUST_SRC_PATH is set, but is invalid (no core: {core:?}), ignoring");
227226
} else {
228227
tracing::debug!("RUST_SRC_PATH is set, but is invalid, ignoring");
229228
}
@@ -250,18 +249,17 @@ fn discover_sysroot_src_dir_or_add_component(
250249
format_err!(
251250
"\
252251
can't load standard library from sysroot
253-
{}
252+
{sysroot_path}
254253
(discovered via `rustc --print sysroot`)
255254
try installing the Rust source the same way you installed rustc",
256-
sysroot_path.display(),
257255
)
258256
})
259257
}
260258

261259
fn get_rustc_src(sysroot_path: &AbsPath) -> Option<ManifestPath> {
262260
let rustc_src = sysroot_path.join("lib/rustlib/rustc-src/rust/compiler/rustc/Cargo.toml");
263261
let rustc_src = ManifestPath::try_from(rustc_src).ok()?;
264-
tracing::debug!("checking for rustc source code: {}", rustc_src.display());
262+
tracing::debug!("checking for rustc source code: {rustc_src}");
265263
if fs::metadata(&rustc_src).is_ok() {
266264
Some(rustc_src)
267265
} else {
@@ -271,7 +269,7 @@ fn get_rustc_src(sysroot_path: &AbsPath) -> Option<ManifestPath> {
271269

272270
fn get_rust_src(sysroot_path: &AbsPath) -> Option<AbsPathBuf> {
273271
let rust_src = sysroot_path.join("lib/rustlib/src/rust/library");
274-
tracing::debug!("checking sysroot library: {}", rust_src.display());
272+
tracing::debug!("checking sysroot library: {rust_src}");
275273
if fs::metadata(&rust_src).is_ok() {
276274
Some(rust_src)
277275
} else {

crates/project-model/src/target_data_layout.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
//! Runs `rustc --print target-spec-json` to get the target_data_layout.
22
use std::process::Command;
33

4-
use anyhow::Result;
54
use rustc_hash::FxHashMap;
65

76
use crate::{utf8_stdout, ManifestPath};
@@ -10,7 +9,7 @@ pub fn get(
109
cargo_toml: Option<&ManifestPath>,
1110
target: Option<&str>,
1211
extra_env: &FxHashMap<String, String>,
13-
) -> Result<String> {
12+
) -> anyhow::Result<String> {
1413
let output = (|| {
1514
if let Some(cargo_toml) = cargo_toml {
1615
let mut cmd = Command::new(toolchain::rustc());

0 commit comments

Comments
 (0)