Skip to content

Commit 67cccaf

Browse files
committed
expand: Pass everything by reference to pre-expansion lint callback
1 parent 05cd755 commit 67cccaf

File tree

3 files changed

+46
-29
lines changed

3 files changed

+46
-29
lines changed

compiler/rustc_expand/src/base.rs

+18-10
Original file line numberDiff line numberDiff line change
@@ -925,6 +925,20 @@ pub trait ResolverExpand {
925925
fn registered_tools(&self) -> &FxHashSet<Ident>;
926926
}
927927

928+
pub trait LintStoreExpand {
929+
fn pre_expansion_lint(
930+
&self,
931+
sess: &Session,
932+
registered_tools: &FxHashSet<Ident>,
933+
node_id: NodeId,
934+
attrs: &[Attribute],
935+
items: &[P<Item>],
936+
name: &str,
937+
);
938+
}
939+
940+
type LintStoreExpandDyn<'a> = Option<&'a (dyn LintStoreExpand + 'a)>;
941+
928942
#[derive(Clone, Default)]
929943
pub struct ModuleData {
930944
/// Path to the module starting from the crate name, like `my_crate::foo::bar`.
@@ -959,10 +973,6 @@ pub struct ExpansionData {
959973
pub is_trailing_mac: bool,
960974
}
961975

962-
type OnExternModLoaded<'a> = Option<
963-
&'a dyn Fn(NodeId, Vec<Attribute>, Vec<P<Item>>, Symbol) -> (Vec<Attribute>, Vec<P<Item>>),
964-
>;
965-
966976
/// One of these is made during expansion and incrementally updated as we go;
967977
/// when a macro expansion occurs, the resulting nodes have the `backtrace()
968978
/// -> expn_data` of their expansion context stored into their span.
@@ -977,10 +987,8 @@ pub struct ExtCtxt<'a> {
977987
/// (or during eager expansion, but that's a hack).
978988
pub force_mode: bool,
979989
pub expansions: FxHashMap<Span, Vec<String>>,
980-
/// Called directly after having parsed an external `mod foo;` in expansion.
981-
///
982-
/// `Ident` is the module name.
983-
pub(super) extern_mod_loaded: OnExternModLoaded<'a>,
990+
/// Used for running pre-expansion lints on freshly loaded modules.
991+
pub(super) lint_store: LintStoreExpandDyn<'a>,
984992
/// When we 'expand' an inert attribute, we leave it
985993
/// in the AST, but insert it here so that we know
986994
/// not to expand it again.
@@ -992,14 +1000,14 @@ impl<'a> ExtCtxt<'a> {
9921000
sess: &'a Session,
9931001
ecfg: expand::ExpansionConfig<'a>,
9941002
resolver: &'a mut dyn ResolverExpand,
995-
extern_mod_loaded: OnExternModLoaded<'a>,
1003+
lint_store: LintStoreExpandDyn<'a>,
9961004
) -> ExtCtxt<'a> {
9971005
ExtCtxt {
9981006
sess,
9991007
ecfg,
10001008
reduced_recursion_limit: None,
10011009
resolver,
1002-
extern_mod_loaded,
1010+
lint_store,
10031011
root_path: PathBuf::new(),
10041012
current_expansion: ExpansionData {
10051013
id: LocalExpnId::ROOT,

compiler/rustc_expand/src/expand.rs

+8-6
Original file line numberDiff line numberDiff line change
@@ -1097,7 +1097,7 @@ impl InvocationCollectorNode for P<ast::Item> {
10971097
ModKind::Unloaded => {
10981098
// We have an outline `mod foo;` so we need to parse the file.
10991099
let old_attrs_len = attrs.len();
1100-
let ParsedExternalMod { mut items, inner_span, file_path, dir_path, dir_ownership } =
1100+
let ParsedExternalMod { items, inner_span, file_path, dir_path, dir_ownership } =
11011101
parse_external_mod(
11021102
&ecx.sess,
11031103
ident,
@@ -1107,12 +1107,14 @@ impl InvocationCollectorNode for P<ast::Item> {
11071107
&mut attrs,
11081108
);
11091109

1110-
if let Some(extern_mod_loaded) = ecx.extern_mod_loaded {
1111-
(attrs, items) = extern_mod_loaded(
1110+
if let Some(lint_store) = ecx.lint_store {
1111+
lint_store.pre_expansion_lint(
1112+
ecx.sess,
1113+
ecx.resolver.registered_tools(),
11121114
ecx.current_expansion.lint_node_id,
1113-
attrs,
1114-
items,
1115-
ident.name,
1115+
&attrs,
1116+
&items,
1117+
ident.name.as_str(),
11161118
);
11171119
}
11181120

compiler/rustc_interface/src/passes.rs

+20-13
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use rustc_data_structures::parallel;
1111
use rustc_data_structures::sync::{Lrc, OnceCell, WorkerLocal};
1212
use rustc_data_structures::temp_dir::MaybeTempDir;
1313
use rustc_errors::{Applicability, ErrorReported, PResult};
14-
use rustc_expand::base::{ExtCtxt, ResolverExpand};
14+
use rustc_expand::base::{ExtCtxt, LintStoreExpand, ResolverExpand};
1515
use rustc_hir::def_id::{StableCrateId, LOCAL_CRATE};
1616
use rustc_hir::Crate;
1717
use rustc_lint::{EarlyCheckNode, LintStore};
@@ -253,6 +253,23 @@ fn pre_expansion_lint<'a>(
253253
});
254254
}
255255

256+
// Cannot implement directly for `LintStore` due to trait coherence.
257+
struct LintStoreExpandImpl<'a>(&'a LintStore);
258+
259+
impl LintStoreExpand for LintStoreExpandImpl<'_> {
260+
fn pre_expansion_lint(
261+
&self,
262+
sess: &Session,
263+
registered_tools: &RegisteredTools,
264+
node_id: ast::NodeId,
265+
attrs: &[ast::Attribute],
266+
items: &[rustc_ast::ptr::P<ast::Item>],
267+
name: &str,
268+
) {
269+
pre_expansion_lint(sess, self.0, registered_tools, (node_id, attrs, items), name);
270+
}
271+
}
272+
256273
/// Runs the "early phases" of the compiler: initial `cfg` processing, loading compiler plugins,
257274
/// syntax expansion, secondary `cfg` expansion, synthesis of a test
258275
/// harness if one is to be provided, injection of a dependency on the
@@ -321,18 +338,8 @@ pub fn configure_and_expand(
321338
..rustc_expand::expand::ExpansionConfig::default(crate_name.to_string())
322339
};
323340

324-
let registered_tools = resolver.registered_tools().clone();
325-
let extern_mod_loaded = |node_id, attrs: Vec<_>, items: Vec<_>, name: Symbol| {
326-
pre_expansion_lint(
327-
sess,
328-
lint_store,
329-
&registered_tools,
330-
(node_id, &*attrs, &*items),
331-
name.as_str(),
332-
);
333-
(attrs, items)
334-
};
335-
let mut ecx = ExtCtxt::new(sess, cfg, resolver, Some(&extern_mod_loaded));
341+
let lint_store = LintStoreExpandImpl(lint_store);
342+
let mut ecx = ExtCtxt::new(sess, cfg, resolver, Some(&lint_store));
336343

337344
// Expand macros now!
338345
let krate = sess.time("expand_crate", || ecx.monotonic_expander().expand_crate(krate));

0 commit comments

Comments
 (0)