Skip to content

Commit ec724ac

Browse files
committed
Auto merge of #89229 - oli-obk:i_love_inferctxt, r=jackh726
Remove redundant member-constraint check impl trait will, for each lifetime in the hidden type, register a "member constraint" that says the lifetime must be equal or outlive one of the lifetimes of the impl trait. These member constraints will be solved by borrowck But, as you can see in the big red block of removed code, there was an ad-hoc check for member constraints happening at the site where they get registered. This check had some minor effects on diagnostics, but will fall down on its feet with my big type alias impl trait refactor. So we removed it and I pulled the removal out into a (hopefully) reviewable PR that works on master directly.
2 parents bd41e09 + 4413f8c commit ec724ac

File tree

38 files changed

+271
-702
lines changed

38 files changed

+271
-702
lines changed

compiler/rustc_borrowck/src/region_infer/mod.rs

+10
Original file line numberDiff line numberDiff line change
@@ -689,6 +689,16 @@ impl<'tcx> RegionInferenceContext<'tcx> {
689689
// them down.
690690
let mut choice_regions: Vec<ty::RegionVid> = choice_regions.to_vec();
691691

692+
// Convert to the SCC representative: sometimes we have inference
693+
// variables in the member constraint that wind up equated with
694+
// universal regions. The scc representative is the minimal numbered
695+
// one from the corresponding scc so it will be the universal region
696+
// if one exists.
697+
for c_r in &mut choice_regions {
698+
let scc = self.constraint_sccs.scc(*c_r);
699+
*c_r = self.scc_representatives[scc];
700+
}
701+
692702
// The 'member region' in a member constraint is part of the
693703
// hidden type, which must be in the root universe. Therefore,
694704
// it cannot have any placeholders in its value.

compiler/rustc_borrowck/src/type_check/free_region_relations.rs

+1-21
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,18 @@
11
use rustc_data_structures::frozen::Frozen;
22
use rustc_data_structures::transitive_relation::TransitiveRelation;
33
use rustc_infer::infer::canonical::QueryRegionConstraints;
4-
use rustc_infer::infer::free_regions::FreeRegionRelations;
54
use rustc_infer::infer::outlives;
65
use rustc_infer::infer::region_constraints::GenericKind;
76
use rustc_infer::infer::InferCtxt;
87
use rustc_middle::mir::ConstraintCategory;
98
use rustc_middle::traits::query::OutlivesBound;
10-
use rustc_middle::ty::{self, RegionVid, Ty, TyCtxt};
9+
use rustc_middle::ty::{self, RegionVid, Ty};
1110
use rustc_span::DUMMY_SP;
1211
use rustc_trait_selection::traits::query::type_op::{self, TypeOp};
1312
use std::rc::Rc;
1413
use type_op::TypeOpOutput;
1514

1615
use crate::{
17-
nll::ToRegionVid,
1816
type_check::constraint_conversion,
1917
type_check::{Locations, MirTypeckRegionConstraints},
2018
universal_regions::UniversalRegions,
@@ -383,21 +381,3 @@ impl UniversalRegionRelationsBuilder<'cx, 'tcx> {
383381
}
384382
}
385383
}
386-
387-
/// This trait is used by the `impl-trait` constraint code to abstract
388-
/// over the `FreeRegionMap` from lexical regions and
389-
/// `UniversalRegions` (from NLL)`.
390-
impl<'tcx> FreeRegionRelations<'tcx> for UniversalRegionRelations<'tcx> {
391-
fn sub_free_regions(
392-
&self,
393-
_tcx: TyCtxt<'tcx>,
394-
shorter: ty::Region<'tcx>,
395-
longer: ty::Region<'tcx>,
396-
) -> bool {
397-
let shorter = shorter.to_region_vid();
398-
assert!(self.universal_regions.is_universal_region(shorter));
399-
let longer = longer.to_region_vid();
400-
assert!(self.universal_regions.is_universal_region(longer));
401-
self.outlives(longer, shorter)
402-
}
403-
}

compiler/rustc_borrowck/src/type_check/mod.rs

