Skip to content

Commit 4ef10f4

Browse files
committed
Helper function for resolve_path
1 parent 200e3f7 commit 4ef10f4

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
@@ -1252,21 +1252,18 @@ pub fn resolve_path(sess: &Session, path: impl Into<PathBuf>, span: Span) -> PRe
12521252
// after macro expansion (that is, they are unhygienic).
12531253
if !path.is_absolute() {
12541254
let callsite = span.source_callsite();
1255-
let mut result = match sess.source_map().span_to_filename(callsite) {
1256-
FileName::Real(name) => name
1257-
.into_local_path()
1258-
.expect("attempting to resolve a file path in an external file"),
1259-
FileName::DocTest(path, _) => path,
1260-
other => {
1261-
return Err(sess.dcx().create_err(errors::ResolveRelativePath {
1262-
span,
1263-
path: sess.source_map().filename_for_diagnostics(&other).to_string(),
1264-
}));
1265-
}
1255+
let source_map = sess.source_map();
1256+
let Some(mut base_path) = source_map.span_to_filename(callsite).into_local_path() else {
1257+
return Err(sess.dcx().create_err(errors::ResolveRelativePath {
1258+
span,
1259+
path: source_map
1260+
.filename_for_diagnostics(&source_map.span_to_filename(callsite))
1261+
.to_string(),
1262+
}));
12661263
};
1267-
result.pop();
1268-
result.push(path);
1269-
Ok(result)
1264+
base_path.pop();
1265+
base_path.push(path);
1266+
Ok(base_path)
12701267
} else {
12711268
Ok(path)
12721269
}

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)