Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 86b3ebe

Browse files
committed
Inline box_region macro calls
1 parent 8f397bc commit 86b3ebe

File tree

2 files changed

+77
-85
lines changed

2 files changed

+77
-85
lines changed

compiler/rustc_data_structures/src/box_region.rs

Lines changed: 0 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -78,83 +78,3 @@ pub enum YieldType<I, A> {
7878
Initial(I),
7979
Accessor(Marker<A>),
8080
}
81-
82-
#[macro_export]
83-
#[allow_internal_unstable(fn_traits)]
84-
macro_rules! declare_box_region_type {
85-
($v:vis $name: ident, ($($args:ty),*) -> ($reti:ty, $retc:ty)) => {
86-
$v struct $name($crate::box_region::PinnedGenerator<
87-
$reti,
88-
fn(($($args,)*)),
89-
$retc
90-
>);
91-
92-
impl $name {
93-
fn new<T>(generator: T) -> ($reti, Self)
94-
where T: ::std::ops::Generator<
95-
$crate::box_region::Action,
96-
Yield = $crate::box_region::YieldType<$reti, fn(($($args,)*))>,
97-
Return = $retc,
98-
> + 'static {
99-
let (initial, pinned) = $crate::box_region::PinnedGenerator::new(generator);
100-
(initial, $name(pinned))
101-
}
102-
103-
$v fn access<F: FnOnce($($args,)*) -> R, R>(&mut self, f: F) -> R {
104-
// Turn the FnOnce closure into *mut dyn FnMut()
105-
// so we can pass it in to the generator
106-
let mut r = None;
107-
let mut f = Some(f);
108-
let mut_f: &mut dyn FnMut(($($args,)*)) =
109-
&mut |args| {
110-
let f = f.take().unwrap();
111-
r = Some(FnOnce::call_once(f, args));
112-
};
113-
let mut_f = mut_f as *mut dyn FnMut(($($args,)*));
114-
115-
// Get the generator to call our closure
116-
unsafe {
117-
self.0.access(::std::mem::transmute(mut_f));
118-
}
119-
120-
// Unwrap the result
121-
r.unwrap()
122-
}
123-
124-
$v fn complete(mut self) -> $retc {
125-
self.0.complete()
126-
}
127-
128-
fn initial_yield(
129-
value: $reti,
130-
) -> $crate::box_region::YieldType<$reti, fn(($($args,)*))> {
131-
$crate::box_region::YieldType::Initial(value)
132-
}
133-
}
134-
};
135-
}
136-
137-
#[macro_export]
138-
#[allow_internal_unstable(fn_traits)]
139-
macro_rules! box_region_allow_access {
140-
(($($args:ty),*), ($($exprs:expr),*), $action:ident) => {
141-
loop {
142-
match $action {
143-
$crate::box_region::Action::Access(accessor) => {
144-
let accessor: &mut dyn FnMut($($args),*) = unsafe {
145-
::std::mem::transmute(accessor.get())
146-
};
147-
(*accessor)(($($exprs),*));
148-
unsafe {
149-
let marker = $crate::box_region::Marker::<
150-
fn(($($args,)*))
151-
>::new();
152-
$action = yield $crate::box_region::YieldType::Accessor(marker);
153-
};
154-
}
155-
$crate::box_region::Action::Complete => break,
156-
$crate::box_region::Action::Initial => panic!("unexpected box_region action: Initial"),
157-
}
158-
}
159-
}
160-
}

compiler/rustc_interface/src/passes.rs

Lines changed: 77 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ use rustc_ast::mut_visit::MutVisitor;
66
use rustc_ast::{self as ast, visit};
77
use rustc_codegen_ssa::back::link::emit_metadata;
88
use rustc_codegen_ssa::traits::CodegenBackend;
9+
use rustc_data_structures::parallel;
910
use rustc_data_structures::steal::Steal;
1011
use rustc_data_structures::sync::{par_iter, Lrc, OnceCell, ParallelIterator, WorkerLocal};
1112
use rustc_data_structures::temp_dir::MaybeTempDir;
12-
use rustc_data_structures::{box_region_allow_access, declare_box_region_type, parallel};
1313
use rustc_errors::{ErrorReported, PResult};
1414
use rustc_expand::base::ExtCtxt;
1515
use rustc_hir::def_id::LOCAL_CRATE;
@@ -85,11 +85,62 @@ fn count_nodes(krate: &ast::Crate) -> usize {
8585
counter.count
8686
}
8787

