Skip to content

Commit d8f43f7

Browse files
committed
merge Deeply Normalize and Normalize
1 parent f3cd788 commit d8f43f7

8 files changed

Lines changed: 43 additions & 149 deletions

File tree

compiler/rustc_borrowck/src/diagnostics/bound_region_errors.rs

Lines changed: 10 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@ use rustc_infer::infer::{
99
};
1010
use rustc_infer::traits::ObligationCause;
1111
use rustc_infer::traits::query::{
12-
CanonicalTypeOpAscribeUserTypeGoal, CanonicalTypeOpDeeplyNormalizeGoal,
13-
CanonicalTypeOpNormalizeGoal, CanonicalTypeOpProvePredicateGoal,
12+
CanonicalTypeOpAscribeUserTypeGoal, CanonicalTypeOpNormalizeGoal,
13+
CanonicalTypeOpProvePredicateGoal,
1414
};
1515
use rustc_middle::ty::error::TypeError;
1616
use rustc_middle::ty::{
17-
self, RePlaceholder, Region, RegionVid, Ty, TyCtxt, TypeFoldable, UniverseIndex, Unnormalized,
17+
self, RePlaceholder, Region, RegionVid, Ty, TyCtxt, TypeFoldable, UniverseIndex,
1818
};
1919
use rustc_span::Span;
2020
use rustc_trait_selection::error_reporting::InferCtxtErrorExt;
@@ -109,14 +109,6 @@ impl<'tcx, T: Copy + fmt::Display + TypeFoldable<TyCtxt<'tcx>> + 'tcx> ToUnivers
109109
}
110110
}
111111

112-
impl<'tcx, T: Copy + fmt::Display + TypeFoldable<TyCtxt<'tcx>> + 'tcx> ToUniverseInfo<'tcx>
113-
for CanonicalTypeOpDeeplyNormalizeGoal<'tcx, T>
114-
{
115-
fn to_universe_info(self, base_universe: ty::UniverseIndex) -> UniverseInfo<'tcx> {
116-
UniverseInfo::TypeOp(Rc::new(DeeplyNormalizeQuery { canonical_query: self, base_universe }))
117-
}
118-
}
119-
120112
impl<'tcx> ToUniverseInfo<'tcx> for CanonicalTypeOpAscribeUserTypeGoal<'tcx> {
121113
fn to_universe_info(self, base_universe: ty::UniverseIndex) -> UniverseInfo<'tcx> {
122114
UniverseInfo::TypeOp(Rc::new(AscribeUserTypeQuery { canonical_query: self, base_universe }))
@@ -241,59 +233,6 @@ struct NormalizeQuery<'tcx, T> {
241233
}
242234

