Skip to content
Open
307 changes: 235 additions & 72 deletions compiler/rustc_mir_build/src/builder/scope.rs

Large diffs are not rendered by default.

15 changes: 15 additions & 0 deletions compiler/rustc_mir_transform/src/cleanup_post_borrowck.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
//! - [`FakeRead`]
//! - [`Assign`] statements with a [`Fake`] borrow
//! - [`Coverage`] statements of kind [`BlockMarker`] or [`SpanMarker`]
//! - [`StorageDead`] statements in cleanup blocks (unwind paths) - these are only needed
//! for borrow-checking and are removed from cleanup blocks after borrowck completes.
//! StorageDead on normal paths may be needed by later passes (e.g., coroutine transforms)
//! and will be conditionally removed by RemoveStorageMarkers during optimization if enabled.
//!
//! [`AscribeUserType`]: rustc_middle::mir::StatementKind::AscribeUserType
//! [`Assign`]: rustc_middle::mir::StatementKind::Assign
Expand All @@ -15,6 +19,7 @@
//! [`Coverage`]: rustc_middle::mir::StatementKind::Coverage
//! [`BlockMarker`]: rustc_middle::mir::coverage::CoverageKind::BlockMarker
//! [`SpanMarker`]: rustc_middle::mir::coverage::CoverageKind::SpanMarker
//! [`StorageDead`]: rustc_middle::mir::StatementKind::StorageDead

