Skip to content

Commit 35d282c

Browse files
Testing perf of reverting the move of resolve_path from rustc_buitin_macros to rustc_expand.
1 parent c52b9c1 commit 35d282c

File tree

6 files changed

+47
-44
lines changed

6 files changed

+47
-44
lines changed

Cargo.lock

+1-1
Original file line numberDiff line numberDiff line change
@@ -4183,9 +4183,9 @@ dependencies = [
41834183
"rustc_ast",
41844184
"rustc_ast_pretty",
41854185
"rustc_attr",
4186+
"rustc_builtin_macros",
41864187
"rustc_data_structures",
41874188
"rustc_errors",
4188-
"rustc_expand",
41894189
"rustc_feature",
41904190
"rustc_hir",
41914191
"rustc_index",

compiler/rustc_builtin_macros/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ mod format;
3838
mod format_foreign;
3939
mod global_allocator;
4040
mod log_syntax;
41-
mod source_util;
41+
pub mod source_util;
4242
mod test;
4343
mod trace_macros;
4444
mod util;

compiler/rustc_builtin_macros/src/source_util.rs

+41-1
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,18 @@ 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;
12+
use rustc_session::parse::ParseSess;
1113
use rustc_span::symbol::Symbol;
12-
use rustc_span::{self, Pos, Span};
14+
use rustc_span::{self, FileName, Pos, Span};
1315

1416
use smallvec::SmallVec;
17+
use std::path::PathBuf;
1518
use std::rc::Rc;
1619

1720
// These macros all relate to the file system; they either return
@@ -223,3 +226,40 @@ pub fn expand_include_bytes(
223226
}
224227
}
225228
}
229+
230+
/// Resolves a `path` mentioned inside Rust code, returning an absolute path.
231+
///
232+
/// This unifies the logic used for resolving `include_X!`.
233+
pub fn resolve_path(
234+
parse_sess: &ParseSess,
235+
path: impl Into<PathBuf>,
236+
span: Span,
237+
) -> PResult<'_, PathBuf> {
238+
let path = path.into();
239+
240+
// Relative paths are resolved relative to the file in which they are found
241+
// after macro expansion (that is, they are unhygienic).
242+
if !path.is_absolute() {
243+
let callsite = span.source_callsite();
244+
let mut result = match parse_sess.source_map().span_to_filename(callsite) {
245+
FileName::Real(name) => name
246+
.into_local_path()
247+
.expect("attempting to resolve a file path in an external file"),
248+
FileName::DocTest(path, _) => path,
249+
other => {
250+
return Err(parse_sess.span_diagnostic.struct_span_err(
251+
span,
252+
&format!(
253+
"cannot resolve relative path in non-file source `{}`",
254+
parse_sess.source_map().filename_for_diagnostics(&other)
255+
),
256+
));
257+
}
258+
};
259+
result.pop();
260+
result.push(path);
261+
Ok(result)
262+
} else {
263+
Ok(path)
264+
}
265+
}

compiler/rustc_expand/src/base.rs

+2-39
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use rustc_ast::{self as ast, Attribute, HasAttrs, 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, MultiSpan, PResult};
13+
use rustc_errors::{Applicability, DiagnosticBuilder, ErrorGuaranteed, MultiSpan};
1414
use rustc_lint_defs::builtin::PROC_MACRO_BACK_COMPAT;
1515
use rustc_lint_defs::BuiltinLintDiagnostics;
1616
use rustc_parse::{self, parser, to_token_stream, 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, Span, DUMMY_SP};
23+
use rustc_span::{Span, DUMMY_SP};
2424
use smallvec::{smallvec, SmallVec};
2525

2626
use std::default::Default;
@@ -1137,43 +1137,6 @@ impl<'a> ExtCtxt<'a> {
11371137
}
11381138
}
11391139

1140-
/// Resolves a `path` mentioned inside Rust code, returning an absolute path.
1141-
///
1142-
/// This unifies the logic used for resolving `include_X!`.
1143-
pub fn resolve_path(
1144-
parse_sess: &ParseSess,
1145-
path: impl Into<PathBuf>,
1146-
span: Span,
1147-
) -> PResult<'_, PathBuf> {
1148-
let path = path.into();
1149-
1150-
// Relative paths are resolved relative to the file in which they are found
1151-
// after macro expansion (that is, they are unhygienic).
1152-
if !path.is_absolute() {
1153-
let callsite = span.source_callsite();
1154-
let mut result = match parse_sess.source_map().span_to_filename(callsite) {
1155-
FileName::Real(name) => name
1156-
.into_local_path()
1157-
.expect("attempting to resolve a file path in an external file"),
1158-
FileName::DocTest(path, _) => path,
1159-
other => {
1160-
return Err(parse_sess.span_diagnostic.struct_span_err(
1161-
span,
1162-
&format!(
1163-
"cannot resolve relative path in non-file source `{}`",
1164-
parse_sess.source_map().filename_for_diagnostics(&other)
1165-
),
1166-
));
1167-
}
1168-
};
1169-
result.pop();
1170-
result.push(path);
1171-
Ok(result)
1172-
} else {
1173-
Ok(path)
1174-
}
1175-
}
1176-
11771140
/// Extracts a string literal from the macro expanded version of `expr`,
11781141
/// returning a diagnostic error of `err_msg` if `expr` is not a string literal.
11791142
/// The returned bool indicates whether an applicable suggestion has already been

compiler/rustc_passes/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ rustc_middle = { path = "../rustc_middle" }
99
rustc_attr = { path = "../rustc_attr" }
1010
rustc_data_structures = { path = "../rustc_data_structures" }
1111
rustc_errors = { path = "../rustc_errors" }
12-
rustc_expand = { path = "../rustc_expand" }
12+
rustc_builtin_macros = { path = "../rustc_builtin_macros" }
1313
rustc_hir = { path = "../rustc_hir" }
1414
rustc_index = { path = "../rustc_index" }
1515
rustc_parse = { path = "../rustc_parse" }

compiler/rustc_passes/src/debugger_visualizer.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
//! Detecting usage of the `#[debugger_visualizer]` attribute.
22
33
use hir::CRATE_HIR_ID;
4+
use rustc_builtin_macros::source_util::resolve_path;
45
use rustc_data_structures::fx::FxHashSet;
5-
use rustc_expand::base::resolve_path;
66
use rustc_hir as hir;
77
use rustc_hir::def_id::CrateNum;
88
use rustc_hir::itemlikevisit::ItemLikeVisitor;

0 commit comments

Comments
 (0)