Skip to content

Commit 51f5892

Browse files
committed
Auto merge of rust-lang#148324 - Zalathar:rollup-yp35ktq, r=Zalathar
Rollup of 3 pull requests Successful merges: - rust-lang#148165 (Use `mut` less in dataflow analysis) - rust-lang#148287 (Fix deferred cast checks using the wrong body for determining constness) - rust-lang#148317 (bootstrap: Extract parts of `bootstrap::core::builder` into a `cli_paths` module) r? `@ghost` `@rustbot` modify labels: rollup
2 parents 23c7bad + 472c7cd commit 51f5892

File tree

25 files changed

+491
-492
lines changed

25 files changed

+491
-492
lines changed

compiler/rustc_borrowck/src/dataflow.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ impl<'a, 'tcx> Analysis<'tcx> for Borrowck<'a, 'tcx> {
4444
}
4545

4646
fn apply_early_statement_effect(
47-
&mut self,
47+
&self,
4848
state: &mut Self::Domain,
4949
stmt: &mir::Statement<'tcx>,
5050
loc: Location,
@@ -55,7 +55,7 @@ impl<'a, 'tcx> Analysis<'tcx> for Borrowck<'a, 'tcx> {
5555
}
5656

5757
fn apply_primary_statement_effect(
58-
&mut self,
58+
&self,
5959
state: &mut Self::Domain,
6060
stmt: &mir::Statement<'tcx>,
6161
loc: Location,
@@ -66,7 +66,7 @@ impl<'a, 'tcx> Analysis<'tcx> for Borrowck<'a, 'tcx> {
6666
}
6767

6868
fn apply_early_terminator_effect(
69-
&mut self,
69+
&self,
7070
state: &mut Self::Domain,
7171
term: &mir::Terminator<'tcx>,
7272
loc: Location,
@@ -77,7 +77,7 @@ impl<'a, 'tcx> Analysis<'tcx> for Borrowck<'a, 'tcx> {
7777
}
7878

7979
fn apply_primary_terminator_effect<'mir>(
80-
&mut self,
80+
&self,
8181
state: &mut Self::Domain,
8282
term: &'mir mir::Terminator<'tcx>,
8383
loc: Location,
@@ -92,7 +92,7 @@ impl<'a, 'tcx> Analysis<'tcx> for Borrowck<'a, 'tcx> {
9292
}
9393