243235
impl<'tcx, T> TypeOpInfo<'tcx> for NormalizeQuery<'tcx, T>
244-
where
245-
T: Copy + fmt::Display + TypeFoldable<TyCtxt<'tcx>> + 'tcx,
246-
{
247-
fn fallback_error(&self, tcx: TyCtxt<'tcx>, span: Span) -> Diag<'tcx> {
248-
tcx.dcx().create_err(HigherRankedLifetimeError {
249-
cause: Some(HigherRankedErrorCause::CouldNotNormalize {
250-
value: self.canonical_query.canonical.value.value.value.to_string(),
251-
}),
252-
span,
253-
})
254-
}
255-
256-
fn base_universe(&self) -> ty::UniverseIndex {
257-
self.base_universe
258-
}
259-
260-
fn nice_error<'infcx>(
261-
&self,
262-
mbcx: &mut MirBorrowckCtxt<'_, 'infcx, 'tcx>,
263-
cause: ObligationCause<'tcx>,
264-
placeholder_region: ty::Region<'tcx>,
265-
error_region: Option<ty::Region<'tcx>>,
266-
) -> Option<Diag<'infcx>> {
267-
let (infcx, key, _) =
268-
mbcx.infcx.tcx.infer_ctxt().build_with_canonical(cause.span, &self.canonical_query);
269-
let ocx = ObligationCtxt::new(&infcx);
270-
271-
// FIXME(lqd): Unify and de-duplicate the following with the actual
272-
// `rustc_traits::type_op::type_op_normalize` query to allow the span we need in the
273-
// `ObligationCause`. The normalization results are currently different between
274-
// `QueryNormalizeExt::query_normalize` used in the query and `normalize` called below:
275-
// the former fails to normalize the `nll/relate_tys/impl-fn-ignore-binder-via-bottom.rs`
276-
// test. Check after #85499 lands to see if its fixes have erased this difference.
277-
let ty::ParamEnvAnd { param_env, value } = key;
278-
let _ = ocx.normalize(&cause, param_env, Unnormalized::new_wip(value.value));
279-
280-
let diag = try_extract_error_from_fulfill_cx(
281-
&ocx,
282-
mbcx.mir_def_id(),
283-
placeholder_region,
284-
error_region,
285-
)?
286-
.with_dcx(mbcx.dcx());
287-
Some(diag)
288-
}
289-
}
290-
291-
struct DeeplyNormalizeQuery<'tcx, T> {
292-
canonical_query: CanonicalTypeOpDeeplyNormalizeGoal<'tcx, T>,
293-
base_universe: ty::UniverseIndex,
294-
}
295-
296-
impl<'tcx, T> TypeOpInfo<'tcx> for DeeplyNormalizeQuery<'tcx, T>
297236
where
298237
T: Copy + fmt::Display + TypeFoldable<TyCtxt<'tcx>> + 'tcx,
299238
{
@@ -328,8 +267,14 @@ where
328267
mbcx.infcx.tcx.infer_ctxt().build_with_canonical(cause.span, &self.canonical_query);
329268
let ocx = ObligationCtxt::new(&infcx);
330269

270+
// FIXME(lqd): Unify and de-duplicate the following with the actual
271+
// `rustc_traits::type_op::type_op_normalize` query to allow the span we need in the
272+
// `ObligationCause`. The normalization results are currently different between
273+
// `QueryNormalizeExt::query_normalize` used in the query and `normalize` called below:
274+
// the former fails to normalize the `nll/relate_tys/impl-fn-ignore-binder-via-bottom.rs`
275+
// test. Check after #85499 lands to see if its fixes have erased this difference.
331276
let ty::ParamEnvAnd { param_env, value } = key;
332-
let _ = ocx.deeply_normalize(&cause, param_env, value.value);
277+
let _ = ocx.normalize(&cause, param_env, value.value);
333278

334279
let diag = try_extract_error_from_fulfill_cx(
335280
&ocx,

compiler/rustc_borrowck/src/type_check/canonical.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
205205
self.fully_perform_op(
206206
location.to_locations(),
207207
ConstraintCategory::Boring,
208-
self.infcx.param_env.and(type_op::normalize::DeeplyNormalize { value }),
208+
self.infcx.param_env.and(type_op::normalize::Normalize { value }),
209209
)
210210
}
211211

compiler/rustc_borrowck/src/type_check/constraint_conversion.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use rustc_infer::infer::canonical::{QueryRegionConstraint, QueryRegionConstraint
55
use rustc_infer::infer::outlives::env::RegionBoundPairs;
66
use rustc_infer::infer::outlives::obligations::{TypeOutlives, TypeOutlivesDelegate};
77
use rustc_infer::infer::region_constraints::{GenericKind, VerifyBound};
8-
use rustc_infer::traits::query::type_op::DeeplyNormalize;
8+
use rustc_infer::traits::query::type_op::Normalize;
99
use rustc_middle::bug;
1010
use rustc_middle::ty::{
1111
self, GenericArgKind, Ty, TyCtxt, TypeFoldable, TypeVisitableExt, elaborate, fold_regions,
@@ -291,7 +291,7 @@ impl<'a, 'tcx> ConstraintConversion<'a, 'tcx> {
291291
) -> Ty<'tcx> {
292292
match self
293293
.infcx
294-
.fully_perform(DeeplyNormalize { value: ty::Unnormalized::new_wip(ty) }, self.span)
294+
.fully_perform(Normalize { value: ty::Unnormalized::new_wip(ty) }, self.span)
295295
{
296296
Ok(TypeOpOutput { output: ty, constraints, .. }) => {
297297
// FIXME(higher_ranked_auto): What should we do with the assumptions here?

compiler/rustc_borrowck/src/type_check/free_region_relations.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use rustc_infer::infer::canonical::QueryRegionConstraints;
55
use rustc_infer::infer::outlives;
66
use rustc_infer::infer::outlives::env::RegionBoundPairs;
77
use rustc_infer::infer::region_constraints::GenericKind;
8-
use rustc_infer::traits::query::type_op::DeeplyNormalize;
8+
use rustc_infer::traits::query::type_op::Normalize;
99
use rustc_middle::mir::ConstraintCategory;
1010
use rustc_middle::traits::query::OutlivesBound;
1111
use rustc_middle::ty::{self, RegionVid, Ty, TypeVisitableExt};
@@ -244,7 +244,7 @@ impl<'tcx> UniversalRegionRelationsBuilder<'_, 'tcx> {
244244
}
245245
let TypeOpOutput { output: norm_ty, constraints: constraints_normalize, .. } = self
246246
.infcx
247-
.fully_perform(DeeplyNormalize { value: ty::Unnormalized::new_wip(ty) }, span)
247+
.fully_perform(Normalize { value: ty::Unnormalized::new_wip(ty) }, span)
248248
.unwrap_or_else(|guar| TypeOpOutput {
249249
output: Ty::new_error(self.infcx.tcx, guar),
250250
constraints: None,
@@ -300,7 +300,7 @@ impl<'tcx> UniversalRegionRelationsBuilder<'_, 'tcx> {
300300
for &(ty, _) in tcx.assumed_wf_types(tcx.local_parent(defining_ty_def_id)) {
301301
let result: Result<_, ErrorGuaranteed> = self
302302
.infcx
303-
.fully_perform(DeeplyNormalize { value: ty::Unnormalized::new_wip(ty) }, span);
303+
.fully_perform(Normalize { value: ty::Unnormalized::new_wip(ty) }, span);
304304
let Ok(TypeOpOutput { output: norm_ty, constraints: c, .. }) = result else {
305305
continue;
306306
};
@@ -351,7 +351,7 @@ impl<'tcx> UniversalRegionRelationsBuilder<'_, 'tcx> {
351351
if self.infcx.next_trait_solver() {
352352
match self
353353
.infcx
354-
.fully_perform(DeeplyNormalize { value: ty::Unnormalized::new_wip(outlives) }, span)
354+
.fully_perform(Normalize { value: ty::Unnormalized::new_wip(outlives) }, span)
355355
{
356356
Ok(TypeOpOutput {
357357
output: normalized_outlives,

compiler/rustc_middle/src/queries.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2510,7 +2510,7 @@ rustc_queries! {
25102510
&'tcx Canonical<'tcx, canonical::QueryResponse<'tcx, Ty<'tcx>>>,
25112511
NoSolution,
25122512
> {
2513-
desc { "normalizing `{}`", goal.canonical.value.value.value }
2513+
desc { "normalizing `{}`", goal.canonical.value.value.value.skip_normalization() }
25142514
}
25152515

25162516
/// Do not call this query directly: part of the `Normalize` type-op
@@ -2520,7 +2520,7 @@ rustc_queries! {
25202520
&'tcx Canonical<'tcx, canonical::QueryResponse<'tcx, ty::Clause<'tcx>>>,
25212521
NoSolution,
25222522
> {
2523-
desc { "normalizing `{:?}`", goal.canonical.value.value.value }
2523+
desc { "normalizing `{:?}`", goal.canonical.value.value.value.skip_normalization() }
25242524
}
25252525

25262526
/// Do not call this query directly: part of the `Normalize` type-op
@@ -2530,7 +2530,7 @@ rustc_queries! {
25302530
&'tcx Canonical<'tcx, canonical::QueryResponse<'tcx, ty::PolyFnSig<'tcx>>>,
25312531
NoSolution,
25322532
> {
2533-
desc { "normalizing `{:?}`", goal.canonical.value.value.value }
2533+
desc { "normalizing `{:?}`", goal.canonical.value.value.value.skip_normalization() }
25342534
}
25352535

25362536
/// Do not call this query directly: part of the `Normalize` type-op
@@ -2540,7 +2540,7 @@ rustc_queries! {
25402540
&'tcx Canonical<'tcx, canonical::QueryResponse<'tcx, ty::FnSig<'tcx>>>,
25412541
NoSolution,
25422542
> {
2543-
desc { "normalizing `{:?}`", goal.canonical.value.value.value }
2543+
desc { "normalizing `{:?}`", goal.canonical.value.value.value.skip_normalization() }
25442544
}
25452545

25462546
query instantiate_and_check_impossible_predicates(key: (DefId, GenericArgsRef<'tcx>)) -> bool {

compiler/rustc_middle/src/traits/query.rs

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -42,15 +42,9 @@ pub mod type_op {
4242
pub predicate: Predicate<'tcx>,
4343
}
4444

45-
/// Normalizes, but not in the new solver.
45+
/// Normalizes a value that may contain unnormalized aliases.
4646
#[derive(Copy, Clone, Debug, Hash, PartialEq, Eq, StableHash, TypeFoldable, TypeVisitable)]
47-
pub struct Normalize<T> {
48-
pub value: T,
49-
}
50-
51-
/// Normalizes, and deeply normalizes in the new solver.
52-
#[derive(Copy, Clone, Debug, Hash, PartialEq, Eq, StableHash, TypeFoldable, TypeVisitable)]
53-
pub struct DeeplyNormalize<'tcx, T> {
47+
pub struct Normalize<'tcx, T> {
5448
pub value: Unnormalized<'tcx, T>,
5549
}
5650

@@ -89,10 +83,7 @@ pub type CanonicalTypeOpProvePredicateGoal<'tcx> =
8983
CanonicalQueryInput<'tcx, ty::ParamEnvAnd<'tcx, type_op::ProvePredicate<'tcx>>>;
9084

9185
pub type CanonicalTypeOpNormalizeGoal<'tcx, T> =
92-
CanonicalQueryInput<'tcx, ty::ParamEnvAnd<'tcx, type_op::Normalize<T>>>;
93-
94-
pub type CanonicalTypeOpDeeplyNormalizeGoal<'tcx, T> =
95-
CanonicalQueryInput<'tcx, ty::ParamEnvAnd<'tcx, type_op::DeeplyNormalize<'tcx, T>>>;
86+
CanonicalQueryInput<'tcx, ty::ParamEnvAnd<'tcx, type_op::Normalize<'tcx, T>>>;
9687

9788
pub type CanonicalImpliedOutlivesBoundsGoal<'tcx> =
9889
CanonicalQueryInput<'tcx, ty::ParamEnvAnd<'tcx, type_op::ImpliedOutlivesBounds<'tcx>>>;

compiler/rustc_trait_selection/src/traits/query/type_op/normalize.rs

Lines changed: 10 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -2,47 +2,14 @@ use std::fmt;
22

33
use rustc_middle::traits::ObligationCause;
44
use rustc_middle::traits::query::NoSolution;
5-
pub use rustc_middle::traits::query::type_op::{DeeplyNormalize, Normalize};
6-
use rustc_middle::ty::{
7-
self, Lift, ParamEnvAnd, Ty, TyCtxt, TypeFoldable, TypeVisitableExt, Unnormalized,
8-
};
5+
pub use rustc_middle::traits::query::type_op::Normalize;
6+
use rustc_middle::ty::{self, Lift, ParamEnvAnd, Ty, TyCtxt, TypeFoldable, TypeVisitableExt};
97
use rustc_span::Span;
108

119
use crate::infer::canonical::{CanonicalQueryInput, CanonicalQueryResponse};
1210
use crate::traits::ObligationCtxt;
1311

14-
impl<'tcx, T> super::QueryTypeOp<'tcx> for Normalize<T>
15-
where
16-
T: Normalizable<'tcx> + 'tcx,
17-
{
18-
type QueryResponse = T;
19-
20-
fn try_fast_path(_tcx: TyCtxt<'tcx>, key: &ParamEnvAnd<'tcx, Self>) -> Option<T> {
21-
if !key.value.value.has_aliases() { Some(key.value.value) } else { None }
22-
}
23-
24-
fn perform_query(
25-
tcx: TyCtxt<'tcx>,
26-
canonicalized: CanonicalQueryInput<'tcx, ParamEnvAnd<'tcx, Self>>,
27-
) -> Result<CanonicalQueryResponse<'tcx, Self::QueryResponse>, NoSolution> {
28-
T::type_op_method(tcx, canonicalized)
29-
}
30-
31-
fn perform_locally_with_next_solver(
32-
ocx: &ObligationCtxt<'_, 'tcx>,
33-
key: ParamEnvAnd<'tcx, Self>,
34-
span: Span,
35-
) -> Result<Self::QueryResponse, NoSolution> {
36-
ocx.deeply_normalize(
37-
&ObligationCause::dummy_with_span(span),
38-
key.param_env,
39-
Unnormalized::new_wip(key.value.value),
40-
)
41-
.map_err(|_| NoSolution)
42-
}
43-
}
44-
45-
impl<'tcx, T> super::QueryTypeOp<'tcx> for DeeplyNormalize<'tcx, T>
12+
impl<'tcx, T> super::QueryTypeOp<'tcx> for Normalize<'tcx, T>
4613
where
4714
T: Normalizable<'tcx> + 'tcx,
4815
{
@@ -60,18 +27,7 @@ where
6027
tcx: TyCtxt<'tcx>,
6128
canonicalized: CanonicalQueryInput<'tcx, ParamEnvAnd<'tcx, Self>>,
6229
) -> Result<CanonicalQueryResponse<'tcx, Self::QueryResponse>, NoSolution> {
63-
T::type_op_method(
64-
tcx,
65-
CanonicalQueryInput {
66-
typing_mode: canonicalized.typing_mode,
67-
canonical: canonicalized.canonical.unchecked_map(
68-
|ty::ParamEnvAnd { param_env, value }| ty::ParamEnvAnd {
69-
param_env,
70-
value: Normalize { value: value.value.skip_normalization() },
71-
},
72-
),
73-
},
74-
)
30+
T::type_op_method(tcx, canonicalized)
7531
}
7632

7733
fn perform_locally_with_next_solver(
@@ -93,14 +49,14 @@ pub trait Normalizable<'tcx>:
9349
{
9450
fn type_op_method(
9551
tcx: TyCtxt<'tcx>,
96-
canonicalized: CanonicalQueryInput<'tcx, ParamEnvAnd<'tcx, Normalize<Self>>>,
52+
canonicalized: CanonicalQueryInput<'tcx, ParamEnvAnd<'tcx, Normalize<'tcx, Self>>>,
9753
) -> Result<CanonicalQueryResponse<'tcx, Self>, NoSolution>;
9854
}
9955

10056
impl<'tcx> Normalizable<'tcx> for Ty<'tcx> {
10157
fn type_op_method(
10258
tcx: TyCtxt<'tcx>,
103-
canonicalized: CanonicalQueryInput<'tcx, ParamEnvAnd<'tcx, Normalize<Self>>>,
59+
canonicalized: CanonicalQueryInput<'tcx, ParamEnvAnd<'tcx, Normalize<'tcx, Self>>>,
10460
) -> Result<CanonicalQueryResponse<'tcx, Self>, NoSolution> {
10561
tcx.type_op_normalize_ty(canonicalized)
10662
}
@@ -109,7 +65,7 @@ impl<'tcx> Normalizable<'tcx> for Ty<'tcx> {
10965
impl<'tcx> Normalizable<'tcx> for ty::Clause<'tcx> {
11066
fn type_op_method(
11167
tcx: TyCtxt<'tcx>,
112-
canonicalized: CanonicalQueryInput<'tcx, ParamEnvAnd<'tcx, Normalize<Self>>>,
68+
canonicalized: CanonicalQueryInput<'tcx, ParamEnvAnd<'tcx, Normalize<'tcx, Self>>>,
11369
) -> Result<CanonicalQueryResponse<'tcx, Self>, NoSolution> {
11470
tcx.type_op_normalize_clause(canonicalized)
11571
}
@@ -118,7 +74,7 @@ impl<'tcx> Normalizable<'tcx> for ty::Clause<'tcx> {
11874
impl<'tcx> Normalizable<'tcx> for ty::PolyFnSig<'tcx> {
11975
fn type_op_method(
12076
tcx: TyCtxt<'tcx>,
121-
canonicalized: CanonicalQueryInput<'tcx, ParamEnvAnd<'tcx, Normalize<Self>>>,
77+
canonicalized: CanonicalQueryInput<'tcx, ParamEnvAnd<'tcx, Normalize<'tcx, Self>>>,
12278
) -> Result<CanonicalQueryResponse<'tcx, Self>, NoSolution> {
12379
tcx.type_op_normalize_poly_fn_sig(canonicalized)
12480
}
@@ -127,7 +83,7 @@ impl<'tcx> Normalizable<'tcx> for ty::PolyFnSig<'tcx> {
12783
impl<'tcx> Normalizable<'tcx> for ty::FnSig<'tcx> {
12884
fn type_op_method(
12985
tcx: TyCtxt<'tcx>,
130-
canonicalized: CanonicalQueryInput<'tcx, ParamEnvAnd<'tcx, Normalize<Self>>>,
86+
canonicalized: CanonicalQueryInput<'tcx, ParamEnvAnd<'tcx, Normalize<'tcx, Self>>>,
13187
) -> Result<CanonicalQueryResponse<'tcx, Self>, NoSolution> {
13288
tcx.type_op_normalize_fn_sig(canonicalized)
13389
}
@@ -138,7 +94,7 @@ impl<'tcx> Normalizable<'tcx> for ty::FnSig<'tcx> {
13894
impl<'tcx> Normalizable<'tcx> for ty::PolyTypeOutlivesPredicate<'tcx> {
13995
fn type_op_method(
14096
_tcx: TyCtxt<'tcx>,
141-
_canonicalized: CanonicalQueryInput<'tcx, ParamEnvAnd<'tcx, Normalize<Self>>>,
97+
_canonicalized: CanonicalQueryInput<'tcx, ParamEnvAnd<'tcx, Normalize<'tcx, Self>>>,
14298
) -> Result<CanonicalQueryResponse<'tcx, Self>, NoSolution> {
14399
unreachable!("we never normalize PolyTypeOutlivesPredicate")
144100
}

0 commit comments

Comments
 (0)