Skip to content

Commit 02b64c5

Browse files
Document normalization methods on At
1 parent e45daa9 commit 02b64c5

File tree

4 files changed

+20
-38
lines changed

4 files changed

+20
-38
lines changed

compiler/rustc_trait_selection/src/traits/coherence.rs

+6-4
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ use crate::traits::select::IntercrateAmbiguityCause;
1111
use crate::traits::util::impl_subject_and_oblig;
1212
use crate::traits::SkipLeakCheck;
1313
use crate::traits::{
14-
self, Normalized, Obligation, ObligationCause, ObligationCtxt, PredicateObligation,
15-
PredicateObligations, SelectionContext,
14+
self, Obligation, ObligationCause, ObligationCtxt, PredicateObligation, PredicateObligations,
15+
SelectionContext,
1616
};
1717
use rustc_data_structures::fx::FxIndexSet;
1818
use rustc_errors::Diagnostic;
@@ -30,6 +30,8 @@ use std::fmt::Debug;
3030
use std::iter;
3131
use std::ops::ControlFlow;
3232

33+
use super::NormalizeExt;
34+
3335
/// Whether we do the orphan check relative to this crate or
3436
/// to some remote crate.
3537
#[derive(Copy, Clone, Debug)]
@@ -128,8 +130,8 @@ fn with_fresh_ty_vars<'cx, 'tcx>(
128130
predicates: tcx.predicates_of(impl_def_id).instantiate(tcx, impl_substs).predicates,
129131
};
130132

131-
let Normalized { value: mut header, obligations } =
132-
traits::normalize(selcx, param_env, ObligationCause::dummy(), header);
133+
let InferOk { value: mut header, obligations } =
134+
selcx.infcx.at(&ObligationCause::dummy(), param_env).normalize(header);
133135

134136
header.predicates.extend(obligations.into_iter().map(|o| o.predicate));
135137
header

compiler/rustc_trait_selection/src/traits/mod.rs

-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@ pub use self::object_safety::astconv_object_safety_violations;
5656
pub use self::object_safety::is_vtable_safe_method;
5757
pub use self::object_safety::MethodViolationCode;
5858
pub use self::object_safety::ObjectSafetyViolation;
59-
pub(crate) use self::project::{normalize, normalize_to};
6059
pub use self::project::{normalize_projection_type, NormalizeExt};
6160
pub use self::select::{EvaluationCache, SelectionCache, SelectionContext};
6261
pub use self::select::{EvaluationResult, IntercrateAmbiguityCause, OverflowError};

compiler/rustc_trait_selection/src/traits/project.rs

+7-33
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,18 @@ pub type ProjectionTyObligation<'tcx> = Obligation<'tcx, ty::ProjectionTy<'tcx>>
5050
pub(super) struct InProgress;
5151

5252
pub trait NormalizeExt<'tcx> {
53+
/// Normalize a value using the `AssocTypeNormalizer`.
54+
///
55+
/// This normalization should be used when the type contains inference variables or the
56+
/// projection may be fallible.
5357
fn normalize<T: TypeFoldable<'tcx>>(&self, t: T) -> InferOk<'tcx, T>;
5458
}
5559

5660
impl<'tcx> NormalizeExt<'tcx> for At<'_, 'tcx> {
5761
fn normalize<T: TypeFoldable<'tcx>>(&self, value: T) -> InferOk<'tcx, T> {
5862
let mut selcx = SelectionContext::new(self.infcx);
5963
let Normalized { value, obligations } =
60-
normalize(&mut selcx, self.param_env, self.cause.clone(), value);
64+
normalize_with_depth(&mut selcx, self.param_env, self.cause.clone(), 0, value);
6165
InferOk { value, obligations }
6266
}
6367
}
@@ -303,37 +307,6 @@ fn project_and_unify_type<'cx, 'tcx>(
303307
}
304308
}
305309

306-
/// Normalizes any associated type projections in `value`, replacing
307-
/// them with a fully resolved type where possible. The return value
308-
/// combines the normalized result and any additional obligations that
309-
/// were incurred as result.
310-
pub(crate) fn normalize<'a, 'b, 'tcx, T>(
311-
selcx: &'a mut SelectionContext<'b, 'tcx>,
312-
param_env: ty::ParamEnv<'tcx>,
313-
cause: ObligationCause<'tcx>,
314-
value: T,
315-
) -> Normalized<'tcx, T>
316-
where
317-
T: TypeFoldable<'tcx>,
318-
{
319-
let mut obligations = Vec::new();
320-
let value = normalize_to(selcx, param_env, cause, value, &mut obligations);
321-
Normalized { value, obligations }
322-
}
323-
324-
pub(crate) fn normalize_to<'a, 'b, 'tcx, T>(
325-
selcx: &'a mut SelectionContext<'b, 'tcx>,
326-
param_env: ty::ParamEnv<'tcx>,
327-
cause: ObligationCause<'tcx>,
328-
value: T,
329-
obligations: &mut Vec<PredicateObligation<'tcx>>,
330-
) -> T
331-
where
332-
T: TypeFoldable<'tcx>,
333-
{
334-
normalize_with_depth_to(selcx, param_env, cause, 0, value, obligations)
335-
}
336-
337310
/// As `normalize`, but with a custom depth.
338311
pub(crate) fn normalize_with_depth<'a, 'b, 'tcx, T>(
339312
selcx: &'a mut SelectionContext<'b, 'tcx>,
@@ -2324,10 +2297,11 @@ fn confirm_impl_trait_in_trait_candidate<'tcx>(
23242297
},
23252298
));
23262299

2327-
let ty = super::normalize_to(
2300+
let ty = normalize_with_depth_to(
23282301
selcx,
23292302
obligation.param_env,
23302303
cause.clone(),
2304+
obligation.recursion_depth + 1,
23312305
tcx.bound_trait_impl_trait_tys(impl_fn_def_id)
23322306
.map_bound(|tys| {
23332307
tys.map_or_else(|_| tcx.ty_error(), |tys| tys[&obligation.predicate.item_def_id])

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

+7
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,13 @@ use super::NoSolution;
2323
pub use rustc_middle::traits::query::NormalizationResult;
2424

2525
pub trait QueryNormalizeExt<'tcx> {
26+
/// Normalize a value using the `QueryNormalizer`.
27+
///
28+
/// This normalization should *only* be used when the projection does not
29+
/// have possible ambiguity or may not be well-formed.
30+
///
31+
/// After codegen, when lifetimes do not matter, it is preferable to instead
32+
/// use [`TyCtxt::normalize_erasing_regions`], which wraps this procedure.
2633
fn query_normalize<T>(&self, value: T) -> Result<Normalized<'tcx, T>, NoSolution>
2734
where
2835
T: TypeFoldable<'tcx>;

0 commit comments

Comments
 (0)