Skip to content

Commit 18e305d

Browse files
committed
Auto merge of rust-lang#109183 - lqd:revert-107376, r=compiler-errors
Revert rust-lang#107376 to fix potential `bincode` breakage and `rustc-perf` benchmark. rust-lang#107376 caused `rustc-perf`'s `webrender` benchmark to break, by regressing on the `bincode-1.3.3` crate. ~~This PR is a draft revert in case we can't land a fix soon enough, and we'd like to land the revert instead~~ (Though I myself think it'd be safer to do the revert, and run crater when relanding rust-lang#107376.) cc `@aliemjay`
2 parents c90eb48 + 5ad1083 commit 18e305d

File tree

14 files changed

+152
-87
lines changed

14 files changed

+152
-87
lines changed

compiler/rustc_hir_analysis/src/check/compare_impl_item.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,7 @@ fn compare_method_predicate_entailment<'tcx>(
330330
// lifetime parameters.
331331
let outlives_env = OutlivesEnvironment::with_bounds(
332332
param_env,
333+
Some(infcx),
333334
infcx.implied_bounds_tys(param_env, impl_m_def_id, wf_tys.clone()),
334335
);
335336
infcx.process_registered_region_obligations(
@@ -727,6 +728,7 @@ pub(super) fn collect_return_position_impl_trait_in_trait_tys<'tcx>(
727728
// lifetime parameters.
728729
let outlives_environment = OutlivesEnvironment::with_bounds(
729730
param_env,
731+
Some(infcx),
730732
infcx.implied_bounds_tys(param_env, impl_m_def_id, wf_tys),
731733
);
732734
infcx
@@ -2056,7 +2058,8 @@ pub(super) fn check_type_bounds<'tcx>(
20562058
// Finally, resolve all regions. This catches wily misuses of
20572059
// lifetime parameters.
20582060
let implied_bounds = infcx.implied_bounds_tys(param_env, impl_ty_def_id, assumed_wf_types);
2059-
let outlives_environment = OutlivesEnvironment::with_bounds(param_env, implied_bounds);
2061+
let outlives_environment =
2062+
OutlivesEnvironment::with_bounds(param_env, Some(&infcx), implied_bounds);
20602063

20612064
infcx.err_ctxt().check_region_obligations_and_report_errors(
20622065
impl_ty.def_id.expect_local(),

compiler/rustc_hir_analysis/src/check/wfcheck.rs

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

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

120121
let _ = infcx
121122
.err_ctxt()
@@ -675,6 +676,7 @@ fn resolve_regions_with_wf_tys<'tcx>(
675676
let infcx = tcx.infer_ctxt().build();
676677
let outlives_environment = OutlivesEnvironment::with_bounds(
677678
param_env,
679+
Some(&infcx),
678680
infcx.implied_bounds_tys(param_env, id, wf_tys.clone()),
679681
);
680682
let region_bound_pairs = outlives_environment.region_bound_pairs();

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, implied_bounds);
182+
let outlives_env = OutlivesEnvironment::with_bounds(param_env, Some(infcx), 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 {

compiler/rustc_hir_typeck/src/pat.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -238,8 +238,15 @@ 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. An outdated issue related to the old HIR borrowck. See the test
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
242244
// `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.
243250
}
244251

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

compiler/rustc_infer/src/infer/canonical/query_response.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -636,9 +636,11 @@ pub fn make_query_region_constraints<'tcx>(
636636
outlives_obligations: impl Iterator<Item = (Ty<'tcx>, ty::Region<'tcx>, ConstraintCategory<'tcx>)>,
637637
region_constraints: &RegionConstraintData<'tcx>,
638638
) -> QueryRegionConstraints<'tcx> {
639-
let RegionConstraintData { constraints, verifys, member_constraints } = region_constraints;
639+
let RegionConstraintData { constraints, verifys, givens, member_constraints } =
640+
region_constraints;
640641

641642
assert!(verifys.is_empty());
643+
assert!(givens.is_empty());
642644

643645
debug!(?constraints);
644646

compiler/rustc_infer/src/infer/lexical_region_resolve/mod.rs

+46-1
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::IndexVec;
16+
use rustc_index::vec::{Idx, IndexVec};
1717
use rustc_middle::ty::fold::TypeFoldable;
1818
use rustc_middle::ty::PlaceholderRegion;
1919
use rustc_middle::ty::{self, Ty, TyCtxt};
@@ -132,6 +132,7 @@ impl<'cx, 'tcx> LexicalResolver<'cx, 'tcx> {
132132
}
133133

134134
let graph = self.construct_graph();
135+
self.expand_givens(&graph);
135136
self.expansion(&mut var_data);
136137
self.collect_errors(&mut var_data, errors);
137138
self.collect_var_errors(&var_data, &graph, errors);
@@ -163,6 +164,38 @@ impl<'cx, 'tcx> LexicalResolver<'cx, 'tcx> {
163164
}
164165
}
165166

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+
166199
/// Gets the LUb of a given region and the empty region
167200
fn lub_empty(&self, a_region: Region<'tcx>) -> Result<Region<'tcx>, PlaceholderRegion> {
168201
match *a_region {
@@ -329,6 +362,18 @@ impl<'cx, 'tcx> LexicalResolver<'cx, 'tcx> {
329362
) -> bool {
330363
debug!("expand_node({:?}, {:?} == {:?})", a_region, b_vid, b_data);
331364

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+
332377
match *b_data {
333378
VarValue::Empty(empty_ui) => {
334379
let lub = match self.lub_empty(a_region) {

compiler/rustc_infer/src/infer/mod.rs

+4
Original file line numberDiff line numberDiff line change
@@ -855,6 +855,10 @@ impl<'tcx> InferCtxt<'tcx> {
855855
self.inner.borrow().undo_log.opaque_types_in_snapshot(&snapshot.undo_snapshot)
856856
}
857857

858+
pub fn add_given(&self, sub: ty::Region<'tcx>, sup: ty::RegionVid) {
859+
self.inner.borrow_mut().unwrap_region_constraints().add_given(sub, sup);
860+
}
861+
858862
pub fn can_sub<T>(&self, param_env: ty::ParamEnv<'tcx>, a: T, b: T) -> bool
859863
where
860864
T: at::ToTrace<'tcx>,

compiler/rustc_infer/src/infer/outlives/env.rs

+32-13
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
use crate::infer::free_regions::FreeRegionMap;
2-
use crate::infer::GenericKind;
2+
use crate::infer::{GenericKind, InferCtxt};
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, Region};
6+
use rustc_middle::ty::{self, ReEarlyBound, ReFree, ReVar, 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(explicit_outlives_bounds(param_env));
78+
builder.add_outlives_bounds(None, explicit_outlives_bounds(param_env));
7979

8080
builder
8181
}
@@ -89,10 +89,11 @@ 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>>,
9293
extra_bounds: impl IntoIterator<Item = OutlivesBound<'tcx>>,
9394
) -> Self {
9495
let mut builder = Self::builder(param_env);
95-
builder.add_outlives_bounds(extra_bounds);
96+
builder.add_outlives_bounds(infcx, extra_bounds);
9697
builder.build()
9798
}
9899

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

121122
/// Processes outlives bounds that are known to hold, whether from implied or other sources.
122-
fn add_outlives_bounds<I>(&mut self, outlives_bounds: I)
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)
123129
where
124130
I: IntoIterator<Item = OutlivesBound<'tcx>>,
125131
{
@@ -136,14 +142,27 @@ impl<'tcx> OutlivesEnvironmentBuilder<'tcx> {
136142
self.region_bound_pairs
137143
.insert(ty::OutlivesPredicate(GenericKind::Alias(alias_b), r_a));
138144
}
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-
_ => bug!("add_outlives_bounds: unexpected regions"),
146-
},
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+
}
147166
}
148167
}
149168
}

compiler/rustc_infer/src/infer/region_constraints/leak_check.rs

+3
Original file line numberDiff line numberDiff line change
@@ -424,6 +424,9 @@ 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+
}
427430
&AddVerify(i) => span_bug!(
428431
verifys[i].origin.span(),
429432
"we never add verifications while doing higher-ranked things",

compiler/rustc_infer/src/infer/region_constraints/mod.rs

+41-3
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;
10+
use rustc_data_structures::fx::{FxHashMap, FxIndexSet};
1111
use rustc_data_structures::intern::Interned;
1212
use rustc_data_structures::sync::Lrc;
1313
use rustc_data_structures::undo_log::UndoLogs;
@@ -104,6 +104,26 @@ 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)>,
107127
}
108128

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

