Skip to content

Commit

Permalink
Update to nightly-2024-12-01
Browse files Browse the repository at this point in the history
  • Loading branch information
willcrichton committed Dec 2, 2024
1 parent ed7cca3 commit 8a0a9cc
Show file tree
Hide file tree
Showing 17 changed files with 109 additions and 133 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
The Rust compiler's interface is not stable, so the only sensible way to develop a Rust compiler plugin is by pinning to a specific nightly. Each version of `rustc_plugin` is pinned to one nightly, and you *have* to use the same nightly version that we do. Therefore each release of `rustc_plugin` has a semantic version number (e.g. `0.1.0`) and the nightly version is added as a prerelease label (e.g. `-nightly-2023-08-25`). You can add a dependency to your `Cargo.toml` like this:

```toml
rustc_plugin = "=0.10.0-nightly-2024-05-20"
rustc_plugin = "=0.11.0-nightly-2024-12-01"
```

We will treat a change to the nightly version as a breaking change, so the semantic version will be correspondingly updated as a breaking update.
Expand Down Expand Up @@ -55,7 +55,7 @@ Normally, Rust libraries have a [minimum supported Rust version][msrv] because t
[Aquascope]: https://github.com/cognitive-engineering-lab/aquascope
[Clippy]: https://github.com/rust-lang/rust-clippy
[example]: https://github.com/cognitive-engineering-lab/rustc_plugin/tree/main/crates/rustc_plugin/examples/print-all-items
[docs]: https://cognitive-engineering-lab.github.io/rustc_plugin/v0.10.0-nightly-2024-05-20/rustc_plugin/
[docs-utils]: https://cognitive-engineering-lab.github.io/rustc_plugin/v0.10.0-nightly-2024-05-20/rustc_utils/
[docs]: https://cognitive-engineering-lab.github.io/rustc_plugin/v0.11.0-nightly-2024-12-01/rustc_plugin/
[docs-utils]: https://cognitive-engineering-lab.github.io/rustc_plugin/v0.11.0-nightly-2024-12-01/rustc_utils/
[msrv]: https://doc.rust-lang.org/cargo/reference/manifest.html#the-rust-version-field

2 changes: 1 addition & 1 deletion crates/rustc_plugin/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "rustc_plugin"
version = "0.10.0-nightly-2024-05-20"
version = "0.11.0-nightly-2024-12-01"
edition = "2021"
authors = ["Will Crichton <[email protected]>"]
description = "A framework for writing plugins that integrate with the Rust compiler"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![feature(rustc_private)]

