Repository: rust-lang/rust
File: compiler/rustc_monomorphize/src/partitioning/default.rs (Rust)

Code before:
use std::collections::hash_map::Entry;

use rustc_data_structures::fx::{FxHashMap, FxHashSet};
use rustc_hir::def::DefKind;
use rustc_hir::def_id::{DefId, LOCAL_CRATE};
use rustc_hir::definitions::DefPathDataName;
use rustc_middle::middle::codegen_fn_attrs::CodegenFnAttrFlags;
use rustc_middle::middle::exported_symbols::{SymbolExportInfo, SymbolExportLevel};
use rustc_middle::mir::mono::{CodegenUnit, CodegenUnitNameBuilder, Linkage, Visibility};
use rustc_middle::mir::mono::{InstantiationMode, MonoItem};
use rustc_middle::ty::print::characteristic_def_id_of_type;
use rustc_middle::ty::{self, visit::TypeVisitableExt, InstanceDef, TyCtxt};
use rustc_span::symbol::Symbol;

use super::PartitioningCx;
use crate::collector::InliningMap;
use crate::partitioning::{MonoItemPlacement, Partition};

pub struct DefaultPartitioning;

impl<'tcx> Partition<'tcx> for DefaultPartitioning {
    fn place_root_mono_items<I>(
        &mut self,
        cx: &PartitioningCx<'_, 'tcx>,
        mono_items: &mut I,
    ) -> (Vec<CodegenUnit<'tcx>>, FxHashSet<MonoItem<'tcx>>, FxHashSet<MonoItem<'tcx>>)
    where
        I: Iterator<Item = MonoItem<'tcx>>,
    {
        let mut roots = FxHashSet::default();
        let mut codegen_units = FxHashMap::default();
        let is_incremental_build = cx.tcx.sess.opts.incremental.is_some();
        let mut internalization_candidates = FxHashSet::default();

        // Determine if monomorphizations instantiated in this crate will be made
        // available to downstream crates. This depends on whether we are in
        // share-generics mode and whether the current crate can even have
        // downstream crates.
        let export_generics =
            cx.tcx.sess.opts.share_generics() && cx.tcx.local_crate_exports_generics();

        let cgu_name_builder = &mut CodegenUnitNameBuilder::new(cx.tcx);
        let cgu_name_cache = &mut FxHashMap::default();

        for mono_item in mono_items {
            match mono_item.instantiation_mode(cx.tcx) {
                InstantiationMode::GloballyShared { .. } => {}
                InstantiationMode::LocalCopy => continue,
            }

            let characteristic_def_id = characteristic_def_id_of_mono_item(cx.tcx, mono_item);

Review comment (suggestion):
Returning a tuple of two values with the same type is a little confusing, I'd prefer seeing the struct here.

Code after:
use std::collections::hash_map::Entry;

use rustc_data_structures::fx::{FxHashMap, FxHashSet};
use rustc_hir::def::DefKind;
use rustc_hir::def_id::{DefId, LOCAL_CRATE};
use rustc_hir::definitions::DefPathDataName;
use rustc_middle::middle::codegen_fn_attrs::CodegenFnAttrFlags;
use rustc_middle::middle::exported_symbols::{SymbolExportInfo, SymbolExportLevel};
use rustc_middle::mir::mono::{CodegenUnit, CodegenUnitNameBuilder, Linkage, Visibility};
use rustc_middle::mir::mono::{InstantiationMode, MonoItem};
use rustc_middle::ty::print::characteristic_def_id_of_type;
use rustc_middle::ty::{self, visit::TypeVisitableExt, InstanceDef, TyCtxt};
use rustc_span::symbol::Symbol;

use super::PartitioningCx;
use crate::collector::InliningMap;
use crate::partitioning::{MonoItemPlacement, Partition, PlacedRootMonoItems};

pub struct DefaultPartitioning;

impl<'tcx> Partition<'tcx> for DefaultPartitioning {
    fn place_root_mono_items<I>(
        &mut self,
        cx: &PartitioningCx<'_, 'tcx>,
        mono_items: &mut I,
    ) -> PlacedRootMonoItems<'tcx>
    where
        I: Iterator<Item = MonoItem<'tcx>>,
    {
        let mut roots = FxHashSet::default();
        let mut codegen_units = FxHashMap::default();
        let is_incremental_build = cx.tcx.sess.opts.incremental.is_some();
        let mut internalization_candidates = FxHashSet::default();

        // Determine if monomorphizations instantiated in this crate will be made
        // available to downstream crates. This depends on whether we are in
        // share-generics mode and whether the current crate can even have
        // downstream crates.
        let export_generics =
            cx.tcx.sess.opts.share_generics() && cx.tcx.local_crate_exports_generics();

        let cgu_name_builder = &mut CodegenUnitNameBuilder::new(cx.tcx);
        let cgu_name_cache = &mut FxHashMap::default();

        for mono_item in mono_items {
            match mono_item.instantiation_mode(cx.tcx) {
                InstantiationMode::GloballyShared { .. } => {}
                InstantiationMode::LocalCopy => continue,
            }

            let characteristic_def_id = characteristic_def_id_of_mono_item(cx.tcx, mono_item);