use rustc_middle::mir::coverage::CoverageKind;
use rustc_middle::mir::*;
Expand All @@ -28,6 +33,11 @@ impl<'tcx> crate::MirPass<'tcx> for CleanupPostBorrowck {
// Manually invalidate CFG caches if we actually change a terminator's edges.
let mut invalidate_cfg = false;
for basic_block in body.basic_blocks.as_mut_preserves_cfg().iter_mut() {
// Only remove StorageDead from cleanup blocks (unwind paths).
// StorageDead on normal paths may be needed by later passes (e.g., coroutine
// transforms) and may be used by codegen backends. RemoveStorageMarkers will
// conditionally remove them during optimization if enabled (when mir_opt_level > 0
// and lifetime markers are not being emitted).
for statement in basic_block.statements.iter_mut() {
match statement.kind {
StatementKind::AscribeUserType(..)
Expand All @@ -41,6 +51,11 @@ impl<'tcx> crate::MirPass<'tcx> for CleanupPostBorrowck {
| StatementKind::BackwardIncompatibleDropHint { .. } => {
statement.make_nop(true)
}
StatementKind::StorageDead(..)
if basic_block.is_cleanup && body.coroutine.is_none() =>
{
statement.make_nop(true)
}
StatementKind::Assign(box (
_,
Rvalue::Cast(
Expand Down
5 changes: 3 additions & 2 deletions src/tools/rust-analyzer/crates/hir-def/src/import_map.rs
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From your commit message:

This fixes compilation errors that are blocking CI, though these are
pre-existing issues unrelated to the StorageDead changes.

I guess this breakage comes from this PR rather than pre-existing, as this isn't failing in other PRs?

Original file line number Diff line number Diff line change
Expand Up @@ -426,11 +426,10 @@ pub fn search_dependencies(
let import_maps: Vec<_> =
krate.data(db).dependencies.iter().map(|dep| db.import_map(dep.crate_id)).collect();

let mut op = fst::map::OpBuilder::new();

match query.search_mode {
SearchMode::Exact => {
let automaton = fst::automaton::Str::new(&query.lowercased);
let mut op = fst::map::OpBuilder::new();

for map in &import_maps {
op = op.add(map.fst.search(&automaton));
Expand All @@ -439,6 +438,7 @@ pub fn search_dependencies(
}
SearchMode::Fuzzy => {
let automaton = fst::automaton::Subsequence::new(&query.lowercased);
let mut op = fst::map::OpBuilder::new();

for map in &import_maps {
op = op.add(map.fst.search(&automaton));
Expand All @@ -447,6 +447,7 @@ pub fn search_dependencies(
}
SearchMode::Prefix => {
let automaton = fst::automaton::Str::new(&query.lowercased).starts_with();
let mut op = fst::map::OpBuilder::new();

for map in &import_maps {
op = op.add(map.fst.search(&automaton));
Expand Down
5 changes: 3 additions & 2 deletions src/tools/rust-analyzer/crates/ide-db/src/symbol_index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -569,11 +569,10 @@ impl Query {
cb: impl FnMut(&'db FileSymbol<'db>) -> ControlFlow<T>,
) -> Option<T> {
let _p = tracing::info_span!("symbol_index::Query::search").entered();

let mut op = fst::map::OpBuilder::new();
match self.mode {
SearchMode::Exact => {
let automaton = fst::automaton::Str::new(&self.lowercased);
let mut op = fst::map::OpBuilder::new();

for index in indices.iter() {
op = op.add(index.map.search(&automaton));
Expand All @@ -582,6 +581,7 @@ impl Query {
}
SearchMode::Fuzzy => {
let automaton = fst::automaton::Subsequence::new(&self.lowercased);
let mut op = fst::map::OpBuilder::new();

for index in indices.iter() {
op = op.add(index.map.search(&automaton));
Expand All @@ -590,6 +590,7 @@ impl Query {
}
SearchMode::Prefix => {
let automaton = fst::automaton::Str::new(&self.lowercased).starts_with();
let mut op = fst::map::OpBuilder::new();

for index in indices.iter() {
op = op.add(index.map.search(&automaton));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,19 @@ fn main() -> () {
}

bb6 (cleanup): {
StorageDead(_6);
drop(_5) -> [return: bb7, unwind terminate(cleanup)];
}

bb7 (cleanup): {
StorageDead(_5);
drop(_4) -> [return: bb8, unwind terminate(cleanup)];
}

bb8 (cleanup): {
StorageDead(_4);
StorageDead(_2);
StorageDead(_1);
resume;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ fn test_complex() -> () {
bb0: {
StorageLive(_1);
StorageLive(_2);
_2 = E::f() -> [return: bb1, unwind: bb35];
_2 = E::f() -> [return: bb1, unwind: bb38];
}

bb1: {
Expand All @@ -42,7 +42,7 @@ fn test_complex() -> () {

bb5: {
StorageLive(_4);
_4 = always_true() -> [return: bb6, unwind: bb35];
_4 = always_true() -> [return: bb6, unwind: bb38];
}

bb6: {
Expand All @@ -64,7 +64,7 @@ fn test_complex() -> () {
}

bb9: {
drop(_7) -> [return: bb11, unwind: bb35];
drop(_7) -> [return: bb11, unwind: bb37];
}

bb10: {
Expand All @@ -78,7 +78,7 @@ fn test_complex() -> () {
}

bb12: {
drop(_7) -> [return: bb13, unwind: bb35];
drop(_7) -> [return: bb13, unwind: bb37];
}

bb13: {
Expand All @@ -98,7 +98,7 @@ fn test_complex() -> () {
}

bb15: {
drop(_10) -> [return: bb17, unwind: bb35];
drop(_10) -> [return: bb17, unwind: bb36];
}

bb16: {
Expand Down Expand Up @@ -139,7 +139,7 @@ fn test_complex() -> () {
StorageDead(_4);
StorageDead(_1);
StorageLive(_11);
_11 = always_true() -> [return: bb23, unwind: bb35];
_11 = always_true() -> [return: bb23, unwind: bb38];
}

bb23: {
Expand All @@ -156,7 +156,7 @@ fn test_complex() -> () {

bb26: {
StorageLive(_12);
_12 = E::f() -> [return: bb27, unwind: bb35];
_12 = E::f() -> [return: bb27, unwind: bb38];
}

bb27: {
Expand Down Expand Up @@ -199,6 +199,25 @@ fn test_complex() -> () {
}

bb35 (cleanup): {
StorageDead(_10);
StorageDead(_9);
StorageDead(_2);
goto -> bb38;
}

bb36 (cleanup): {
StorageDead(_10);
StorageDead(_9);
goto -> bb38;
}

bb37 (cleanup): {
StorageDead(_7);
StorageDead(_6);
goto -> bb38;
}

bb38 (cleanup): {
resume;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ fn test_or() -> () {
}

bb1: {
drop(_3) -> [return: bb3, unwind: bb13];
drop(_3) -> [return: bb3, unwind: bb14];
}

bb2: {
Expand All @@ -34,7 +34,7 @@ fn test_or() -> () {
}

bb4: {
drop(_3) -> [return: bb5, unwind: bb13];
drop(_3) -> [return: bb5, unwind: bb14];
}

bb5: {
Expand Down Expand Up @@ -86,6 +86,19 @@ fn test_or() -> () {
}

bb13 (cleanup): {
StorageDead(_6);
StorageDead(_5);
goto -> bb15;
}

bb14 (cleanup): {
StorageDead(_3);
StorageDead(_2);
goto -> bb15;
}

bb15 (cleanup): {
StorageDead(_1);
resume;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ fn const_array_len(_1: [T; 5]) -> () {
StorageLive(_6);
StorageLive(_7);
_7 = move _2;
_6 = opaque::<T>(move _7) -> [return: bb3, unwind: bb17];
_6 = opaque::<T>(move _7) -> [return: bb3, unwind: bb20];
}

bb3: {
Expand All @@ -52,7 +52,7 @@ fn const_array_len(_1: [T; 5]) -> () {
StorageLive(_8);
StorageLive(_9);
_9 = move _3;
_8 = opaque::<T>(move _9) -> [return: bb4, unwind: bb16];
_8 = opaque::<T>(move _9) -> [return: bb4, unwind: bb18];
}

bb4: {
Expand All @@ -61,7 +61,7 @@ fn const_array_len(_1: [T; 5]) -> () {
StorageLive(_10);
StorageLive(_11);
_11 = move _4;
_10 = opaque::<[T; 2]>(move _11) -> [return: bb5, unwind: bb15];
_10 = opaque::<[T; 2]>(move _11) -> [return: bb5, unwind: bb16];
}

bb5: {
Expand All @@ -77,7 +77,7 @@ fn const_array_len(_1: [T; 5]) -> () {
StorageDead(_13);
StorageDead(_12);
_0 = const ();
drop(_5) -> [return: bb8, unwind: bb19];
drop(_5) -> [return: bb8, unwind: bb23];
}

bb7: {
Expand All @@ -87,17 +87,17 @@ fn const_array_len(_1: [T; 5]) -> () {

bb8: {
StorageDead(_5);
drop(_4) -> [return: bb9, unwind: bb20];
drop(_4) -> [return: bb9, unwind: bb24];
}

bb9: {
StorageDead(_4);
drop(_3) -> [return: bb10, unwind: bb21];
drop(_3) -> [return: bb10, unwind: bb25];
}

bb10: {
StorageDead(_3);
drop(_2) -> [return: bb11, unwind: bb22];
drop(_2) -> [return: bb11, unwind: bb26];
}

bb11: {
Expand All @@ -106,50 +106,78 @@ fn const_array_len(_1: [T; 5]) -> () {
}

bb12: {
drop(_1) -> [return: bb13, unwind: bb23];
drop(_1) -> [return: bb13, unwind: bb27];
}

bb13: {
return;
}

bb14 (cleanup): {
drop(_13) -> [return: bb18, unwind terminate(cleanup)];
drop(_13) -> [return: bb15, unwind terminate(cleanup)];
}

bb15 (cleanup): {
drop(_11) -> [return: bb18, unwind terminate(cleanup)];
StorageDead(_13);
StorageDead(_12);
goto -> bb22;
}

bb16 (cleanup): {
drop(_9) -> [return: bb18, unwind terminate(cleanup)];
drop(_11) -> [return: bb17, unwind terminate(cleanup)];
}

bb17 (cleanup): {
drop(_7) -> [return: bb18, unwind terminate(cleanup)];
StorageDead(_11);
StorageDead(_10);
goto -> bb22;
}

bb18 (cleanup): {
drop(_5) -> [return: bb19, unwind terminate(cleanup)];
drop(_9) -> [return: bb19, unwind terminate(cleanup)];
}

bb19 (cleanup): {
drop(_4) -> [return: bb20, unwind terminate(cleanup)];
StorageDead(_9);
StorageDead(_8);
goto -> bb22;
}

bb20 (cleanup): {
drop(_3) -> [return: bb21, unwind terminate(cleanup)];
drop(_7) -> [return: bb21, unwind terminate(cleanup)];
}

bb21 (cleanup): {
drop(_2) -> [return: bb22, unwind terminate(cleanup)];
StorageDead(_7);
StorageDead(_6);
goto -> bb22;
}

bb22 (cleanup): {
drop(_1) -> [return: bb23, unwind terminate(cleanup)];
drop(_5) -> [return: bb23, unwind terminate(cleanup)];
}

bb23 (cleanup): {
StorageDead(_5);
drop(_4) -> [return: bb24, unwind terminate(cleanup)];
}

bb24 (cleanup): {
StorageDead(_4);
drop(_3) -> [return: bb25, unwind terminate(cleanup)];
}

bb25 (cleanup): {
StorageDead(_3);
drop(_2) -> [return: bb26, unwind terminate(cleanup)];
}

bb26 (cleanup): {
StorageDead(_2);
drop(_1) -> [return: bb27, unwind terminate(cleanup)];
}

bb27 (cleanup): {
resume;
}
}
Loading
Loading