+3-24
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ use rustc_span::def_id::CRATE_DEF_ID;
3636
use rustc_span::{Span, DUMMY_SP};
3737
use rustc_target::abi::VariantIdx;
3838
use rustc_trait_selection::infer::InferCtxtExt as _;
39-
use rustc_trait_selection::opaque_types::{GenerateMemberConstraints, InferCtxtExt};
39+
use rustc_trait_selection::opaque_types::InferCtxtExt;
4040
use rustc_trait_selection::traits::error_reporting::InferCtxtExt as _;
4141
use rustc_trait_selection::traits::query::type_op;
4242
use rustc_trait_selection::traits::query::type_op::custom::CustomTypeOp;
@@ -185,7 +185,6 @@ pub(crate) fn type_check<'mir, 'tcx>(
185185
&region_bound_pairs,
186186
implicit_region_bound,
187187
&mut borrowck_context,
188-
&universal_region_relations,
189188
|mut cx| {
190189
cx.equate_inputs_and_outputs(&body, universal_regions, &normalized_inputs_and_output);
191190
liveness::generate(&mut cx, body, elements, flow_inits, move_data, location_table);
@@ -253,15 +252,7 @@ pub(crate) fn type_check<'mir, 'tcx>(
253252
}
254253

255254
#[instrument(
256-
skip(
257-
infcx,
258-
body,
259-
promoted,
260-
region_bound_pairs,
261-
borrowck_context,
262-
universal_region_relations,
263-
extra
264-
),
255+
skip(infcx, body, promoted, region_bound_pairs, borrowck_context, extra),
265256
level = "debug"
266257
)]
267258
fn type_check_internal<'a, 'tcx, R>(
@@ -272,7 +263,6 @@ fn type_check_internal<'a, 'tcx, R>(
272263
region_bound_pairs: &'a RegionBoundPairs<'tcx>,
273264
implicit_region_bound: ty::Region<'tcx>,
274265
borrowck_context: &'a mut BorrowCheckContext<'a, 'tcx>,
275-
universal_region_relations: &'a UniversalRegionRelations<'tcx>,
276266
extra: impl FnOnce(TypeChecker<'a, 'tcx>) -> R,
277267
) -> R {
278268
let mut checker = TypeChecker::new(
@@ -282,7 +272,6 @@ fn type_check_internal<'a, 'tcx, R>(
282272
region_bound_pairs,
283273
implicit_region_bound,
284274
borrowck_context,
285-
universal_region_relations,
286275
);
287276
let errors_reported = {
288277
let mut verifier = TypeVerifier::new(&mut checker, body, promoted);
@@ -901,7 +890,6 @@ struct TypeChecker<'a, 'tcx> {
901890
implicit_region_bound: ty::Region<'tcx>,
902891
reported_errors: FxHashSet<(Ty<'tcx>, Span)>,
903892
borrowck_context: &'a mut BorrowCheckContext<'a, 'tcx>,
904-
universal_region_relations: &'a UniversalRegionRelations<'tcx>,
905893
}
906894

907895
struct BorrowCheckContext<'a, 'tcx> {
@@ -1050,7 +1038,6 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
10501038
region_bound_pairs: &'a RegionBoundPairs<'tcx>,
10511039
implicit_region_bound: ty::Region<'tcx>,
10521040
borrowck_context: &'a mut BorrowCheckContext<'a, 'tcx>,
1053-
universal_region_relations: &'a UniversalRegionRelations<'tcx>,
10541041
) -> Self {
10551042
let mut checker = Self {
10561043
infcx,
@@ -1062,7 +1049,6 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
10621049
implicit_region_bound,
10631050
borrowck_context,
10641051
reported_errors: Default::default(),
1065-
universal_region_relations,
10661052
};
10671053
checker.check_user_type_annotations();
10681054
checker
@@ -1322,8 +1308,6 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
13221308
),
13231309
)?;
13241310

1325-
let universal_region_relations = self.universal_region_relations;
1326-
13271311
// Finally, if we instantiated the anon types successfully, we
13281312
// have to solve any bounds (e.g., `-> impl Iterator` needs to
13291313
// prove that `T: Iterator` where `T` is the type we
@@ -1335,12 +1319,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
13351319
ConstraintCategory::OpaqueType,
13361320
CustomTypeOp::new(
13371321
|infcx| {
1338-
infcx.constrain_opaque_type(
1339-
opaque_type_key,
1340-
&opaque_decl,
1341-
GenerateMemberConstraints::IfNoStaticBound,
1342-
universal_region_relations,
1343-
);
1322+
infcx.constrain_opaque_type(opaque_type_key, &opaque_decl);
13441323
Ok(InferOk { value: (), obligations: vec![] })
13451324
},
13461325
|| "opaque_type_map".to_string(),

compiler/rustc_error_codes/src/error_codes/E0482.md

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1+
#### Note: this error code is no longer emitted by the compiler.
2+
13
A lifetime of a returned value does not outlive the function call.
24

35
Erroneous code example:
46

5-
```compile_fail,E0482
7+
```compile_fail,E0700
68
fn prefix<'a>(
79
words: impl Iterator<Item = &'a str>
810
) -> impl Iterator<Item = String> { // error!
@@ -41,7 +43,7 @@ fn prefix(
4143

4244
A similar lifetime problem might arise when returning closures:
4345

44-
```compile_fail,E0482
46+
```compile_fail,E0700
4547
fn foo(
4648
x: &mut Vec<i32>
4749
) -> impl FnMut(&mut Vec<i32>) -> &[i32] { // error!

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

+1-18
Original file line numberDiff line numberDiff line change
@@ -386,21 +386,6 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
386386

387387
self.report_placeholder_failure(sup_origin, sub_r, sup_r).emit();
388388
}
389-
390-
RegionResolutionError::MemberConstraintFailure {
391-
hidden_ty,
392-
member_region,
393-
span,
394-
} => {
395-
let hidden_ty = self.resolve_vars_if_possible(hidden_ty);
396-
unexpected_hidden_region_diagnostic(
397-
self.tcx,
398-
span,
399-
hidden_ty,
400-
member_region,
401-
)
402-
.emit();
403-
}
404389
}
405390
}
406391
}
@@ -438,8 +423,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
438423
RegionResolutionError::GenericBoundFailure(..) => true,
439424
RegionResolutionError::ConcreteFailure(..)
440425
| RegionResolutionError::SubSupConflict(..)
441-
| RegionResolutionError::UpperBoundUniverseConflict(..)
442-
| RegionResolutionError::MemberConstraintFailure { .. } => false,
426+
| RegionResolutionError::UpperBoundUniverseConflict(..) => false,
443427
};
444428

445429
let mut errors = if errors.iter().all(|e| is_bound_failure(e)) {
@@ -454,7 +438,6 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
454438
RegionResolutionError::GenericBoundFailure(ref sro, _, _) => sro.span(),
455439
RegionResolutionError::SubSupConflict(_, ref rvo, _, _, _, _) => rvo.span(),
456440
RegionResolutionError::UpperBoundUniverseConflict(_, ref rvo, _, _, _) => rvo.span(),
457-
RegionResolutionError::MemberConstraintFailure { span, .. } => span,
458441
});
459442
errors
460443
}

