Skip to content
/ rust Public
forked from rust-lang/rust

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();

compiler/rustc_hir_typeck/src/generator_interior/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ pub fn resolve_interior<'a, 'tcx>(
235235
counter += 1;
236236
ty::BoundRegion { var, kind }
237237
};
238-
let ty = fcx.normalize_associated_types_in(cause.span, cause.ty);
238+
let ty = fcx.normalize(cause.span, cause.ty);
239239
let ty = fcx.tcx.fold_regions(ty, |region, current_depth| {
240240
let br = match region.kind() {
241241
ty::ReVar(vid) => {

compiler/rustc_hir_typeck/src/inherited.rs

Lines changed: 1 addition & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,12 @@ use rustc_hir::def_id::LocalDefId;
77
use rustc_hir::HirIdMap;
88
use rustc_infer::infer;
99
use rustc_infer::infer::{DefiningAnchor, InferCtxt, InferOk, TyCtxtInferExt};
10-
use rustc_middle::ty::fold::TypeFoldable;
1110
use rustc_middle::ty::visit::TypeVisitable;
1211
use rustc_middle::ty::{self, Ty, TyCtxt};
1312
use rustc_span::def_id::LocalDefIdMap;
1413
use rustc_span::{self, Span};
1514
use rustc_trait_selection::traits::{
16-
self, NormalizeExt, ObligationCause, ObligationCtxt, TraitEngine, TraitEngineExt as _,
15+
self, ObligationCause, ObligationCtxt, TraitEngine, TraitEngineExt as _,
1716
};
1817

1918
use std::cell::RefCell;
@@ -178,35 +177,4 @@ impl<'tcx> Inherited<'tcx> {
178177
self.register_predicates(infer_ok.obligations);
179178
infer_ok.value
180179
}
181-
182-
pub(super) fn normalize_associated_types_in<T>(
183-
&self,
184-
span: Span,
185-
body_id: hir::HirId,
186-
param_env: ty::ParamEnv<'tcx>,
187-
value: T,
188-
) -> T
189-
where
190-
T: TypeFoldable<'tcx>,
191-
{
192-
self.normalize_associated_types_in_with_cause(
193-
ObligationCause::misc(span, body_id),
194-
param_env,
195-
value,
196-
)
197-
}
198-
199-
pub(super) fn normalize_associated_types_in_with_cause<T>(
200-
&self,
201-
cause: ObligationCause<'tcx>,
202-
param_env: ty::ParamEnv<'tcx>,
203-
value: T,
204-
) -> T
205-
where
206-
T: TypeFoldable<'tcx>,
207-
{
208-
let ok = self.at(&cause, param_env).normalize(value);
209-
debug!(?ok);
210-
self.register_infer_ok_obligations(ok)
211-
}
212180
}

compiler/rustc_hir_typeck/src/lib.rs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ pub use diverges::Diverges;
4848
pub use expectation::Expectation;
4949
pub use fn_ctxt::*;
5050
pub use inherited::{Inherited, InheritedBuilder};
51+
use rustc_infer::traits::ObligationCause;
52+
use rustc_trait_selection::traits::NormalizeExt;
5153

5254
use crate::check::check_fn;
5355
use crate::coercion::DynamicCoerceMany;
@@ -245,12 +247,13 @@ fn typeck_with_fallback<'tcx>(
245247

246248
// Compute the function signature from point of view of inside the fn.
247249
let fn_sig = tcx.liberate_late_bound_regions(def_id.to_def_id(), fn_sig);
248-
let fn_sig = inh.normalize_associated_types_in(
249-
body.value.span,
250-
body_id.hir_id,
251-
param_env,
252-
fn_sig,
253-
);
250+
// FIXME(compiler-errors): Remove
251+
let fn_sig = inh
252+
.register_infer_ok_obligations(
253+
inh.at(&ObligationCause::misc(body.value.span, body_id.hir_id),
254+
param_env,
255+
)
256+
.normalize(fn_sig));
254257
check_fn(&inh, param_env, fn_sig, decl, def_id, body, None).0
255258
} else {
256259
let fcx = FnCtxt::new(&inh, param_env, body.value.hir_id);
@@ -304,7 +307,7 @@ fn typeck_with_fallback<'tcx>(
304307
_ => fallback(),
305308
});
306309

307-
let expected_type = fcx.normalize_associated_types_in(body.value.span, expected_type);
310+
let expected_type = fcx.normalize(body.value.span, expected_type);
308311
fcx.require_type_is_sized(expected_type, body.value.span, traits::ConstSized);
309312

310313
// Gather locals in statics (because of block expressions).

compiler/rustc_hir_typeck/src/method/confirm.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,15 +106,15 @@ impl<'a, 'tcx> ConfirmContext<'a, 'tcx> {
106106
// traits, no trait system method can be called before this point because they
107107
// could alter our Self-type, except for normalizing the receiver from the
108108
// signature (which is also done during probing).
109-
let method_sig_rcvr = self.normalize_associated_types_in(self.span, method_sig.inputs()[0]);
109+
let method_sig_rcvr = self.normalize(self.span, method_sig.inputs()[0]);
110110
debug!(
111111
"confirm: self_ty={:?} method_sig_rcvr={:?} method_sig={:?} method_predicates={:?}",
112112
self_ty, method_sig_rcvr, method_sig, method_predicates
113113
);
114114
self.unify_receivers(self_ty, method_sig_rcvr, &pick, all_substs);
115115

116116
let (method_sig, method_predicates) =
117-
self.normalize_associated_types_in(self.span, (method_sig, method_predicates));
117+
self.normalize(self.span, (method_sig, method_predicates));
118118
let method_sig = ty::Binder::dummy(method_sig);
119119

120120
// Make sure nobody calls `drop()` explicitly.

src/tools/clippy/clippy_utils/src/ty.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,7 @@ use rustc_span::symbol::Ident;
2222
use rustc_span::{sym, Span, Symbol, DUMMY_SP};
2323
use rustc_target::abi::{Size, VariantIdx};
2424
use rustc_trait_selection::infer::InferCtxtExt;
25-
use rustc_trait_selection::traits::NormalizeExt;
26-
use rustc_trait_selection::traits::query::normalize::AtExt;
25+
use rustc_trait_selection::traits::query::normalize::QueryNormalizeExt;
2726
use std::iter;
2827

2928
use crate::{match_def_path, path_res, paths};
@@ -284,7 +283,7 @@ fn is_normalizable_helper<'tcx>(
284283
cache.insert(ty, false);
285284
let infcx = cx.tcx.infer_ctxt().build();
286285
let cause = rustc_middle::traits::ObligationCause::dummy();
287-
let result = if infcx.at(&cause, param_env).normalize(ty).is_ok() {
286+
let result = if infcx.at(&cause, param_env).query_normalize(ty).is_ok() {
288287
match ty.kind() {
289288
ty::Adt(def, substs) => def.variants().iter().all(|variant| {
290289
variant

0 commit comments

Comments
 (0)