88-
declare_box_region_type!(
89-
pub BoxedResolver,
90-
(&mut Resolver<'_>) -> (Result<ast::Crate>, ResolverOutputs)
88+
pub struct BoxedResolver(
89+
rustc_data_structures::box_region::PinnedGenerator<
90+
Result<ast::Crate>,
91+
fn(&mut Resolver<'_>),
92+
ResolverOutputs,
93+
>,
9194
);
9295

96+
impl BoxedResolver {
97+
fn new<T>(generator: T) -> (Result<ast::Crate>, Self)
98+
where
99+
T: ::std::ops::Generator<
100+
rustc_data_structures::box_region::Action,
101+
Yield = rustc_data_structures::box_region::YieldType<
102+
Result<ast::Crate>,
103+
fn(&mut Resolver<'_>),
104+
>,
105+
Return = ResolverOutputs,
106+
> + 'static,
107+
{
108+
let (initial, pinned) = rustc_data_structures::box_region::PinnedGenerator::new(generator);
109+
(initial, BoxedResolver(pinned))
110+
}
111+
112+
pub fn access<F: FnOnce(&mut Resolver<'_>) -> R, R>(&mut self, f: F) -> R {
113+
// Turn the FnOnce closure into *mut dyn FnMut()
114+
// so we can pass it in to the generator
115+
let mut r = None;
116+
let mut f = Some(f);
117+
let mut_f: &mut dyn FnMut(&mut Resolver<'_>) = &mut |resolver| {
118+
let f = f.take().unwrap();
119+
r = Some(f(resolver));
120+
};
121+
let mut_f = mut_f as *mut dyn FnMut(&mut Resolver<'_>);
122+
123+
// Get the generator to call our closure
124+
unsafe {
125+
self.0.access(::std::mem::transmute(mut_f));
126+
}
127+
128+
// Unwrap the result
129+
r.unwrap()
130+
}
131+
132+
pub fn complete(mut self) -> ResolverOutputs {
133+
self.0.complete()
134+
}
135+
136+
fn initial_yield(
137+
value: Result<ast::Crate>,
138+
) -> rustc_data_structures::box_region::YieldType<Result<ast::Crate>, fn(&mut Resolver<'_>)>
139+
{
140+
rustc_data_structures::box_region::YieldType::Initial(value)
141+
}
142+
}
143+
93144
/// Runs the "early phases" of the compiler: initial `cfg` processing, loading compiler plugins,
94145
/// syntax expansion, secondary `cfg` expansion, synthesis of a test
95146
/// harness if one is to be provided, injection of a dependency on the
@@ -132,7 +183,28 @@ pub fn configure_and_expand(
132183
resolver
133184
}
134185
};
135-
box_region_allow_access!((&mut Resolver<'_>), (&mut resolver), action);
186+
187+
loop {
188+
match action {
189+
rustc_data_structures::box_region::Action::Access(accessor) => {
190+
let accessor: &mut dyn FnMut(&mut Resolver<'_>) =
191+
unsafe { ::std::mem::transmute(accessor.get()) };
192+
(*accessor)(&mut resolver);
193+
unsafe {
194+
let marker = rustc_data_structures::box_region::Marker::<
195+
fn(&mut Resolver<'_>),
196+
>::new();
197+
action =
198+
yield rustc_data_structures::box_region::YieldType::Accessor(marker);
199+
};
200+
}
201+
rustc_data_structures::box_region::Action::Complete => break,
202+
rustc_data_structures::box_region::Action::Initial => {
203+
panic!("unexpected box_region action: Initial")
204+
}
205+
}
206+
}
207+
136208
resolver.into_outputs()
137209
});
138210
result.map(|k| (k, resolver))

0 commit comments

Comments
 (0)