Skip to content

Commit 6bd726b

Browse files
Deeply normalize when processing registered region obligations
1 parent e0f1903 commit 6bd726b

File tree

17 files changed

+96
-21
lines changed

17 files changed

+96
-21
lines changed

compiler/rustc_hir_analysis/src/check/compare_impl_item.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ use rustc_middle::ty::{
2020
};
2121
use rustc_middle::ty::{GenericParamDefKind, TyCtxt};
2222
use rustc_span::{Span, DUMMY_SP};
23+
use rustc_trait_selection::regions::InferCtxtRegionExt;
2324
use rustc_trait_selection::traits::error_reporting::TypeErrCtxtExt;
2425
use rustc_trait_selection::traits::outlives_bounds::InferCtxtExt as _;
2526
use rustc_trait_selection::traits::{
@@ -380,7 +381,7 @@ fn compare_method_predicate_entailment<'tcx>(
380381
param_env,
381382
infcx.implied_bounds_tys(param_env, impl_m_def_id, wf_tys),
382383
);
383-
let errors = infcx.resolve_regions(&outlives_env);
384+
let errors = infcx.resolve_regions_normalizing_outlives_obligations(&outlives_env);
384385
if !errors.is_empty() {
385386
return Err(infcx
386387
.tainted_by_errors()

compiler/rustc_hir_analysis/src/check/compare_impl_item/refine.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use rustc_middle::ty::{
88
self, Ty, TyCtxt, TypeFoldable, TypeFolder, TypeSuperVisitable, TypeVisitable, TypeVisitor,
99
};
1010
use rustc_span::{Span, DUMMY_SP};
11+
use rustc_trait_selection::regions::InferCtxtRegionExt;
1112
use rustc_trait_selection::traits::{
1213
elaborate, normalize_param_env_or_error, outlives_bounds::InferCtxtExt, ObligationCtxt,
1314
};
@@ -163,7 +164,7 @@ pub(super) fn check_refining_return_position_impl_trait_in_trait<'tcx>(
163164
param_env,
164165
infcx.implied_bounds_tys(param_env, impl_m.def_id.expect_local(), implied_wf_types),
165166
);
166-
let errors = infcx.resolve_regions(&outlives_env);
167+
let errors = infcx.resolve_regions_normalizing_outlives_obligations(&outlives_env);
167168
if !errors.is_empty() {
168169
tcx.sess.span_delayed_bug(
169170
DUMMY_SP,

compiler/rustc_hir_analysis/src/check/dropck.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use rustc_infer::infer::{RegionResolutionError, TyCtxtInferExt};
88
use rustc_middle::ty::util::CheckRegions;
99
use rustc_middle::ty::GenericArgsRef;
1010
use rustc_middle::ty::{self, TyCtxt};
11+
use rustc_trait_selection::regions::InferCtxtRegionExt;
1112
use rustc_trait_selection::traits::{self, ObligationCtxt};
1213

1314
use crate::errors;
@@ -169,7 +170,9 @@ fn ensure_drop_predicates_are_implied_by_item_defn<'tcx>(
169170
return Err(guar.unwrap());
170171
}
171172

172-
let errors = ocx.infcx.resolve_regions(&OutlivesEnvironment::new(param_env));
173+
let errors = ocx
174+
.infcx
175+
.resolve_regions_normalizing_outlives_obligations(&OutlivesEnvironment::new(param_env));
173176
if !errors.is_empty() {
174177
let mut guar = None;
175178
for error in errors {
@@ -184,6 +187,7 @@ fn ensure_drop_predicates_are_implied_by_item_defn<'tcx>(
184187
RegionResolutionError::UpperBoundUniverseConflict(a, _, _, _, b) => {
185188
format!("{b}: {a}", a = ty::Region::new_var(tcx, a))
186189
}
190+
RegionResolutionError::CannotNormalize(..) => todo!(),
187191
};
188192
guar = Some(
189193
struct_span_err!(

compiler/rustc_hir_analysis/src/check/wfcheck.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ use rustc_session::parse::feature_err;
2121
use rustc_span::symbol::{sym, Ident, Symbol};
2222
use rustc_span::{Span, DUMMY_SP};
2323
use rustc_target::spec::abi::Abi;
24+
use rustc_trait_selection::regions::InferCtxtRegionExt;
2425
use rustc_trait_selection::traits::error_reporting::TypeErrCtxtExt;
2526
use rustc_trait_selection::traits::misc::{
2627
type_allowed_to_implement_const_param_ty, ConstParamTyImplementationError,
@@ -717,7 +718,7 @@ fn test_region_obligations<'tcx>(
717718
infcx.implied_bounds_tys(param_env, id, wf_tys.clone()),
718719
);
719720

720-
let errors = infcx.resolve_regions(&outlives_environment);
721+
let errors = infcx.resolve_regions_normalizing_outlives_obligations(&outlives_environment);
721722
debug!(?errors, "errors");
722723

723724
// If we were able to prove that the type outlives the region without

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -511,6 +511,13 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
511511

512512
self.report_placeholder_failure(sup_origin, sub_r, sup_r).emit();
513513
}
514+
515+
RegionResolutionError::CannotNormalize(ty, origin) => {
516+
self.tcx
517+
.sess
518+
.struct_span_err(origin.span(), format!("cannot normalize `{ty}`"))
519+
.emit();
520+
}
514521
}
515522
}
516523
}
@@ -552,7 +559,8 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
552559
RegionResolutionError::GenericBoundFailure(..) => true,
553560
RegionResolutionError::ConcreteFailure(..)
554561
| RegionResolutionError::SubSupConflict(..)
555-
| RegionResolutionError::UpperBoundUniverseConflict(..) => false,
562+
| RegionResolutionError::UpperBoundUniverseConflict(..)
563+
| RegionResolutionError::CannotNormalize(..) => false,
556564
};
557565

