Skip to content

Commit 60ce19d

Browse files
authored
Rollup merge of #109629 - aliemjay:remove-givens, r=lcnr
remove obsolete `givens` from regionck Revives #107376. The only change is the last commit (2a3177a) which should fix the regression. Fixes #106567 r? `@lcnr`
2 parents ef5ef53 + 2a3177a commit 60ce19d

File tree

15 files changed

+114
-152
lines changed

15 files changed

+114
-152
lines changed

Diff for: compiler/rustc_hir_analysis/src/check/compare_impl_item.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,6 @@ fn compare_method_predicate_entailment<'tcx>(
330330
// lifetime parameters.
331331
let outlives_env = OutlivesEnvironment::with_bounds(
332332
param_env,
333-
Some(infcx),
334333
infcx.implied_bounds_tys(param_env, impl_m_def_id, wf_tys.clone()),
335334
);
336335
infcx.process_registered_region_obligations(
@@ -727,7 +726,6 @@ pub(super) fn collect_return_position_impl_trait_in_trait_tys<'tcx>(
727726
// lifetime parameters.
728727
let outlives_environment = OutlivesEnvironment::with_bounds(
729728
param_env,
730-
Some(infcx),
731729
infcx.implied_bounds_tys(param_env, impl_m_def_id, wf_tys),
732730
);
733731
infcx
@@ -2065,8 +2063,7 @@ pub(super) fn check_type_bounds<'tcx>(
20652063
// Finally, resolve all regions. This catches wily misuses of
20662064
// lifetime parameters.
20672065
let implied_bounds = infcx.implied_bounds_tys(param_env, impl_ty_def_id, assumed_wf_types);
2068-
let outlives_environment =
2069-
OutlivesEnvironment::with_bounds(param_env, Some(&infcx), implied_bounds);
2066+
let outlives_environment = OutlivesEnvironment::with_bounds(param_env, implied_bounds);
20702067

20712068
infcx.err_ctxt().check_region_obligations_and_report_errors(
20722069
impl_ty.def_id.expect_local(),

Diff for: compiler/rustc_hir_analysis/src/check/wfcheck.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,7 @@ pub(super) fn enter_wf_checking_ctxt<'tcx, F>(
114114
return;
115115
}
116116

117-
let outlives_environment =
118-
OutlivesEnvironment::with_bounds(param_env, Some(infcx), implied_bounds);
117+
let outlives_environment = OutlivesEnvironment::with_bounds(param_env, implied_bounds);
119118

120119
let _ = infcx
121120
.err_ctxt()
@@ -675,7 +674,6 @@ fn resolve_regions_with_wf_tys<'tcx>(
675674
let infcx = tcx.infer_ctxt().build();
676675
let outlives_environment = OutlivesEnvironment::with_bounds(
677676
param_env,
678-
Some(&infcx),
679677
infcx.implied_bounds_tys(param_env, id, wf_tys.clone()),
680678
);
681679
let region_bound_pairs = outlives_environment.region_bound_pairs();

Diff for: compiler/rustc_hir_analysis/src/impl_wf_check/min_specialization.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ fn get_impl_substs(
179179
}
180180

181181
let implied_bounds = infcx.implied_bounds_tys(param_env, impl1_def_id, assumed_wf_types);
182-
let outlives_env = OutlivesEnvironment::with_bounds(param_env, Some(infcx), implied_bounds);
182+
let outlives_env = OutlivesEnvironment::with_bounds(param_env, implied_bounds);
183183
let _ =
184184
infcx.err_ctxt().check_region_obligations_and_report_errors(impl1_def_id, &outlives_env);
185185
let Ok(impl2_substs) = infcx.fully_resolve(impl2_substs) else {

Diff for: compiler/rustc_hir_typeck/src/pat.rs

+1-8
Original file line numberDiff line numberDiff line change
@@ -238,15 +238,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
238238
// Note that there are two tests to check that this remains true
239239
// (`regions-reassign-{match,let}-bound-pointer.rs`).
240240
//
241-
// 2. Things go horribly wrong if we use subtype. The reason for
242-
// THIS is a fairly subtle case involving bound regions. See the
243-
// `givens` field in `region_constraints`, as well as the test
241+
// 2. An outdated issue related to the old HIR borrowck. See the test
244242
// `regions-relate-bound-regions-on-closures-to-inference-variables.rs`,
245-
// for details. Short version is that we must sometimes detect
246-
// relationships between specific region variables and regions
247-
// bound in a closure signature, and that detection gets thrown
248-
// off when we substitute fresh region variables here to enable
249-
// subtyping.
250243
}
251244

252245
/// Compute the new expected type and default binding mode from the old ones

Diff for: compiler/rustc_infer/src/infer/canonical/query_response.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -640,11 +640,9 @@ pub fn make_query_region_constraints<'tcx>(
640640
outlives_obligations: impl Iterator<Item = (Ty<'tcx>, ty::Region<'tcx>, ConstraintCategory<'tcx>)>,
641641
region_constraints: &RegionConstraintData<'tcx>,
642642
) -> QueryRegionConstraints<'tcx> {
643-
let RegionConstraintData { constraints, verifys, givens, member_constraints } =
644-
region_constraints;
643+
let RegionConstraintData { constraints, verifys, member_constraints } = region_constraints;
645644

646645
assert!(verifys.is_empty());
647-
assert!(givens.is_empty());
648646

649647
debug!(?constraints);
650648

Diff for: compiler/rustc_infer/src/infer/lexical_region_resolve/mod.rs

+1-46
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use rustc_data_structures::graph::implementation::{
1313
Direction, Graph, NodeIndex, INCOMING, OUTGOING,
1414
};
1515
use rustc_data_structures::intern::Interned;
16-
use rustc_index::vec::{Idx, IndexVec};
16+
use rustc_index::vec::IndexVec;
1717
use rustc_middle::ty::fold::TypeFoldable;
1818
use rustc_middle::ty::PlaceholderRegion;
1919
use rustc_middle::ty::{self, Ty, TyCtxt};
@@ -132,7 +132,6 @@ impl<'cx, 'tcx> LexicalResolver<'cx, 'tcx> {
132132
}
133133

134134
let graph = self.construct_graph();
135-
self.expand_givens(&graph);
136135
self.expansion(&mut var_data);
137136
self.collect_errors(&mut var_data, errors);
138137
self.collect_var_errors(&var_data, &graph, errors);
@@ -164,38 +163,6 @@ impl<'cx, 'tcx> LexicalResolver<'cx, 'tcx> {
164163
}
165164
}
166165