9494
fn apply_call_return_effect(
95-
&mut self,
95+
&self,
9696
_state: &mut Self::Domain,
9797
_block: BasicBlock,
9898
_return_places: CallReturnPlaces<'_, 'tcx>,
@@ -533,7 +533,7 @@ impl<'tcx> rustc_mir_dataflow::Analysis<'tcx> for Borrows<'_, 'tcx> {
533533
}
534534

535535
fn apply_early_statement_effect(
536-
&mut self,
536+
&self,
537537
state: &mut Self::Domain,
538538
_statement: &mir::Statement<'tcx>,
539539
location: Location,
@@ -542,7 +542,7 @@ impl<'tcx> rustc_mir_dataflow::Analysis<'tcx> for Borrows<'_, 'tcx> {
542542
}
543543

544544
fn apply_primary_statement_effect(
545-
&mut self,
545+
&self,
546546
state: &mut Self::Domain,
547547
stmt: &mir::Statement<'tcx>,
548548
location: Location,
@@ -590,7 +590,7 @@ impl<'tcx> rustc_mir_dataflow::Analysis<'tcx> for Borrows<'_, 'tcx> {
590590
}
591591

592592
fn apply_early_terminator_effect(
593-
&mut self,
593+
&self,
594594
state: &mut Self::Domain,
595595
_terminator: &mir::Terminator<'tcx>,
596596
location: Location,
@@ -599,7 +599,7 @@ impl<'tcx> rustc_mir_dataflow::Analysis<'tcx> for Borrows<'_, 'tcx> {
599599
}
600600

601601
fn apply_primary_terminator_effect<'mir>(
602-
&mut self,
602+
&self,
603603
state: &mut Self::Domain,
604604
terminator: &'mir mir::Terminator<'tcx>,
605605
_location: Location,

compiler/rustc_borrowck/src/lib.rs

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ use rustc_mir_dataflow::move_paths::{
4949
InitIndex, InitLocation, LookupResult, MoveData, MovePathIndex,
5050
};
5151
use rustc_mir_dataflow::points::DenseLocationMap;
52-
use rustc_mir_dataflow::{Analysis, Results, ResultsVisitor, visit_results};
52+
use rustc_mir_dataflow::{Analysis, EntryStates, Results, ResultsVisitor, visit_results};
5353
use rustc_session::lint::builtin::{TAIL_EXPR_DROP_ORDER, UNUSED_MUT};
5454
use rustc_span::{ErrorGuaranteed, Span, Symbol};
5555
use smallvec::SmallVec;
@@ -537,13 +537,11 @@ fn borrowck_check_region_constraints<'tcx>(
537537
mbcx.report_region_errors(nll_errors);
538538
}
539539

540-
let (mut flow_analysis, flow_entry_states) =
541-
get_flow_results(tcx, body, &move_data, &borrow_set, &regioncx);
540+
let flow_results = get_flow_results(tcx, body, &move_data, &borrow_set, &regioncx);
542541
visit_results(
543542
body,
544543
traversal::reverse_postorder(body).map(|(bb, _)| bb),
545-
&mut flow_analysis,
546-
&flow_entry_states,
544+
&flow_results,
547545
&mut mbcx,
548546
);
549547

@@ -604,7 +602,7 @@ fn get_flow_results<'a, 'tcx>(
604602
move_data: &'a MoveData<'tcx>,
605603
borrow_set: &'a BorrowSet<'tcx>,
606604
regioncx: &RegionInferenceContext<'tcx>,
607-
) -> (Borrowck<'a, 'tcx>, Results<BorrowckDomain>) {
605+
) -> Results<'tcx, Borrowck<'a, 'tcx>> {
608606
// We compute these three analyses individually, but them combine them into
609607
// a single results so that `mbcx` can visit them all together.
610608
let borrows = Borrows::new(tcx, body, regioncx, borrow_set).iterate_to_fixpoint(
@@ -629,14 +627,14 @@ fn get_flow_results<'a, 'tcx>(
629627
ever_inits: ever_inits.analysis,
630628
};
631629

632-
assert_eq!(borrows.results.len(), uninits.results.len());
633-
assert_eq!(borrows.results.len(), ever_inits.results.len());
634-
let results: Results<_> =
635-
itertools::izip!(borrows.results, uninits.results, ever_inits.results)
630+
assert_eq!(borrows.entry_states.len(), uninits.entry_states.len());
631+
assert_eq!(borrows.entry_states.len(), ever_inits.entry_states.len());
632+
let entry_states: EntryStates<_> =
633+
itertools::izip!(borrows.entry_states, uninits.entry_states, ever_inits.entry_states)
636634
.map(|(borrows, uninits, ever_inits)| BorrowckDomain { borrows, uninits, ever_inits })
637635
.collect();
638636

639-
(analysis, results)
637+
Results { analysis, entry_states }
640638
}
641639

642640
pub(crate) struct BorrowckInferCtxt<'tcx> {
@@ -790,7 +788,7 @@ struct MirBorrowckCtxt<'a, 'infcx, 'tcx> {
790788
impl<'a, 'tcx> ResultsVisitor<'tcx, Borrowck<'a, 'tcx>> for MirBorrowckCtxt<'a, '_, 'tcx> {
791789
fn visit_after_early_statement_effect(
792790
&mut self,
793-
_analysis: &mut Borrowck<'a, 'tcx>,
791+
_analysis: &Borrowck<'a, 'tcx>,
794792
state: &BorrowckDomain,
795793
stmt: &Statement<'tcx>,
796794
location: Location,
@@ -865,7 +863,7 @@ impl<'a, 'tcx> ResultsVisitor<'tcx, Borrowck<'a, 'tcx>> for MirBorrowckCtxt<'a,
865863

866864
fn visit_after_early_terminator_effect(
867865
&mut self,
868-
_analysis: &mut Borrowck<'a, 'tcx>,
866+
_analysis: &Borrowck<'a, 'tcx>,
869867
state: &BorrowckDomain,
870868
term: &Terminator<'tcx>,
871869
loc: Location,
@@ -985,7 +983,7 @@ impl<'a, 'tcx> ResultsVisitor<'tcx, Borrowck<'a, 'tcx>> for MirBorrowckCtxt<'a,
985983

986984
fn visit_after_primary_terminator_effect(
987985
&mut self,
988-
_analysis: &mut Borrowck<'a, 'tcx>,
986+
_analysis: &Borrowck<'a, 'tcx>,
989987
state: &BorrowckDomain,
990988
term: &Terminator<'tcx>,
991989
loc: Location,

compiler/rustc_const_eval/src/check_consts/resolver.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,7 @@ where
332332
}
333333

334334
fn apply_primary_statement_effect(
335-
&mut self,
335+
&self,
336336
state: &mut Self::Domain,
337337
statement: &mir::Statement<'tcx>,
338338
location: Location,
@@ -341,7 +341,7 @@ where
341341
}
342342

343343
fn apply_primary_terminator_effect<'mir>(
344-
&mut self,
344+
&self,
345345
state: &mut Self::Domain,
346346
terminator: &'mir mir::Terminator<'tcx>,
347347
location: Location,
@@ -351,7 +351,7 @@ where
351351
}
352352

353353
fn apply_call_return_effect(
354-
&mut self,
354+
&self,
355355
state: &mut Self::Domain,
356356
block: BasicBlock,
357357
return_places: CallReturnPlaces<'_, 'tcx>,

compiler/rustc_hir_typeck/src/cast.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ use rustc_ast::util::parser::ExprPrecedence;
3232
use rustc_data_structures::fx::FxHashSet;
3333
use rustc_errors::codes::*;
3434
use rustc_errors::{Applicability, Diag, ErrorGuaranteed};
35-
use rustc_hir::def_id::DefId;
35+
use rustc_hir::def_id::{DefId, LocalDefId};
3636
use rustc_hir::{self as hir, ExprKind};
3737
use rustc_infer::infer::DefineOpaqueTypes;
3838
use rustc_macros::{TypeFoldable, TypeVisitable};
@@ -63,6 +63,7 @@ pub(crate) struct CastCheck<'tcx> {
6363
cast_ty: Ty<'tcx>,
6464
cast_span: Span,
6565
span: Span,
66+
pub body_id: LocalDefId,
6667
}
6768

6869
/// The kind of pointer and associated metadata (thin, length or vtable) - we
@@ -244,7 +245,8 @@ impl<'a, 'tcx> CastCheck<'tcx> {
244245
span: Span,
245246
) -> Result<CastCheck<'tcx>, ErrorGuaranteed> {
246247
let expr_span = expr.span.find_ancestor_inside(span).unwrap_or(expr.span);
247-
let check = CastCheck { expr, expr_ty, expr_span, cast_ty, cast_span, span };
248+
let check =
249+
CastCheck { expr, expr_ty, expr_span, cast_ty, cast_span, span, body_id: fcx.body_id };
248250

249251
// For better error messages, check for some obviously unsized
250252
// cases now. We do a more thorough check at the end, once

compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use std::ops::Deref;
2-
use std::{fmt, iter, mem};
2+
use std::{fmt, iter};
33

44
use itertools::Itertools;
55
use rustc_data_structures::fx::FxIndexSet;
@@ -72,16 +72,13 @@ pub(crate) enum DivergingBlockBehavior {
7272

7373
impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
7474
pub(in super::super) fn check_casts(&mut self) {
75-
// don't hold the borrow to deferred_cast_checks while checking to avoid borrow checker errors
76-
// when writing to `self.param_env`.
77-
let mut deferred_cast_checks = mem::take(&mut *self.deferred_cast_checks.borrow_mut());
78-
75+
let mut deferred_cast_checks = self.root_ctxt.deferred_cast_checks.borrow_mut();
7976
debug!("FnCtxt::check_casts: {} deferred checks", deferred_cast_checks.len());
8077
for cast in deferred_cast_checks.drain(..) {
78+
let body_id = std::mem::replace(&mut self.body_id, cast.body_id);
8179
cast.check(self);
80+
self.body_id = body_id;
8281
}
83-
84-
*self.deferred_cast_checks.borrow_mut() = deferred_cast_checks;
8582
}
8683

8784
pub(in super::super) fn check_asms(&self) {

compiler/rustc_mir_dataflow/src/framework/cursor.rs

Lines changed: 20 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,28 @@
11
//! Random access inspection of the results of a dataflow analysis.
22
3-
use std::borrow::Cow;
43
use std::cmp::Ordering;
5-
use std::ops::{Deref, DerefMut};
4+
use std::ops::Deref;
65

76
#[cfg(debug_assertions)]
87
use rustc_index::bit_set::DenseBitSet;
98
use rustc_middle::mir::{self, BasicBlock, Location};
109

1110
use super::{Analysis, Direction, Effect, EffectIndex, Results};
1211

13-
/// Some `ResultsCursor`s want to own an `Analysis`, and some want to borrow an `Analysis`, either
14-
/// mutable or immutably. This type allows all of the above. It's similar to `Cow`, but `Cow`
15-
/// doesn't allow mutable borrowing.
16-
enum CowMut<'a, T> {
17-
BorrowedMut(&'a mut T),
12+
/// This is like `Cow`, but it lacks the `T: ToOwned` bound and doesn't support
13+
/// `to_owned`/`into_owned`.
14+
enum SimpleCow<'a, T> {
15+
Borrowed(&'a T),
1816
Owned(T),
1917
}
2018

21-
impl<T> Deref for CowMut<'_, T> {
19+
impl<T> Deref for SimpleCow<'_, T> {
2220
type Target = T;
2321

2422
fn deref(&self) -> &T {
2523
match self {
26-
CowMut::BorrowedMut(borrowed) => borrowed,
27-
CowMut::Owned(owned) => owned,
28-
}
29-
}
30-
}
31-
32-
impl<T> DerefMut for CowMut<'_, T> {
33-
fn deref_mut(&mut self) -> &mut T {
34-
match self {
35-
CowMut::BorrowedMut(borrowed) => borrowed,
36-
CowMut::Owned(owned) => owned,
24+
SimpleCow::Borrowed(borrowed) => borrowed,
25+
SimpleCow::Owned(owned) => owned,
3726
}
3827
}
3928
}
@@ -53,8 +42,7 @@ where
5342
A: Analysis<'tcx>,
5443
{
5544
body: &'mir mir::Body<'tcx>,
56-
analysis: CowMut<'mir, A>,
57-
results: Cow<'mir, Results<A::Domain>>,
45+
results: SimpleCow<'mir, Results<'tcx, A>>,
5846
state: A::Domain,
5947

6048
pos: CursorPosition,
@@ -82,15 +70,10 @@ where
8270
self.body
8371
}
8472

85-
fn new(
86-
body: &'mir mir::Body<'tcx>,
87-
analysis: CowMut<'mir, A>,
88-
results: Cow<'mir, Results<A::Domain>>,
89-
) -> Self {
90-
let bottom_value = analysis.bottom_value(body);
73+
fn new(body: &'mir mir::Body<'tcx>, results: SimpleCow<'mir, Results<'tcx, A>>) -> Self {
74+
let bottom_value = results.analysis.bottom_value(body);
9175
ResultsCursor {
9276
body,
93-
analysis,
9477
results,
9578

9679
// Initialize to the `bottom_value` and set `state_needs_reset` to tell the cursor that
@@ -106,21 +89,13 @@ where
10689
}
10790

10891
/// Returns a new cursor that takes ownership of and inspects analysis results.
109-
pub fn new_owning(
110-
body: &'mir mir::Body<'tcx>,
111-
analysis: A,
112-
results: Results<A::Domain>,
113-
) -> Self {
114-
Self::new(body, CowMut::Owned(analysis), Cow::Owned(results))
92+
pub fn new_owning(body: &'mir mir::Body<'tcx>, results: Results<'tcx, A>) -> Self {
93+
Self::new(body, SimpleCow::Owned(results))
11594
}
11695

11796
/// Returns a new cursor that borrows and inspects analysis results.
118-
pub fn new_borrowing(
119-
body: &'mir mir::Body<'tcx>,
120-
analysis: &'mir mut A,
121-
results: &'mir Results<A::Domain>,
122-
) -> Self {
123-
Self::new(body, CowMut::BorrowedMut(analysis), Cow::Borrowed(results))
97+
pub fn new_borrowing(body: &'mir mir::Body<'tcx>, results: &'mir Results<'tcx, A>) -> Self {
98+
Self::new(body, SimpleCow::Borrowed(results))
12499
}
125100

126101
/// Allows inspection of unreachable basic blocks even with `debug_assertions` enabled.
@@ -132,7 +107,7 @@ where
132107

133108
/// Returns the `Analysis` used to generate the underlying `Results`.
134109
pub fn analysis(&self) -> &A {
135-
&self.analysis
110+
&self.results.analysis
136111
}
137112

138113
/// Resets the cursor to hold the entry set for the given basic block.
@@ -144,7 +119,7 @@ where
144119
#[cfg(debug_assertions)]
145120
assert!(self.reachable_blocks.contains(block));
146121

147-
self.state.clone_from(&self.results[block]);
122+
self.state.clone_from(&self.results.entry_states[block]);
148123
self.pos = CursorPosition::block_entry(block);
149124
self.state_needs_reset = false;
150125
}
@@ -236,7 +211,7 @@ where
236211
let target_effect_index = effect.at_index(target.statement_index);
237212

238213
A::Direction::apply_effects_in_range(
239-
&mut *self.analysis,
214+
&self.results.analysis,
240215
&mut self.state,
241216
target.block,
242217
block_data,
@@ -251,8 +226,8 @@ where
251226
///
252227
/// This can be used, e.g., to apply the call return effect directly to the cursor without
253228
/// creating an extra copy of the dataflow state.
254-
pub fn apply_custom_effect(&mut self, f: impl FnOnce(&mut A, &mut A::Domain)) {
255-
f(&mut self.analysis, &mut self.state);
229+
pub fn apply_custom_effect(&mut self, f: impl FnOnce(&A, &mut A::Domain)) {
230+
f(&self.results.analysis, &mut self.state);
256231
self.state_needs_reset = true;
257232
}
258233
}

0 commit comments

Comments
 (0)