fn main() {
env_logger::init();
rustc_plugin::cli_main(print_all_items::PrintAllItemsPlugin);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![feature(rustc_private)]

fn main() {
env_logger::init();
rustc_plugin::driver_main(print_all_items::PrintAllItemsPlugin);
Expand Down
10 changes: 3 additions & 7 deletions crates/rustc_plugin/examples/print-all-items/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,14 +78,10 @@ impl rustc_driver::Callbacks for PrintAllItemsCallbacks {
fn after_analysis<'tcx>(
&mut self,
_compiler: &rustc_interface::interface::Compiler,
queries: &'tcx rustc_interface::Queries<'tcx>,
tcx: TyCtxt<'tcx>,
) -> rustc_driver::Compilation {
// We extract a key data structure, the `TyCtxt`, which is all we need
// for our simple task of printing out item names.
queries
.global_ctxt()
.unwrap()
.enter(|tcx| print_all_items(tcx, &self.args));
// We call our top-level function with access to the type context `tcx` and the CLI arguments.
print_all_items(tcx, &self.args);

// Note that you should generally allow compilation to continue. If
// your plugin is being invoked on a dependency, then you need to ensure
Expand Down
2 changes: 1 addition & 1 deletion crates/rustc_plugin/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ fn only_run_on_file(
CompileKind::ProcMacro => {}
}

cmd.env(SPECIFIC_CRATE, &pkg.name.replace('-', "_"));
cmd.env(SPECIFIC_CRATE, pkg.name.replace('-', "_"));
cmd.env(SPECIFIC_TARGET, kind_str);

log::debug!(
Expand Down
2 changes: 1 addition & 1 deletion crates/rustc_utils/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "rustc_utils"
version = "0.10.0-nightly-2024-05-20"
version = "0.11.0-nightly-2024-12-01"
edition = "2021"
authors = ["Will Crichton <[email protected]>"]
description = "Utilities for working with the Rust compiler"
Expand Down
23 changes: 13 additions & 10 deletions crates/rustc_utils/src/hir/ty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
use rustc_data_structures::captures::Captures;
use rustc_hir::def_id::DefId;
use rustc_infer::infer::TyCtxtInferExt;
use rustc_middle::ty::{GenericArgKind, ParamEnv, Region, Ty, TyCtxt};
use rustc_middle::ty::{GenericArgKind, ParamEnv, Region, Ty, TyCtxt, TypingEnv};
use rustc_trait_selection::infer::InferCtxtExt;
use rustc_type_ir::TypingMode;

/// Extension trait for [`Ty`].
pub trait TyExt<'tcx> {
Expand All @@ -24,12 +25,14 @@ pub trait TyExt<'tcx> {
) -> bool;

/// Returns true if a type implements `Copy`.
fn is_copyable(&self, tcx: TyCtxt<'tcx>, param_env: ParamEnv<'tcx>) -> bool;
fn is_copyable(&self, tcx: TyCtxt<'tcx>, typing_env: TypingEnv<'tcx>) -> bool;
}

impl<'tcx> TyExt<'tcx> for Ty<'tcx> {
type AllRegionsIter<'a> = impl Iterator<Item = Region<'tcx>> + Captures<'tcx> + 'a
where Self: 'a;
type AllRegionsIter<'a>
= impl Iterator<Item = Region<'tcx>> + Captures<'tcx> + 'a
where
Self: 'a;

fn inner_regions(&self) -> Self::AllRegionsIter<'_> {
self.walk().filter_map(|part| match part.unpack() {
Expand All @@ -46,7 +49,7 @@ impl<'tcx> TyExt<'tcx> for Ty<'tcx> {
) -> bool {
use rustc_infer::traits::EvaluationResult;

let infcx = tcx.infer_ctxt().build();
let infcx = tcx.infer_ctxt().build(TypingMode::non_body_analysis());
let ty = tcx.erase_regions(*self);
let result = infcx.type_implements_trait(trait_def_id, [ty], param_env);
matches!(
Expand All @@ -55,15 +58,15 @@ impl<'tcx> TyExt<'tcx> for Ty<'tcx> {
)
}

fn is_copyable(&self, tcx: TyCtxt<'tcx>, param_env: ParamEnv<'tcx>) -> bool {
fn is_copyable(&self, tcx: TyCtxt<'tcx>, typing_env: TypingEnv<'tcx>) -> bool {
let ty = tcx.erase_regions(*self);
ty.is_copy_modulo_regions(tcx, param_env)
ty.is_copy_modulo_regions(tcx, typing_env)
}
}

#[cfg(test)]
mod test {
use rustc_middle::ty::ParamEnv;
use rustc_middle::ty::TypingEnv;

use super::TyExt;
use crate::{test_utils, BodyExt};
Expand All @@ -84,8 +87,8 @@ fn main() {
assert_eq!(x.ty.inner_regions().count(), 1);
assert_eq!(y.ty.inner_regions().count(), 0);

assert!(!x.ty.is_copyable(tcx, ParamEnv::empty()));
assert!(y.ty.is_copyable(tcx, ParamEnv::empty()));
assert!(!x.ty.is_copyable(tcx, TypingEnv::fully_monomorphized()));
assert!(y.ty.is_copyable(tcx, TypingEnv::fully_monomorphized()));
});
}
}
1 change: 0 additions & 1 deletion crates/rustc_utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
negative_impls, // for !Send
min_specialization, // for rustc_index::newtype_index
type_alias_impl_trait, // for iterators in traits
lazy_cell, // for global constants w/ heap allocation
box_patterns, // for ergonomics
let_chains, // for places_conflict module
exact_size_is_empty, // for graphviz module
Expand Down
33 changes: 26 additions & 7 deletions crates/rustc_utils/src/mir/body.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use std::{
};

use anyhow::{ensure, Result};
use pretty::PrettyPrintMirOptions;
use rustc_data_structures::{captures::Captures, fx::FxHashMap as HashMap};
use rustc_hir::{def_id::DefId, CoroutineDesugaring, CoroutineKind, HirId};
use rustc_middle::{
Expand Down Expand Up @@ -84,7 +85,10 @@ pub trait BodyExt<'tcx> {
}

impl<'tcx> BodyExt<'tcx> for Body<'tcx> {
type AllReturnsIter<'a> = impl Iterator<Item = Location> + Captures<'tcx> + 'a where Self: 'a;
type AllReturnsIter<'a>
= impl Iterator<Item = Location> + Captures<'tcx> + 'a
where
Self: 'a;
fn all_returns(&self) -> Self::AllReturnsIter<'_> {
self
.basic_blocks
Expand All @@ -98,7 +102,10 @@ impl<'tcx> BodyExt<'tcx> for Body<'tcx> {
})
}

