Skip to content

Commit 1269793

Browse files
committed
Helper function for resolve_path
1 parent b0696a5 commit 1269793

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
@@ -1227,21 +1227,18 @@ pub fn resolve_path(
12271227
// after macro expansion (that is, they are unhygienic).
12281228
if !path.is_absolute() {
12291229
let callsite = span.source_callsite();
1230-
let mut result = match parse_sess.source_map().span_to_filename(callsite) {
1231-
FileName::Real(name) => name
1232-
.into_local_path()
1233-
.expect("attempting to resolve a file path in an external file"),
1234-
FileName::DocTest(path, _) => path,
1235-
other => {
1236-
return Err(parse_sess.dcx().create_err(errors::ResolveRelativePath {
1237-
span,
1238-
path: parse_sess.source_map().filename_for_diagnostics(&other).to_string(),
1239-
}));
1240-
}
1230+
let source_map = parse_sess.source_map();
1231+
let Some(mut base_path) = source_map.span_to_filename(callsite).into_local_path() else {
1232+
return Err(parse_sess.dcx().create_err(errors::ResolveRelativePath {
1233+
span,
1234+
path: source_map
1235+
.filename_for_diagnostics(&source_map.span_to_filename(callsite))
1236+
.to_string(),
1237+
}));
12411238
};
1242-
result.pop();
1243-
result.push(path);
1244-
Ok(result)
1239+
base_path.pop();
1240+
base_path.push(path);
1241+
Ok(base_path)
12451242
} else {
12461243
Ok(path)
12471244
}

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)