Skip to content

Commit 5aa732a

Browse files
committed
resolve: Cache module loading for all foreign modules
It was previously cached for modules loaded from `fn get_module`, but not for modules loaded from `fn build_reduced_graph_for_external_crate_res`. This also makes all foreign modules use their real parent, span and expansion instead of possibly a parent/span/expansion of their reexport. An ICE happening on attempt to decode expansions for foreign enums and traits is avoided. Also local enums and traits are now added to the module map.
1 parent b27661e commit 5aa732a

9 files changed

+71
-29
lines changed

compiler/rustc_resolve/src/build_reduced_graph.rs

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,6 @@ impl<'a> Resolver<'a> {
127127
/// If `def_id` refers to a module (in resolver's sense, i.e. a module item, crate root, enum,
128128
/// or trait), then this function returns that module's resolver representation, otherwise it
129129
/// returns `None`.
130-
/// FIXME: `Module`s for local enums and traits are not currently found.
131130
crate fn get_module(&mut self, def_id: DefId) -> Option<Module<'a>> {
132131
if let module @ Some(..) = self.module_map.get(&def_id) {
133132
return module.copied();
@@ -146,17 +145,21 @@ impl<'a> Resolver<'a> {
146145
} else {
147146
def_key.disambiguated_data.data.get_opt_name().expect("module without name")
148147
};
148+
let expn_id = if def_kind == DefKind::Mod {
149+
self.cstore().module_expansion_untracked(def_id, &self.session)
150+
} else {
151+
// FIXME: Parent expansions for enums and traits are not kept in metadata.
152+
ExpnId::root()
153+
};
149154

150-
let module = self.arenas.new_module(
155+
Some(self.new_module(
151156
parent,
152157
ModuleKind::Def(def_kind, def_id, name),
153-
self.cstore().module_expansion_untracked(def_id, &self.session),
158+
expn_id,
154159
self.cstore().get_span_untracked(def_id, &self.session),
155160
// FIXME: Account for `#[no_implicit_prelude]` attributes.
156161
parent.map_or(false, |module| module.no_implicit_prelude),
157-
);
158-
self.module_map.insert(def_id, module);
159-
Some(module)
162+
))
160163
}
161164
_ => None,
162165
}
@@ -759,7 +762,7 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
759762
}
760763

