Skip to content

Commit

Permalink
Remove ForeignMod struct.
Browse files Browse the repository at this point in the history
  • Loading branch information
cjgillot committed Nov 26, 2020
1 parent 419a918 commit 032f68d
Show file tree
Hide file tree
Showing 26 changed files with 69 additions and 85 deletions.
16 changes: 6 additions & 10 deletions compiler/rustc_ast_lowering/src/item.rs
Expand Up @@ -316,7 +316,12 @@ impl<'hir> LoweringContext<'_, 'hir> {
})
}
ItemKind::Mod(ref m) => hir::ItemKind::Mod(self.lower_mod(m)),
ItemKind::ForeignMod(ref nm) => hir::ItemKind::ForeignMod(self.lower_foreign_mod(nm)),
ItemKind::ForeignMod(ref fm) => hir::ItemKind::ForeignMod {
abi: fm.abi.map_or(abi::Abi::C, |abi| self.lower_abi(abi)),
items: self
.arena
.alloc_from_iter(fm.items.iter().map(|x| self.lower_foreign_item_ref(x))),
},
ItemKind::GlobalAsm(ref ga) => hir::ItemKind::GlobalAsm(self.lower_global_asm(ga)),
ItemKind::TyAlias(_, ref gen, _, Some(ref ty)) => {
// We lower
Expand Down Expand Up @@ -725,15 +730,6 @@ impl<'hir> LoweringContext<'_, 'hir> {
}
}

fn lower_foreign_mod(&mut self, fm: &ForeignMod) -> hir::ForeignMod<'hir> {
hir::ForeignMod {
abi: fm.abi.map_or(abi::Abi::C, |abi| self.lower_abi(abi)),
items: self
.arena
.alloc_from_iter(fm.items.iter().map(|x| self.lower_foreign_item_ref(x))),
}
}

fn lower_global_asm(&mut self, ga: &GlobalAsm) -> &'hir hir::GlobalAsm {
self.arena.alloc(hir::GlobalAsm { asm: ga.asm })
}
Expand Down
8 changes: 1 addition & 7 deletions compiler/rustc_hir/src/hir.rs
Expand Up @@ -2284,12 +2284,6 @@ pub struct Mod<'hir> {
pub item_ids: &'hir [ItemId],
}

#[derive(Debug, HashStable_Generic)]
pub struct ForeignMod<'hir> {
pub abi: Abi,
pub items: &'hir [ForeignItemRef<'hir>],
}

