Skip to content

Commit edb2187

Browse files
Make path::resolve a method on ExtCtxt
1 parent 8ccf52c commit edb2187

File tree

5 files changed

+30
-36
lines changed

5 files changed

+30
-36
lines changed

src/libsyntax/ext/base.rs

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::ast::{self, Attribute, Name, PatKind};
22
use crate::attr::{HasAttrs, Stability, Deprecation};
3-
use crate::source_map::{SourceMap, Spanned, respan};
3+
use crate::source_map::{SourceMap, Spanned, FileName, respan};
44
use crate::edition::Edition;
55
use crate::ext::expand::{self, AstFragment, Invocation};
66
use crate::ext::hygiene::{ExpnId, SyntaxContext, Transparency};
@@ -889,6 +889,31 @@ impl<'a> ExtCtxt<'a> {
889889
pub fn check_unused_macros(&self) {
890890
self.resolver.check_unused_macros();
891891
}
892+
893+
/// Resolve a path mentioned inside Rust code.
894+
///
895+
/// This unifies the logic used for resolving `include_X!`, and `#[doc(include)]` file paths.
896+
///
897+
/// Returns an absolute path to the file that `path` refers to.
898+
pub fn resolve_path(&self, path: impl Into<PathBuf>, span: Span) -> PathBuf {
899+
let path = path.into();
900+
901+
// Relative paths are resolved relative to the file in which they are found
902+
// after macro expansion (that is, they are unhygienic).
903+
if !path.is_absolute() {
904+
let callsite = span.source_callsite();
905+
let mut result = match self.source_map().span_to_unmapped_path(callsite) {
906+
FileName::Real(path) => path,
907+
FileName::DocTest(path, _) => path,
908+
other => panic!("cannot resolve relative path in non-file source `{}`", other),
909+
};
910+
result.pop();
911+
result.push(path);
912+
result
913+
} else {
914+
path
915+
}
916+
}
892917
}
893918

894919
/// Extracts a string literal from the macro expanded version of `expr`,

src/libsyntax/ext/expand.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ use crate::symbol::{sym, Symbol};
1717
use crate::tokenstream::{TokenStream, TokenTree};
1818
use crate::visit::{self, Visitor};
1919
use crate::util::map_in_place::MapInPlace;
20-
use crate::util::path;
2120

2221
use errors::{Applicability, FatalError};
2322
use smallvec::{smallvec, SmallVec};
@@ -1254,7 +1253,7 @@ impl<'a, 'b> MutVisitor for InvocationCollector<'a, 'b> {
12541253
return noop_visit_attribute(at, self);
12551254
}
12561255

1257-
let filename = path::resolve(&*file.as_str(), it.span(), self.cx.source_map());
1256+
let filename = self.cx.resolve_path(&*file.as_str(), it.span());
12581257
match fs::read_to_string(&filename) {
12591258
Ok(src) => {
12601259
let src_interned = Symbol::intern(&src);

src/libsyntax/ext/source_util.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ use crate::print::pprust;
66
use crate::ptr::P;
77
use crate::symbol::Symbol;
88
use crate::tokenstream;
9-
use crate::util::path;
109

1110
use smallvec::SmallVec;
1211
use syntax_pos::{self, Pos, Span};
@@ -78,7 +77,7 @@ pub fn expand_include<'cx>(cx: &'cx mut ExtCtxt<'_>, sp: Span, tts: &[tokenstrea
7877
None => return DummyResult::any(sp),
7978
};
8079
// The file will be added to the code map by the parser
81-
let file = path::resolve(file, sp, cx.source_map());
80+
let file = cx.resolve_path(file, sp);
8281
let directory_ownership = DirectoryOwnership::Owned { relative: None };
8382
let p = parse::new_sub_parser_from_file(cx.parse_sess(), &file, directory_ownership, None, sp);
8483

@@ -115,7 +114,7 @@ pub fn expand_include_str(cx: &mut ExtCtxt<'_>, sp: Span, tts: &[tokenstream::To
115114
Some(f) => f,
116115
None => return DummyResult::expr(sp)
117116
};
118-
let file = path::resolve(file, sp, cx.source_map());
117+
let file = cx.resolve_path(file, sp);
119118
match fs::read_to_string(&file) {
120119
Ok(src) => {
121120
let interned_src = Symbol::intern(&src);
@@ -143,7 +142,7 @@ pub fn expand_include_bytes(cx: &mut ExtCtxt<'_>, sp: Span, tts: &[tokenstream::
143142
Some(f) => f,
144143
None => return DummyResult::expr(sp)
145144
};
146-
let file = path::resolve(file, sp, cx.source_map());
145+
let file = cx.resolve_path(file, sp);
147146
match fs::read(&file) {
148147
Ok(bytes) => {
149148
// Add the contents to the source map if it contains UTF-8.

src/libsyntax/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,6 @@ pub mod util {
135135
#[cfg(test)]
136136
pub mod parser_testing;
137137
pub mod map_in_place;
138-
pub mod path;
139138
}
140139

141140
pub mod json;

src/libsyntax/util/path.rs

Lines changed: 0 additions & 28 deletions
This file was deleted.

0 commit comments

Comments
 (0)