Skip to content

Commit 24606de

Browse files
committed
Auto merge of rust-lang#104905 - compiler-errors:normalization-changes, r=spastorino
Some initial normalization method changes 1. Rename `AtExt::normalize` to `QueryNormalizeExt::query_normalize` (using the `QueryNormalizer`) 2. Introduce `NormalizeExt::normalize` to replace `partially_normalize_associated_types_in` (using the `AssocTypeNormalizer`) 3. Rename `FnCtxt::normalize_associated_types_in` to `FnCtxt::normalize` 4. Remove some unused other normalization fns in `Inherited` and `FnCtxt` Also includes one drive-by where we're no longer creating a `FnCtxt` inside of `check_fn`, but passing it in. This means we don't need such weird `FnCtxt` construction logic. Stacked on top of rust-lang#104835 for convenience. r? types
2 parents a569a88 + 1e236ac commit 24606de

File tree

37 files changed

+159
-285
lines changed

37 files changed

+159
-285
lines changed

compiler/rustc_borrowck/src/diagnostics/bound_region_errors.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -291,11 +291,11 @@ where
291291
// FIXME(lqd): Unify and de-duplicate the following with the actual
292292
// `rustc_traits::type_op::type_op_normalize` query to allow the span we need in the
293293
// `ObligationCause`. The normalization results are currently different between
294-
// `AtExt::normalize` used in the query and `normalize` called below: the former fails
295-
// to normalize the `nll/relate_tys/impl-fn-ignore-binder-via-bottom.rs` test. Check
296-
// after #85499 lands to see if its fixes have erased this difference.
294+
// `QueryNormalizeExt::query_normalize` used in the query and `normalize` called below:
295+
// the former fails to normalize the `nll/relate_tys/impl-fn-ignore-binder-via-bottom.rs` test.
296+
// Check after #85499 lands to see if its fixes have erased this difference.
297297
let (param_env, value) = key.into_parts();
298-
let _ = ocx.normalize(cause, param_env, value.value);
298+
let _ = ocx.normalize(&cause, param_env, value.value);
299299

300300
try_extract_error_from_fulfill_cx(&ocx, placeholder_region, error_region)
301301
}

