|
181 | 181 | use rustc_data_structures::fx::{FxHashMap, FxHashSet}; |
182 | 182 | use rustc_data_structures::sync::{par_iter, MTLock, MTRef, ParallelIterator}; |
183 | 183 | use rustc_hir as hir; |
| 184 | +use rustc_hir::def::DefKind; |
184 | 185 | use rustc_hir::def_id::{DefId, DefIdMap, LocalDefId, LOCAL_CRATE}; |
185 | | -use rustc_hir::itemlikevisit::ItemLikeVisitor; |
186 | 186 | use rustc_hir::lang_items::LangItem; |
187 | 187 | use rustc_index::bit_set::GrowableBitSet; |
188 | 188 | use rustc_middle::mir::interpret::{AllocId, ConstValue}; |
@@ -327,11 +327,19 @@ fn collect_roots(tcx: TyCtxt<'_>, mode: MonoItemCollectionMode) -> Vec<MonoItem< |
327 | 327 |
|
328 | 328 | debug!("collect_roots: entry_fn = {:?}", entry_fn); |
329 | 329 |
|
330 | | - let mut visitor = RootCollector { tcx, mode, entry_fn, output: &mut roots }; |
| 330 | + let mut collector = RootCollector { tcx, mode, entry_fn, output: &mut roots }; |
331 | 331 |
|
332 | | - tcx.hir().visit_all_item_likes(&mut visitor); |
| 332 | + let crate_items = tcx.hir_crate_items(()); |
333 | 333 |
|
334 | | - visitor.push_extra_entry_roots(); |
| 334 | + for id in crate_items.items() { |
| 335 | + collector.process_item(id); |
| 336 | + } |
| 337 | + |
| 338 | + for id in crate_items.impl_items() { |
| 339 | + collector.process_impl_item(id); |
| 340 | + } |
| 341 | + |
| 342 | + collector.push_extra_entry_roots(); |
335 | 343 | } |
336 | 344 |
|
337 | 345 | // We can only codegen items that are instantiable - items all of |
@@ -1139,87 +1147,74 @@ struct RootCollector<'a, 'tcx> { |
1139 | 1147 | entry_fn: Option<(DefId, EntryFnType)>, |
1140 | 1148 | } |
1141 | 1149 |
|
1142 | | -impl<'v> ItemLikeVisitor<'v> for RootCollector<'_, 'v> { |
1143 | | - fn visit_item(&mut self, item: &'v hir::Item<'v>) { |
1144 | | - match item.kind { |
1145 | | - hir::ItemKind::ExternCrate(..) |
1146 | | - | hir::ItemKind::Use(..) |
1147 | | - | hir::ItemKind::Macro(..) |
1148 | | - | hir::ItemKind::ForeignMod { .. } |
1149 | | - | hir::ItemKind::TyAlias(..) |
1150 | | - | hir::ItemKind::Trait(..) |
1151 | | - | hir::ItemKind::TraitAlias(..) |
1152 | | - | hir::ItemKind::OpaqueTy(..) |
1153 | | - | hir::ItemKind::Mod(..) => { |
1154 | | - // Nothing to do, just keep recursing. |
1155 | | - } |
1156 | | - |
1157 | | - hir::ItemKind::Impl { .. } => { |
1158 | | - if self.mode == MonoItemCollectionMode::Eager { |
1159 | | - create_mono_items_for_default_impls(self.tcx, item, self.output); |
1160 | | - } |
1161 | | - } |
1162 | | - |
1163 | | - hir::ItemKind::Enum(_, ref generics) |
1164 | | - | hir::ItemKind::Struct(_, ref generics) |
1165 | | - | hir::ItemKind::Union(_, ref generics) => { |
1166 | | - if generics.params.is_empty() { |
1167 | | - if self.mode == MonoItemCollectionMode::Eager { |
1168 | | - debug!( |
1169 | | - "RootCollector: ADT drop-glue for {}", |
1170 | | - self.tcx.def_path_str(item.def_id.to_def_id()) |
1171 | | - ); |
1172 | | - |
1173 | | - let ty = Instance::new(item.def_id.to_def_id(), InternalSubsts::empty()) |
1174 | | - .ty(self.tcx, ty::ParamEnv::reveal_all()); |
1175 | | - visit_drop_use(self.tcx, ty, true, DUMMY_SP, self.output); |
| 1150 | +impl<'v> RootCollector<'_, 'v> { |
| 1151 | + fn process_item(&mut self, id: hir::ItemId) { |
| 1152 | + match self.tcx.hir().def_kind(id.def_id) { |
| 1153 | + DefKind::Enum | DefKind::Struct | DefKind::Union => { |
| 1154 | + let item = self.tcx.hir().item(id); |
| 1155 | + match item.kind { |
| 1156 | + hir::ItemKind::Enum(_, ref generics) |
| 1157 | + | hir::ItemKind::Struct(_, ref generics) |
| 1158 | + | hir::ItemKind::Union(_, ref generics) => { |
| 1159 | + if generics.params.is_empty() { |
| 1160 | + if self.mode == MonoItemCollectionMode::Eager { |
| 1161 | + debug!( |
| 1162 | + "RootCollector: ADT drop-glue for {}", |
| 1163 | + self.tcx.def_path_str(item.def_id.to_def_id()) |
| 1164 | + ); |
| 1165 | + |
| 1166 | + let ty = |
| 1167 | + Instance::new(item.def_id.to_def_id(), InternalSubsts::empty()) |
| 1168 | + .ty(self.tcx, ty::ParamEnv::reveal_all()); |
| 1169 | + visit_drop_use(self.tcx, ty, true, DUMMY_SP, self.output); |
| 1170 | + } |
| 1171 | + } |
1176 | 1172 | } |
| 1173 | + _ => {} |
1177 | 1174 | } |
1178 | 1175 | } |
1179 | | - hir::ItemKind::GlobalAsm(..) => { |
| 1176 | + DefKind::GlobalAsm => { |
1180 | 1177 | debug!( |
1181 | 1178 | "RootCollector: ItemKind::GlobalAsm({})", |
1182 | | - self.tcx.def_path_str(item.def_id.to_def_id()) |
| 1179 | + self.tcx.def_path_str(id.def_id.to_def_id()) |
1183 | 1180 | ); |
1184 | | - self.output.push(dummy_spanned(MonoItem::GlobalAsm(item.item_id()))); |
| 1181 | + self.output.push(dummy_spanned(MonoItem::GlobalAsm(id))); |
1185 | 1182 | } |
1186 | | - hir::ItemKind::Static(..) => { |
| 1183 | + DefKind::Static(..) => { |
1187 | 1184 | debug!( |
1188 | 1185 | "RootCollector: ItemKind::Static({})", |
1189 | | - self.tcx.def_path_str(item.def_id.to_def_id()) |
| 1186 | + self.tcx.def_path_str(id.def_id.to_def_id()) |
1190 | 1187 | ); |
1191 | | - self.output.push(dummy_spanned(MonoItem::Static(item.def_id.to_def_id()))); |
| 1188 | + self.output.push(dummy_spanned(MonoItem::Static(id.def_id.to_def_id()))); |
1192 | 1189 | } |
1193 | | - hir::ItemKind::Const(..) => { |
| 1190 | + DefKind::Const => { |
1194 | 1191 | // const items only generate mono items if they are |
1195 | 1192 | // actually used somewhere. Just declaring them is insufficient. |
1196 | 1193 |
|
1197 | 1194 | // but even just declaring them must collect the items they refer to |
1198 | | - if let Ok(val) = self.tcx.const_eval_poly(item.def_id.to_def_id()) { |
| 1195 | + if let Ok(val) = self.tcx.const_eval_poly(id.def_id.to_def_id()) { |
1199 | 1196 | collect_const_value(self.tcx, val, &mut self.output); |
1200 | 1197 | } |
1201 | 1198 | } |
1202 | | - hir::ItemKind::Fn(..) => { |
1203 | | - self.push_if_root(item.def_id); |
| 1199 | + DefKind::Impl => { |
| 1200 | + let item = self.tcx.hir().item(id); |
| 1201 | + if self.mode == MonoItemCollectionMode::Eager { |
| 1202 | + create_mono_items_for_default_impls(self.tcx, item, self.output); |
| 1203 | + } |
| 1204 | + } |
| 1205 | + DefKind::Fn => { |
| 1206 | + self.push_if_root(id.def_id); |
1204 | 1207 | } |
| 1208 | + _ => {} |
1205 | 1209 | } |
1206 | 1210 | } |
1207 | 1211 |
|
1208 | | - fn visit_trait_item(&mut self, _: &'v hir::TraitItem<'v>) { |
1209 | | - // Even if there's a default body with no explicit generics, |
1210 | | - // it's still generic over some `Self: Trait`, so not a root. |
1211 | | - } |
1212 | | - |
1213 | | - fn visit_impl_item(&mut self, ii: &'v hir::ImplItem<'v>) { |
1214 | | - if let hir::ImplItemKind::Fn(hir::FnSig { .. }, _) = ii.kind { |
1215 | | - self.push_if_root(ii.def_id); |
| 1212 | + fn process_impl_item(&mut self, id: hir::ImplItemId) { |
| 1213 | + if matches!(self.tcx.hir().def_kind(id.def_id), DefKind::AssocFn) { |
| 1214 | + self.push_if_root(id.def_id); |
1216 | 1215 | } |
1217 | 1216 | } |
1218 | 1217 |
|
1219 | | - fn visit_foreign_item(&mut self, _foreign_item: &'v hir::ForeignItem<'v>) {} |
1220 | | -} |
1221 | | - |
1222 | | -impl<'v> RootCollector<'_, 'v> { |
1223 | 1218 | fn is_root(&self, def_id: LocalDefId) -> bool { |
1224 | 1219 | !item_requires_monomorphization(self.tcx, def_id) |
1225 | 1220 | && match self.mode { |
|
0 commit comments