Skip to content

Commit b2c6b12

Browse files
committed
Helper function for resolve_path
1 parent b6d2d84 commit b2c6b12

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
@@ -1223,21 +1223,18 @@ pub fn resolve_path(sess: &Session, path: impl Into<PathBuf>, span: Span) -> PRe
12231223
// after macro expansion (that is, they are unhygienic).
12241224
if !path.is_absolute() {
12251225
let callsite = span.source_callsite();
1226-
let mut result = match sess.source_map().span_to_filename(callsite) {
1227-
FileName::Real(name) => name
1228-
.into_local_path()
1229-
.expect("attempting to resolve a file path in an external file"),
1230-
FileName::DocTest(path, _) => path,
1231-
other => {
1232-
return Err(sess.dcx().create_err(errors::ResolveRelativePath {
1233-
span,
1234-
path: sess.source_map().filename_for_diagnostics(&other).to_string(),
1235-
}));
1236-
}
1226+
let source_map = sess.source_map();
1227+
let Some(mut base_path) = source_map.span_to_filename(callsite).into_local_path() else {
1228+
return Err(sess.dcx().create_err(errors::ResolveRelativePath {
1229+
span,
1230+
path: source_map
1231+
.filename_for_diagnostics(&source_map.span_to_filename(callsite))
1232+
.to_string(),
1233+
}));
12371234
};
1238-
result.pop();
1239-
result.push(path);
1240-
Ok(result)
1235+
base_path.pop();
1236+
base_path.push(path);
1237+
Ok(base_path)
12411238
} else {
12421239
Ok(path)
12431240
}

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)