compiler/rustc_const_eval/src/transform/check_consts/check.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -761,8 +761,7 @@ impl<'tcx> Visitor<'tcx> for Checker<'_, 'tcx> {
761761
hir_id,
762762
ObligationCauseCode::ItemObligation(callee),
763763
);
764-
let normalized_predicates =
765-
ocx.normalize(cause.clone(), param_env, predicates);
764+
let normalized_predicates = ocx.normalize(&cause, param_env, predicates);
766765
ocx.register_obligations(traits::predicates_for_generics(
767766
|_, _| cause.clone(),
768767
self.param_env,

compiler/rustc_const_eval/src/util/compare_types.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ pub fn is_subtype<'tcx>(
4646
let infcx = builder.build();
4747
let ocx = ObligationCtxt::new(&infcx);
4848
let cause = ObligationCause::dummy();
49-
let src = ocx.normalize(cause.clone(), param_env, src);
50-
let dest = ocx.normalize(cause.clone(), param_env, dest);
49+
let src = ocx.normalize(&cause, param_env, src);
50+
let dest = ocx.normalize(&cause, param_env, dest);
5151
match ocx.sub(&cause, param_env, src, dest) {
5252
Ok(()) => {}
5353
Err(_) => return false,

compiler/rustc_hir_analysis/src/astconv/mod.rs

+3
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/compare_method.rs

+10-10
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ fn compare_predicate_entailment<'tcx>(
221221
let impl_m_own_bounds = impl_m_predicates.instantiate_own(tcx, impl_to_placeholder_substs);
222222
for (predicate, span) in iter::zip(impl_m_own_bounds.predicates, impl_m_own_bounds.spans) {
223223
let normalize_cause = traits::ObligationCause::misc(span, impl_m_hir_id);
224-
let predicate = ocx.normalize(normalize_cause, param_env, predicate);
224+
let predicate = ocx.normalize(&normalize_cause, param_env, predicate);
225225

226226
let cause = ObligationCause::new(
227227
span,
@@ -260,7 +260,7 @@ fn compare_predicate_entailment<'tcx>(
260260
);
261261

262262
let norm_cause = ObligationCause::misc(impl_m_span, impl_m_hir_id);
263-
let impl_sig = ocx.normalize(norm_cause.clone(), param_env, impl_sig);
263+
let impl_sig = ocx.normalize(&norm_cause, param_env, impl_sig);
264264
let impl_fty = tcx.mk_fn_ptr(ty::Binder::dummy(impl_sig));
265265
debug!("compare_impl_method: impl_fty={:?}", impl_fty);
266266

@@ -271,7 +271,7 @@ fn compare_predicate_entailment<'tcx>(
271271
// we have to do this before normalization, since the normalized ty may
272272
// not contain the input parameters. See issue #87748.
273273
wf_tys.extend(trait_sig.inputs_and_output.iter());
274-
let trait_sig = ocx.normalize(norm_cause, param_env, trait_sig);
274+
let trait_sig = ocx.normalize(&norm_cause, param_env, trait_sig);
275275
// We also have to add the normalized trait signature
276276
// as we don't normalize during implied bounds computation.
277277
wf_tys.extend(trait_sig.inputs_and_output.iter());
@@ -366,7 +366,7 @@ pub fn collect_trait_impl_trait_tys<'tcx>(
366366
// Normalize the impl signature with fresh variables for lifetime inference.
367367
let norm_cause = ObligationCause::misc(return_span, impl_m_hir_id);
368368
let impl_sig = ocx.normalize(
369-
norm_cause.clone(),
369+
&norm_cause,
370370
param_env,
371371
infcx.replace_bound_vars_with_fresh_vars(
372372
return_span,
@@ -387,7 +387,7 @@ pub fn collect_trait_impl_trait_tys<'tcx>(
387387
tcx.bound_fn_sig(trait_m.def_id).subst(tcx, trait_to_placeholder_substs),
388388
)
389389
.fold_with(&mut collector);
390-
let trait_sig = ocx.normalize(norm_cause.clone(), param_env, unnormalized_trait_sig);
390+
let trait_sig = ocx.normalize(&norm_cause, param_env, unnormalized_trait_sig);
391391
let trait_return_ty = trait_sig.output();
392392

393393
let wf_tys = FxIndexSet::from_iter(
@@ -592,7 +592,7 @@ impl<'tcx> TypeFolder<'tcx> for ImplTraitInTraitCollector<'_, 'tcx> {
592592
for (pred, pred_span) in self.tcx().bound_explicit_item_bounds(proj.item_def_id).subst_iter_copied(self.tcx(), proj.substs) {
593593
let pred = pred.fold_with(self);
594594
let pred = self.ocx.normalize(
595-
ObligationCause::misc(self.span, self.body_id),
595+
&ObligationCause::misc(self.span, self.body_id),
596596
self.param_env,
597597
pred,
598598
);
@@ -1403,11 +1403,11 @@ pub(crate) fn raw_compare_const_impl<'tcx>(
14031403
);
14041404

14051405
// There is no "body" here, so just pass dummy id.
1406-
let impl_ty = ocx.normalize(cause.clone(), param_env, impl_ty);
1406+
let impl_ty = ocx.normalize(&cause, param_env, impl_ty);
14071407

14081408
debug!("compare_const_impl: impl_ty={:?}", impl_ty);
14091409

1410-
let trait_ty = ocx.normalize(cause.clone(), param_env, trait_ty);
1410+
let trait_ty = ocx.normalize(&cause, param_env, trait_ty);
14111411

14121412
debug!("compare_const_impl: trait_ty={:?}", trait_ty);
14131413

@@ -1556,7 +1556,7 @@ fn compare_type_predicate_entailment<'tcx>(
15561556
for (span, predicate) in std::iter::zip(impl_ty_own_bounds.spans, impl_ty_own_bounds.predicates)
15571557
{
15581558
let cause = ObligationCause::misc(span, impl_ty_hir_id);
1559-
let predicate = ocx.normalize(cause, param_env, predicate);
1559+
let predicate = ocx.normalize(&cause, param_env, predicate);
15601560

15611561
let cause = ObligationCause::new(
15621562
span,
@@ -1778,7 +1778,7 @@ pub fn check_type_bounds<'tcx>(
17781778

17791779
for mut obligation in util::elaborate_obligations(tcx, obligations) {
17801780
let normalized_predicate =
1781-
ocx.normalize(normalize_cause.clone(), normalize_param_env, obligation.predicate);
1781+
ocx.normalize(&normalize_cause, normalize_param_env, obligation.predicate);
17821782
debug!("compare_projection_bounds: normalized predicate = {:?}", normalized_predicate);
17831783
obligation.predicate = normalized_predicate;
17841784

compiler/rustc_hir_analysis/src/check/wfcheck.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,14 @@ 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>,
5961
{
6062
self.ocx.normalize(
61-
ObligationCause::new(span, self.body_id, ObligationCauseCode::WellFormed(loc)),
63+
&ObligationCause::new(span, self.body_id, ObligationCauseCode::WellFormed(loc)),
6264
self.param_env,
6365
value,
6466
)

compiler/rustc_hir_analysis/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,7 @@ fn check_main_fn_ty(tcx: TyCtxt<'_>, main_def_id: DefId) {
332332
ObligationCauseCode::MainFunctionType,
333333
);
334334
let ocx = traits::ObligationCtxt::new(&infcx);
335-
let norm_return_ty = ocx.normalize(cause.clone(), param_env, return_ty);
335+
let norm_return_ty = ocx.normalize(&cause, param_env, return_ty);
336336
ocx.register_bound(cause, param_env, norm_return_ty, term_did);
337337
let errors = ocx.select_all_or_error();
338338
if !errors.is_empty() {

compiler/rustc_hir_typeck/src/callee.rs

+1-1
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

+1-4
Original file line numberDiff line numberDiff line change
@@ -752,10 +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(
756-
self.expr_span,
757-
self.expr_ty.fn_sig(fcx.tcx),
758-
);
755+
let f = fcx.normalize(self.expr_span, self.expr_ty.fn_sig(fcx.tcx));
759756
let res = fcx.try_coerce(
760757
self.expr,
761758
self.expr_ty,

compiler/rustc_hir_typeck/src/check.rs

+8-13
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::coercion::CoerceMany;
22
use crate::gather_locals::GatherLocalsVisitor;
3-
use crate::{FnCtxt, Inherited};
3+
use crate::FnCtxt;
44
use crate::{GeneratorTypes, UnsafetyState};
55
use rustc_hir as hir;
66
use rustc_hir::def::DefKind;
@@ -20,21 +20,16 @@ use std::cell::RefCell;
2020
///
2121
/// * ...
2222
/// * inherited: other fields inherited from the enclosing fn (if any)
23-
#[instrument(skip(inherited, body), level = "debug")]
23+
#[instrument(skip(fcx, body), level = "debug")]
2424
pub(super) fn check_fn<'a, 'tcx>(
25-
inherited: &'a Inherited<'tcx>,
26-
param_env: ty::ParamEnv<'tcx>,
25+
fcx: &mut FnCtxt<'a, 'tcx>,
2726
fn_sig: ty::FnSig<'tcx>,
2827
decl: &'tcx hir::FnDecl<'tcx>,
2928
fn_def_id: LocalDefId,
3029
body: &'tcx hir::Body<'tcx>,
3130
can_be_generator: Option<hir::Movability>,
32-
) -> (FnCtxt<'a, 'tcx>, Option<GeneratorTypes<'tcx>>) {
33-
let fn_id = inherited.tcx.hir().local_def_id_to_hir_id(fn_def_id);
34-
35-
// Create the function context. This is either derived from scratch or,
36-
// in the case of closures, based on the outer context.
37-
let mut fcx = FnCtxt::new(inherited, param_env, body.value.hir_id);
31+
) -> Option<GeneratorTypes<'tcx>> {
32+
let fn_id = fcx.tcx.hir().local_def_id_to_hir_id(fn_def_id);
3833
fcx.ps.set(UnsafetyState::function(fn_sig.unsafety, fn_id));
3934

4035
let tcx = fcx.tcx;
@@ -47,7 +42,7 @@ pub(super) fn check_fn<'a, 'tcx>(
4742
declared_ret_ty,
4843
body.value.hir_id,
4944
decl.output.span(),
50-
param_env,
45+
fcx.param_env,
5146
));
5247

5348
fcx.ret_coercion = Some(RefCell::new(CoerceMany::new(ret_ty)));
@@ -105,7 +100,7 @@ pub(super) fn check_fn<'a, 'tcx>(
105100
fcx.write_ty(param.hir_id, param_ty);
106101
}
107102

108-
inherited.typeck_results.borrow_mut().liberated_fn_sigs_mut().insert(fn_id, fn_sig);
103+
fcx.typeck_results.borrow_mut().liberated_fn_sigs_mut().insert(fn_id, fn_sig);
109104

110105
if let ty::Dynamic(_, _, ty::Dyn) = declared_ret_ty.kind() {
111106
// FIXME: We need to verify that the return type is `Sized` after the return expression has
@@ -174,7 +169,7 @@ pub(super) fn check_fn<'a, 'tcx>(
174169
check_panic_info_fn(tcx, panic_impl_did.expect_local(), fn_sig, decl, declared_ret_ty);
175170
}
176171

177-
(fcx, gen_ty)
172+
gen_ty
178173
}
179174

180175
fn check_panic_info_fn(

compiler/rustc_hir_typeck/src/closure.rs

+6-12
Original file line numberDiff line numberDiff line change
@@ -79,16 +79,15 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
7979

8080
debug!(?bound_sig, ?liberated_sig);
8181

82+
let mut fcx = FnCtxt::new(self, self.param_env.without_const(), body.value.hir_id);
8283
let generator_types = check_fn(
83-
self,
84-
self.param_env.without_const(),
84+
&mut fcx,
8585
liberated_sig,
8686
closure.fn_decl,
8787
expr_def_id,
8888
body,
8989
closure.movability,
90-
)
91-
.1;
90+
);
9291

9392
let parent_substs = InternalSubsts::identity_for_item(
9493
self.tcx,
@@ -214,7 +213,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
214213
if expected_sig.is_none()
215214
&& let ty::PredicateKind::Clause(ty::Clause::Projection(proj_predicate)) = bound_predicate.skip_binder()
216215
{
217-
expected_sig = self.normalize_associated_types_in(
216+
expected_sig = self.normalize(
218217
obligation.cause.span,
219218
self.deduce_sig_from_projection(
220219
Some(obligation.cause.span),
@@ -623,7 +622,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
623622
);
624623
// Astconv can't normalize inputs or outputs with escaping bound vars,
625624
// 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);
625+
let result = self.normalize(self.tcx.hir().span(hir_id), result);
627626

628627
let c_result = self.inh.infcx.canonicalize_response(result);
629628
self.typeck_results.borrow_mut().user_provided_sigs.insert(expr_def_id, c_result);
@@ -797,12 +796,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
797796
) -> ClosureSignatures<'tcx> {
798797
let liberated_sig =
799798
self.tcx().liberate_late_bound_regions(expr_def_id.to_def_id(), bound_sig);
800-
let liberated_sig = self.inh.normalize_associated_types_in(
801-
body.value.span,
802-
self.tcx.hir().local_def_id_to_hir_id(expr_def_id),
803-
self.param_env,
804-
liberated_sig,
805-
);
799+
let liberated_sig = self.normalize(body.value.span, liberated_sig);
806800
ClosureSignatures { bound_sig, liberated_sig }
807801
}
808802
}

compiler/rustc_hir_typeck/src/coercion.rs

+6-5
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,9 @@ use rustc_span::{self, BytePos, DesugaringKind, Span};
6262
use rustc_target::spec::abi::Abi;
6363
use rustc_trait_selection::infer::InferCtxtExt as _;
6464
use rustc_trait_selection::traits::error_reporting::TypeErrCtxtExt as _;
65-
use rustc_trait_selection::traits::{self, ObligationCause, ObligationCauseCode, ObligationCtxt};
65+
use rustc_trait_selection::traits::{
66+
self, NormalizeExt, ObligationCause, ObligationCauseCode, ObligationCtxt,
67+
};
6668

6769
use smallvec::{smallvec, SmallVec};
6870
use std::ops::Deref;
@@ -832,7 +834,7 @@ impl<'f, 'tcx> Coerce<'f, 'tcx> {
832834
833835
let b = self.shallow_resolve(b);
834836
let InferOk { value: b, mut obligations } =
835-
self.normalize_associated_types_in_as_infer_ok(self.cause.span, b);
837+
self.at(&self.cause, self.param_env).normalize(b);
836838
debug!("coerce_from_fn_item(a={:?}, b={:?})", a, b);
837839

838840
match b.kind() {
@@ -854,7 +856,7 @@ impl<'f, 'tcx> Coerce<'f, 'tcx> {
854856
}
855857

856858
let InferOk { value: a_sig, obligations: o1 } =
857-
self.normalize_associated_types_in_as_infer_ok(self.cause.span, a_sig);
859+
self.at(&self.cause, self.param_env).normalize(a_sig);
858860
obligations.extend(o1);
859861

860862
let a_fn_pointer = self.tcx.mk_fn_ptr(a_sig);
@@ -1141,8 +1143,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
11411143
return Err(TypeError::IntrinsicCast);
11421144
}
11431145
// 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);
1146+
let (a_sig, b_sig) = self.normalize(new.span, (a_sig, b_sig));
11461147
let sig = self
11471148
.at(cause, self.param_env)
11481149
.trace(prev_ty, new_ty)

compiler/rustc_hir_typeck/src/expr.rs

+2-4
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
);
@@ -1748,9 +1748,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
17481748
ty::Adt(adt, substs) if adt.is_struct() => variant
17491749
.fields
17501750
.iter()
1751-
.map(|f| {
1752-
self.normalize_associated_types_in(expr_span, f.ty(self.tcx, substs))
1753-
})
1751+
.map(|f| self.normalize(expr_span, f.ty(self.tcx, substs)))
17541752
.collect(),
17551753
_ => {
17561754
self.tcx

0 commit comments

Comments
 (0)