Skip to content

Commit

Permalink
Make path::resolve a method on ExtCtxt
Browse files Browse the repository at this point in the history
  • Loading branch information
jonas-schievink committed Jul 23, 2019
1 parent 8ccf52c commit edb2187
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 36 deletions.
27 changes: 26 additions & 1 deletion src/libsyntax/ext/base.rs
@@ -1,6 +1,6 @@
use crate::ast::{self, Attribute, Name, PatKind};
use crate::attr::{HasAttrs, Stability, Deprecation};
use crate::source_map::{SourceMap, Spanned, respan};
use crate::source_map::{SourceMap, Spanned, FileName, respan};
use crate::edition::Edition;
use crate::ext::expand::{self, AstFragment, Invocation};
use crate::ext::hygiene::{ExpnId, SyntaxContext, Transparency};
Expand Down Expand Up @@ -889,6 +889,31 @@ impl<'a> ExtCtxt<'a> {
pub fn check_unused_macros(&self) {
self.resolver.check_unused_macros();
}

/// Resolve a path mentioned inside Rust code.
///
/// This unifies the logic used for resolving `include_X!`, and `#[doc(include)]` file paths.
///
/// Returns an absolute path to the file that `path` refers to.
pub fn resolve_path(&self, path: impl Into<PathBuf>, span: Span) -> PathBuf {
let path = path.into();

// Relative paths are resolved relative to the file in which they are found
// after macro expansion (that is, they are unhygienic).
if !path.is_absolute() {
let callsite = span.source_callsite();
let mut result = match self.source_map().span_to_unmapped_path(callsite) {
FileName::Real(path) => path,
FileName::DocTest(path, _) => path,
other => panic!("cannot resolve relative path in non-file source `{}`", other),
};
result.pop();
result.push(path);
result
} else {
path
}
}
}

/// Extracts a string literal from the macro expanded version of `expr`,
Expand Down
3 changes: 1 addition & 2 deletions src/libsyntax/ext/expand.rs
Expand Up @@ -17,7 +17,6 @@ use crate::symbol::{sym, Symbol};
use crate::tokenstream::{TokenStream, TokenTree};
use crate::visit::{self, Visitor};
use crate::util::map_in_place::MapInPlace;
use crate::util::path;

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

let filename = path::resolve(&*file.as_str(), it.span(), self.cx.source_map());
let filename = self.cx.resolve_path(&*file.as_str(), it.span());
match fs::read_to_string(&filename) {
Ok(src) => {
let src_interned = Symbol::intern(&src);
Expand Down
7 changes: 3 additions & 4 deletions src/libsyntax/ext/source_util.rs
Expand Up @@ -6,7 +6,6 @@ use crate::print::pprust;
use crate::ptr::P;
use crate::symbol::Symbol;
use crate::tokenstream;
use crate::util::path;

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

Expand Down Expand Up @@ -115,7 +114,7 @@ pub fn expand_include_str(cx: &mut ExtCtxt<'_>, sp: Span, tts: &[tokenstream::To
Some(f) => f,
None => return DummyResult::expr(sp)
};
let file = path::resolve(file, sp, cx.source_map());
let file = cx.resolve_path(file, sp);
match fs::read_to_string(&file) {
Ok(src) => {
let interned_src = Symbol::intern(&src);
Expand Down Expand Up @@ -143,7 +142,7 @@ pub fn expand_include_bytes(cx: &mut ExtCtxt<'_>, sp: Span, tts: &[tokenstream::
Some(f) => f,
None => return DummyResult::expr(sp)
};
let file = path::resolve(file, sp, cx.source_map());
let file = cx.resolve_path(file, sp);
match fs::read(&file) {
Ok(bytes) => {
// Add the contents to the source map if it contains UTF-8.
Expand Down
1 change: 0 additions & 1 deletion src/libsyntax/lib.rs
Expand Up @@ -135,7 +135,6 @@ pub mod util {
#[cfg(test)]
pub mod parser_testing;
pub mod map_in_place;
pub mod path;
}

pub mod json;
Expand Down
28 changes: 0 additions & 28 deletions src/libsyntax/util/path.rs

This file was deleted.

0 comments on commit edb2187

Please sign in to comment.