From edb21873cced7a179571aa3a9c25eb1cfc05c2db Mon Sep 17 00:00:00 2001 From: Jonas Schievink Date: Sat, 22 Jun 2019 21:51:51 +0200 Subject: [PATCH] Make path::resolve a method on ExtCtxt --- src/libsyntax/ext/base.rs | 27 ++++++++++++++++++++++++++- src/libsyntax/ext/expand.rs | 3 +-- src/libsyntax/ext/source_util.rs | 7 +++---- src/libsyntax/lib.rs | 1 - src/libsyntax/util/path.rs | 28 ---------------------------- 5 files changed, 30 insertions(+), 36 deletions(-) delete mode 100644 src/libsyntax/util/path.rs diff --git a/src/libsyntax/ext/base.rs b/src/libsyntax/ext/base.rs index 926c9e88efe..11b7a984aaa 100644 --- a/src/libsyntax/ext/base.rs +++ b/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}; @@ -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, 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`, diff --git a/src/libsyntax/ext/expand.rs b/src/libsyntax/ext/expand.rs index 6cfcfdfbf7d..ae8b11ff9d5 100644 --- a/src/libsyntax/ext/expand.rs +++ b/src/libsyntax/ext/expand.rs @@ -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}; @@ -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); diff --git a/src/libsyntax/ext/source_util.rs b/src/libsyntax/ext/source_util.rs index aad390f3bf6..ae080c05eec 100644 --- a/src/libsyntax/ext/source_util.rs +++ b/src/libsyntax/ext/source_util.rs @@ -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}; @@ -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); @@ -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); @@ -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. diff --git a/src/libsyntax/lib.rs b/src/libsyntax/lib.rs index 1b2873b1de7..3dea1977c4d 100644 --- a/src/libsyntax/lib.rs +++ b/src/libsyntax/lib.rs @@ -135,7 +135,6 @@ pub mod util { #[cfg(test)] pub mod parser_testing; pub mod map_in_place; - pub mod path; } pub mod json; diff --git a/src/libsyntax/util/path.rs b/src/libsyntax/util/path.rs deleted file mode 100644 index a3511bac8e7..00000000000 --- a/src/libsyntax/util/path.rs +++ /dev/null @@ -1,28 +0,0 @@ -use crate::source_map::SourceMap; -use std::path::PathBuf; -use syntax_pos::{Span, FileName}; - -/// 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: impl Into, span: Span, map: &SourceMap) -> 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 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 - } -}