300+
/// We added the given `given`.
301+
AddGiven(Region<'tcx>, ty::RegionVid),
302+
280303
/// We added a GLB/LUB "combination variable".
281304
AddCombination(CombineMapType, TwoRegions<'tcx>),
282305
}
@@ -325,6 +348,9 @@ impl<'tcx> RegionConstraintStorage<'tcx> {
325348
self.data.verifys.pop();
326349
assert_eq!(self.data.verifys.len(), index);
327350
}
351+
AddGiven(sub, sup) => {
352+
self.data.givens.remove(&(sub, sup));
353+
}
328354
AddCombination(Glb, ref regions) => {
329355
self.glbs.remove(regions);
330356
}
@@ -466,6 +492,15 @@ impl<'tcx> RegionConstraintCollector<'_, 'tcx> {
466492
self.undo_log.push(AddVerify(index));
467493
}
468494

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+
469504
pub(super) fn make_eqregion(
470505
&mut self,
471506
origin: SubregionOrigin<'tcx>,
@@ -769,8 +804,11 @@ impl<'tcx> RegionConstraintData<'tcx> {
769804
/// Returns `true` if this region constraint data contains no constraints, and `false`
770805
/// otherwise.
771806
pub fn is_empty(&self) -> bool {
772-
let RegionConstraintData { constraints, member_constraints, verifys } = self;
773-
constraints.is_empty() && member_constraints.is_empty() && verifys.is_empty()
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()
774812
}
775813
}
776814

compiler/rustc_trait_selection/src/traits/coherence.rs

+1
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,7 @@ fn resolve_negative_obligation<'tcx>(
388388
let wf_tys = ocx.assumed_wf_types(param_env, DUMMY_SP, body_def_id);
389389
let outlives_env = OutlivesEnvironment::with_bounds(
390390
param_env,
391+
Some(&infcx),
391392
infcx.implied_bounds_tys(param_env, body_def_id, wf_tys),
392393
);
393394

compiler/rustc_trait_selection/src/traits/misc.rs

+1
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ 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),
114115
infcx.implied_bounds_tys(
115116
param_env,
116117
parent_cause.body_id,

compiler/rustc_trait_selection/src/traits/outlives_bounds.rs

+5-7
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@ use crate::traits::query::type_op::{self, TypeOp, TypeOpOutput};
33
use crate::traits::query::NoSolution;
44
use crate::traits::ObligationCause;
55
use rustc_data_structures::fx::FxIndexSet;
6-
use rustc_infer::infer::resolve::OpportunisticRegionResolver;
7-
use rustc_middle::ty::{self, ParamEnv, Ty, TypeFolder, TypeVisitableExt};
6+
use rustc_middle::ty::{self, ParamEnv, Ty};
87
use rustc_span::def_id::LocalDefId;
98

109
pub use rustc_middle::traits::query::OutlivesBound;
@@ -53,10 +52,6 @@ impl<'a, 'tcx: 'a> InferCtxtExt<'a, 'tcx> for InferCtxt<'tcx> {
5352
body_id: LocalDefId,
5453
ty: Ty<'tcx>,
5554
) -> Vec<OutlivesBound<'tcx>> {
56-
let ty = self.resolve_vars_if_possible(ty);
57-
let ty = OpportunisticRegionResolver::new(self).fold_ty(ty);
58-
assert!(!ty.needs_infer());
59-
6055
let span = self.tcx.def_span(body_id);
6156
let result = param_env
6257
.and(type_op::implied_outlives_bounds::ImpliedOutlivesBounds { ty })
@@ -110,7 +105,10 @@ impl<'a, 'tcx: 'a> InferCtxtExt<'a, 'tcx> for InferCtxt<'tcx> {
110105
tys: FxIndexSet<Ty<'tcx>>,
111106
) -> Bounds<'a, 'tcx> {
112107
tys.into_iter()
113-
.map(move |ty| self.implied_outlives_bounds(param_env, body_id, ty))
108+
.map(move |ty| {
109+
let ty = self.resolve_vars_if_possible(ty);
110+
self.implied_outlives_bounds(param_env, body_id, ty)
111+
})
114112
.flatten()
115113
}
116114
}

0 commit comments

Comments
 (0)