Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 52cd342

Browse files
FnCtxt normalization stuff
1 parent fc71083 commit 52cd342

File tree

15 files changed

+41
-67
lines changed

15 files changed

+41
-67
lines changed

compiler/rustc_hir_analysis/src/astconv/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,9 @@ pub trait AstConv<'tcx> {
109109
) -> Ty<'tcx>;
110110

111111
/// Normalize an associated type coming from the user.
112+
///
113+
/// This should only be used by astconv. Use `FnCtxt::normalize`
114+
/// or `ObligationCtxt::normalize` in downstream crates.
112115
fn normalize_ty(&self, span: Span, ty: Ty<'tcx>) -> Ty<'tcx>;
113116

114117
/// Invoked when we encounter an error from some prior pass

compiler/rustc_hir_analysis/src/check/wfcheck.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ impl<'tcx> WfCheckingCtxt<'_, 'tcx> {
5353
self.ocx.infcx.tcx
5454
}
5555

56+
// Convenience function to normalize during wfcheck. This performs
57+
// `ObligationCtxt::normalize`, but provides a nice `ObligationCauseCode`.
5658
fn normalize<T>(&self, span: Span, loc: Option<WellFormedLoc>, value: T) -> T
5759
where
5860
T: TypeFoldable<'tcx>,

compiler/rustc_hir_typeck/src/callee.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -448,7 +448,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
448448
// previously appeared within a `Binder<>` and hence would not
449449
// have been normalized before.
450450
let fn_sig = self.replace_bound_vars_with_fresh_vars(call_expr.span, infer::FnCall, fn_sig);
451-
let fn_sig = self.normalize_associated_types_in(call_expr.span, fn_sig);
451+
let fn_sig = self.normalize(call_expr.span, fn_sig);
452452

