Skip to content

Commit 979c8e8

Browse files
authored
Rollup merge of rust-lang#95335 - Badel2:resolve-path, r=Dylan-DPC
Move resolve_path to rustc_builtin_macros and make it private Fixing a FIXME introduced by `@jyn514` in rust-lang#85457
2 parents c6bb219 + ea26d72 commit 979c8e8

File tree

2 files changed

+45
-41
lines changed

2 files changed

+45
-41
lines changed

compiler/rustc_builtin_macros/src/source_util.rs

+43-4
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,17 @@ use rustc_ast::ptr::P;
33
use rustc_ast::token;
44
use rustc_ast::tokenstream::TokenStream;
55
use rustc_ast_pretty::pprust;
6+
use rustc_errors::PResult;
67
use rustc_expand::base::{self, *};
78
use rustc_expand::module::DirOwnership;
89
use rustc_parse::parser::{ForceCollect, Parser};
910
use rustc_parse::{self, new_parser_from_file};
1011
use rustc_session::lint::builtin::INCOMPLETE_INCLUDE;
1112
use rustc_span::symbol::Symbol;
12-
use rustc_span::{self, Pos, Span};
13+
use rustc_span::{self, FileName, Pos, Span};
1314

1415
use smallvec::SmallVec;
16+
use std::path::PathBuf;
1517
use std::rc::Rc;
1618

1719
// These macros all relate to the file system; they either return
@@ -102,7 +104,7 @@ pub fn expand_include<'cx>(
102104
return DummyResult::any(sp);
103105
};
104106
// The file will be added to the code map by the parser
105-
let file = match cx.resolve_path(file, sp) {
107+
let file = match resolve_path(cx, file, sp) {
106108
Ok(f) => f,
107109
Err(mut err) => {
108110
err.emit();
@@ -171,7 +173,7 @@ pub fn expand_include_str(
171173
let Some(file) = get_single_str_from_tts(cx, sp, tts, "include_str!") else {
172174
return DummyResult::any(sp);
173175
};
174-
let file = match cx.resolve_path(file, sp) {
176+
let file = match resolve_path(cx, file, sp) {
175177
Ok(f) => f,
176178
Err(mut err) => {
177179
err.emit();
@@ -205,7 +207,7 @@ pub fn expand_include_bytes(
205207
let Some(file) = get_single_str_from_tts(cx, sp, tts, "include_bytes!") else {
206208
return DummyResult::any(sp);
207209
};
208-
let file = match cx.resolve_path(file, sp) {
210+
let file = match resolve_path(cx, file, sp) {
209211
Ok(f) => f,
210212
Err(mut err) => {
211213
err.emit();
@@ -220,3 +222,40 @@ pub fn expand_include_bytes(
220222
}
221223
}
222224
}
225+
226+
/// Resolves a `path` mentioned inside Rust code, returning an absolute path.
227+
///
228+
/// This unifies the logic used for resolving `include_X!`.
229+
fn resolve_path<'a>(
230+
cx: &mut ExtCtxt<'a>,
231+
path: impl Into<PathBuf>,
232+
span: Span,
233+
) -> PResult<'a, PathBuf> {
234+
let path = path.into();
235+
236+
// Relative paths are resolved relative to the file in which they are found
237+
// after macro expansion (that is, they are unhygienic).
238+
if !path.is_absolute() {
239+
let callsite = span.source_callsite();
240+
let mut result = match cx.source_map().span_to_filename(callsite) {
241+
FileName::Real(name) => name
242+
.into_local_path()
243+
.expect("attempting to resolve a file path in an external file"),
244+
FileName::DocTest(path, _) => path,
245+
other => {
246+
return Err(cx.struct_span_err(
247+
span,
248+
&format!(
249+
"cannot resolve relative path in non-file source `{}`",
250+
cx.source_map().filename_for_diagnostics(&other)
251+
),
252+
));
253+
}
254+
};
255+
result.pop();
256+
result.push(path);
257+
Ok(result)
258+
} else {
259+
Ok(path)
260+
}
261+
}

compiler/rustc_expand/src/base.rs

+2-37
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use rustc_ast::{self as ast, AstLike, Attribute, Item, NodeId, PatKind};
1010
use rustc_attr::{self as attr, Deprecation, Stability};
1111
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
1212
use rustc_data_structures::sync::{self, Lrc};
13-
use rustc_errors::{Applicability, DiagnosticBuilder, ErrorGuaranteed, PResult};
13+
use rustc_errors::{Applicability, DiagnosticBuilder, ErrorGuaranteed};
1414
use rustc_lint_defs::builtin::PROC_MACRO_BACK_COMPAT;
1515
use rustc_lint_defs::BuiltinLintDiagnostics;
1616
use rustc_parse::{self, nt_to_tokenstream, parser, MACRO_ARGUMENTS};
@@ -20,7 +20,7 @@ use rustc_span::edition::Edition;
2020
use rustc_span::hygiene::{AstPass, ExpnData, ExpnKind, LocalExpnId};
2121
use rustc_span::source_map::SourceMap;
2222
use rustc_span::symbol::{kw, sym, Ident, Symbol};
23-
use rustc_span::{FileName, MultiSpan, Span, DUMMY_SP};
23+
use rustc_span::{MultiSpan, Span, DUMMY_SP};
2424
use smallvec::{smallvec, SmallVec};
2525

2626
use std::default::Default;
@@ -1128,41 +1128,6 @@ impl<'a> ExtCtxt<'a> {
11281128
pub fn check_unused_macros(&mut self) {
11291129
self.resolver.check_unused_macros();
11301130
}
1131-
1132-
/// Resolves a `path` mentioned inside Rust code, returning an absolute path.
1133-
///
1134-
/// This unifies the logic used for resolving `include_X!`.
1135-
///
1136-
/// FIXME: move this to `rustc_builtin_macros` and make it private.
1137-
pub fn resolve_path(&self, path: impl Into<PathBuf>, span: Span) -> PResult<'a, PathBuf> {
1138-
let path = path.into();
1139-
1140-
// Relative paths are resolved relative to the file in which they are found
1141-
// after macro expansion (that is, they are unhygienic).
1142-
if !path.is_absolute() {
1143-
let callsite = span.source_callsite();
1144-
let mut result = match self.source_map().span_to_filename(callsite) {
1145-
FileName::Real(name) => name
1146-
.into_local_path()
1147-
.expect("attempting to resolve a file path in an external file"),
1148-
FileName::DocTest(path, _) => path,
1149-
other => {
1150-
return Err(self.struct_span_err(
1151-
span,
1152-
&format!(
1153-
"cannot resolve relative path in non-file source `{}`",
1154-
self.source_map().filename_for_diagnostics(&other)
1155-
),
1156-
));
1157-
}
1158-
};
1159-
result.pop();
1160-
result.push(path);
1161-
Ok(result)
1162-
} else {
1163-
Ok(path)
1164-
}
1165-
}
11661131
}
11671132

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

0 commit comments

Comments
 (0)