Skip to content

Commit

Permalink
Upgrade to Rust 1.72
Browse files Browse the repository at this point in the history
  • Loading branch information
nbdd0121 committed Jul 29, 2023
1 parent 6075644 commit f1f25e7
Show file tree
Hide file tree
Showing 12 changed files with 110 additions and 82 deletions.
2 changes: 1 addition & 1 deletion rust-toolchain
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
[toolchain]
channel = "1.71.0"
channel = "beta"
components = ["llvm-tools-preview", "rustc-dev", "rust-src"]
1 change: 1 addition & 0 deletions src/ctxt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,7 @@ impl<'tcx> AnalysisCtxt<'tcx> {
let output_filenames = tcx.output_filenames(());
let rmeta_path =
rustc_session::output::filename_for_metadata(tcx.sess, crate_name, output_filenames);
let rmeta_path = rmeta_path.as_path();

// Double check that the rmeta file is .rlib or .rmeta
let ext = rmeta_path.extension().unwrap();
Expand Down
2 changes: 1 addition & 1 deletion src/infallible_allocation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ impl<'tcx> LateLintPass<'tcx> for InfallibleAllocation {
let mut forward = FxHashMap::default();
let mut backward = FxHashMap::<_, Vec<_>>::default();

access_map.iter_accesses(|accessor, accessees| {
access_map.for_each_item_and_its_used_items(|accessor, accessees| {
let accessor = match accessor {
MonoItem::Static(s) => Instance::mono(cx.tcx, s),
MonoItem::Fn(v) => v,
Expand Down
5 changes: 4 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ use std::sync::atomic::AtomicPtr;

use rustc_driver::Callbacks;
use rustc_interface::interface::Config;
use rustc_session::config::ErrorOutputType;
use rustc_session::EarlyErrorHandler;
use std::sync::atomic::Ordering;

#[macro_use]
Expand Down Expand Up @@ -103,7 +105,8 @@ impl Callbacks for MyCallbacks {
}

fn main() -> ExitCode {
rustc_driver::init_env_logger("KLINT_LOG");
let handler = EarlyErrorHandler::new(ErrorOutputType::default());
rustc_driver::init_env_logger(&handler, "KLINT_LOG");
let args: Vec<_> = std::env::args().collect();

match rustc_driver::RunCompiler::new(&args, &mut MyCallbacks).run() {
Expand Down
3 changes: 2 additions & 1 deletion src/mir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use std::sync::{LazyLock, Mutex};

use rustc_data_structures::fx::FxHashMap;
use rustc_hir::{self as hir, def::DefKind};
use rustc_middle::mir::CallSource;
use rustc_middle::mir::{
Body, Constant, LocalDecl, Operand, Place, ProjectionElem, Rvalue, SourceInfo, Statement,
StatementKind, TerminatorKind,
Expand Down Expand Up @@ -103,7 +104,7 @@ fn remap_mir_for_const_eval_select<'tcx>(
};
method(place)
}).collect();
terminator.kind = TerminatorKind::Call { func, args: arguments, destination, target, unwind, from_hir_call: false, fn_span };
terminator.kind = TerminatorKind::Call { func, args: arguments, destination, target, unwind, call_source: CallSource::Misc, fn_span };
}
_ => {}
}
Expand Down
2 changes: 1 addition & 1 deletion src/mir/drop_shim.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ pub fn build_drop_shim<'tcx>(
) -> Body<'tcx> {
if let ty::Generator(gen_def_id, substs, _) = ty.kind() {
let body = cx.analysis_mir(*gen_def_id).generator_drop().unwrap();
let body = EarlyBinder(body.clone()).subst(cx.tcx, substs);
let body = EarlyBinder::bind(body.clone()).subst(cx.tcx, substs);
return body;
}

Expand Down
89 changes: 45 additions & 44 deletions src/monomorphize_collector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use rustc_middle::mir::mono::MonoItem;
use rustc_middle::mir::visit::Visitor as MirVisitor;
use rustc_middle::mir::{self, Local, Location};
use rustc_middle::query::TyCtxtAt;
use rustc_middle::ty::adjustment::{CustomCoerceUnsized, PointerCast};
use rustc_middle::ty::adjustment::{CustomCoerceUnsized, PointerCoercion};
use rustc_middle::ty::print::with_no_trimmed_paths;
use rustc_middle::ty::subst::{GenericArgKind, InternalSubsts};
use rustc_middle::ty::{
Expand All @@ -37,12 +37,12 @@ fn custom_coerce_unsize_info<'tcx>(
source_ty: Ty<'tcx>,
target_ty: Ty<'tcx>,
) -> CustomCoerceUnsized {
let trait_ref = ty::Binder::dummy(ty::TraitRef::from_lang_item(
let trait_ref = ty::TraitRef::from_lang_item(
tcx.tcx,
LangItem::CoerceUnsized,
tcx.span,
[source_ty, target_ty],
));
);

match tcx.codegen_select_candidate((param_env, trait_ref)) {
Ok(traits::ImplSource::UserDefined(traits::ImplSourceUserDefinedData {
Expand All @@ -61,40 +61,41 @@ pub enum MonoItemCollectionMode {
Lazy,
}

/// Maps every mono item to all mono items it references in its
/// body.
pub struct UsageMap<'tcx> {
// Maps a source mono item to the range of mono items
// accessed by it.
// The range selects elements within the `targets` vecs.
index: FxHashMap<MonoItem<'tcx>, Range<usize>>,
targets: Vec<Spanned<MonoItem<'tcx>>>,
// Maps every mono item to the mono items used by it. Those mono items
// are represented as a range, which indexes into `used_items`.
used_map: FxHashMap<MonoItem<'tcx>, Range<usize>>,

// A mono item that is used by N different other mono items will appear
// here N times. Indexed into by the ranges in `used_map`.
used_items: Vec<Spanned<MonoItem<'tcx>>>,
}

impl<'tcx> UsageMap<'tcx> {
fn new() -> UsageMap<'tcx> {
UsageMap {
index: FxHashMap::default(),
targets: Vec::new(),
used_map: FxHashMap::default(),
used_items: Vec::new(),
}
}

fn record_accesses(&mut self, source: MonoItem<'tcx>, new_targets: &[Spanned<MonoItem<'tcx>>]) {
let start_index = self.targets.len();
fn record_used(&mut self, user_item: MonoItem<'tcx>, used_items: &[Spanned<MonoItem<'tcx>>]) {
let old_len = self.used_items.len();

self.targets.extend_from_slice(new_targets);
self.used_items.extend_from_slice(used_items);

let end_index = self.targets.len();
assert!(self.index.insert(source, start_index..end_index).is_none());
let new_len = self.used_items.len();
let new_items_range = old_len..new_len;
assert!(self.used_map.insert(user_item, new_items_range).is_none());
}

// Internally iterate over all items and the things each accesses.
pub fn iter_accesses<F>(&self, mut f: F)
pub fn for_each_item_and_its_used_items<F>(&self, mut f: F)
where
F: FnMut(MonoItem<'tcx>, &[Spanned<MonoItem<'tcx>>]),
{
for (&accessor, range) in &self.index {
f(accessor, &self.targets[range.clone()])
for (&item, range) in &self.used_map {
f(item, &self.used_items[range.clone()])
}
}
}
Expand All @@ -114,12 +115,12 @@ pub fn collect_crate_mono_items(
debug!("building mono item graph, beginning at roots");

let mut visited = MTLock::new(FxHashSet::default());
let mut access_map = MTLock::new(UsageMap::new());
let mut usage_map = MTLock::new(UsageMap::new());
let recursion_limit = tcx.recursion_limit();

{
let visited: MTLockRef<'_, _> = &mut visited;
let access_map: MTLockRef<'_, _> = &mut access_map;
let usage_map: MTLockRef<'_, _> = &mut usage_map;

tcx.sess.time("monomorphization_collector_graph_walk", || {
par_for_each_in(roots, |root| {
Expand All @@ -139,14 +140,14 @@ pub fn collect_crate_mono_items(
visited,
&mut recursion_depths,
recursion_limit,
access_map,
usage_map,
);
}
});
});
}

(visited.into_inner(), access_map.into_inner())
(visited.into_inner(), usage_map.into_inner())
}

// Find all non-generic items by walking the HIR. These items serve as roots to
Expand Down Expand Up @@ -201,14 +202,14 @@ fn collect_items_rec<'tcx>(
visited: MTLockRef<'_, FxHashSet<MonoItem<'tcx>>>,
recursion_depths: &mut DefIdMap<usize>,
recursion_limit: Limit,
access_map: MTLockRef<'_, UsageMap<'tcx>>,
usage_map: MTLockRef<'_, UsageMap<'tcx>>,
) {
if !visited.lock_mut().insert(starting_item.node) {
// We've been here already, no need to search again.
return;
}

let mut neighbors = Vec::new();
let mut used_items = Vec::new();
let recursion_depth_reset;

//
Expand Down Expand Up @@ -245,13 +246,13 @@ fn collect_items_rec<'tcx>(
debug_assert!(should_codegen_locally(tcx, &instance));

let ty = instance.ty(tcx, ty::ParamEnv::reveal_all());
visit_drop_use(tcx, ty, true, starting_item.span, &mut neighbors);
visit_drop_use(tcx, ty, true, starting_item.span, &mut used_items);

recursion_depth_reset = None;

if let Ok(alloc) = tcx.eval_static_initializer(def_id) {
for id in alloc.inner().provenance().provenances() {
collect_miri(tcx, id, &mut neighbors);
collect_miri(tcx, id, &mut used_items);
}
}
}
Expand All @@ -270,7 +271,7 @@ fn collect_items_rec<'tcx>(
check_type_length_limit(tcx, instance);

rustc_data_structures::stack::ensure_sufficient_stack(|| {
collect_neighbours(tcx, instance, &mut neighbors);
collect_used_items(tcx, instance, &mut used_items);
});
}
MonoItem::GlobalAsm(item_id) => {
Expand All @@ -289,11 +290,11 @@ fn collect_items_rec<'tcx>(
let fn_ty = tcx
.typeck_body(anon_const.body)
.node_type(anon_const.hir_id);
visit_fn_use(tcx, fn_ty, false, *op_sp, &mut neighbors);
visit_fn_use(tcx, fn_ty, false, *op_sp, &mut used_items);
}
hir::InlineAsmOperand::SymStatic { path: _, def_id } => {
trace!("collecting static {:?}", def_id);
neighbors.push(dummy_spanned(MonoItem::Static(*def_id)));
used_items.push(dummy_spanned(MonoItem::Static(*def_id)));
}
hir::InlineAsmOperand::In { .. }
| hir::InlineAsmOperand::Out { .. }
Expand Down Expand Up @@ -328,11 +329,11 @@ fn collect_items_rec<'tcx>(
);
}

access_map
usage_map
.lock_mut()
.record_accesses(starting_item.node, &neighbors);
.record_used(starting_item.node, &used_items);

for neighbour in neighbors {
for neighbour in used_items {
let should_gen = match neighbour.node {
MonoItem::Static(def_id) => {
let instance = Instance::mono(tcx, def_id);
Expand All @@ -348,7 +349,7 @@ fn collect_items_rec<'tcx>(
visited,
recursion_depths,
recursion_limit,
access_map,
usage_map,
);
}
}
Expand Down Expand Up @@ -475,14 +476,14 @@ fn check_type_length_limit<'tcx>(tcx: TyCtxt<'tcx>, instance: Instance<'tcx>) {
}
}