#[derive(Encodable, Debug, HashStable_Generic)]
pub struct GlobalAsm {
pub asm: Symbol,
Expand Down Expand Up @@ -2536,7 +2530,7 @@ pub enum ItemKind<'hir> {
/// A module.
Mod(Mod<'hir>),
/// An external module, e.g. `extern { .. }`.
ForeignMod(ForeignMod<'hir>),
ForeignMod { abi: Abi, items: &'hir [ForeignItemRef<'hir>] },
/// Module-level inline assembly (from `global_asm!`).
GlobalAsm(&'hir GlobalAsm),
/// A type alias, e.g., `type Foo = Bar<u8>`.
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_hir/src/intravisit.rs
Expand Up @@ -589,9 +589,9 @@ pub fn walk_item<'v, V: Visitor<'v>>(visitor: &mut V, item: &'v Item<'v>) {
// `visit_mod()` takes care of visiting the `Item`'s `HirId`.
visitor.visit_mod(module, item.span, item.hir_id)
}
ItemKind::ForeignMod(ref foreign_module) => {
ItemKind::ForeignMod { abi: _, items } => {
visitor.visit_id(item.hir_id);
walk_list!(visitor, visit_foreign_item_ref, foreign_module.items);
walk_list!(visitor, visit_foreign_item_ref, items);
}
ItemKind::GlobalAsm(_) => {
visitor.visit_id(item.hir_id);
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_hir/src/target.rs
Expand Up @@ -91,7 +91,7 @@ impl Target {
ItemKind::Const(..) => Target::Const,
ItemKind::Fn(..) => Target::Fn,
ItemKind::Mod(..) => Target::Mod,
ItemKind::ForeignMod(..) => Target::ForeignMod,
ItemKind::ForeignMod { .. } => Target::ForeignMod,
ItemKind::GlobalAsm(..) => Target::GlobalAsm,
ItemKind::TyAlias(..) => Target::TyAlias,
ItemKind::OpaqueTy(..) => Target::OpaqueTy,
Expand Down
16 changes: 6 additions & 10 deletions compiler/rustc_hir_pretty/src/lib.rs
Expand Up @@ -352,13 +352,6 @@ impl<'a> State<'a> {
}
}

pub fn print_foreign_mod(&mut self, nmod: &hir::ForeignMod<'_>, attrs: &[ast::Attribute]) {
self.print_inner_attributes(attrs);
for item in nmod.items {
self.ann.nested(self, Nested::ForeignItem(item.id));
}
}

pub fn print_opt_lifetime(&mut self, lifetime: &hir::Lifetime) {
if !lifetime.is_elided() {
self.print_lifetime(lifetime);
Expand Down Expand Up @@ -647,11 +640,14 @@ impl<'a> State<'a> {
self.print_mod(_mod, &item.attrs);
self.bclose(item.span);
}
hir::ItemKind::ForeignMod(ref nmod) => {
hir::ItemKind::ForeignMod { abi, items } => {
self.head("extern");
self.word_nbsp(nmod.abi.to_string());
self.word_nbsp(abi.to_string());
self.bopen();
self.print_foreign_mod(nmod, &item.attrs);
self.print_inner_attributes(item.attrs);
for item in items {
self.ann.nested(self, Nested::ForeignItem(item.id));
}
self.bclose(item.span);
}
hir::ItemKind::GlobalAsm(ref ga) => {
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_incremental/src/persist/dirty_clean.rs
Expand Up @@ -280,7 +280,7 @@ impl DirtyCleanVisitor<'tcx> {
HirItem::Mod(..) => ("ItemMod", LABELS_HIR_ONLY),

// // An external module
HirItem::ForeignMod(..) => ("ItemForeignMod", LABELS_HIR_ONLY),
HirItem::ForeignMod { .. } => ("ItemForeignMod", LABELS_HIR_ONLY),

// Module-level inline assembly (from global_asm!)
HirItem::GlobalAsm(..) => ("ItemGlobalAsm", LABELS_HIR_ONLY),
Expand Down
11 changes: 4 additions & 7 deletions compiler/rustc_metadata/src/foreign_modules.rs
Expand Up @@ -16,16 +16,13 @@ struct Collector<'tcx> {

impl ItemLikeVisitor<'tcx> for Collector<'tcx> {
fn visit_item(&mut self, it: &'tcx hir::Item<'tcx>) {
let fm = match it.kind {
hir::ItemKind::ForeignMod(ref fm) => fm,
let items = match it.kind {
hir::ItemKind::ForeignMod { items, .. } => items,
_ => return,
};

let foreign_items = fm
.items
.iter()
.map(|it| self.tcx.hir().local_def_id(it.id.hir_id).to_def_id())
.collect();
let foreign_items =
items.iter().map(|it| self.tcx.hir().local_def_id(it.id.hir_id).to_def_id()).collect();
self.modules.push(ForeignModule {
foreign_items,
def_id: self.tcx.hir().local_def_id(it.hir_id).to_def_id(),
Expand Down
6 changes: 3 additions & 3 deletions compiler/rustc_metadata/src/link_args.rs
Expand Up @@ -26,11 +26,11 @@ struct Collector<'tcx> {

impl<'tcx> ItemLikeVisitor<'tcx> for Collector<'tcx> {
fn visit_item(&mut self, it: &'tcx hir::Item<'tcx>) {
let fm = match it.kind {
hir::ItemKind::ForeignMod(ref fm) => fm,
let abi = match it.kind {
hir::ItemKind::ForeignMod { abi, .. } => abi,
_ => return,
};
if fm.abi == Abi::Rust || fm.abi == Abi::RustIntrinsic || fm.abi == Abi::PlatformIntrinsic {
if abi == Abi::Rust || abi == Abi::RustIntrinsic || abi == Abi::PlatformIntrinsic {
return;
}

Expand Down
6 changes: 3 additions & 3 deletions compiler/rustc_metadata/src/native_libs.rs
Expand Up @@ -33,12 +33,12 @@ struct Collector<'tcx> {

impl ItemLikeVisitor<'tcx> for Collector<'tcx> {
fn visit_item(&mut self, it: &'tcx hir::Item<'tcx>) {
let fm = match it.kind {
hir::ItemKind::ForeignMod(ref fm) => fm,
let abi = match it.kind {
hir::ItemKind::ForeignMod { abi, .. } => abi,
_ => return,
};

if fm.abi == Abi::Rust || fm.abi == Abi::RustIntrinsic || fm.abi == Abi::PlatformIntrinsic {
if abi == Abi::Rust || abi == Abi::RustIntrinsic || abi == Abi::PlatformIntrinsic {
return;
}

Expand Down
8 changes: 4 additions & 4 deletions compiler/rustc_metadata/src/rmeta/encoder.rs
Expand Up @@ -1225,7 +1225,7 @@ impl EncodeContext<'a, 'tcx> {
hir::ItemKind::Mod(ref m) => {
return self.encode_info_for_mod(item.hir_id, m, &item.attrs);
}
hir::ItemKind::ForeignMod(_) => EntryKind::ForeignMod,
hir::ItemKind::ForeignMod{..} => EntryKind::ForeignMod,
hir::ItemKind::GlobalAsm(..) => EntryKind::GlobalAsm,
hir::ItemKind::TyAlias(..) => EntryKind::Type,
hir::ItemKind::OpaqueTy(..) => {
Expand Down Expand Up @@ -1320,8 +1320,8 @@ impl EncodeContext<'a, 'tcx> {
record!(self.tables.expn_that_defined[def_id] <- self.tcx.expansion_that_defined(def_id));
// FIXME(eddyb) there should be a nicer way to do this.
match item.kind {
hir::ItemKind::ForeignMod(ref fm) => record!(self.tables.children[def_id] <-
fm.items
hir::ItemKind::ForeignMod { items, .. } => record!(self.tables.children[def_id] <-
items
.iter()
.map(|foreign_item| tcx.hir().local_def_id(
foreign_item.id.hir_id).local_def_index)
Expand Down Expand Up @@ -1836,7 +1836,7 @@ impl EncodeContext<'a, 'tcx> {
| hir::ItemKind::Const(..)
| hir::ItemKind::Fn(..)
| hir::ItemKind::Mod(..)
| hir::ItemKind::ForeignMod(..)
| hir::ItemKind::ForeignMod { .. }
| hir::ItemKind::GlobalAsm(..)
| hir::ItemKind::ExternCrate(..)
| hir::ItemKind::Use(..)
Expand Down
9 changes: 5 additions & 4 deletions compiler/rustc_middle/src/hir/map/mod.rs
Expand Up @@ -205,7 +205,7 @@ impl<'hir> Map<'hir> {
ItemKind::TraitAlias(..) => DefKind::TraitAlias,
ItemKind::ExternCrate(_) => DefKind::ExternCrate,
ItemKind::Use(..) => DefKind::Use,
ItemKind::ForeignMod(..) => DefKind::ForeignMod,
ItemKind::ForeignMod { .. } => DefKind::ForeignMod,
ItemKind::GlobalAsm(..) => DefKind::GlobalAsm,
ItemKind::Impl { .. } => DefKind::Impl,
},
Expand Down Expand Up @@ -729,10 +729,11 @@ impl<'hir> Map<'hir> {
let parent = self.get_parent_item(hir_id);
if let Some(entry) = self.find_entry(parent) {
if let Entry {
node: Node::Item(Item { kind: ItemKind::ForeignMod(ref nm), .. }), ..
node: Node::Item(Item { kind: ItemKind::ForeignMod { abi, .. }, .. }),
..
} = entry
{
return nm.abi;
return *abi;
}
}
bug!("expected foreign mod or inlined parent, found {}", self.node_to_string(parent))
Expand Down Expand Up @@ -1045,7 +1046,7 @@ fn hir_id_to_string(map: &Map<'_>, id: HirId) -> String {
ItemKind::Const(..) => "const",
ItemKind::Fn(..) => "fn",
ItemKind::Mod(..) => "mod",
ItemKind::ForeignMod(..) => "foreign mod",
ItemKind::ForeignMod { .. } => "foreign mod",
ItemKind::GlobalAsm(..) => "global asm",
ItemKind::TyAlias(..) => "ty",
ItemKind::OpaqueTy(..) => "opaque type",
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_mir/src/monomorphize/collector.rs
Expand Up @@ -993,7 +993,7 @@ impl ItemLikeVisitor<'v> for RootCollector<'_, 'v> {
match item.kind {
hir::ItemKind::ExternCrate(..)
| hir::ItemKind::Use(..)
| hir::ItemKind::ForeignMod(..)
| hir::ItemKind::ForeignMod { .. }
| hir::ItemKind::TyAlias(..)
| hir::ItemKind::Trait(..)
| hir::ItemKind::TraitAlias(..)
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_passes/src/dead.rs
Expand Up @@ -190,7 +190,7 @@ impl<'tcx> MarkSymbolVisitor<'tcx> {

intravisit::walk_item(self, &item);
}
hir::ItemKind::ForeignMod(..) => {}
hir::ItemKind::ForeignMod { .. } => {}
_ => {
intravisit::walk_item(self, &item);
}
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_passes/src/reachable.rs
Expand Up @@ -262,7 +262,7 @@ impl<'tcx> ReachableContext<'tcx> {
| hir::ItemKind::TyAlias(..)
| hir::ItemKind::Static(..)
| hir::ItemKind::Mod(..)
| hir::ItemKind::ForeignMod(..)
| hir::ItemKind::ForeignMod { .. }
| hir::ItemKind::Impl { .. }
| hir::ItemKind::Trait(..)
| hir::ItemKind::TraitAlias(..)
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_passes/src/stability.rs
Expand Up @@ -326,7 +326,7 @@ impl<'a, 'tcx> Visitor<'tcx> for Annotator<'a, 'tcx> {
// they don't have their own stability. They still can be annotated as unstable
// and propagate this unstability to children, but this annotation is completely
// optional. They inherit stability from their parents when unannotated.
hir::ItemKind::Impl { of_trait: None, .. } | hir::ItemKind::ForeignMod(..) => {
hir::ItemKind::Impl { of_trait: None, .. } | hir::ItemKind::ForeignMod { .. } => {
self.in_trait_impl = false;
kind = AnnotationKind::Container;
}
Expand Down Expand Up @@ -499,7 +499,7 @@ impl<'tcx> Visitor<'tcx> for MissingStabilityAnnotations<'tcx> {
// optional. They inherit stability from their parents when unannotated.
if !matches!(
i.kind,
hir::ItemKind::Impl { of_trait: None, .. } | hir::ItemKind::ForeignMod(..)
hir::ItemKind::Impl { of_trait: None, .. } | hir::ItemKind::ForeignMod{..}
) {
self.check_missing_stability(i.hir_id, i.span);
}
Expand Down
16 changes: 8 additions & 8 deletions compiler/rustc_privacy/src/lib.rs
Expand Up @@ -592,7 +592,7 @@ impl Visitor<'tcx> for EmbargoVisitor<'tcx> {
Option::<AccessLevel>::of_impl(item.hir_id, self.tcx, &self.access_levels)
}
// Foreign modules inherit level from parents.
hir::ItemKind::ForeignMod(..) => self.prev_level,
hir::ItemKind::ForeignMod { .. } => self.prev_level,
// Other `pub` items inherit levels from parents.
hir::ItemKind::Const(..)
| hir::ItemKind::Enum(..)
Expand Down Expand Up @@ -654,8 +654,8 @@ impl Visitor<'tcx> for EmbargoVisitor<'tcx> {
}
}
}
hir::ItemKind::ForeignMod(ref foreign_mod) => {
for foreign_item in foreign_mod.items {
hir::ItemKind::ForeignMod { items, .. } => {
for foreign_item in items {
if foreign_item.vis.node.is_pub() {
self.update(foreign_item.id.hir_id, item_level);
}
Expand Down Expand Up @@ -770,8 +770,8 @@ impl Visitor<'tcx> for EmbargoVisitor<'tcx> {
}
}
// Visit everything, but foreign items have their own levels.
hir::ItemKind::ForeignMod(ref foreign_mod) => {
for foreign_item in foreign_mod.items {
hir::ItemKind::ForeignMod { items, .. } => {
for foreign_item in items {
let foreign_item_level = self.get(foreign_item.id.hir_id);
if foreign_item_level.is_some() {
self.reach(foreign_item.id.hir_id, foreign_item_level)
Expand Down Expand Up @@ -1430,7 +1430,7 @@ impl<'a, 'tcx> Visitor<'tcx> for ObsoleteVisiblePrivateTypesVisitor<'a, 'tcx> {

// An `extern {}` doesn't introduce a new privacy
// namespace (the contents have their own privacies).
hir::ItemKind::ForeignMod(_) => {}
hir::ItemKind::ForeignMod { .. } => {}

hir::ItemKind::Trait(.., ref bounds, _) => {
if !self.trait_is_public(item.hir_id) {
Expand Down Expand Up @@ -1948,8 +1948,8 @@ impl<'a, 'tcx> Visitor<'tcx> for PrivateItemsInPublicInterfacesVisitor<'a, 'tcx>
}
}
// Subitems of foreign modules have their own publicity.
hir::ItemKind::ForeignMod(ref foreign_mod) => {
for foreign_item in foreign_mod.items {
hir::ItemKind::ForeignMod { items, .. } => {
for foreign_item in items {
let vis = tcx.visibility(tcx.hir().local_def_id(foreign_item.id.hir_id));
self.check(foreign_item.id.hir_id, vis).generics().predicates().ty();
}
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_resolve/src/late/lifetimes.rs
Expand Up @@ -388,7 +388,7 @@ impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> {
hir::ItemKind::ExternCrate(_)
| hir::ItemKind::Use(..)
| hir::ItemKind::Mod(..)
| hir::ItemKind::ForeignMod(..)
| hir::ItemKind::ForeignMod { .. }
| hir::ItemKind::GlobalAsm(..) => {
// These sorts of items have no lifetime parameters at all.
intravisit::walk_item(self, item);
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_save_analysis/src/sig.rs
Expand Up @@ -550,7 +550,7 @@ impl<'hir> Sig for hir::Item<'hir> {

// FIXME where clause
}
hir::ItemKind::ForeignMod(_) => Err("extern mod"),
hir::ItemKind::ForeignMod { .. } => Err("extern mod"),
hir::ItemKind::GlobalAsm(_) => Err("global asm"),
hir::ItemKind::ExternCrate(_) => Err("extern crate"),
hir::ItemKind::OpaqueTy(..) => Err("opaque type"),
Expand Down
16 changes: 8 additions & 8 deletions compiler/rustc_typeck/src/check/check.rs
Expand Up @@ -746,21 +746,21 @@ pub fn check_item_type<'tcx>(tcx: TyCtxt<'tcx>, it: &'tcx hir::Item<'tcx>) {
let generics = tcx.generics_of(def_id);
check_type_params_are_used(tcx, &generics, pty_ty);
}
hir::ItemKind::ForeignMod(ref m) => {
check_abi(tcx, it.span, m.abi);
hir::ItemKind::ForeignMod { abi, items } => {
check_abi(tcx, it.span, abi);

if m.abi == Abi::RustIntrinsic {
for item in m.items {
if abi == Abi::RustIntrinsic {
for item in items {
let item = tcx.hir().foreign_item(item.id);
intrinsic::check_intrinsic_type(tcx, item);
}
} else if m.abi == Abi::PlatformIntrinsic {
for item in m.items {
} else if abi == Abi::PlatformIntrinsic {
for item in items {
let item = tcx.hir().foreign_item(item.id);
intrinsic::check_platform_intrinsic_type(tcx, item);
}
} else {
for item in m.items {
for item in items {
let def_id = tcx.hir().local_def_id(item.id.hir_id);
let generics = tcx.generics_of(def_id);
let own_counts = generics.own_counts();
Expand Down Expand Up @@ -796,7 +796,7 @@ pub fn check_item_type<'tcx>(tcx: TyCtxt<'tcx>, it: &'tcx hir::Item<'tcx>) {
let item = tcx.hir().foreign_item(item.id);
match item.kind {
hir::ForeignItemKind::Fn(ref fn_decl, _, _) => {
require_c_abi_if_c_variadic(tcx, fn_decl, m.abi, item.span);
require_c_abi_if_c_variadic(tcx, fn_decl, abi, item.span);
}
hir::ForeignItemKind::Static(..) => {
check_static_inhabited(tcx, def_id, item.span);
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_typeck/src/check/wfcheck.rs
Expand Up @@ -156,8 +156,8 @@ pub fn check_item_well_formed(tcx: TyCtxt<'_>, def_id: LocalDefId) {
hir::ItemKind::Const(ref ty, ..) => {
check_item_type(tcx, item.hir_id, ty.span, false);
}
hir::ItemKind::ForeignMod(ref module) => {
for it in module.items.iter() {
hir::ItemKind::ForeignMod { items, .. } => {
for it in items.iter() {
let it = tcx.hir().foreign_item(it.id);
match it.kind {
hir::ForeignItemKind::Fn(ref decl, ..) => {
Expand Down

0 comments on commit 032f68d

Please sign in to comment.