1
Fork 0

Helper function for resolve_path

This commit is contained in:
Kornel 2024-03-01 13:37:08 +00:00
parent 10a7aa14fe
commit 89ceced6f6
2 changed files with 22 additions and 14 deletions

View file

@ -1252,21 +1252,18 @@ pub fn resolve_path(sess: &Session, path: impl Into<PathBuf>, span: Span) -> PRe
// after macro expansion (that is, they are unhygienic). // after macro expansion (that is, they are unhygienic).
if !path.is_absolute() { if !path.is_absolute() {
let callsite = span.source_callsite(); let callsite = span.source_callsite();
let mut result = match sess.source_map().span_to_filename(callsite) { let source_map = sess.source_map();
FileName::Real(name) => name let Some(mut base_path) = source_map.span_to_filename(callsite).into_local_path() else {
.into_local_path() return Err(sess.dcx().create_err(errors::ResolveRelativePath {
.expect("attempting to resolve a file path in an external file"), span,
FileName::DocTest(path, _) => path, path: source_map
other => { .filename_for_diagnostics(&source_map.span_to_filename(callsite))
return Err(sess.dcx().create_err(errors::ResolveRelativePath { .to_string(),
span, }));
path: sess.source_map().filename_for_diagnostics(&other).to_string(),
}));
}
}; };
result.pop(); base_path.pop();
result.push(path); base_path.push(path);
Ok(result) Ok(base_path)
} else { } else {
Ok(path) Ok(path)
} }

View file

@ -427,6 +427,17 @@ impl FileName {
src.hash(&mut hasher); src.hash(&mut hasher);
FileName::InlineAsm(hasher.finish()) FileName::InlineAsm(hasher.finish())
} }
/// Returns the path suitable for reading from the file system on the local host,
/// if this information exists.
/// Avoid embedding this in build artifacts; see `remapped_path_if_available()` for that.
pub fn into_local_path(self) -> Option<PathBuf> {
match self {
FileName::Real(path) => path.into_local_path(),
FileName::DocTest(path, _) => Some(path),
_ => None,
}
}
} }
/// Represents a span. /// Represents a span.