struct MirNeighborCollector<'a, 'tcx> {
struct MirUsedCollector<'a, 'tcx> {
tcx: TyCtxt<'tcx>,
body: &'a mir::Body<'tcx>,
output: &'a mut Vec<Spanned<MonoItem<'tcx>>>,
instance: Instance<'tcx>,
}

impl<'a, 'tcx> MirNeighborCollector<'a, 'tcx> {
impl<'a, 'tcx> MirUsedCollector<'a, 'tcx> {
pub fn monomorphize<T>(&self, value: T) -> T
where
T: TypeFoldable<TyCtxt<'tcx>>,
Expand All @@ -491,12 +492,12 @@ impl<'a, 'tcx> MirNeighborCollector<'a, 'tcx> {
self.instance.subst_mir_and_normalize_erasing_regions(
self.tcx,
ty::ParamEnv::reveal_all(),
ty::EarlyBinder(value),
ty::EarlyBinder::bind(value),
)
}
}

impl<'a, 'tcx> MirVisitor<'tcx> for MirNeighborCollector<'a, 'tcx> {
impl<'a, 'tcx> MirVisitor<'tcx> for MirUsedCollector<'a, 'tcx> {
fn visit_rvalue(&mut self, rvalue: &mir::Rvalue<'tcx>, location: Location) {
debug!("visiting rvalue {:?}", *rvalue);

Expand All @@ -507,7 +508,7 @@ impl<'a, 'tcx> MirVisitor<'tcx> for MirNeighborCollector<'a, 'tcx> {
// have to instantiate all methods of the trait being cast to, so we
// can build the appropriate vtable.
mir::Rvalue::Cast(
mir::CastKind::Pointer(PointerCast::Unsize),
mir::CastKind::PointerCoercion(PointerCoercion::Unsize),
ref operand,
target_ty,
)
Expand Down Expand Up @@ -537,7 +538,7 @@ impl<'a, 'tcx> MirVisitor<'tcx> for MirNeighborCollector<'a, 'tcx> {
}
}
mir::Rvalue::Cast(
mir::CastKind::Pointer(PointerCast::ReifyFnPointer),
mir::CastKind::PointerCoercion(PointerCoercion::ReifyFnPointer),
ref operand,
_,
) => {
Expand All @@ -546,7 +547,7 @@ impl<'a, 'tcx> MirVisitor<'tcx> for MirNeighborCollector<'a, 'tcx> {
visit_fn_use(self.tcx, fn_ty, false, span, &mut self.output);
}
mir::Rvalue::Cast(
mir::CastKind::Pointer(PointerCast::ClosureFnPointer(_)),
mir::CastKind::PointerCoercion(PointerCoercion::ClosureFnPointer(_)),
ref operand,
_,
) => {
Expand Down Expand Up @@ -1189,13 +1190,13 @@ fn collect_miri<'tcx>(
}

/// Scans the MIR in order to find function calls, closures, and drop-glue.
fn collect_neighbours<'tcx>(
fn collect_used_items<'tcx>(
tcx: TyCtxt<'tcx>,
instance: Instance<'tcx>,
output: &mut Vec<Spanned<MonoItem<'tcx>>>,
) {
let body = tcx.instance_mir(instance.def);
MirNeighborCollector {
MirUsedCollector {
tcx,
body: &body,
output,
Expand Down
16 changes: 11 additions & 5 deletions src/preempt_count/adjustment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ impl<'tcx> AnalysisCtxt<'tcx> {
limit -= 1;
}
}
UseSiteKind::PointerCast(span) => {
UseSiteKind::PointerCoercion(span) => {
if diag.span.is_dummy() {
diag.set_span(*span);
} else {
Expand Down Expand Up @@ -369,10 +369,16 @@ memoize!(

ty::Adt(def, substs) if def.is_box() => {
let adj = cx.drop_adjustment(param_env.and(substs.type_at(0)))?;
let box_free = cx.require_lang_item(LangItem::BoxFree, None);
let box_free_adj =
cx.instance_adjustment(param_env.and(Instance::new(box_free, substs)))?;
let Some(adj) = adj.checked_add(box_free_adj) else { cx.drop_adjustment_overflow(poly_ty)? };
let drop_trait = cx.require_lang_item(LangItem::Drop, None);
let drop_fn = cx.associated_item_def_ids(drop_trait)[0];
let box_free =
ty::Instance::resolve(cx.tcx, param_env, drop_fn, cx.mk_substs(&[ty.into()]))
.unwrap()
.unwrap();
let box_free_adj = cx.instance_adjustment(param_env.and(box_free))?;
let Some(adj) = adj.checked_add(box_free_adj) else {
cx.drop_adjustment_overflow(poly_ty)?
};
return Ok(adj);
}

Expand Down
Loading

0 comments on commit f1f25e7

Please sign in to comment.