Stop using higher-order macros to declare arenas#159955
Conversation
|
r? @camelid rustbot has assigned @camelid. Use Why was this reviewer chosen?The reviewer was selected based on:
|
|
This will have textual conflicts with #159893. |
|
You should be able to get around the indirection and still keep the #[macro_export]
macro_rules! eat {
(decode) => {};
}
#[rustc_macro_transparency = "semiopaque"]
pub macro declare_arena([$([$($a:ident)?] $name:ident: $ty:ty,)*]) {
$(
$(
$crate::eat!($a); // or ${ignore($a)}
impl<'tcx, D: TyDecoder<'tcx>> RefDecodable<'tcx, D> for $ty {
#[inline]
fn decode(decoder: &mut D) -> &'tcx Self {
todo!()
}
}
impl<'tcx, D: TyDecoder<'tcx>> rRefDecodable<'tcx, D> for [$ty] {
#[inline]
fn decode(decoder: &mut D) -> &'tcx Self {
todo!()
}
}
)?
)*
// stuff
}That said, I haven't yet looked in-depth at how comfy either design would be if someone down the line has to add a Also another thought; maybe we should make the syntax a bit more rust-y? rustc_arena::declare_arena! {
pub struct Arena<'tcx> {
asm_template: rustc_ast::InlineAsmTemplatePiece,
#[decode]
attribute: rustc_hir::Attribute,
macro_def: rustc_ast::MacroDef,
}
}(then it can also be an attribute macro, with |
This comment has been minimized.
This comment has been minimized.
This is a little more verbose than using a `[decode]` modifier, but will allow us to stop using higher-order macros when declaring arenas.
|
This PR was rebased onto a different main commit. Here's a range-diff highlighting what actually changed. Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers. |
I think the value of being able to write I also have a strong preference for avoiding complex macros whenever reasonably possible. In this case, I think anything beyond a very simple macro is a high price to pay, and the value of
IMO it's a good thing that the E.g. if I saw something like that in some unfamiliar code, I would end up being pretty annoyed that the code was “lying” to me by resembling real code that is meaningfully different from the actual expansion. |
The arenas used by
rustc_hirandrustc_middleare declared via macros. But instead of invokingrustc_arena::declare_arena!directly, those crates declare a higher-order macro containing a list of types, and then invoke that macro withdeclare_arena!as an argument.From what I can tell, the only reason for this arrangement is so that the list of types can also contain
[decode]modifiers that produce a corresponding implementation ofRefDecodablethat decodes into the arena. But the number of types involved is small enough that it's easy to list them separately, and the advantage of doing so is that we can remove a layer of macro indirection, making it easier to navigate to the real code.There should be no change to compiler behaviour.