Skip to content
/ rust Public
forked from rust-lang/rust

Commit 1195518

Browse files
committed
Helper function for resolve_path
1 parent 8c9a75b commit 1195518

File tree

2 files changed

+22
-14
lines changed

2 files changed

+22
-14
lines changed

compiler/rustc_expand/src/base.rs

+11-14
Original file line numberDiff line numberDiff line change
@@ -1233,21 +1233,18 @@ pub fn resolve_path(sess: &Session, path: impl Into<PathBuf>, span: Span) -> PRe
12331233
// after macro expansion (that is, they are unhygienic).
12341234
if !path.is_absolute() {
12351235
let callsite = span.source_callsite();
1236-
let mut result = match sess.source_map().span_to_filename(callsite) {
1237-
FileName::Real(name) => name
1238-
.into_local_path()
1239-
.expect("attempting to resolve a file path in an external file"),
1240-
FileName::DocTest(path, _) => path,
1241-
other => {
1242-
return Err(sess.dcx().create_err(errors::ResolveRelativePath {
1243-
span,
1244-
path: sess.source_map().filename_for_diagnostics(&other).to_string(),
1245-
}));
1246-
}
1236+
let source_map = sess.source_map();
1237+
let Some(mut base_path) = source_map.span_to_filename(callsite).into_local_path() else {
1238+
return Err(sess.dcx().create_err(errors::ResolveRelativePath {
1239+
span,
1240+
path: source_map
1241+
.filename_for_diagnostics(&source_map.span_to_filename(callsite))
1242+
.to_string(),
1243+
}));
12471244
};
1248-
result.pop();
1249-
result.push(path);
1250-
Ok(result)
1245+
base_path.pop();
1246+
base_path.push(path);
1247+
Ok(base_path)
12511248
} else {
12521249
Ok(path)
12531250
}

compiler/rustc_span/src/lib.rs

+11
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,17 @@ impl FileName {
427427
src.hash(&mut hasher);
428428
FileName::InlineAsm(hasher.finish())
429429
}
430+
431+
/// Returns the path suitable for reading from the file system on the local host,
432+
/// if this information exists.
433+
/// Avoid embedding this in build artifacts; see `remapped_path_if_available()` for that.
434+
pub fn into_local_path(self) -> Option<PathBuf> {
435+
match self {
436+
FileName::Real(path) => path.into_local_path(),
437+
FileName::DocTest(path, _) => Some(path),
438+
_ => None,
439+
}
440+
}
430441
}
431442

432443
/// Represents a span.

0 commit comments

Comments
 (0)