761764
ItemKind::Mod(..) => {
762-
let module = self.r.arenas.new_module(
765+
let module = self.r.new_module(
763766
Some(parent),
764767
ModuleKind::Def(DefKind::Mod, def_id, ident.name),
765768
expansion.to_expn_id(),
@@ -768,7 +771,6 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
768771
|| self.r.session.contains_name(&item.attrs, sym::no_implicit_prelude),
769772
);
770773
self.r.define(parent, ident, TypeNS, (module, vis, sp, expansion));
771-
self.r.module_map.insert(def_id, module);
772774

773775
// Descend into the module.
774776
self.parent_scope.module = module;
@@ -799,7 +801,7 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
799801
}
800802

801803
ItemKind::Enum(_, _) => {
802-
let module = self.r.arenas.new_module(
804+
let module = self.r.new_module(
803805
Some(parent),
804806
ModuleKind::Def(DefKind::Enum, def_id, ident.name),
805807
expansion.to_expn_id(),
@@ -873,7 +875,7 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
873875

874876
ItemKind::Trait(..) => {
875877
// Add all the items within to a new module.
876-
let module = self.r.arenas.new_module(
878+
let module = self.r.new_module(
877879
Some(parent),
878880
ModuleKind::Def(DefKind::Trait, def_id, ident.name),
879881
expansion.to_expn_id(),
@@ -916,7 +918,7 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
916918
let parent = self.parent_scope.module;
917919
let expansion = self.parent_scope.expansion;
918920
if self.block_needs_anonymous_module(block) {
919-
let module = self.r.arenas.new_module(
921+
let module = self.r.new_module(
920922
Some(parent),
921923
ModuleKind::Block(block.id),
922924
expansion.to_expn_id(),
@@ -936,15 +938,8 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
936938
let expansion = self.parent_scope.expansion;
937939
// Record primary definitions.
938940
match res {
939-
Res::Def(kind @ (DefKind::Mod | DefKind::Enum | DefKind::Trait), def_id) => {
940-
let module = self.r.arenas.new_module(
941-
Some(parent),
942-
ModuleKind::Def(kind, def_id, ident.name),
943-
expansion.to_expn_id(),
944-
span,
945-
// FIXME: Account for `#[no_implicit_prelude]` attributes.
946-
parent.no_implicit_prelude,
947-
);
941+
Res::Def(DefKind::Mod | DefKind::Enum | DefKind::Trait, def_id) => {
942+
let module = self.r.expect_module(def_id);
948943
self.r.define(parent, ident, TypeNS, (module, vis, span, expansion));
949944
}
950945
Res::Def(

compiler/rustc_resolve/src/lib.rs

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1071,12 +1071,17 @@ impl<'a> ResolverArenas<'a> {
10711071
expn_id: ExpnId,
10721072
span: Span,
10731073
no_implicit_prelude: bool,
1074+
module_map: &mut FxHashMap<DefId, Module<'a>>,
10741075
) -> Module<'a> {
10751076
let module =
10761077
self.modules.alloc(ModuleData::new(parent, kind, expn_id, span, no_implicit_prelude));
1077-
if module.def_id().map_or(true, |def_id| def_id.is_local()) {
1078+
let def_id = module.def_id();
1079+
if def_id.map_or(true, |def_id| def_id.is_local()) {
10781080
self.local_modules.borrow_mut().push(module);
10791081
}
1082+
if let Some(def_id) = def_id {
1083+
module_map.insert(def_id, module);
1084+
}
10801085
module
10811086
}
10821087
fn local_modules(&'a self) -> std::cell::Ref<'a, Vec<Module<'a>>> {
@@ -1276,22 +1281,23 @@ impl<'a> Resolver<'a> {
12761281
arenas: &'a ResolverArenas<'a>,
12771282
) -> Resolver<'a> {
12781283
let root_def_id = CRATE_DEF_ID.to_def_id();
1284+
let mut module_map = FxHashMap::default();
12791285
let graph_root = arenas.new_module(
12801286
None,
12811287
ModuleKind::Def(DefKind::Mod, root_def_id, kw::Empty),
12821288
ExpnId::root(),
12831289
krate.span,
12841290
session.contains_name(&krate.attrs, sym::no_implicit_prelude),
1291+
&mut module_map,
12851292
);
12861293
let empty_module = arenas.new_module(
12871294
None,
12881295
ModuleKind::Def(DefKind::Mod, root_def_id, kw::Empty),
12891296
ExpnId::root(),
12901297
DUMMY_SP,
12911298
true,
1299+
&mut FxHashMap::default(),
12921300
);
1293-
let mut module_map = FxHashMap::default();
1294-
module_map.insert(root_def_id, graph_root);
12951301

12961302
let definitions = Definitions::new(session.local_stable_crate_id(), krate.span);
12971303
let root = definitions.get_root_def();
@@ -1434,6 +1440,18 @@ impl<'a> Resolver<'a> {
14341440
resolver
14351441
}
14361442

1443+
fn new_module(
1444+
&mut self,
1445+
parent: Option<Module<'a>>,
1446+
kind: ModuleKind,
1447+
expn_id: ExpnId,
1448+
span: Span,
1449+
no_implicit_prelude: bool,
1450+
) -> Module<'a> {
1451+
let module_map = &mut self.module_map;
1452+
self.arenas.new_module(parent, kind, expn_id, span, no_implicit_prelude, module_map)
1453+
}
1454+
14371455
fn create_stable_hashing_context(&self) -> ExpandHasher<'_, 'a> {
14381456
ExpandHasher {
14391457
source_map: CachingSourceMapView::new(self.session.source_map()),
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#![feature(decl_macro)]
2+
3+
mod inner1 {
4+
pub struct Struct {}
5+
6+
pub mod inner2 {
7+
pub macro mac() {
8+
super::Struct
9+
}
10+
}
11+
}
12+
13+
pub use inner1::inner2 as public;
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// `super` in a `macro` refers to the parent module of the macro itself and not its reexport.
2+
3+
// check-pass
4+
// aux-build:macro-def-site-super.rs
5+
6+
extern crate macro_def_site_super;
7+
8+
type A = macro_def_site_super::public::mac!();
9+
10+
fn main() {}

src/test/ui/proc-macro/meta-macro-hygiene.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
// compile-flags: -Z span-debug -Z macro-backtrace -Z unpretty=expanded,hygiene -Z trim-diagnostic-paths=no
55
// check-pass
66
// normalize-stdout-test "\d+#" -> "0#"
7+
// normalize-stdout-test "expn\d{3,}" -> "expnNNN"
78
//
89
// We don't care about symbol ids, so we set them all to 0
910
// in the stdout

src/test/ui/proc-macro/meta-macro-hygiene.stdout

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
Def site: $DIR/auxiliary/make-macro.rs:7:9: 7:56 (#5)
2-
Input: TokenStream [Ident { ident: "$crate", span: $DIR/meta-macro-hygiene.rs:23:37: 23:43 (#4) }, Punct { ch: ':', spacing: Joint, span: $DIR/meta-macro-hygiene.rs:23:43: 23:45 (#4) }, Punct { ch: ':', spacing: Alone, span: $DIR/meta-macro-hygiene.rs:23:43: 23:45 (#4) }, Ident { ident: "dummy", span: $DIR/meta-macro-hygiene.rs:23:45: 23:50 (#4) }, Punct { ch: '!', spacing: Alone, span: $DIR/meta-macro-hygiene.rs:23:50: 23:51 (#4) }, Group { delimiter: Parenthesis, stream: TokenStream [], span: $DIR/meta-macro-hygiene.rs:23:51: 23:53 (#4) }]
2+
Input: TokenStream [Ident { ident: "$crate", span: $DIR/meta-macro-hygiene.rs:24:37: 24:43 (#4) }, Punct { ch: ':', spacing: Joint, span: $DIR/meta-macro-hygiene.rs:24:43: 24:45 (#4) }, Punct { ch: ':', spacing: Alone, span: $DIR/meta-macro-hygiene.rs:24:43: 24:45 (#4) }, Ident { ident: "dummy", span: $DIR/meta-macro-hygiene.rs:24:45: 24:50 (#4) }, Punct { ch: '!', spacing: Alone, span: $DIR/meta-macro-hygiene.rs:24:50: 24:51 (#4) }, Group { delimiter: Parenthesis, stream: TokenStream [], span: $DIR/meta-macro-hygiene.rs:24:51: 24:53 (#4) }]
33
Respanned: TokenStream [Ident { ident: "$crate", span: $DIR/auxiliary/make-macro.rs:7:9: 7:56 (#5) }, Punct { ch: ':', spacing: Joint, span: $DIR/auxiliary/make-macro.rs:7:9: 7:56 (#5) }, Punct { ch: ':', spacing: Alone, span: $DIR/auxiliary/make-macro.rs:7:9: 7:56 (#5) }, Ident { ident: "dummy", span: $DIR/auxiliary/make-macro.rs:7:9: 7:56 (#5) }, Punct { ch: '!', spacing: Alone, span: $DIR/auxiliary/make-macro.rs:7:9: 7:56 (#5) }, Group { delimiter: Parenthesis, stream: TokenStream [], span: $DIR/auxiliary/make-macro.rs:7:9: 7:56 (#5) }]
44
#![feature /* 0#0 */(prelude_import)]
55
// aux-build:make-macro.rs
@@ -8,6 +8,7 @@ Respanned: TokenStream [Ident { ident: "$crate", span: $DIR/auxiliary/make-macro
88
// compile-flags: -Z span-debug -Z macro-backtrace -Z unpretty=expanded,hygiene -Z trim-diagnostic-paths=no
99
// check-pass
1010
// normalize-stdout-test "\d+#" -> "0#"
11+
// normalize-stdout-test "expn\d{3,}" -> "expnNNN"
1112
//
1213
// We don't care about symbol ids, so we set them all to 0
1314
// in the stdout
@@ -48,6 +49,7 @@ crate0::{{expn1}}: parent: crate0::{{expn0}}, call_site_ctxt: #0, def_site_ctxt:
4849
crate0::{{expn2}}: parent: crate0::{{expn0}}, call_site_ctxt: #0, def_site_ctxt: #0, kind: Macro(Bang, "produce_it")
4950
crate0::{{expn3}}: parent: crate0::{{expn2}}, call_site_ctxt: #4, def_site_ctxt: #0, kind: Macro(Bang, "meta_macro::print_def_site")
5051
crate0::{{expn4}}: parent: crate0::{{expn3}}, call_site_ctxt: #5, def_site_ctxt: #0, kind: Macro(Bang, "$crate::dummy")
52+
crate1::{{expnNNN}}: parent: crate0::{{expn0}}, call_site_ctxt: #0, def_site_ctxt: #0, kind: Macro(Bang, "include")
5153
crate2::{{expn1}}: parent: crate0::{{expn0}}, call_site_ctxt: #0, def_site_ctxt: #0, kind: AstPass(StdImports)
5254

5355
SyntaxContexts:

src/test/ui/proc-macro/nonterminal-token-hygiene.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
// compile-flags: -Z span-debug -Z macro-backtrace -Z unpretty=expanded,hygiene
55
// compile-flags: -Z trim-diagnostic-paths=no
66
// normalize-stdout-test "\d+#" -> "0#"
7+
// normalize-stdout-test "expn\d{3,}" -> "expnNNN"
78
// aux-build:test-macros.rs
89

910
#![feature(decl_macro)]

src/test/ui/proc-macro/nonterminal-token-hygiene.stdout

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,19 @@ PRINT-BANG INPUT (DEBUG): TokenStream [
66
stream: TokenStream [
77
Ident {
88
ident: "struct",
9-
span: $DIR/nonterminal-token-hygiene.rs:30:5: 30:11 (#5),
9+
span: $DIR/nonterminal-token-hygiene.rs:31:5: 31:11 (#5),
1010
},
1111
Ident {
1212
ident: "S",
13-
span: $DIR/nonterminal-token-hygiene.rs:30:12: 30:13 (#5),
13+
span: $DIR/nonterminal-token-hygiene.rs:31:12: 31:13 (#5),
1414
},
1515
Punct {
1616
ch: ';',
1717
spacing: Alone,
18-
span: $DIR/nonterminal-token-hygiene.rs:30:13: 30:14 (#5),
18+
span: $DIR/nonterminal-token-hygiene.rs:31:13: 31:14 (#5),
1919
},
2020
],
21-
span: $DIR/nonterminal-token-hygiene.rs:20:27: 20:32 (#6),
21+
span: $DIR/nonterminal-token-hygiene.rs:21:27: 21:32 (#6),
2222
},
2323
]
2424
#![feature /* 0#0 */(prelude_import)]
@@ -29,6 +29,7 @@ PRINT-BANG INPUT (DEBUG): TokenStream [
2929
// compile-flags: -Z span-debug -Z macro-backtrace -Z unpretty=expanded,hygiene
3030
// compile-flags: -Z trim-diagnostic-paths=no
3131
// normalize-stdout-test "\d+#" -> "0#"
32+
// normalize-stdout-test "expn\d{3,}" -> "expnNNN"
3233
// aux-build:test-macros.rs
3334

3435
#![feature /* 0#0 */(decl_macro)]
@@ -72,6 +73,7 @@ crate0::{{expn1}}: parent: crate0::{{expn0}}, call_site_ctxt: #0, def_site_ctxt:
7273
crate0::{{expn2}}: parent: crate0::{{expn0}}, call_site_ctxt: #0, def_site_ctxt: #0, kind: Macro(Bang, "outer")
7374
crate0::{{expn3}}: parent: crate0::{{expn2}}, call_site_ctxt: #4, def_site_ctxt: #4, kind: Macro(Bang, "inner")
7475
crate0::{{expn4}}: parent: crate0::{{expn3}}, call_site_ctxt: #6, def_site_ctxt: #0, kind: Macro(Bang, "print_bang")
76+
crate1::{{expnNNN}}: parent: crate0::{{expn0}}, call_site_ctxt: #0, def_site_ctxt: #0, kind: Macro(Bang, "include")
7577
crate2::{{expn1}}: parent: crate0::{{expn0}}, call_site_ctxt: #0, def_site_ctxt: #0, kind: AstPass(StdImports)
7678

7779
SyntaxContexts:

src/test/ui/use/use-from-trait-xc.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ error[E0432]: unresolved import `use_from_trait_xc::Baz::new`
3838
--> $DIR/use-from-trait-xc.rs:23:5
3939
|
4040
LL | use use_from_trait_xc::Baz::new as baznew;
41-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ no `new` in `Baz`
41+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ no `new` in `sub::Baz`
4242

4343
error[E0603]: struct `Foo` is private
4444
--> $DIR/use-from-trait-xc.rs:14:24

0 commit comments

Comments
 (0)