453453
// Call the generic checker.
454454
let expected_arg_tys = self.expected_inputs_for_expected_output(

compiler/rustc_hir_typeck/src/cast.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -752,7 +752,7 @@ impl<'a, 'tcx> CastCheck<'tcx> {
752752
match *self.expr_ty.kind() {
753753
ty::FnDef(..) => {
754754
// Attempt a coercion to a fn pointer type.
755-
let f = fcx.normalize_associated_types_in(
755+
let f = fcx.normalize(
756756
self.expr_span,
757757
self.expr_ty.fn_sig(fcx.tcx),
758758
);

compiler/rustc_hir_typeck/src/closure.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
214214
if expected_sig.is_none()
215215
&& let ty::PredicateKind::Clause(ty::Clause::Projection(proj_predicate)) = bound_predicate.skip_binder()
216216
{
217-
expected_sig = self.normalize_associated_types_in(
217+
expected_sig = self.normalize(
218218
obligation.cause.span,
219219
self.deduce_sig_from_projection(
220220
Some(obligation.cause.span),
@@ -623,7 +623,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
623623
);
624624
// Astconv can't normalize inputs or outputs with escaping bound vars,
625625
// so normalize them here, after we've wrapped them in a binder.
626-
let result = self.normalize_associated_types_in(self.tcx.hir().span(hir_id), result);
626+
let result = self.normalize(self.tcx.hir().span(hir_id), result);
627627

628628
let c_result = self.inh.infcx.canonicalize_response(result);
629629
self.typeck_results.borrow_mut().user_provided_sigs.insert(expr_def_id, c_result);
@@ -797,10 +797,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
797797
) -> ClosureSignatures<'tcx> {
798798
let liberated_sig =
799799
self.tcx().liberate_late_bound_regions(expr_def_id.to_def_id(), bound_sig);
800-
let liberated_sig = self.inh.normalize_associated_types_in(
800+
let liberated_sig = self.normalize(
801801
body.value.span,
802-
self.tcx.hir().local_def_id_to_hir_id(expr_def_id),
803-
self.param_env,
804802
liberated_sig,
805803
);
806804
ClosureSignatures { bound_sig, liberated_sig }

compiler/rustc_hir_typeck/src/coercion.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1141,8 +1141,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
11411141
return Err(TypeError::IntrinsicCast);
11421142
}
11431143
// The signature must match.
1144-
let a_sig = self.normalize_associated_types_in(new.span, a_sig);
1145-
let b_sig = self.normalize_associated_types_in(new.span, b_sig);
1144+
let (a_sig, b_sig) = self.normalize(new.span, (a_sig, b_sig));
11461145
let sig = self
11471146
.at(cause, self.param_env)
11481147
.trace(prev_ty, new_ty)

compiler/rustc_hir_typeck/src/expr.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1664,7 +1664,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
16641664
.fields
16651665
.iter()
16661666
.map(|f| {
1667-
let fru_ty = self.normalize_associated_types_in(
1667+
let fru_ty = self.normalize(
16681668
expr_span,
16691669
self.field_ty(base_expr.span, f, fresh_substs),
16701670
);
@@ -1749,7 +1749,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
17491749
.fields
17501750
.iter()
17511751
.map(|f| {
1752-
self.normalize_associated_types_in(expr_span, f.ty(self.tcx, substs))
1752+
self.normalize(expr_span, f.ty(self.tcx, substs))
17531753
})
17541754
.collect(),
17551755
_ => {

compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
344344
{
345345
debug!("instantiate_type_scheme(value={:?}, substs={:?})", value, substs);
346346
let value = EarlyBinder(value).subst(self.tcx, substs);
347-
let result = self.normalize_associated_types_in(span, value);
347+
let result = self.normalize(span, value);
348348
debug!("instantiate_type_scheme = {:?}", result);
349349
result
350350
}
@@ -360,19 +360,21 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
360360
let bounds = self.tcx.predicates_of(def_id);
361361
let spans: Vec<Span> = bounds.predicates.iter().map(|(_, span)| *span).collect();
362362
let result = bounds.instantiate(self.tcx, substs);
363-
let result = self.normalize_associated_types_in(span, result);
363+
let result = self.normalize(span, result);
364364
debug!(
365365
"instantiate_bounds(bounds={:?}, substs={:?}) = {:?}, {:?}",
366366
bounds, substs, result, spans,
367367
);
368368
(result, spans)
369369
}
370370

371-
pub(in super::super) fn normalize_associated_types_in<T>(&self, span: Span, value: T) -> T
371+
pub(in super::super) fn normalize<T>(&self, span: Span, value: T) -> T
372372
where
373373
T: TypeFoldable<'tcx>,
374374
{
375-
self.inh.normalize_associated_types_in(span, self.body_id, self.param_env, value)
375+
self.register_infer_ok_obligations(
376+
self.at(&self.misc(span), self.param_env).normalize(value),
377+
)
376378
}
377379

378380
pub(in super::super) fn normalize_associated_types_in_as_infer_ok<T>(
@@ -487,7 +489,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
487489
let span = self.tcx.def_span(anon_const.def_id);
488490
let c = ty::Const::from_anon_const(self.tcx, anon_const.def_id);
489491
self.register_wf_obligation(c.into(), span, ObligationCauseCode::WellFormed(None));
490-
self.normalize_associated_types_in(span, c)
492+
self.normalize(span, c)
491493
}
492494
}
493495
}
@@ -580,7 +582,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
580582
field: &'tcx ty::FieldDef,
581583
substs: SubstsRef<'tcx>,
582584
) -> Ty<'tcx> {
583-
self.normalize_associated_types_in(span, field.ty(self.tcx, substs))
585+
self.normalize(span, field.ty(self.tcx, substs))
584586
}
585587

586588
pub(in super::super) fn resolve_rvalue_scopes(&self, def_id: DefId) {
@@ -1107,7 +1109,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
11071109

11081110
if let Res::Local(hid) = res {
11091111
let ty = self.local_ty(span, hid).decl_ty;
1110-
let ty = self.normalize_associated_types_in(span, ty);
1112+
let ty = self.normalize(span, ty);
11111113
self.write_ty(hir_id, ty);
11121114
return (ty, res);
11131115
}

compiler/rustc_hir_typeck/src/fn_ctxt/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ impl<'a, 'tcx> AstConv<'tcx> for FnCtxt<'a, 'tcx> {
288288
if ty.has_escaping_bound_vars() {
289289
ty // FIXME: normalization and escaping regions
290290
} else {
291-
self.normalize_associated_types_in(span, ty)
291+
self.normalize(span, ty)
292292
}
293293
}
294294

compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -759,7 +759,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
759759
debug!("suggest_missing_return_type: expected type {:?}", ty);
760760
let bound_vars = self.tcx.late_bound_vars(fn_id);
761761
let ty = Binder::bind_with_vars(ty, bound_vars);
762-
let ty = self.normalize_associated_types_in(span, ty);
762+
let ty = self.normalize(span, ty);
763763
let ty = self.tcx.erase_late_bound_regions(ty);
764764
if self.can_coerce(expected, ty) {
765765
err.subdiagnostic(ExpectedReturnTypeLabel::Other { span, expected });
@@ -920,7 +920,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
920920
let ty = <dyn AstConv<'_>>::ast_ty_to_ty(self, ty);
921921
let bound_vars = self.tcx.late_bound_vars(fn_id);
922922
let ty = self.tcx.erase_late_bound_regions(Binder::bind_with_vars(ty, bound_vars));
923-
let ty = self.normalize_associated_types_in(expr.span, ty);
923+
let ty = self.normalize(expr.span, ty);
924924
let ty = match self.tcx.asyncness(fn_id.owner) {
925925
hir::IsAsync::Async => {
926926
let infcx = self.tcx.infer_ctxt().build();

0 commit comments

Comments
 (0)