Generate proc macro harness in AST order.
This ensures that we match the order used by proc macro metadata serialization. Fixes #64251
This commit is contained in:
@@ -489,7 +489,11 @@ impl<'a, 'tcx> CrateMetadata {
|
|||||||
|
|
||||||
fn raw_proc_macro(&self, id: DefIndex) -> &ProcMacro {
|
fn raw_proc_macro(&self, id: DefIndex) -> &ProcMacro {
|
||||||
// DefIndex's in root.proc_macro_data have a one-to-one correspondence
|
// DefIndex's in root.proc_macro_data have a one-to-one correspondence
|
||||||
// with items in 'raw_proc_macros'
|
// with items in 'raw_proc_macros'.
|
||||||
|
// NOTE: If you update the order of macros in 'proc_macro_data' for any reason,
|
||||||
|
// you must also update src/libsyntax_ext/proc_macro_harness.rs
|
||||||
|
// Failing to do so will result in incorrect data being associated
|
||||||
|
// with proc macros when deserialized.
|
||||||
let pos = self.root.proc_macro_data.unwrap().decode(self).position(|i| i == id).unwrap();
|
let pos = self.root.proc_macro_data.unwrap().decode(self).position(|i| i == id).unwrap();
|
||||||
&self.raw_proc_macros.unwrap()[pos]
|
&self.raw_proc_macros.unwrap()[pos]
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -20,15 +20,24 @@ struct ProcMacroDerive {
|
|||||||
attrs: Vec<ast::Name>,
|
attrs: Vec<ast::Name>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
enum ProcMacroDefType {
|
||||||
|
Attr,
|
||||||
|
Bang
|
||||||
|
}
|
||||||
|
|
||||||
struct ProcMacroDef {
|
struct ProcMacroDef {
|
||||||
function_name: Ident,
|
function_name: Ident,
|
||||||
span: Span,
|
span: Span,
|
||||||
|
def_type: ProcMacroDefType
|
||||||
|
}
|
||||||
|
|
||||||
|
enum ProcMacro {
|
||||||
|
Derive(ProcMacroDerive),
|
||||||
|
Def(ProcMacroDef)
|
||||||
}
|
}
|
||||||
|
|
||||||
struct CollectProcMacros<'a> {
|
struct CollectProcMacros<'a> {
|
||||||
derives: Vec<ProcMacroDerive>,
|
macros: Vec<ProcMacro>,
|
||||||
attr_macros: Vec<ProcMacroDef>,
|
|
||||||
bang_macros: Vec<ProcMacroDef>,
|
|
||||||
in_root: bool,
|
in_root: bool,
|
||||||
handler: &'a errors::Handler,
|
handler: &'a errors::Handler,
|
||||||
is_proc_macro_crate: bool,
|
is_proc_macro_crate: bool,
|
||||||
@@ -46,21 +55,21 @@ pub fn inject(sess: &ParseSess,
|
|||||||
let ecfg = ExpansionConfig::default("proc_macro".to_string());
|
let ecfg = ExpansionConfig::default("proc_macro".to_string());
|
||||||
let mut cx = ExtCtxt::new(sess, ecfg, resolver);
|
let mut cx = ExtCtxt::new(sess, ecfg, resolver);
|
||||||
|
|
||||||
let (derives, attr_macros, bang_macros) = {
|
|
||||||
let mut collect = CollectProcMacros {
|
let mut collect = CollectProcMacros {
|
||||||
derives: Vec::new(),
|
macros: Vec::new(),
|
||||||
attr_macros: Vec::new(),
|
|
||||||
bang_macros: Vec::new(),
|
|
||||||
in_root: true,
|
in_root: true,
|
||||||
handler,
|
handler,
|
||||||
is_proc_macro_crate,
|
is_proc_macro_crate,
|
||||||
is_test_crate,
|
is_test_crate,
|
||||||
};
|
};
|
||||||
|
|
||||||
if has_proc_macro_decls || is_proc_macro_crate {
|
if has_proc_macro_decls || is_proc_macro_crate {
|
||||||
visit::walk_crate(&mut collect, &krate);
|
visit::walk_crate(&mut collect, &krate);
|
||||||
}
|
}
|
||||||
(collect.derives, collect.attr_macros, collect.bang_macros)
|
// NOTE: If you change the order of macros in this vec
|
||||||
};
|
// for any reason, you must also update 'raw_proc_macro'
|
||||||
|
// in src/librustc_metadata/decoder.rs
|
||||||
|
let macros = collect.macros;
|
||||||
|
|
||||||
if !is_proc_macro_crate {
|
if !is_proc_macro_crate {
|
||||||
return krate
|
return krate
|
||||||
@@ -74,7 +83,7 @@ pub fn inject(sess: &ParseSess,
|
|||||||
return krate;
|
return krate;
|
||||||
}
|
}
|
||||||
|
|
||||||
krate.module.items.push(mk_decls(&mut cx, &derives, &attr_macros, &bang_macros));
|
krate.module.items.push(mk_decls(&mut cx, ¯os));
|
||||||
|
|
||||||
krate
|
krate
|
||||||
}
|
}
|
||||||
@@ -161,12 +170,12 @@ impl<'a> CollectProcMacros<'a> {
|
|||||||
};
|
};
|
||||||
|
|
||||||
if self.in_root && item.vis.node.is_pub() {
|
if self.in_root && item.vis.node.is_pub() {
|
||||||
self.derives.push(ProcMacroDerive {
|
self.macros.push(ProcMacro::Derive(ProcMacroDerive {
|
||||||
span: item.span,
|
span: item.span,
|
||||||
trait_name: trait_ident.name,
|
trait_name: trait_ident.name,
|
||||||
function_name: item.ident,
|
function_name: item.ident,
|
||||||
attrs: proc_attrs,
|
attrs: proc_attrs,
|
||||||
});
|
}));
|
||||||
} else {
|
} else {
|
||||||
let msg = if !self.in_root {
|
let msg = if !self.in_root {
|
||||||
"functions tagged with `#[proc_macro_derive]` must \
|
"functions tagged with `#[proc_macro_derive]` must \
|
||||||
@@ -180,10 +189,11 @@ impl<'a> CollectProcMacros<'a> {
|
|||||||
|
|
||||||
fn collect_attr_proc_macro(&mut self, item: &'a ast::Item) {
|
fn collect_attr_proc_macro(&mut self, item: &'a ast::Item) {
|
||||||
if self.in_root && item.vis.node.is_pub() {
|
if self.in_root && item.vis.node.is_pub() {
|
||||||
self.attr_macros.push(ProcMacroDef {
|
self.macros.push(ProcMacro::Def(ProcMacroDef {
|
||||||
span: item.span,
|
span: item.span,
|
||||||
function_name: item.ident,
|
function_name: item.ident,
|
||||||
});
|
def_type: ProcMacroDefType::Attr
|
||||||
|
}));
|
||||||
} else {
|
} else {
|
||||||
let msg = if !self.in_root {
|
let msg = if !self.in_root {
|
||||||
"functions tagged with `#[proc_macro_attribute]` must \
|
"functions tagged with `#[proc_macro_attribute]` must \
|
||||||
@@ -197,10 +207,11 @@ impl<'a> CollectProcMacros<'a> {
|
|||||||
|
|
||||||
fn collect_bang_proc_macro(&mut self, item: &'a ast::Item) {
|
fn collect_bang_proc_macro(&mut self, item: &'a ast::Item) {
|
||||||
if self.in_root && item.vis.node.is_pub() {
|
if self.in_root && item.vis.node.is_pub() {
|
||||||
self.bang_macros.push(ProcMacroDef {
|
self.macros.push(ProcMacro::Def(ProcMacroDef {
|
||||||
span: item.span,
|
span: item.span,
|
||||||
function_name: item.ident,
|
function_name: item.ident,
|
||||||
});
|
def_type: ProcMacroDefType::Bang
|
||||||
|
}));
|
||||||
} else {
|
} else {
|
||||||
let msg = if !self.in_root {
|
let msg = if !self.in_root {
|
||||||
"functions tagged with `#[proc_macro]` must \
|
"functions tagged with `#[proc_macro]` must \
|
||||||
@@ -322,9 +333,7 @@ impl<'a> Visitor<'a> for CollectProcMacros<'a> {
|
|||||||
// }
|
// }
|
||||||
fn mk_decls(
|
fn mk_decls(
|
||||||
cx: &mut ExtCtxt<'_>,
|
cx: &mut ExtCtxt<'_>,
|
||||||
custom_derives: &[ProcMacroDerive],
|
macros: &[ProcMacro],
|
||||||
custom_attrs: &[ProcMacroDef],
|
|
||||||
custom_macros: &[ProcMacroDef],
|
|
||||||
) -> P<ast::Item> {
|
) -> P<ast::Item> {
|
||||||
let expn_id = cx.resolver.expansion_for_ast_pass(
|
let expn_id = cx.resolver.expansion_for_ast_pass(
|
||||||
DUMMY_SP,
|
DUMMY_SP,
|
||||||
@@ -354,7 +363,9 @@ fn mk_decls(
|
|||||||
let proc_macro_ty_method_path = |method| cx.expr_path(cx.path(span, vec![
|
let proc_macro_ty_method_path = |method| cx.expr_path(cx.path(span, vec![
|
||||||
proc_macro, bridge, client, proc_macro_ty, method,
|
proc_macro, bridge, client, proc_macro_ty, method,
|
||||||
]));
|
]));
|
||||||
custom_derives.iter().map(|cd| {
|
macros.iter().map(|m| {
|
||||||
|
match m {
|
||||||
|
ProcMacro::Derive(cd) => {
|
||||||
cx.expr_call(span, proc_macro_ty_method_path(custom_derive), vec![
|
cx.expr_call(span, proc_macro_ty_method_path(custom_derive), vec![
|
||||||
cx.expr_str(cd.span, cd.trait_name),
|
cx.expr_str(cd.span, cd.trait_name),
|
||||||
cx.expr_vec_slice(
|
cx.expr_vec_slice(
|
||||||
@@ -363,17 +374,21 @@ fn mk_decls(
|
|||||||
),
|
),
|
||||||
local_path(cd.span, cd.function_name),
|
local_path(cd.span, cd.function_name),
|
||||||
])
|
])
|
||||||
}).chain(custom_attrs.iter().map(|ca| {
|
},
|
||||||
cx.expr_call(span, proc_macro_ty_method_path(attr), vec![
|
ProcMacro::Def(ca) => {
|
||||||
|
let ident = match ca.def_type {
|
||||||
|
ProcMacroDefType::Attr => attr,
|
||||||
|
ProcMacroDefType::Bang => bang
|
||||||
|
};
|
||||||
|
|
||||||
|
cx.expr_call(span, proc_macro_ty_method_path(ident), vec![
|
||||||
cx.expr_str(ca.span, ca.function_name.name),
|
cx.expr_str(ca.span, ca.function_name.name),
|
||||||
local_path(ca.span, ca.function_name),
|
local_path(ca.span, ca.function_name),
|
||||||
])
|
])
|
||||||
})).chain(custom_macros.iter().map(|cm| {
|
|
||||||
cx.expr_call(span, proc_macro_ty_method_path(bang), vec![
|
}
|
||||||
cx.expr_str(cm.span, cm.function_name.name),
|
}
|
||||||
local_path(cm.span, cm.function_name),
|
}).collect()
|
||||||
])
|
|
||||||
})).collect()
|
|
||||||
};
|
};
|
||||||
|
|
||||||
let decls_static = cx.item_static(
|
let decls_static = cx.item_static(
|
||||||
|
|||||||
@@ -10,8 +10,19 @@ extern crate some_macros;
|
|||||||
// @has proc_macro/macro.some_proc_macro.html
|
// @has proc_macro/macro.some_proc_macro.html
|
||||||
// @has proc_macro/attr.some_proc_attr.html
|
// @has proc_macro/attr.some_proc_attr.html
|
||||||
// @has proc_macro/derive.SomeDerive.html
|
// @has proc_macro/derive.SomeDerive.html
|
||||||
pub use some_macros::{some_proc_macro, some_proc_attr, SomeDerive};
|
|
||||||
|
// @has proc_macro/macro.some_proc_macro.html
|
||||||
|
// @has - 'a proc-macro that swallows its input and does nothing.'
|
||||||
|
pub use some_macros::some_proc_macro;
|
||||||
|
|
||||||
// @has proc_macro/macro.reexported_macro.html
|
// @has proc_macro/macro.reexported_macro.html
|
||||||
// @has - 'Doc comment from the original crate'
|
// @has - 'Doc comment from the original crate'
|
||||||
pub use some_macros::reexported_macro;
|
pub use some_macros::reexported_macro;
|
||||||
|
|
||||||
|
// @has proc_macro/attr.some_proc_attr.html
|
||||||
|
// @has - 'a proc-macro attribute that passes its item through verbatim.'
|
||||||
|
pub use some_macros::some_proc_attr;
|
||||||
|
|
||||||
|
// @has proc_macro/derive.SomeDerive.html
|
||||||
|
// @has - 'a derive attribute that adds nothing to its input.'
|
||||||
|
pub use some_macros::SomeDerive;
|
||||||
|
|||||||
Reference in New Issue
Block a user