Skip to content

Commit 67995dd

Browse files
committed
Auto merge of #155083 - adwinwhite:introduce-unnormalized, r=<try>
Introduce `Unnormalized` wrapper
2 parents 7659cec + ef96c03 commit 67995dd

File tree

193 files changed

+1848
-932
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

193 files changed

+1848
-932
lines changed

compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs

Lines changed: 41 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -472,8 +472,13 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
472472
// The move occurred as one of the arguments to a function call. Is that
473473
// argument generic? `def_id` can't be a closure here, so using `fn_sig` is fine
474474
let arg_param = if self.infcx.tcx.def_kind(def_id).is_fn_like()
475-
&& let sig =
476-
self.infcx.tcx.fn_sig(def_id).instantiate_identity().skip_binder()
475+
&& let sig = self
476+
.infcx
477+
.tcx
478+
.fn_sig(def_id)
479+
.instantiate_identity()
480+
.skip_normalization()
481+
.skip_binder()
477482
&& let Some(arg_ty) = sig.inputs().get(pos + offset)
478483
&& let ty::Param(arg_param) = arg_ty.kind()
479484
{
@@ -685,7 +690,7 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
685690
place_span: Span,
686691
) -> Option<ty::Mutability> {
687692
let tcx = self.infcx.tcx;
688-
let sig = tcx.fn_sig(callee_did).instantiate_identity().skip_binder();
693+
let sig = tcx.fn_sig(callee_did).instantiate_identity().skip_normalization().skip_binder();
689694
let clauses = tcx.predicates_of(callee_did);
690695

691696
let generic_args = match call_expr.kind {
@@ -703,7 +708,7 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
703708

704709
// First, is there at least one method on one of `param`'s trait bounds?
705710
// This keeps us from suggesting borrowing the argument to `mem::drop`, e.g.
706-
if !clauses.instantiate_identity(tcx).predicates.iter().any(|clause| {
711+
if !clauses.instantiate_identity(tcx).skip_normalization().predicates.iter().any(|clause| {
707712
clause.as_trait_clause().is_some_and(|tc| {
708713
tc.self_ty().skip_binder().is_param(param.index)
709714
&& tc.polarity() == ty::PredicatePolarity::Positive
@@ -729,8 +734,10 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
729734
));
730735
let can_subst = |ty: Ty<'tcx>| {
731736
// Normalize before comparing to see through type aliases and projections.
732-
let old_ty = ty::EarlyBinder::bind(ty).instantiate(tcx, generic_args);
733-
let new_ty = ty::EarlyBinder::bind(ty).instantiate(tcx, new_args);
737+
let old_ty =
738+
ty::EarlyBinder::bind(ty).instantiate(tcx, generic_args).skip_normalization();
739+
let new_ty =
740+
ty::EarlyBinder::bind(ty).instantiate(tcx, new_args).skip_normalization();
734741
if let Ok(old_ty) = tcx.try_normalize_erasing_regions(
735742
self.infcx.typing_env(self.infcx.param_env),
736743
old_ty,
@@ -754,21 +761,23 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
754761
}
755762

756763
// Test the callee's predicates, substituting in `ref_ty` for the moved argument type.
757-
clauses.instantiate(tcx, new_args).predicates.iter().all(|&(mut clause)| {
758-
// Normalize before testing to see through type aliases and projections.
759-
if let Ok(normalized) = tcx.try_normalize_erasing_regions(
760-
self.infcx.typing_env(self.infcx.param_env),
761-
clause,
762-
) {
763-
clause = normalized;
764-
}
765-
self.infcx.predicate_must_hold_modulo_regions(&Obligation::new(
766-
tcx,
767-
ObligationCause::dummy(),
768-
self.infcx.param_env,
769-
clause,
770-
))
771-
})
764+
clauses.instantiate(tcx, new_args).skip_normalization().predicates.iter().all(
765+
|&(mut clause)| {
766+
// Normalize before testing to see through type aliases and projections.
767+
if let Ok(normalized) = tcx.try_normalize_erasing_regions(
768+
self.infcx.typing_env(self.infcx.param_env),
769+
clause,
770+
) {
771+
clause = normalized;
772+
}
773+
self.infcx.predicate_must_hold_modulo_regions(&Obligation::new(
774+
tcx,
775+
ObligationCause::dummy(),
776+
self.infcx.param_env,
777+
clause,
778+
))
779+
},
780+
)
772781
}) {
773782
let place_desc = if let Some(desc) = self.describe_place(moved_place) {
774783
format!("`{desc}`")
@@ -4153,11 +4162,20 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
41534162
if is_closure {
41544163
None
41554164
} else {
4156-
let ty = self.infcx.tcx.type_of(self.mir_def_id()).instantiate_identity();
4165+
let ty = self
4166+
.infcx
4167+
.tcx
4168+
.type_of(self.mir_def_id())
4169+
.instantiate_identity()
4170+
.skip_normalization();
41574171
match ty.kind() {
41584172
ty::FnDef(_, _) | ty::FnPtr(..) => self.annotate_fn_sig(
41594173
self.mir_def_id(),
4160-
self.infcx.tcx.fn_sig(self.mir_def_id()).instantiate_identity(),
4174+
self.infcx
4175+
.tcx
4176+
.fn_sig(self.mir_def_id())
4177+
.instantiate_identity()
4178+
.skip_normalization(),
41614179
),
41624180
_ => None,
41634181
}

compiler/rustc_borrowck/src/diagnostics/mod.rs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1366,9 +1366,16 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
13661366
let parent_self_ty =
13671367
matches!(tcx.def_kind(parent_did), rustc_hir::def::DefKind::Impl { .. })
13681368
.then_some(parent_did)
1369-
.and_then(|did| match tcx.type_of(did).instantiate_identity().kind() {
1370-
ty::Adt(def, ..) => Some(def.did()),
1371-
_ => None,
1369+
.and_then(|did| {
1370+
match tcx
1371+
.type_of(did)
1372+
.instantiate_identity()
1373+
.skip_normalization()
1374+
.kind()
1375+
{
1376+
ty::Adt(def, ..) => Some(def.did()),
1377+
_ => None,
1378+
}
13721379
});
13731380
let is_option_or_result = parent_self_ty.is_some_and(|def_id| {
13741381
matches!(tcx.get_diagnostic_name(def_id), Some(sym::Option | sym::Result))
@@ -1445,7 +1452,10 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
14451452
&& let self_ty = self.infcx.instantiate_binder_with_fresh_vars(
14461453
fn_call_span,
14471454
BoundRegionConversionTime::FnCall,
1448-
tcx.fn_sig(method_did).instantiate(tcx, method_args).input(0),
1455+
tcx.fn_sig(method_did)
1456+
.instantiate(tcx, method_args)
1457+
.skip_normalization()
1458+
.input(0),
14491459
)
14501460
&& self.infcx.can_eq(self.infcx.param_env, ty, self_ty)
14511461
{

compiler/rustc_borrowck/src/diagnostics/move_errors.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -627,12 +627,12 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
627627
hir::ExprKind::Call(callee, _) => {
628628
let ty = typeck_result.node_type_opt(callee.hir_id)?;
629629
let ty::FnDef(fn_def_id, args) = *ty.kind() else { return None };
630-
tcx.predicates_of(fn_def_id).instantiate(tcx, args)
630+
tcx.predicates_of(fn_def_id).instantiate(tcx, args).skip_normalization()
631631
}
632632
hir::ExprKind::MethodCall(..) => {
633633
let (_, method) = typeck_result.type_dependent_def(parent.hir_id)?;
634634
let args = typeck_result.node_args(parent.hir_id);
635-
tcx.predicates_of(method).instantiate(tcx, args)
635+
tcx.predicates_of(method).instantiate(tcx, args).skip_normalization()
636636
}
637637
_ => return None,
638638
};

compiler/rustc_borrowck/src/diagnostics/opaque_types.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use rustc_hir::def_id::DefId;
99
use rustc_middle::mir::{self, ConstraintCategory, Location};
1010
use rustc_middle::ty::{
1111
self, Ty, TyCtxt, TypeSuperVisitable, TypeVisitable, TypeVisitableExt, TypeVisitor,
12+
Unnormalized,
1213
};
1314
use rustc_span::Span;
1415
use rustc_trait_selection::error_reporting::infer::region::unexpected_hidden_region_diagnostic;
@@ -282,6 +283,7 @@ impl<'tcx> TypeVisitor<TyCtxt<'tcx>> for CheckExplicitRegionMentionAndCollectGen
282283
.tcx
283284
.explicit_item_bounds(def_id)
284285
.iter_instantiated_copied(self.tcx, opaque.args)
286+
.map(Unnormalized::skip_normalization)
285287
{
286288
bound.visit_with(self)?;
287289
}

compiler/rustc_borrowck/src/diagnostics/region_errors.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -595,7 +595,7 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
595595

596596
let mut output_ty = self.regioncx.universal_regions().unnormalized_output_ty;
597597
if let ty::Alias(ty::AliasTy { kind: ty::Opaque { def_id }, .. }) = *output_ty.kind() {
598-
output_ty = self.infcx.tcx.type_of(def_id).instantiate_identity()
598+
output_ty = self.infcx.tcx.type_of(def_id).instantiate_identity().skip_normalization()
599599
};
600600

601601
debug!("report_fnmut_error: output_ty={:?}", output_ty);
@@ -930,7 +930,7 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
930930
debug!(?fn_did, ?args);
931931

932932
// Only suggest this on function calls, not closures
933-
let ty = tcx.type_of(fn_did).instantiate_identity();
933+
let ty = tcx.type_of(fn_did).instantiate_identity().skip_normalization();
934934
debug!("ty: {:?}, ty.kind: {:?}", ty, ty.kind());
935935
if let ty::Closure(_, _) = ty.kind() {
936936
return;
@@ -1050,7 +1050,8 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
10501050
else {
10511051
return;
10521052
};
1053-
let ty::Closure(_, args) = *tcx.type_of(closure_def_id).instantiate_identity().kind()
1053+
let ty::Closure(_, args) =
1054+
*tcx.type_of(closure_def_id).instantiate_identity().skip_normalization().kind()
10541055
else {
10551056
return;
10561057
};
@@ -1143,7 +1144,7 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
11431144
}
11441145
});
11451146

1146-
let preds = tcx.predicates_of(method_def_id).instantiate(tcx, args);
1147+
let preds = tcx.predicates_of(method_def_id).instantiate(tcx, args).skip_normalization();
11471148

11481149
let ocx = ObligationCtxt::new(&self.infcx);
11491150
ocx.register_obligations(preds.iter().map(|(pred, span)| {

compiler/rustc_borrowck/src/diagnostics/region_name.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -418,7 +418,7 @@ impl<'tcx> MirBorrowckCtxt<'_, '_, 'tcx> {
418418

419419
// Get the parent fn's signature with liberated late-bound regions,
420420
// so we have `ReLateParam` instead of `ReBound`.
421-
let parent_fn_sig = tcx.fn_sig(parent_def_id).instantiate_identity();
421+
let parent_fn_sig = tcx.fn_sig(parent_def_id).instantiate_identity().skip_normalization();
422422
let liberated_sig = tcx.liberate_late_bound_regions(parent_def_id, parent_fn_sig);
423423
let parent_param_ty = *liberated_sig.inputs().get(param_index)?;
424424

@@ -1023,10 +1023,10 @@ impl<'tcx> MirBorrowckCtxt<'_, '_, 'tcx> {
10231023
return None;
10241024
};
10251025

1026-
let found = tcx
1027-
.any_free_region_meets(&tcx.type_of(region_parent).instantiate_identity(), |r| {
1028-
r.kind() == ty::ReEarlyParam(region)
1029-
});
1026+
let found = tcx.any_free_region_meets(
1027+
&tcx.type_of(region_parent).instantiate_identity().skip_normalization(),
1028+
|r| r.kind() == ty::ReEarlyParam(region),
1029+
);
10301030

10311031
Some(RegionName {
10321032
name: self.synthesize_region_name(),
@@ -1056,6 +1056,7 @@ impl<'tcx> MirBorrowckCtxt<'_, '_, 'tcx> {
10561056
.tcx
10571057
.predicates_of(self.body.source.def_id())
10581058
.instantiate_identity(self.infcx.tcx)
1059+
.skip_normalization()
10591060
.predicates;
10601061

10611062
if let Some(upvar_index) = self

compiler/rustc_borrowck/src/region_infer/opaque_types/mod.rs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -569,16 +569,17 @@ pub(crate) fn apply_definition_site_hidden_types<'tcx>(
569569
};
570570

571571
// We erase all non-member region of the opaque and need to treat these as existentials.
572-
let expected_ty =
573-
ty::fold_regions(tcx, expected.ty.instantiate(tcx, key.args), |re, _dbi| {
574-
match re.kind() {
575-
ty::ReErased => infcx.next_nll_region_var(
576-
NllRegionVariableOrigin::Existential { name: None },
577-
|| crate::RegionCtxt::Existential(None),
578-
),
579-
_ => re,
580-
}
581-
});
572+
let expected_ty = ty::fold_regions(
573+
tcx,
574+
expected.ty.instantiate(tcx, key.args).skip_normalization(),
575+
|re, _dbi| match re.kind() {
576+
ty::ReErased => infcx.next_nll_region_var(
577+
NllRegionVariableOrigin::Existential { name: None },
578+
|| crate::RegionCtxt::Existential(None),
579+
),
580+
_ => re,
581+
},
582+
);
582583

583584
// We now simply equate the expected with the actual hidden type.
584585
let locations = Locations::All(hidden_type.span);

compiler/rustc_borrowck/src/type_check/mod.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1759,7 +1759,8 @@ impl<'a, 'tcx> Visitor<'tcx> for TypeChecker<'a, 'tcx> {
17591759
);
17601760
}
17611761
} else if let Some(static_def_id) = constant.check_static_ptr(tcx) {
1762-
let unnormalized_ty = tcx.type_of(static_def_id).instantiate_identity();
1762+
let unnormalized_ty =
1763+
tcx.type_of(static_def_id).instantiate_identity().skip_normalization();
17631764
let normalized_ty = self.normalize(unnormalized_ty, locations);
17641765
let literal_ty = constant.const_.ty().builtin_deref(true).unwrap();
17651766

@@ -1787,7 +1788,8 @@ impl<'a, 'tcx> Visitor<'tcx> for TypeChecker<'a, 'tcx> {
17871788
}
17881789

17891790
if let ty::FnDef(def_id, args) = *constant.const_.ty().kind() {
1790-
let instantiated_predicates = tcx.predicates_of(def_id).instantiate(tcx, args);
1791+
let instantiated_predicates =
1792+
tcx.predicates_of(def_id).instantiate(tcx, args).skip_normalization();
17911793
self.normalize_and_prove_instantiated_predicates(
17921794
def_id,
17931795
instantiated_predicates,
@@ -2435,7 +2437,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
24352437

24362438
let (def_id, instantiated_predicates) = match *aggregate_kind {
24372439
AggregateKind::Adt(adt_did, _, args, _, _) => {
2438-
(adt_did, tcx.predicates_of(adt_did).instantiate(tcx, args))
2440+
(adt_did, tcx.predicates_of(adt_did).instantiate(tcx, args).skip_normalization())
24392441
}
24402442

24412443
// For closures, we have some **extra requirements** we
@@ -2521,7 +2523,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
25212523
);
25222524
}
25232525

2524-
tcx.predicates_of(def_id).instantiate(tcx, args)
2526+
tcx.predicates_of(def_id).instantiate(tcx, args).skip_normalization()
25252527
}
25262528
}
25272529

compiler/rustc_borrowck/src/universal_regions.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -585,7 +585,8 @@ impl<'cx, 'tcx> UniversalRegionsBuilder<'cx, 'tcx> {
585585

586586
match tcx.hir_body_owner_kind(self.mir_def) {
587587
BodyOwnerKind::Closure | BodyOwnerKind::Fn => {
588-
let defining_ty = tcx.type_of(self.mir_def).instantiate_identity();
588+
let defining_ty =
589+
tcx.type_of(self.mir_def).instantiate_identity().skip_normalization();
589590

590591
debug!("defining_ty (pre-replacement): {:?}", defining_ty);
591592

@@ -780,7 +781,7 @@ impl<'cx, 'tcx> UniversalRegionsBuilder<'cx, 'tcx> {
780781
}
781782

782783
DefiningTy::FnDef(def_id, _) => {
783-
let sig = tcx.fn_sig(def_id).instantiate_identity();
784+
let sig = tcx.fn_sig(def_id).instantiate_identity().skip_normalization();
784785
let sig = indices.fold_to_region_vids(tcx, sig);
785786
let inputs_and_output = sig.inputs_and_output();
786787

@@ -804,7 +805,8 @@ impl<'cx, 'tcx> UniversalRegionsBuilder<'cx, 'tcx> {
804805
.infcx
805806
.tcx
806807
.type_of(va_list_did)
807-
.instantiate(self.infcx.tcx, &[region.into()]);
808+
.instantiate(self.infcx.tcx, &[region.into()])
809+
.skip_normalization();
808810

809811
// The signature needs to follow the order [input_tys, va_list_ty, output_ty]
810812
return inputs_and_output.map_bound(|tys| {
@@ -822,7 +824,7 @@ impl<'cx, 'tcx> UniversalRegionsBuilder<'cx, 'tcx> {
822824
// For a constant body, there are no inputs, and one
823825
// "output" (the type of the constant).
824826
assert_eq!(self.mir_def.to_def_id(), def_id);
825-
let ty = tcx.type_of(self.mir_def).instantiate_identity();
827+
let ty = tcx.type_of(self.mir_def).instantiate_identity().skip_normalization();
826828

827829
let ty = indices.fold_to_region_vids(tcx, ty);
828830
ty::Binder::dummy(tcx.mk_type_list(&[ty]))
@@ -835,7 +837,9 @@ impl<'cx, 'tcx> UniversalRegionsBuilder<'cx, 'tcx> {
835837
}
836838

837839
DefiningTy::GlobalAsm(def_id) => {
838-
ty::Binder::dummy(tcx.mk_type_list(&[tcx.type_of(def_id).instantiate_identity()]))
840+
ty::Binder::dummy(tcx.mk_type_list(&[
841+
tcx.type_of(def_id).instantiate_identity().skip_normalization(),
842+
]))
839843
}
840844
};
841845

@@ -974,7 +978,7 @@ fn for_each_late_bound_region_in_item<'tcx>(
974978
// only deduced that a param in the closure signature is late-bound from a constraint
975979
// that we discover during typeck.
976980
DefKind::Closure => {
977-
let ty = tcx.type_of(mir_def_id).instantiate_identity();
981+
let ty = tcx.type_of(mir_def_id).instantiate_identity().skip_normalization();
978982
match *ty.kind() {
979983
ty::Closure(_, args) => args.as_closure().sig().bound_vars(),
980984
ty::CoroutineClosure(_, args) => {

compiler/rustc_codegen_llvm/src/intrinsic.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -824,9 +824,9 @@ impl<'ll, 'tcx> IntrinsicCallBuilderMethods<'tcx> for Builder<'_, 'll, 'tcx> {
824824

825825
let fn_ty = instance.ty(tcx, self.typing_env());
826826
let fn_sig = match *fn_ty.kind() {
827-
ty::FnDef(def_id, args) => {
828-
tcx.instantiate_bound_regions_with_erased(tcx.fn_sig(def_id).instantiate(tcx, args))
829-
}
827+
ty::FnDef(def_id, args) => tcx.instantiate_bound_regions_with_erased(
828+
tcx.fn_sig(def_id).instantiate(tcx, args).skip_normalization(),
829+
),
830830
_ => unreachable!(),
831831
};
832832
assert!(!fn_sig.c_variadic);

0 commit comments

Comments
 (0)