type AllLocationsIter<'a> = impl Iterator<Item = Location> + Captures<'tcx> + 'a where Self: 'a;
type AllLocationsIter<'a>
= impl Iterator<Item = Location> + Captures<'tcx> + 'a
where
Self: 'a;
fn all_locations(&self) -> Self::AllLocationsIter<'_> {
self
.basic_blocks
Expand Down Expand Up @@ -133,7 +140,15 @@ impl<'tcx> BodyExt<'tcx> for Body<'tcx> {

fn to_string(&self, tcx: TyCtxt<'tcx>) -> Result<String> {
let mut buffer = Vec::new();
write_mir_fn(tcx, self, &mut |_, _| Ok(()), &mut buffer)?;
write_mir_fn(
tcx,
self,
&mut |_, _| Ok(()),
&mut buffer,
PrettyPrintMirOptions {
include_extra_comments: false,
},
)?;
Ok(String::from_utf8(buffer)?)
}

Expand Down Expand Up @@ -166,13 +181,17 @@ impl<'tcx> BodyExt<'tcx> for Body<'tcx> {
}
}

type ArgRegionsIter<'a> = impl Iterator<Item = Region<'tcx>> + Captures<'tcx> + 'a
where Self: 'a;
type ArgRegionsIter<'a>
= impl Iterator<Item = Region<'tcx>> + Captures<'tcx> + 'a
where
Self: 'a;

type ReturnRegionsIter = impl Iterator<Item = Region<'tcx>>;

type PlacesIter<'a> = impl Iterator<Item = Place<'tcx>> + Captures<'tcx> + 'a
where Self: 'a;
type PlacesIter<'a>
= impl Iterator<Item = Place<'tcx>> + Captures<'tcx> + 'a
where
Self: 'a;

