Skip to content

Commit 0a56446

Browse files
committed
feat(hygiene): Collect loaded proc macros using all_proc_macro_def_ids
The recently introduced CStore::all_proc_macro_def_ids function exposes the information on the loaded proc macos we were trying to access, for which we had to use custom workarounds before. The previous workaround relied on enumerating the def indices of each proc macro crate, and then attempting to read the def entries of each of the def indices within to find the proc macro entries. This wasn't much of a performance concern due to the general size of proc macro crates and the reads being made to previously cached metadata. However, because proc macro crates have a different rmeta representation which removes lots of unnecessary def entries, and because the publicly accessible interfaces (CStore::def_kind_untracked, CStore::load_proc_macro_untracked) all panicked upon a missing def entry, we had to catch panics and even remove the panic hook for the duration of these lookups to gather the information on loaded proc macros that this new publicly-available interface now provides. We primarily use information on loaded proc macros to clean up residual helper attributes which are left behind after proc macro expansion, and are not interpretable without the call to the proc macro.
1 parent 963bede commit 0a56446

1 file changed

Lines changed: 1 addition & 30 deletions

File tree

mutest-emit/src/codegen/hygiene.rs

Lines changed: 1 addition & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use std::iter;
22
use std::mem;
33
use std::ops::DerefMut;
4-
use std::panic;
54
use std::sync::Arc;
65

76
use rustc_data_structures::flat_map_in_place::FlatMapInPlace;
@@ -1807,36 +1806,8 @@ pub fn sanitize_macro_expansions<'tcx>(tcx: TyCtxt<'tcx>, crate_res: &res::Crate
18071806

18081807
let cstore = CStore::from_tcx(tcx);
18091808

1810-
// HACK: The only way to enumerate the definitions of an external crate though the public API is
1811-
// by enumerating the def indices up to `CStore::num_def_ids_untracked`.
1812-
// See https://github.com/rust-lang/rust/pull/85889.
1813-
let crate_def_indices = |cnum: hir::CrateNum| {
1814-
const BASE_DEF_IDX: usize = hir::CRATE_DEF_INDEX.as_usize();
1815-
let max_def_idx = cstore.num_def_ids_untracked(cnum);
1816-
1817-
(BASE_DEF_IDX..max_def_idx).map(hir::DefIndex::from_usize)
1818-
};
1819-
18201809
// Find all loaded proc macro syntax extensions.
1821-
tcx.crates(()).iter()
1822-
// Ensure that we are only looking at crates with `CrateKind::ProcMacro`.
1823-
.filter(|&&cnum| tcx.dep_kind(cnum).macros_only())
1824-
.flat_map(|&cnum| crate_def_indices(cnum).map(move |index| hir::DefId { krate: cnum, index }))
1825-
.filter(|&def_id| {
1826-
// HACK: Not all def indices are encoded for proc macro crates, only the macro definitions themselves.
1827-
// To avoid crashes due to missing def index lookups, we must only generate "valid" indices,
1828-
// however, there is no stable mechanism for this.
1829-
// Instead, we use a def kind query to trigger a crate metadata lookup, and
1830-
// catch the panic, if it occurs.
1831-
// We also disable printing of panic messages with an empty hook.
1832-
// See https://github.com/rust-lang/rust/pull/76897.
1833-
let builtin_panic_hook = panic::take_hook();
1834-
panic::set_hook(Box::new(|_panic_info| {}));
1835-
let Ok(def_kind) = panic::catch_unwind(panic::AssertUnwindSafe(|| cstore.def_kind_untracked(def_id))) else { return false; };
1836-
panic::set_hook(builtin_panic_hook);
1837-
1838-
matches!(def_kind, hir::DefKind::Macro(_))
1839-
})
1810+
cstore.all_proc_macro_def_ids()
18401811
.filter_map(|def_id| {
18411812
match cstore.load_macro_untracked(def_id, tcx) {
18421813
LoadedMacro::ProcMacro(syntax_extension) => Some(syntax_extension),

0 commit comments

Comments
 (0)