558566
let mut errors = if errors.iter().all(|e| is_bound_failure(e)) {
@@ -567,6 +575,7 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
567575
RegionResolutionError::GenericBoundFailure(ref sro, _, _) => sro.span(),
568576
RegionResolutionError::SubSupConflict(_, ref rvo, _, _, _, _, _) => rvo.span(),
569577
RegionResolutionError::UpperBoundUniverseConflict(_, ref rvo, _, _, _) => rvo.span(),
578+
RegionResolutionError::CannotNormalize(_, ref sro) => sro.span(),
570579
});
571580
errors
572581
}

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,8 @@ pub enum RegionResolutionError<'tcx> {
9898
SubregionOrigin<'tcx>, // cause of the constraint
9999
Region<'tcx>, // the placeholder `'b`
100100
),
101+
102+
CannotNormalize(Ty<'tcx>, SubregionOrigin<'tcx>),
101103
}
102104

103105
impl<'tcx> RegionResolutionError<'tcx> {
@@ -106,7 +108,8 @@ impl<'tcx> RegionResolutionError<'tcx> {
106108
RegionResolutionError::ConcreteFailure(origin, _, _)
107109
| RegionResolutionError::GenericBoundFailure(origin, _, _)
108110
| RegionResolutionError::SubSupConflict(_, _, origin, _, _, _, _)
109-
| RegionResolutionError::UpperBoundUniverseConflict(_, _, _, origin, _) => origin,
111+
| RegionResolutionError::UpperBoundUniverseConflict(_, _, _, origin, _)
112+
| RegionResolutionError::CannotNormalize(_, origin) => origin,
110113
}
111114
}
112115
}

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

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use super::{InferCtxt, RegionResolutionError};
55
use crate::infer::free_regions::RegionRelations;
66
use crate::infer::lexical_region_resolve;
77
use rustc_middle::traits::query::OutlivesBound;
8-
use rustc_middle::ty;
8+
use rustc_middle::ty::{self, Ty};
99

1010
pub mod components;
1111
pub mod env;
@@ -41,12 +41,22 @@ impl<'tcx> InferCtxt<'tcx> {
4141
/// result. After this, no more unification operations should be
4242
/// done -- or the compiler will panic -- but it is legal to use
4343
/// `resolve_vars_if_possible` as well as `fully_resolve`.
44+
///
45+
/// If you are in a crate that has access to `rustc_trai_selection`,
46+
/// then it's probably better to use `resolve_regions_normalizing_outlives_obligations`,
47+
/// which knows how to normalize registered region obligations.
4448
#[must_use]
4549
pub fn resolve_regions(
4650
&self,
4751
outlives_env: &OutlivesEnvironment<'tcx>,
52+
deeply_normalize_ty: impl Fn(Ty<'tcx>) -> Result<Ty<'tcx>, Ty<'tcx>>,
4853
) -> Vec<RegionResolutionError<'tcx>> {
49-
self.process_registered_region_obligations(outlives_env);
54+
match self.process_registered_region_obligations(outlives_env, deeply_normalize_ty) {
55+
Ok(()) => {}
56+
Err((ty, origin)) => {
57+
return vec![RegionResolutionError::CannotNormalize(ty, origin)];
58+
}
59+
};
5060

5161
let (var_infos, data) = {
5262
let mut inner = self.inner.borrow_mut();

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

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -123,15 +123,19 @@ impl<'tcx> InferCtxt<'tcx> {
123123
/// flow of the inferencer. The key point is that it is
124124
/// invoked after all type-inference variables have been bound --
125125
/// right before lexical region resolution.
126-
#[instrument(level = "debug", skip(self, outlives_env))]
127-
pub fn process_registered_region_obligations(&self, outlives_env: &OutlivesEnvironment<'tcx>) {
126+
#[instrument(level = "debug", skip(self, outlives_env, deeply_normalize_ty))]
127+
pub fn process_registered_region_obligations<E>(
128+
&self,
129+
outlives_env: &OutlivesEnvironment<'tcx>,
130+
mut deeply_normalize_ty: impl FnMut(Ty<'tcx>) -> Result<Ty<'tcx>, E>,
131+
) -> Result<(), (E, SubregionOrigin<'tcx>)> {
128132
assert!(!self.in_snapshot(), "cannot process registered region obligations in a snapshot");
129133

130134
let my_region_obligations = self.take_registered_region_obligations();
131135

132136
for RegionObligation { sup_type, sub_region, origin } in my_region_obligations {
137+
let sup_type = deeply_normalize_ty(sup_type).map_err(|e| (e, origin.clone()))?;
133138
debug!(?sup_type, ?sub_region, ?origin);
134-
let sup_type = self.resolve_vars_if_possible(sup_type);
135139

136140
let outlives = &mut TypeOutlives::new(
137141
self,
@@ -143,6 +147,8 @@ impl<'tcx> InferCtxt<'tcx> {
143147
let category = origin.to_constraint_category();
144148
outlives.type_must_outlive(origin, sup_type, sub_region, category);
145149
}
150+
151+
Ok(())
146152
}
147153
}
148154

compiler/rustc_trait_selection/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ extern crate smallvec;
3939

4040
pub mod errors;
4141
pub mod infer;
42+
pub mod regions;
4243
pub mod solve;
4344
pub mod traits;
4445

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
use rustc_infer::infer::outlives::env::OutlivesEnvironment;
2+
use rustc_infer::infer::{InferCtxt, RegionResolutionError};
3+
use rustc_middle::traits::ObligationCause;
4+
5+
pub trait InferCtxtRegionExt<'tcx> {
6+
fn resolve_regions_normalizing_outlives_obligations(
7+
&self,
8+
outlives_env: &OutlivesEnvironment<'tcx>,
9+
) -> Vec<RegionResolutionError<'tcx>>;
10+
}
11+
12+
impl<'tcx> InferCtxtRegionExt<'tcx> for InferCtxt<'tcx> {
13+
fn resolve_regions_normalizing_outlives_obligations(
14+
&self,
15+
outlives_env: &OutlivesEnvironment<'tcx>,
16+
) -> Vec<RegionResolutionError<'tcx>> {
17+
self.resolve_regions(outlives_env, |ty| {
18+
let ty = self.resolve_vars_if_possible(ty);
19+
20+
if self.next_trait_solver() {
21+
crate::solve::deeply_normalize(
22+
self.at(&ObligationCause::dummy(), outlives_env.param_env),
23+
ty,
24+
)
25+
.map_err(|_| ty)
26+
} else {
27+
Ok(ty)
28+
}
29+
})
30+
}
31+
}

0 commit comments

Comments
 (0)