Skip to content

Commit c4977a3

Browse files
committed
Auto merge of rust-lang#139484 - lcnr:revealing-use, r=<try>
support revealing uses of opaques in MIR borrowck major yikes r? compiler-errors
2 parents f820b75 + 6e8df86 commit c4977a3

33 files changed

+1234
-1667
lines changed

compiler/rustc_borrowck/messages.ftl

-6
Original file line numberDiff line numberDiff line change
@@ -156,12 +156,6 @@ borrowck_moved_due_to_usage_in_operator =
156156
*[false] operator
157157
}
158158
159-
borrowck_opaque_type_lifetime_mismatch =
160-
opaque type used twice with different lifetimes
161-
.label = lifetime `{$arg}` used here
162-
.prev_lifetime_label = lifetime `{$prev}` previously used here
163-
.note = if all non-lifetime generic parameters are the same, but the lifetime parameters differ, it is not possible to differentiate the opaque types
164-
165159
borrowck_partial_var_move_by_use_in_closure =
166160
variable {$is_partial ->
167161
[true] partially moved

compiler/rustc_borrowck/src/consumers.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ pub use super::polonius::legacy::{
1515
RichLocation, RustcFacts,
1616
};
1717
pub use super::region_infer::RegionInferenceContext;
18+
use crate::{BorrowCheckRootCtxt, do_mir_borrowck};
1819

1920
/// Options determining the output behavior of [`get_body_with_borrowck_facts`].
2021
///
@@ -97,8 +98,9 @@ pub struct BodyWithBorrowckFacts<'tcx> {
9798
/// * Polonius is highly unstable, so expect regular changes in its signature or other details.
9899
pub fn get_body_with_borrowck_facts(
99100
tcx: TyCtxt<'_>,
100-
def: LocalDefId,
101+
def_id: LocalDefId,
101102
options: ConsumerOptions,
102103
) -> BodyWithBorrowckFacts<'_> {
103-
*super::do_mir_borrowck(tcx, def, Some(options)).1.unwrap()
104+
let mut root_cx = BorrowCheckRootCtxt::new(tcx, def_id);
105+
*do_mir_borrowck(&mut root_cx, def_id, Some(options)).1.unwrap()
104106
}

compiler/rustc_borrowck/src/dataflow.rs

+2-42
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use std::fmt;
22

33
use rustc_data_structures::fx::FxIndexMap;
4-
use rustc_data_structures::graph;
54
use rustc_index::bit_set::DenseBitSet;
65
use rustc_middle::mir::{
76
self, BasicBlock, Body, CallReturnPlaces, Location, Place, TerminatorEdges,
@@ -317,9 +316,8 @@ impl<'tcx> PoloniusOutOfScopePrecomputer<'_, 'tcx> {
317316
loans_out_of_scope_at_location: FxIndexMap::default(),
318317
};
319318
for (loan_idx, loan_data) in borrow_set.iter_enumerated() {
320-
let issuing_region = loan_data.region;
321319
let loan_issued_at = loan_data.reserve_location;
322-
prec.precompute_loans_out_of_scope(loan_idx, issuing_region, loan_issued_at);
320+
prec.precompute_loans_out_of_scope(loan_idx, loan_issued_at);
323321
}
324322

325323
prec.loans_out_of_scope_at_location
@@ -328,45 +326,7 @@ impl<'tcx> PoloniusOutOfScopePrecomputer<'_, 'tcx> {
328326
/// Loans are in scope while they are live: whether they are contained within any live region.
329327
/// In the location-insensitive analysis, a loan will be contained in a region if the issuing
330328
/// region can reach it in the subset graph. So this is a reachability problem.
331-
fn precompute_loans_out_of_scope(
332-
&mut self,
333-
loan_idx: BorrowIndex,
334-
issuing_region: RegionVid,
335-
loan_issued_at: Location,
336-
) {
337-
let sccs = self.regioncx.constraint_sccs();
338-
let universal_regions = self.regioncx.universal_regions();
339-
340-
// The loop below was useful for the location-insensitive analysis but shouldn't be
341-
// impactful in the location-sensitive case. It seems that it does, however, as without it a
342-
// handful of tests fail. That likely means some liveness or outlives data related to choice
343-
// regions is missing
344-
// FIXME: investigate the impact of loans traversing applied member constraints and why some
345-
// tests fail otherwise.
346-
//
347-
// We first handle the cases where the loan doesn't go out of scope, depending on the
348-
// issuing region's successors.
349-
for successor in graph::depth_first_search(&self.regioncx.region_graph(), issuing_region) {
350-
// Via applied member constraints
351-
//
352-
// The issuing region can flow into the choice regions, and they are either:
353-
// - placeholders or free regions themselves,
354-
// - or also transitively outlive a free region.
355-
//
356-
// That is to say, if there are applied member constraints here, the loan escapes the
357-
// function and cannot go out of scope. We could early return here.
358-
//
359-
// For additional insurance via fuzzing and crater, we verify that the constraint's min
360-
// choice indeed escapes the function. In the future, we could e.g. turn this check into
361-
// a debug assert and early return as an optimization.
362-
let scc = sccs.scc(successor);
363-
for constraint in self.regioncx.applied_member_constraints(scc) {
364-
if universal_regions.is_universal_region(constraint.min_choice) {
365-
return;
366-
}
367-
}
368-
}
369-
329+
fn precompute_loans_out_of_scope(&mut self, loan_idx: BorrowIndex, loan_issued_at: Location) {
370330
let first_block = loan_issued_at.block;
371331
let first_bb_data = &self.body.basic_blocks[first_block];
372332

compiler/rustc_borrowck/src/diagnostics/bound_region_errors.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -406,8 +406,8 @@ impl<'tcx> TypeOpInfo<'tcx> for crate::type_check::InstantiateOpaqueType<'tcx> {
406406
// started MIR borrowchecking with, so the region
407407
// constraints have already been taken. Use the data from
408408
// our `mbcx` instead.
409-
|vid| mbcx.regioncx.var_infos[vid].origin,
410-
|vid| mbcx.regioncx.var_infos[vid].universe,
409+
|vid| RegionVariableOrigin::Nll(mbcx.regioncx.definitions[vid].origin),
410+
|vid| mbcx.regioncx.definitions[vid].universe,
411411
)
412412
}
413413
}