fn regions_in_args(&self) -> Self::ArgRegionsIter<'_> {
self
Expand Down
67 changes: 32 additions & 35 deletions crates/rustc_utils/src/mir/borrowck_facts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use rustc_borrowck::consumers::{BodyWithBorrowckFacts, ConsumerOptions};
use rustc_data_structures::fx::FxHashSet as HashSet;
use rustc_hir::def_id::LocalDefId;
use rustc_middle::{
mir::{Body, BorrowCheckResult, MirPass, StatementKind, TerminatorKind},
mir::{Body, BorrowCheckResult, StatementKind, TerminatorKind},
ty::TyCtxt,
util::Providers,
};
Expand All @@ -17,40 +17,37 @@ use crate::{block_timer, cache::Cache, BodyExt};
///
/// This pass helps reduce the number of intermediates during dataflow analysis, which
/// reduces memory usage.
pub struct SimplifyMir;
impl<'tcx> MirPass<'tcx> for SimplifyMir {
fn run_pass(&self, _tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
let return_blocks = body
.all_returns()
.filter_map(|loc| {
let bb = &body.basic_blocks[loc.block];
(bb.statements.len() == 0).then_some(loc.block)
})
.collect::<HashSet<_>>();

for block in body.basic_blocks_mut() {
block.statements.retain(|stmt| {
!matches!(
stmt.kind,
StatementKind::StorageLive(..) | StatementKind::StorageDead(..)
)
});

let terminator = block.terminator_mut();
terminator.kind = match terminator.kind {
TerminatorKind::FalseEdge { real_target, .. } => TerminatorKind::Goto {
target: real_target,
},
TerminatorKind::FalseUnwind { real_target, .. } => TerminatorKind::Goto {
target: real_target,
},
// Ensures that control dependencies can determine the independence of differnet
// return paths
TerminatorKind::Goto { target } if return_blocks.contains(&target) => {
TerminatorKind::Return
}
_ => continue,
pub fn simplify_mir(body: &mut Body<'_>) {
let return_blocks = body
.all_returns()
.filter_map(|loc| {
let bb = &body.basic_blocks[loc.block];
(bb.statements.len() == 0).then_some(loc.block)
})
.collect::<HashSet<_>>();

for block in body.basic_blocks_mut() {
block.statements.retain(|stmt| {
!matches!(
stmt.kind,
StatementKind::StorageLive(..) | StatementKind::StorageDead(..)
)
});

let terminator = block.terminator_mut();
terminator.kind = match terminator.kind {
TerminatorKind::FalseEdge { real_target, .. } => TerminatorKind::Goto {
target: real_target,
},
TerminatorKind::FalseUnwind { real_target, .. } => TerminatorKind::Goto {
target: real_target,
},
// Ensures that control dependencies can determine the independence of differnet
// return paths
TerminatorKind::Goto { target } if return_blocks.contains(&target) => {
TerminatorKind::Return
}
_ => continue,
}
}
}
Expand Down Expand Up @@ -86,7 +83,7 @@ fn mir_borrowck(tcx: TyCtxt<'_>, def_id: LocalDefId) -> &BorrowCheckResult<'_> {
);

if SIMPLIFY_MIR.load(Ordering::SeqCst) {
SimplifyMir.run_pass(tcx, &mut body_with_facts.body);
simplify_mir(&mut body_with_facts.body);
}

// SAFETY: The reader casts the 'static lifetime to 'tcx before using it.
Expand Down
4 changes: 2 additions & 2 deletions crates/rustc_utils/src/mir/control_dependencies.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use std::fmt;

use rustc_data_structures::graph::{dominators::Dominators, vec_graph::VecGraph, *};
use rustc_index::{
bit_set::{BitSet, HybridBitSet, SparseBitMatrix},
bit_set::{BitSet, ChunkedBitSet, SparseBitMatrix},
Idx,
};
use smallvec::SmallVec;
Expand Down Expand Up @@ -191,7 +191,7 @@ impl<Node: Idx + Ord> ControlDependencies<Node> {
}

/// Returns the set of all node that are control-dependent on the given `node`.
pub fn dependent_on(&self, node: Node) -> Option<&HybridBitSet<Node>> {
pub fn dependent_on(&self, node: Node) -> Option<&ChunkedBitSet<Node>> {
self.0.row(node)
}
}
Expand Down
Loading

0 comments on commit 8a0a9cc

Please sign in to comment.