Auto merge of #119849 - lcnr:eagerly-instantiate-binders, r=compiler-… · rust-lang/rust@fd27e87 (original) (raw)

`@@ -33,6 +33,7 @@ use rustc_errors::{Diag, EmissionGuarantee};

`

33

33

`use rustc_hir as hir;

`

34

34

`use rustc_hir::def_id::DefId;

`

35

35

`use rustc_infer::infer::BoundRegionConversionTime;

`

``

36

`+

use rustc_infer::infer::BoundRegionConversionTime::HigherRankedType;

`

36

37

`use rustc_infer::infer::DefineOpaqueTypes;

`

37

38

`use rustc_infer::traits::TraitObligation;

`

38

39

`use rustc_middle::dep_graph::dep_kinds;

`

`@@ -42,7 +43,7 @@ use rustc_middle::ty::_match::MatchAgainstFreshVars;

`

42

43

`use rustc_middle::ty::abstract_const::NotConstEvaluatable;

`

43

44

`use rustc_middle::ty::relate::TypeRelation;

`

44

45

`use rustc_middle::ty::GenericArgsRef;

`

45

``

`-

use rustc_middle::ty::{self, PolyProjectionPredicate, ToPolyTraitRef, ToPredicate};

`

``

46

`+

use rustc_middle::ty::{self, PolyProjectionPredicate, ToPredicate};

`

46

47

`use rustc_middle::ty::{Ty, TyCtxt, TypeFoldable, TypeVisitableExt};

`

47

48

`use rustc_span::symbol::sym;

`

48

49

`use rustc_span::Symbol;

`

`@@ -1651,15 +1652,20 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {

`

1651

1652

`fn match_normalize_trait_ref(

`

1652

1653

`&mut self,

`

1653

1654

`obligation: &PolyTraitObligation<'tcx>,

`

1654

``

`-

trait_bound: ty::PolyTraitRef<'tcx>,

`

1655

1655

`placeholder_trait_ref: ty::TraitRef<'tcx>,

`

1656

``

`-

) -> Result<Option<ty::PolyTraitRef<'tcx>>, ()> {

`

``

1656

`+

trait_bound: ty::PolyTraitRef<'tcx>,

`

``

1657

`+

) -> Result<Option<ty::TraitRef<'tcx>>, ()> {

`

1657

1658

`debug_assert!(!placeholder_trait_ref.has_escaping_bound_vars());

`

1658

1659

`if placeholder_trait_ref.def_id != trait_bound.def_id() {

`

1659

1660

`// Avoid unnecessary normalization

`

1660

1661

`return Err(());

`

1661

1662

`}

`

1662

1663

``

``

1664

`+

let trait_bound = self.infcx.instantiate_binder_with_fresh_vars(

`

``

1665

`+

obligation.cause.span,

`

``

1666

`+

HigherRankedType,

`

``

1667

`+

trait_bound,

`

``

1668

`+

);

`

1663

1669

`let Normalized { value: trait_bound, obligations: _ } = ensure_sufficient_stack(|| {

`

1664

1670

`normalize_with_depth(

`

1665

1671

`self,

`

`@@ -1671,7 +1677,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {

`

1671

1677

`});

`

1672

1678

`self.infcx

`

1673

1679

`.at(&obligation.cause, obligation.param_env)

`

1674

``

`-

.sup(DefineOpaqueTypes::No, ty::Binder::dummy(placeholder_trait_ref), trait_bound)

`

``

1680

`+

.eq(DefineOpaqueTypes::No, placeholder_trait_ref, trait_bound)

`

1675

1681

`.map(|InferOk { obligations: _, value: () }| {

`

1676

1682

`// This method is called within a probe, so we can't have

`

1677

1683

`// inference variables and placeholders escape.

`

`@@ -1683,7 +1689,6 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {

`

1683

1689

`})

`

1684

1690

`.map_err(|_| ())

`

1685

1691

`}

`

1686

``

-

1687

1692

`fn where_clause_may_apply<'o>(

`

1688

1693

`&mut self,

`

1689

1694

`stack: &TraitObligationStack<'o, 'tcx>,

`

`@@ -1733,7 +1738,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {

`

1733

1738

`let is_match = self

`

1734

1739

`.infcx

`

1735

1740

`.at(&obligation.cause, obligation.param_env)

`

1736

``

`-

.sup(DefineOpaqueTypes::No, obligation.predicate, infer_projection)

`

``

1741

`+

.eq(DefineOpaqueTypes::No, obligation.predicate, infer_projection)

`

1737

1742

`.is_ok_and(|InferOk { obligations, value: () }| {

`

1738

1743

`self.evaluate_predicates_recursively(

`

1739

1744

`TraitObligationStackList::empty(&ProvisionalEvaluationCache::default()),

`

`@@ -2533,7 +2538,7 @@ impl<'tcx> SelectionContext<'_, 'tcx> {

`

2533

2538

` nested.extend(

`

2534

2539

`self.infcx

`

2535

2540

`.at(&obligation.cause, obligation.param_env)

`

2536

``

`-

.sup(

`

``

2541

`+

.eq(

`

2537

2542

`DefineOpaqueTypes::No,

`

2538

2543

` upcast_principal.map_bound(|trait_ref| {

`

2539

2544

` ty::ExistentialTraitRef::erase_self_ty(tcx, trait_ref)

`

`@@ -2571,7 +2576,7 @@ impl<'tcx> SelectionContext<'_, 'tcx> {

`

2571

2576

` nested.extend(

`

2572

2577

`self.infcx

`

2573

2578

`.at(&obligation.cause, obligation.param_env)

`

2574

``

`-

.sup(DefineOpaqueTypes::No, source_projection, target_projection)

`

``

2579

`+

.eq(DefineOpaqueTypes::No, source_projection, target_projection)

`

2575

2580

`.map_err(|_| SelectionError::Unimplemented)?

`

2576

2581

`.into_obligations(),

`

2577

2582

`);

`

`@@ -2615,9 +2620,15 @@ impl<'tcx> SelectionContext<'_, 'tcx> {

`

2615

2620

`obligation: &PolyTraitObligation<'tcx>,

`

2616

2621

`poly_trait_ref: ty::PolyTraitRef<'tcx>,

`

2617

2622

`) -> Result<Vec<PredicateObligation<'tcx>>, ()> {

`

``

2623

`+

let predicate = self.infcx.enter_forall_and_leak_universe(obligation.predicate);

`

``

2624

`+

let trait_ref = self.infcx.instantiate_binder_with_fresh_vars(

`

``

2625

`+

obligation.cause.span,

`

``

2626

`+

HigherRankedType,

`

``

2627

`+

poly_trait_ref,

`

``

2628

`+

);

`

2618

2629

`self.infcx

`

2619

2630

`.at(&obligation.cause, obligation.param_env)

`

2620

``

`-

.sup(DefineOpaqueTypes::No, obligation.predicate.to_poly_trait_ref(), poly_trait_ref)

`

``

2631

`+

.eq(DefineOpaqueTypes::No, predicate.trait_ref, trait_ref)

`

2621

2632

`.map(|InferOk { obligations, .. }| obligations)

`

2622

2633

`.map_err(|_| ())

`

2623

2634

`}

`