Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions compiler/rustc_middle/src/middle/region.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ impl fmt::Debug for Scope {
ScopeData::IfThen => write!(fmt, "IfThen({:?})", self.local_id),
ScopeData::IfThenRescope => write!(fmt, "IfThen[edition2024]({:?})", self.local_id),
ScopeData::MatchGuard => write!(fmt, "MatchGuard({:?})", self.local_id),
ScopeData::MatchFakeBorrows => write!(fmt, "MatchFakeBorrows({:?})", self.local_id),
ScopeData::Remainder(fsi) => write!(
fmt,
"Remainder {{ block: {:?}, first_statement_index: {}}}",
Expand Down Expand Up @@ -137,6 +138,9 @@ pub enum ScopeData {
/// whose lifetimes do not cross beyond this scope.
MatchGuard,

/// Dummy scope used for matches' fake borrow temporaries.
MatchFakeBorrows,

/// Scope following a `let id = expr;` binding in a block.
Remainder(FirstStatementIndex),
}
Expand Down Expand Up @@ -323,6 +327,7 @@ impl ScopeTree {
| ScopeData::CallSite
| ScopeData::Arguments
| ScopeData::IfThen
| ScopeData::MatchFakeBorrows
| ScopeData::Remainder(_) => {
// If we haven't already passed through a backwards-incompatible node,
// then check if we are passing through one now and record it if so.
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_middle/src/mir/syntax.rs
Original file line number Diff line number Diff line change
Expand Up @@ -503,7 +503,7 @@ impl WithRetag {
}

/// The `FakeReadCause` describes the type of pattern why a FakeRead statement exists.
#[derive(Copy, Clone, TyEncodable, TyDecodable, Debug, StableHash, PartialEq)]
#[derive(Copy, Clone, TyEncodable, TyDecodable, Debug, StableHash, PartialEq, Eq, Hash)]
pub enum FakeReadCause {
/// A fake read injected into a match guard to ensure that the discriminants
/// that are being matched on aren't modified while the match guard is being
Expand Down
55 changes: 23 additions & 32 deletions compiler/rustc_mir_build/src/builder/expr/as_place.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use std::{assert_matches, iter};
use rustc_abi::{FIRST_VARIANT, FieldIdx, VariantIdx};
use rustc_hir::def_id::LocalDefId;
use rustc_middle::hir::place::{Projection as HirProjection, ProjectionKind as HirProjectionKind};
use rustc_middle::middle::region;
use rustc_middle::mir::AssertKind::BoundsCheck;
use rustc_middle::mir::*;
use rustc_middle::thir::*;
Expand Down Expand Up @@ -419,7 +420,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
mut block: BasicBlock,
expr_id: ExprId,
mutability: Mutability,
fake_borrow_temps: Option<&mut Vec<Local>>,
fake_borrow_scope: Option<region::Scope>,
) -> BlockAnd<PlaceBuilder<'tcx>> {
let expr = &self.thir[expr_id];
debug!("expr_as_place(block={:?}, expr={:?}, mutability={:?})", block, expr, mutability);
Expand All @@ -431,13 +432,13 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
ExprKind::Scope { region_scope, hir_id, value } => {
this.in_scope((region_scope, source_info), LintLevel::Explicit(hir_id), |this| {
this.push_coverage_point_for_expr(block, source_info, hir_id);
this.expr_as_place(block, value, mutability, fake_borrow_temps)
this.expr_as_place(block, value, mutability, fake_borrow_scope)
})
}
ExprKind::Field { lhs, variant_index, name } => {
let lhs_expr = &this.thir[lhs];
let mut place_builder =
unpack!(block = this.expr_as_place(block, lhs, mutability, fake_borrow_temps,));
unpack!(block = this.expr_as_place(block, lhs, mutability, fake_borrow_scope,));
if let ty::Adt(adt_def, _) = lhs_expr.ty.kind() {
if adt_def.is_enum() {
place_builder = place_builder.downcast(*adt_def, variant_index);
Expand All @@ -447,15 +448,15 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
}
ExprKind::Deref { arg } => {
let place_builder =
unpack!(block = this.expr_as_place(block, arg, mutability, fake_borrow_temps,));
unpack!(block = this.expr_as_place(block, arg, mutability, fake_borrow_scope,));
block.and(place_builder.deref())
}
ExprKind::Index { lhs, index } => this.lower_index_expression(
block,
lhs,
index,
mutability,
fake_borrow_temps,
fake_borrow_scope,
expr_span,
source_info,
),
Expand All @@ -476,7 +477,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {

ExprKind::PlaceTypeAscription { source, ref user_ty, user_ty_span } => {
let place_builder = unpack!(
block = this.expr_as_place(block, source, mutability, fake_borrow_temps,)
block = this.expr_as_place(block, source, mutability, fake_borrow_scope,)
);
if let Some(user_ty) = user_ty {
let ty_source_info = this.source_info(user_ty_span);
Expand Down Expand Up @@ -535,7 +536,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {

ExprKind::PlaceUnwrapUnsafeBinder { source } => {
let place_builder = unpack!(
block = this.expr_as_place(block, source, mutability, fake_borrow_temps,)
block = this.expr_as_place(block, source, mutability, fake_borrow_scope,)
);
block.and(place_builder.project(PlaceElem::UnwrapUnsafeBinder(expr.ty)))
}
Expand Down Expand Up @@ -625,16 +626,15 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
base: ExprId,
index: ExprId,
mutability: Mutability,
fake_borrow_temps: Option<&mut Vec<Local>>,
fake_borrow_scope: Option<region::Scope>,
expr_span: Span,
source_info: SourceInfo,
) -> BlockAnd<PlaceBuilder<'tcx>> {
let base_fake_borrow_temps = &mut Vec::new();
let is_outermost_index = fake_borrow_temps.is_none();
let fake_borrow_temps = fake_borrow_temps.unwrap_or(base_fake_borrow_temps);
let is_outermost_index = fake_borrow_scope.is_none();
let fake_borrow_scope = fake_borrow_scope.unwrap_or_else(|| self.local_scope());

let base_place =
unpack!(block = self.expr_as_place(block, base, mutability, Some(fake_borrow_temps),));
unpack!(block = self.expr_as_place(block, base, mutability, Some(fake_borrow_scope),));

// Making this a *fresh* temporary means we do not have to worry about
// the index changing later: Nothing will ever change this temporary.
Expand All @@ -647,13 +647,11 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {

block = self.bounds_check(block, &base_place, idx, expr_span, source_info);

if is_outermost_index {
self.read_fake_borrows(block, fake_borrow_temps, source_info)
} else {
if !is_outermost_index {
self.add_fake_borrows_of_base(
base_place.to_place(self),
block,
fake_borrow_temps,
fake_borrow_scope,
expr_span,
source_info,
);
Expand Down Expand Up @@ -760,7 +758,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
&mut self,
base_place: Place<'tcx>,
block: BasicBlock,
fake_borrow_temps: &mut Vec<Local>,
fake_borrow_scope: region::Scope,
expr_span: Span,
source_info: SourceInfo,
) {
Expand Down Expand Up @@ -791,7 +789,14 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
Place { local: base_place.local, projection },
),
);
fake_borrow_temps.push(fake_borrow_temp);
// Schedule a fake read to keep the fake borrow alive until exiting
// `fake_borrow_scope`.
self.schedule_drop_fake_read(
expr_span,
fake_borrow_scope,
fake_borrow_temp,
FakeReadCause::ForIndex,
);
}
ProjectionElem::Index(_) => {
let index_ty = base_place.ty(&self.local_decls, tcx);
Expand All @@ -814,20 +819,6 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
}
}
}

fn read_fake_borrows(
&mut self,
bb: BasicBlock,
fake_borrow_temps: &mut Vec<Local>,
source_info: SourceInfo,
) {
// All indexes have been evaluated now, read all of the
// fake borrows so that they are live across those index
// expressions.
for temp in fake_borrow_temps {
self.cfg.push_fake_read(bb, source_info, FakeReadCause::ForIndex, Place::from(*temp));
}
}
}

/// Precise capture is enabled if user is using Rust Edition 2021 or higher.
Expand Down
50 changes: 29 additions & 21 deletions compiler/rustc_mir_build/src/builder/matches/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use rustc_abi::{FIRST_VARIANT, FieldIdx, VariantIdx};
use rustc_data_structures::fx::FxIndexMap;
use rustc_hir::attrs::lang_items::LangItem;
use rustc_hir::{BindingMode, ByRef, LetStmt, LocalSource, Node};
use rustc_middle::middle::region::{self, TempLifetime};
use rustc_middle::middle::region::{self, ScopeData, TempLifetime};
use rustc_middle::mir::*;
use rustc_middle::thir::{self, *};
use rustc_middle::ty::{self, CanonicalUserTypeAnnotation, Ty, ValTree, ValTreeKind};
Expand Down Expand Up @@ -2432,21 +2432,35 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
self.cfg.push_assign(block, scrutinee_source_info, Place::from(temp), borrow);
}

let mut guard_span = rustc_span::DUMMY_SP;
let guard_span = self.thir[guard].span;
let source_info = self.source_info(guard_span);

let (guard_true_block, guard_false_block) =
self.in_if_then_scope(match_scope, guard_span, |this| {
guard_span = this.thir[guard].span;
this.lower_if_condition(
block,
guard,
LowerIfCondArgs {
temp_scope_override: None, // Use `this.local_scope()`.
variable_source_info: this.source_info(arm.span),
// For guards, `let` bindings are declared separately.
declare_let_bindings: DeclareLetBindings::No,
},
)
let fake_borrow_scope = region::Scope {
data: ScopeData::MatchFakeBorrows,
local_id: self.thir[guard].temp_scope_id,
};
let BlockAnd(guard_true_block, guard_false_block) =
self.in_scope((fake_borrow_scope, source_info), LintLevel::Inherited, |this| {
// Schedule fake reads on guard exit to keep fake borrows alive on every path.
let cause = FakeReadCause::ForMatchGuard;
for &(_, temp, _) in fake_borrows {
this.schedule_drop_fake_read(guard_span, fake_borrow_scope, temp, cause);
}

let (guard_true_block, guard_false_block) =
this.in_if_then_scope(match_scope, guard_span, |this| {
this.lower_if_condition(
block,
guard,
LowerIfCondArgs {
temp_scope_override: None, // Use `this.local_scope()`.
variable_source_info: this.source_info(arm.span),
// For guards, `let` bindings are declared separately.
declare_let_bindings: DeclareLetBindings::No,
},
)
});
guard_true_block.and(guard_false_block)
});

// If this isn't the final sub-branch being lowered, we need to unschedule drops of
Expand All @@ -2456,16 +2470,10 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
self.clear_match_arm_and_guard_scopes(arm.scope);
}

let source_info = self.source_info(guard_span);
let guard_end = self.source_info(tcx.sess.source_map().end_point(guard_span));
let guard_frame = self.guard_context.pop().unwrap();
debug!("Exiting guard building context with locals: {:?}", guard_frame);

for &(_, temp, _) in fake_borrows {
let cause = FakeReadCause::ForMatchGuard;
self.cfg.push_fake_read(guard_true_block, guard_end, cause, Place::from(temp));
}

self.cfg.goto(guard_false_block, source_info, sub_branch.otherwise_block);

// We want to ensure that the matched candidates are bound
Expand Down
Loading
Loading