167-
fn expand_givens(&mut self, graph: &RegionGraph<'_>) {
168-
// Givens are a kind of horrible hack to account for
169-
// constraints like 'c <= '0 that are known to hold due to
170-
// closure signatures (see the comment above on the `givens`
171-
// field). They should go away. But until they do, the role
172-
// of this fn is to account for the transitive nature:
173-
//
174-
// Given 'c <= '0
175-
// and '0 <= '1
176-
// then 'c <= '1
177-
178-
let seeds: Vec<_> = self.data.givens.iter().cloned().collect();
179-
for (r, vid) in seeds {
180-
// While all things transitively reachable in the graph
181-
// from the variable (`'0` in the example above).
182-
let seed_index = NodeIndex(vid.index() as usize);
183-
for succ_index in graph.depth_traverse(seed_index, OUTGOING) {
184-
let succ_index = succ_index.0;
185-
186-
// The first N nodes correspond to the region
187-
// variables. Other nodes correspond to constant
188-
// regions.
189-
if succ_index < self.num_vars() {
190-
let succ_vid = RegionVid::new(succ_index);
191-
192-
// Add `'c <= '1`.
193-
self.data.givens.insert((r, succ_vid));
194-
}
195-
}
196-
}
197-
}
198-
199166
/// Gets the LUb of a given region and the empty region
200167
fn lub_empty(&self, a_region: Region<'tcx>) -> Result<Region<'tcx>, PlaceholderRegion> {
201168
match *a_region {
@@ -362,18 +329,6 @@ impl<'cx, 'tcx> LexicalResolver<'cx, 'tcx> {
362329
) -> bool {
363330
debug!("expand_node({:?}, {:?} == {:?})", a_region, b_vid, b_data);
364331

365-
match *a_region {
366-
// Check if this relationship is implied by a given.
367-
ty::ReEarlyBound(_) | ty::ReFree(_) => {
368-
if self.data.givens.contains(&(a_region, b_vid)) {
369-
debug!("given");
370-
return false;
371-
}
372-
}
373-
374-
_ => {}
375-
}
376-
377332
match *b_data {
378333
VarValue::Empty(empty_ui) => {
379334
let lub = match self.lub_empty(a_region) {

Diff for: compiler/rustc_infer/src/infer/mod.rs

-4
Original file line numberDiff line numberDiff line change
@@ -869,10 +869,6 @@ impl<'tcx> InferCtxt<'tcx> {
869869
self.inner.borrow().undo_log.opaque_types_in_snapshot(&snapshot.undo_snapshot)
870870
}
871871

872-
pub fn add_given(&self, sub: ty::Region<'tcx>, sup: ty::RegionVid) {
873-
self.inner.borrow_mut().unwrap_region_constraints().add_given(sub, sup);
874-
}
875-
876872
pub fn can_sub<T>(&self, param_env: ty::ParamEnv<'tcx>, a: T, b: T) -> bool
877873
where
878874
T: at::ToTrace<'tcx>,

Diff for: compiler/rustc_infer/src/infer/outlives/env.rs

+16-32
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
use crate::infer::free_regions::FreeRegionMap;
2-
use crate::infer::{GenericKind, InferCtxt};
2+
use crate::infer::GenericKind;
33
use crate::traits::query::OutlivesBound;
44
use rustc_data_structures::fx::FxIndexSet;
55
use rustc_data_structures::transitive_relation::TransitiveRelationBuilder;
6-
use rustc_middle::ty::{self, ReEarlyBound, ReFree, ReVar, Region};
6+
use rustc_middle::ty::{self, Region};
77

88
use super::explicit_outlives_bounds;
99

@@ -75,7 +75,7 @@ impl<'tcx> OutlivesEnvironment<'tcx> {
7575
region_bound_pairs: Default::default(),
7676
};
7777

78-
builder.add_outlives_bounds(None, explicit_outlives_bounds(param_env));
78+
builder.add_outlives_bounds(explicit_outlives_bounds(param_env));
7979

8080
builder
8181
}
@@ -89,11 +89,10 @@ impl<'tcx> OutlivesEnvironment<'tcx> {
8989
/// Create a new `OutlivesEnvironment` with extra outlives bounds.
9090
pub fn with_bounds(
9191
param_env: ty::ParamEnv<'tcx>,
92-
infcx: Option<&InferCtxt<'tcx>>,
9392
extra_bounds: impl IntoIterator<Item = OutlivesBound<'tcx>>,
9493
) -> Self {
9594
let mut builder = Self::builder(param_env);
96-
builder.add_outlives_bounds(infcx, extra_bounds);
95+
builder.add_outlives_bounds(extra_bounds);
9796
builder.build()
9897
}
9998

@@ -120,12 +119,7 @@ impl<'tcx> OutlivesEnvironmentBuilder<'tcx> {
120119
}
121120

122121
/// Processes outlives bounds that are known to hold, whether from implied or other sources.
123-
///
124-
/// The `infcx` parameter is optional; if the implied bounds may
125-
/// contain inference variables, it must be supplied, in which
126-
/// case we will register "givens" on the inference context. (See
127-
/// `RegionConstraintData`.)
128-
fn add_outlives_bounds<I>(&mut self, infcx: Option<&InferCtxt<'tcx>>, outlives_bounds: I)
122+
fn add_outlives_bounds<I>(&mut self, outlives_bounds: I)
129123
where
130124
I: IntoIterator<Item = OutlivesBound<'tcx>>,
131125
{
@@ -142,27 +136,17 @@ impl<'tcx> OutlivesEnvironmentBuilder<'tcx> {
142136
self.region_bound_pairs
143137
.insert(ty::OutlivesPredicate(GenericKind::Alias(alias_b), r_a));
144138
}
145-
OutlivesBound::RegionSubRegion(r_a, r_b) => {
146-
if let (ReEarlyBound(_) | ReFree(_), ReVar(vid_b)) = (r_a.kind(), r_b.kind()) {
147-
infcx
148-
.expect("no infcx provided but region vars found")
149-
.add_given(r_a, vid_b);
150-
} else {
151-
// In principle, we could record (and take
152-
// advantage of) every relationship here, but
153-
// we are also free not to -- it simply means
154-
// strictly less that we can successfully type
155-
// check. Right now we only look for things
156-
// relationships between free regions. (It may
157-
// also be that we should revise our inference
158-
// system to be more general and to make use
159-
// of *every* relationship that arises here,
160-
// but presently we do not.)
161-
if r_a.is_free_or_static() && r_b.is_free() {
162-
self.region_relation.add(r_a, r_b)
163-
}
164-
}
165-
}
139+
OutlivesBound::RegionSubRegion(r_a, r_b) => match (*r_a, *r_b) {
140+
(
141+
ty::ReStatic | ty::ReEarlyBound(_) | ty::ReFree(_),
142+
ty::ReStatic | ty::ReEarlyBound(_) | ty::ReFree(_),
143+
) => self.region_relation.add(r_a, r_b),
144+
(ty::ReError(_), _) | (_, ty::ReError(_)) => {}
145+
// FIXME(#109628): We shouldn't have existential variables in implied bounds.
146+
// Panic here once the linked issue is resolved!
147+
(ty::ReVar(_), _) | (_, ty::ReVar(_)) => {}
148+
_ => bug!("add_outlives_bounds: unexpected regions: ({r_a:?}, {r_b:?})"),
149+
},
166150
}
167151
}
168152
}

Diff for: compiler/rustc_infer/src/infer/region_constraints/leak_check.rs

-3
Original file line numberDiff line numberDiff line change
@@ -424,9 +424,6 @@ impl<'tcx> MiniGraph<'tcx> {
424424
&AddConstraint(Constraint::RegSubReg(a, b)) => {
425425
each_edge(a, b);
426426
}
427-
&AddGiven(a, b) => {
428-
each_edge(a, tcx.mk_re_var(b));
429-
}
430427
&AddVerify(i) => span_bug!(
431428
verifys[i].origin.span(),
432429
"we never add verifications while doing higher-ranked things",

Diff for: compiler/rustc_infer/src/infer/region_constraints/mod.rs

+3-41
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use super::{
77
InferCtxtUndoLogs, MiscVariable, RegionVariableOrigin, Rollback, Snapshot, SubregionOrigin,
88
};
99

10-
use rustc_data_structures::fx::{FxHashMap, FxIndexSet};
10+
use rustc_data_structures::fx::FxHashMap;
1111
use rustc_data_structures::intern::Interned;
1212
use rustc_data_structures::sync::Lrc;
1313
use rustc_data_structures::undo_log::UndoLogs;
@@ -104,26 +104,6 @@ pub struct RegionConstraintData<'tcx> {
104104
/// An example is a `A <= B` where neither `A` nor `B` are
105105
/// inference variables.
106106
pub verifys: Vec<Verify<'tcx>>,
107-
108-
/// A "given" is a relationship that is known to hold. In
109-
/// particular, we often know from closure fn signatures that a
110-
/// particular free region must be a subregion of a region
111-
/// variable:
112-
///
113-
/// foo.iter().filter(<'a> |x: &'a &'b T| ...)
114-
///
115-
/// In situations like this, `'b` is in fact a region variable
116-
/// introduced by the call to `iter()`, and `'a` is a bound region
117-
/// on the closure (as indicated by the `<'a>` prefix). If we are
118-
/// naive, we wind up inferring that `'b` must be `'static`,
119-
/// because we require that it be greater than `'a` and we do not
120-
/// know what `'a` is precisely.
121-
///
122-
/// This hashmap is used to avoid that naive scenario. Basically
123-
/// we record the fact that `'a <= 'b` is implied by the fn
124-
/// signature, and then ignore the constraint when solving
125-
/// equations. This is a bit of a hack but seems to work.
126-
pub givens: FxIndexSet<(Region<'tcx>, ty::RegionVid)>,
127107
}
128108

129109
/// Represents a constraint that influences the inference process.
@@ -297,9 +277,6 @@ pub(crate) enum UndoLog<'tcx> {
297277
/// We added the given `verify`.
298278
AddVerify(usize),
299279

300-
/// We added the given `given`.
301-
AddGiven(Region<'tcx>, ty::RegionVid),
302-
303280
/// We added a GLB/LUB "combination variable".
304281
AddCombination(CombineMapType, TwoRegions<'tcx>),
305282
}
@@ -348,9 +325,6 @@ impl<'tcx> RegionConstraintStorage<'tcx> {
348325
self.data.verifys.pop();
349326
assert_eq!(self.data.verifys.len(), index);
350327
}
351-
AddGiven(sub, sup) => {
352-
self.data.givens.remove(&(sub, sup));
353-
}
354328
AddCombination(Glb, ref regions) => {
355329
self.glbs.remove(regions);
356330
}
@@ -492,15 +466,6 @@ impl<'tcx> RegionConstraintCollector<'_, 'tcx> {
492466
self.undo_log.push(AddVerify(index));
493467
}
494468

495-
pub(super) fn add_given(&mut self, sub: Region<'tcx>, sup: ty::RegionVid) {
496-
// cannot add givens once regions are resolved
497-
if self.data.givens.insert((sub, sup)) {
498-
debug!("add_given({:?} <= {:?})", sub, sup);
499-
500-
self.undo_log.push(AddGiven(sub, sup));
501-
}
502-
}
503-
504469
pub(super) fn make_eqregion(
505470
&mut self,
506471
origin: SubregionOrigin<'tcx>,
@@ -804,11 +769,8 @@ impl<'tcx> RegionConstraintData<'tcx> {
804769
/// Returns `true` if this region constraint data contains no constraints, and `false`
805770
/// otherwise.
806771
pub fn is_empty(&self) -> bool {
807-
let RegionConstraintData { constraints, member_constraints, verifys, givens } = self;
808-
constraints.is_empty()
809-
&& member_constraints.is_empty()
810-
&& verifys.is_empty()
811-
&& givens.is_empty()
772+
let RegionConstraintData { constraints, member_constraints, verifys } = self;
773+
constraints.is_empty() && member_constraints.is_empty() && verifys.is_empty()
812774
}
813775
}
814776

Diff for: compiler/rustc_trait_selection/src/traits/coherence.rs

-1
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,6 @@ fn resolve_negative_obligation<'tcx>(
402402
let wf_tys = ocx.assumed_wf_types(param_env, DUMMY_SP, body_def_id);
403403
let outlives_env = OutlivesEnvironment::with_bounds(
404404
param_env,
405-
Some(&infcx),
406405
infcx.implied_bounds_tys(param_env, body_def_id, wf_tys),
407406
);
408407

Diff for: compiler/rustc_trait_selection/src/traits/misc.rs

-1
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,6 @@ pub fn type_allowed_to_implement_copy<'tcx>(
111111
// Check regions assuming the self type of the impl is WF
112112
let outlives_env = OutlivesEnvironment::with_bounds(
113113
param_env,
114-
Some(&infcx),
115114
infcx.implied_bounds_tys(
116115
param_env,
117116
parent_cause.body_id,

0 commit comments

Comments
 (0)