Skip to content

Commit 10b3a57

Browse files
committed
Factor out build reduced graph for extern crate
1 parent 3c51718 commit 10b3a57

File tree

1 file changed

+83
-69
lines changed

1 file changed

+83
-69
lines changed

compiler/rustc_resolve/src/build_reduced_graph.rs

+83-69
Original file line numberDiff line numberDiff line change
@@ -683,75 +683,16 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
683683
}
684684

685685
ItemKind::ExternCrate(orig_name) => {
686-
let module = if orig_name.is_none() && ident.name == kw::SelfLower {
687-
self.r
688-
.session
689-
.struct_span_err(item.span, "`extern crate self;` requires renaming")
690-
.span_suggestion(
691-
item.span,
692-
"try",
693-
"extern crate self as name;".into(),
694-
Applicability::HasPlaceholders,
695-
)
696-
.emit();
697-
return;
698-
} else if orig_name == Some(kw::SelfLower) {
699-
self.r.graph_root
700-
} else {
701-
let crate_id = self.r.crate_loader.process_extern_crate(
702-
item,
703-
&self.r.definitions,
704-
local_def_id,
705-
);
706-
self.r.extern_crate_map.insert(local_def_id, crate_id);
707-
self.r.expect_module(crate_id.as_def_id())
708-
};
709-
710-
let used = self.process_macro_use_imports(item, module);
711-
let binding =
712-
(module, ty::Visibility::Public, sp, expansion).to_name_binding(self.r.arenas);
713-
let import = self.r.arenas.alloc_import(Import {
714-
kind: ImportKind::ExternCrate { source: orig_name, target: ident },
715-
root_id: item.id,
716-
id: item.id,
717-
parent_scope: self.parent_scope,
718-
imported_module: Cell::new(Some(ModuleOrUniformRoot::Module(module))),
719-
has_attributes: !item.attrs.is_empty(),
720-
use_span_with_attributes: item.span_with_attributes(),
721-
use_span: item.span,
722-
root_span: item.span,
723-
span: item.span,
724-
module_path: Vec::new(),
725-
vis: Cell::new(vis),
726-
used: Cell::new(used),
727-
});
728-
self.r.potentially_unused_imports.push(import);
729-
let imported_binding = self.r.import(binding, import);
730-
if ptr::eq(parent, self.r.graph_root) {
731-
if let Some(entry) = self.r.extern_prelude.get(&ident.normalize_to_macros_2_0())
732-
{
733-
if expansion != LocalExpnId::ROOT
734-
&& orig_name.is_some()
735-
&& entry.extern_crate_item.is_none()
736-
{
737-
let msg = "macro-expanded `extern crate` items cannot \
738-
shadow names passed with `--extern`";
739-
self.r.session.span_err(item.span, msg);
740-
}
741-
}
742-
let entry =
743-
self.r.extern_prelude.entry(ident.normalize_to_macros_2_0()).or_insert(
744-
ExternPreludeEntry {
745-
extern_crate_item: None,
746-
introduced_by_item: true,
747-
},
748-
);
749-
entry.extern_crate_item = Some(imported_binding);
750-
if orig_name.is_some() {
751-
entry.introduced_by_item = true;
752-
}
753-
}
754-
self.r.define(parent, ident, TypeNS, imported_binding);
686+
self.build_reduced_graph_for_extern_crate(
687+
orig_name,
688+
ident,
689+
item,
690+
local_def_id,
691+
sp,
692+
expansion,
693+
vis,
694+
parent,
695+
);
755696
}
756697

757698
ItemKind::Mod(..) => {
@@ -889,6 +830,79 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
889830
}
890831
}
891832

833+
fn build_reduced_graph_for_extern_crate(
834+
&mut self,
835+
orig_name: Option<Symbol>,
836+
ident: Ident,
837+
item: &Item,
838+
local_def_id: LocalDefId,
839+
sp: Span,
840+
expansion: LocalExpnId,
841+
vis: ty::Visibility,
842+
parent: Module<'a>,
843+
) {
844+
let module = if orig_name.is_none() && ident.name == kw::SelfLower {
845+
self.r
846+
.session
847+
.struct_span_err(item.span, "`extern crate self;` requires renaming")
848+
.span_suggestion(
849+
item.span,
850+
"try",
851+
"extern crate self as name;".into(),
852+
Applicability::HasPlaceholders,
853+
)
854+
.emit();
855+
return;
856+
} else if orig_name == Some(kw::SelfLower) {
857+
self.r.graph_root
858+
} else {
859+
let crate_id =
860+
self.r.crate_loader.process_extern_crate(item, &self.r.definitions, local_def_id);
861+
self.r.extern_crate_map.insert(local_def_id, crate_id);
862+
self.r.expect_module(crate_id.as_def_id())
863+
};
864+
let used = self.process_macro_use_imports(item, module);
865+
let binding =
866+
(module, ty::Visibility::Public, sp, expansion).to_name_binding(self.r.arenas);
867+
let import = self.r.arenas.alloc_import(Import {
868+
kind: ImportKind::ExternCrate { source: orig_name, target: ident },
869+
root_id: item.id,
870+
id: item.id,
871+
parent_scope: self.parent_scope,
872+
imported_module: Cell::new(Some(ModuleOrUniformRoot::Module(module))),
873+
has_attributes: !item.attrs.is_empty(),
874+
use_span_with_attributes: item.span_with_attributes(),
875+
use_span: item.span,
876+
root_span: item.span,
877+
span: item.span,
878+
module_path: Vec::new(),
879+
vis: Cell::new(vis),
880+
used: Cell::new(used),
881+
});
882+
self.r.potentially_unused_imports.push(import);
883+
let imported_binding = self.r.import(binding, import);
884+
if ptr::eq(parent, self.r.graph_root) {
885+
if let Some(entry) = self.r.extern_prelude.get(&ident.normalize_to_macros_2_0()) {
886+
if expansion != LocalExpnId::ROOT
887+
&& orig_name.is_some()
888+
&& entry.extern_crate_item.is_none()
889+
{
890+
let msg = "macro-expanded `extern crate` items cannot \
891+
shadow names passed with `--extern`";
892+
self.r.session.span_err(item.span, msg);
893+
}
894+
}
895+
let entry = self.r.extern_prelude.entry(ident.normalize_to_macros_2_0()).or_insert(
896+
ExternPreludeEntry { extern_crate_item: None, introduced_by_item: true },
897+
);
898+
entry.extern_crate_item = Some(imported_binding);
899+
if orig_name.is_some() {
900+
entry.introduced_by_item = true;
901+
}
902+
}
903+
self.r.define(parent, ident, TypeNS, imported_binding);
904+
}
905+
892906
/// Constructs the reduced graph for one foreign item.
893907
fn build_reduced_graph_for_foreign_item(&mut self, item: &ForeignItem) {
894908
let local_def_id = self.r.local_def_id(item.id);

0 commit comments

Comments
 (0)