compiler/rustc_infer/src/infer/error_reporting/note.rs

-20
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,6 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
5353
infer::RelateObjectBound(span) => {
5454
label_or_note(span, "...so that it can be closed over into an object");
5555
}
56-
infer::CallReturn(span) => {
57-
label_or_note(span, "...so that return value is valid for the call");
58-
}
5956
infer::DataBorrowed(ty, span) => {
6057
label_or_note(
6158
span,
@@ -281,23 +278,6 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
281278
);
282279
err
283280
}
284-
infer::CallReturn(span) => {
285-
let mut err = struct_span_err!(
286-
self.tcx.sess,
287-
span,
288-
E0482,
289-
"lifetime of return value does not outlive the function call"
290-
);
291-
note_and_explain_region(
292-
self.tcx,
293-
&mut err,
294-
"the return value is only valid for ",
295-
sup,
296-
"",
297-
None,
298-
);
299-
err
300-
}
301281
infer::DataBorrowed(ty, span) => {
302282
let mut err = struct_span_err!(
303283
self.tcx.sess,

compiler/rustc_infer/src/infer/free_regions.rs

-23
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,6 @@ impl<'tcx> FreeRegionMap<'tcx> {
6666
/// follows. If we know that `r_b: 'static`, then this function
6767
/// will return true, even though we don't know anything that
6868
/// directly relates `r_a` and `r_b`.
69-
///
70-
/// Also available through the `FreeRegionRelations` trait below.
7169
pub fn sub_free_regions(
7270
&self,
7371
tcx: TyCtxt<'tcx>,
@@ -131,27 +129,6 @@ impl<'tcx> FreeRegionMap<'tcx> {
131129
}
132130
}
133131

134-
/// The NLL region handling code represents free region relations in a
135-
/// slightly different way; this trait allows functions to be abstract
136-
/// over which version is in use.
137-
pub trait FreeRegionRelations<'tcx> {
138-
/// Tests whether `r_a <= r_b`. Both must be free regions or
139-
/// `'static`.
140-
fn sub_free_regions(
141-
&self,
142-
tcx: TyCtxt<'tcx>,
143-
shorter: ty::Region<'tcx>,
144-
longer: ty::Region<'tcx>,
145-
) -> bool;
146-
}
147-
148-
impl<'tcx> FreeRegionRelations<'tcx> for FreeRegionMap<'tcx> {
149-
fn sub_free_regions(&self, tcx: TyCtxt<'tcx>, r_a: Region<'tcx>, r_b: Region<'tcx>) -> bool {
150-
// invoke the "inherent method"
151-
self.sub_free_regions(tcx, r_a, r_b)
152-
}
153-
}
154-
155132
impl<'a, 'tcx> Lift<'tcx> for FreeRegionMap<'a> {
156133
type Lifted = FreeRegionMap<'tcx>;
157134
fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<FreeRegionMap<'tcx>> {

0 commit comments

Comments
 (0)