compiler/rustc_borrowck/src/diagnostics/mod.rs

+3-6
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,7 @@ use rustc_errors::{Applicability, Diag, EmissionGuarantee, MultiSpan, listify};
88
use rustc_hir::def::{CtorKind, Namespace};
99
use rustc_hir::{self as hir, CoroutineKind, LangItem};
1010
use rustc_index::IndexSlice;
11-
use rustc_infer::infer::{
12-
BoundRegionConversionTime, NllRegionVariableOrigin, RegionVariableOrigin,
13-
};
11+
use rustc_infer::infer::{BoundRegionConversionTime, NllRegionVariableOrigin};
1412
use rustc_infer::traits::SelectionError;
1513
use rustc_middle::bug;
1614
use rustc_middle::mir::{
@@ -633,9 +631,8 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
633631
) {
634632
let predicate_span = path.iter().find_map(|constraint| {
635633
let outlived = constraint.sub;
636-
if let Some(origin) = self.regioncx.var_infos.get(outlived)
637-
&& let RegionVariableOrigin::Nll(NllRegionVariableOrigin::Placeholder(_)) =
638-
origin.origin
634+
if let Some(origin) = self.regioncx.definitions.get(outlived)
635+
&& let NllRegionVariableOrigin::Placeholder(_) = origin.origin
639636
&& let ConstraintCategory::Predicate(span) = constraint.category
640637
{
641638
Some(span)

compiler/rustc_borrowck/src/diagnostics/region_errors.rs

-43
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ use rustc_trait_selection::error_reporting::infer::nice_region_error::{
2323
self, HirTraitObjectVisitor, NiceRegionError, TraitObjectVisitor, find_anon_type,
2424
find_param_with_region, suggest_adding_lifetime_params,
2525
};
26-
use rustc_trait_selection::error_reporting::infer::region::unexpected_hidden_region_diagnostic;
2726
use rustc_trait_selection::infer::InferCtxtExt;
2827
use rustc_trait_selection::traits::{Obligation, ObligationCtxt};
2928
use tracing::{debug, instrument, trace};
@@ -84,9 +83,6 @@ impl<'tcx> RegionErrors<'tcx> {
8483
let guar = self.1.sess.dcx().delayed_bug(format!("{val:?}"));
8584
self.0.push((val, guar));
8685
}
87-
pub(crate) fn is_empty(&self) -> bool {
88-
self.0.is_empty()
89-
}
9086
pub(crate) fn into_iter(
9187
self,
9288
) -> impl Iterator<Item = (RegionErrorKind<'tcx>, ErrorGuaranteed)> {
@@ -108,18 +104,6 @@ pub(crate) enum RegionErrorKind<'tcx> {
108104
/// A generic bound failure for a type test (`T: 'a`).
109105
TypeTestError { type_test: TypeTest<'tcx> },
110106

111-
/// An unexpected hidden region for an opaque type.
112-
UnexpectedHiddenRegion {
113-
/// The span for the member constraint.
114-
span: Span,
115-
/// The hidden type.
116-
hidden_ty: Ty<'tcx>,
117-
/// The opaque type.
118-
key: ty::OpaqueTypeKey<'tcx>,
119-
/// The unexpected region.
120-
member_region: ty::Region<'tcx>,
121-
},
122-
123107
/// Higher-ranked subtyping error.
124108
BoundUniversalRegionError {
125109
/// The placeholder free region.
@@ -311,9 +295,6 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
311295
// buffered in the `MirBorrowckCtxt`.
312296

313297
let mut outlives_suggestion = OutlivesSuggestionBuilder::default();
314-
let mut last_unexpected_hidden_region: Option<(Span, Ty<'_>, ty::OpaqueTypeKey<'tcx>)> =
315-
None;
316-
317298
for (nll_error, _) in nll_errors.into_iter() {
318299
match nll_error {
319300
RegionErrorKind::TypeTestError { type_test } => {
@@ -363,30 +344,6 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
363344
}
364345
}
365346

366-
RegionErrorKind::UnexpectedHiddenRegion { span, hidden_ty, key, member_region } => {
367-
let named_ty =
368-
self.regioncx.name_regions_for_member_constraint(self.infcx.tcx, hidden_ty);
369-
let named_key =
370-
self.regioncx.name_regions_for_member_constraint(self.infcx.tcx, key);
371-
let named_region = self
372-
.regioncx
373-
.name_regions_for_member_constraint(self.infcx.tcx, member_region);
374-
let diag = unexpected_hidden_region_diagnostic(
375-
self.infcx,
376-
self.mir_def_id(),
377-
span,
378-
named_ty,
379-
named_region,
380-
named_key,
381-
);
382-
if last_unexpected_hidden_region != Some((span, named_ty, named_key)) {
383-
self.buffer_error(diag);
384-
last_unexpected_hidden_region = Some((span, named_ty, named_key));
385-
} else {
386-
diag.delay_as_bug();
387-
}
388-
}
389-
390347
RegionErrorKind::BoundUniversalRegionError {
391348
longer_fr,
392349
placeholder,

0 commit comments

Comments
 (0)