Skip to content

Commit da3419e

Browse files
committed
rustc_interface: Hide some hacky details of early linting from expand
1 parent 46b67aa commit da3419e

File tree

4 files changed

+15
-11
lines changed

4 files changed

+15
-11
lines changed

compiler/rustc_expand/src/base.rs

+6-3
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use rustc_ast::ptr::P;
55
use rustc_ast::token::{self, Nonterminal};
66
use rustc_ast::tokenstream::{CanSynthesizeMissingTokens, LazyTokenStream, TokenStream};
77
use rustc_ast::visit::{AssocCtxt, Visitor};
8-
use rustc_ast::{self as ast, AstLike, Attribute, NodeId, PatKind};
8+
use rustc_ast::{self as ast, AstLike, Attribute, Item, NodeId, PatKind};
99
use rustc_attr::{self as attr, Deprecation, Stability};
1010
use rustc_data_structures::fx::FxHashMap;
1111
use rustc_data_structures::sync::{self, Lrc};
@@ -925,6 +925,9 @@ pub struct ExpansionData {
925925
pub prior_type_ascription: Option<(Span, bool)>,
926926
}
927927

928+
type OnExternModLoaded<'a> =
929+
Option<&'a dyn Fn(Ident, Vec<Attribute>, Vec<P<Item>>, Span) -> (Vec<Attribute>, Vec<P<Item>>)>;
930+
928931
/// One of these is made during expansion and incrementally updated as we go;
929932
/// when a macro expansion occurs, the resulting nodes have the `backtrace()
930933
/// -> expn_data` of their expansion context stored into their span.
@@ -942,15 +945,15 @@ pub struct ExtCtxt<'a> {
942945
/// Called directly after having parsed an external `mod foo;` in expansion.
943946
///
944947
/// `Ident` is the module name.
945-
pub(super) extern_mod_loaded: Option<&'a dyn Fn(&ast::Crate, Ident)>,
948+
pub(super) extern_mod_loaded: OnExternModLoaded<'a>,
946949
}
947950

948951
impl<'a> ExtCtxt<'a> {
949952
pub fn new(
950953
sess: &'a Session,
951954
ecfg: expand::ExpansionConfig<'a>,
952955
resolver: &'a mut dyn ResolverExpand,
953-
extern_mod_loaded: Option<&'a dyn Fn(&ast::Crate, Ident)>,
956+
extern_mod_loaded: OnExternModLoaded<'a>,
954957
) -> ExtCtxt<'a> {
955958
ExtCtxt {
956959
sess,

compiler/rustc_expand/src/expand.rs

+4-6
Original file line numberDiff line numberDiff line change
@@ -1298,7 +1298,7 @@ impl<'a, 'b> MutVisitor for InvocationCollector<'a, 'b> {
12981298
ModKind::Unloaded => {
12991299
// We have an outline `mod foo;` so we need to parse the file.
13001300
let ParsedExternalMod {
1301-
items,
1301+
mut items,
13021302
inner_span,
13031303
file_path,
13041304
dir_path,
@@ -1312,14 +1312,12 @@ impl<'a, 'b> MutVisitor for InvocationCollector<'a, 'b> {
13121312
&mut attrs,
13131313
);
13141314

1315-
let krate =
1316-
ast::Crate { attrs, items, span: inner_span, proc_macros: vec![] };
13171315
if let Some(extern_mod_loaded) = self.cx.extern_mod_loaded {
1318-
extern_mod_loaded(&krate, ident);
1316+
(attrs, items) = extern_mod_loaded(ident, attrs, items, inner_span);
13191317
}
13201318

1321-
*mod_kind = ModKind::Loaded(krate.items, Inline::No, inner_span);
1322-
item.attrs = krate.attrs;
1319+
*mod_kind = ModKind::Loaded(items, Inline::No, inner_span);
1320+
item.attrs = attrs;
13231321
// File can have inline attributes, e.g., `#![cfg(...)]` & co. => Reconfigure.
13241322
item = configure!(self, item);
13251323
(Some(file_path), dir_path, dir_ownership)

compiler/rustc_expand/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#![feature(crate_visibility_modifier)]
22
#![feature(decl_macro)]
3+
#![feature(destructuring_assignment)]
34
#![feature(or_patterns)]
45
#![feature(proc_macro_diagnostic)]
56
#![feature(proc_macro_internals)]

compiler/rustc_interface/src/passes.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -302,8 +302,10 @@ fn configure_and_expand_inner<'a>(
302302
..rustc_expand::expand::ExpansionConfig::default(crate_name.to_string())
303303
};
304304

305-
let extern_mod_loaded = |k: &ast::Crate, ident: Ident| {
306-
pre_expansion_lint(sess, lint_store, k, &*ident.name.as_str())
305+
let extern_mod_loaded = |ident: Ident, attrs, items, span| {
306+
let krate = ast::Crate { attrs, items, span, proc_macros: vec![] };
307+
pre_expansion_lint(sess, lint_store, &krate, &ident.name.as_str());
308+
(krate.attrs, krate.items)
307309
};
308310
let mut ecx = ExtCtxt::new(&sess, cfg, &mut resolver, Some(&extern_mod_loaded));
309311

0 commit comments

Comments
 (0)