Skip to content

Commit 24606de

Browse files
committed
Auto merge of #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 #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

Lines changed: 4 additions & 4 deletions
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

Lines changed: 1 addition & 2 deletions
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

Lines changed: 2 additions & 2 deletions
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

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

Lines changed: 10 additions & 10 deletions
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

Lines changed: 3 additions & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

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 & 4 deletions
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

Lines changed: 8 additions & 13 deletions
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(

0 commit comments

Comments
 (0)