Skip to content

Commit f257f35

Browse files
committed
Remove Binder::bind() and use Binder::dummy()
See [1] for motivation. In one place, it manually uses the `BoundVarsCollector` for now because we don't know what the bound variables are, and adding support for tracking the bound variables will probably be a tricky change. [1]: #76814 (comment)
1 parent 18db83f commit f257f35

File tree

10 files changed

+28
-31
lines changed

10 files changed

+28
-31
lines changed

compiler/rustc_middle/src/ty/sty.rs

-8
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
use self::TyKind::*;
66

77
use crate::infer::canonical::Canonical;
8-
use crate::ty::fold::BoundVarsCollector;
98
use crate::ty::fold::ValidateBoundVars;
109
use crate::ty::subst::{GenericArg, InternalSubsts, Subst, SubstsRef};
1110
use crate::ty::InferTy::{self, *};
@@ -970,13 +969,6 @@ where
970969
Binder(value, ty::List::empty())
971970
}
972971

973-
/// Wraps `value` in a binder, binding higher-ranked vars (if any).
974-
pub fn bind(value: T, tcx: TyCtxt<'tcx>) -> Binder<'tcx, T> {
975-
let mut collector = BoundVarsCollector::new();
976-
value.visit_with(&mut collector);
977-
Binder(value, collector.into_vars(tcx))
978-
}
979-
980972
pub fn bind_with_vars(value: T, vars: &'tcx List<BoundVariableKind>) -> Binder<'tcx, T> {
981973
if cfg!(debug_assertions) {
982974
let mut validator = ValidateBoundVars::new(vars);

compiler/rustc_mir/src/transform/check_consts/validation.rs

+3-6
Original file line numberDiff line numberDiff line change
@@ -822,12 +822,9 @@ impl Visitor<'tcx> for Validator<'mir, 'tcx> {
822822
let obligation = Obligation::new(
823823
ObligationCause::dummy(),
824824
param_env,
825-
Binder::bind(
826-
TraitPredicate {
827-
trait_ref: TraitRef::from_method(tcx, trait_id, substs),
828-
},
829-
tcx,
830-
),
825+
Binder::dummy(TraitPredicate {
826+
trait_ref: TraitRef::from_method(tcx, trait_id, substs),
827+
}),
831828
);
832829

833830
let implsrc = tcx.infer_ctxt().enter(|infcx| {

compiler/rustc_trait_selection/src/traits/project.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1301,7 +1301,7 @@ fn confirm_pointee_candidate<'cx, 'tcx>(
13011301
ty: self_ty.ptr_metadata_ty(tcx),
13021302
};
13031303

1304-
confirm_param_env_candidate(selcx, obligation, ty::Binder::bind(predicate, tcx), false)
1304+
confirm_param_env_candidate(selcx, obligation, ty::Binder::dummy(predicate), false)
13051305
}
13061306

13071307
fn confirm_fn_pointer_candidate<'cx, 'tcx>(

compiler/rustc_ty_utils/src/instance.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use rustc_errors::ErrorReported;
22
use rustc_hir::def_id::{DefId, LocalDefId};
33
use rustc_infer::infer::TyCtxtInferExt;
4+
use rustc_middle::ty::fold::BoundVarsCollector;
45
use rustc_middle::ty::subst::SubstsRef;
56
use rustc_middle::ty::{self, Instance, TyCtxt, TypeFoldable};
67
use rustc_span::{sym, DUMMY_SP};
@@ -115,7 +116,12 @@ fn resolve_associated_item<'tcx>(
115116
);
116117

117118
let trait_ref = ty::TraitRef::from_method(tcx, trait_id, rcvr_substs);
118-
let vtbl = tcx.codegen_fulfill_obligation((param_env, ty::Binder::bind(trait_ref, tcx)))?;
119+
// FIXME: we should instead track bound variables from their origin,
120+
// rather than using the `BoundVarsCollector` (cc #83825)
121+
let mut bound_vars_collector = BoundVarsCollector::new();
122+
trait_ref.visit_with(&mut bound_vars_collector);
123+
let trait_binder = ty::Binder::bind_with_vars(trait_ref, bound_vars_collector.into_vars(tcx));
124+
let vtbl = tcx.codegen_fulfill_obligation((param_env, trait_binder))?;
119125

120126
// Now that we know which impl is being used, we can dispatch to
121127
// the actual function:

compiler/rustc_typeck/src/astconv/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1694,7 +1694,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
16941694
};
16951695

16961696
self.one_bound_for_assoc_type(
1697-
|| traits::supertraits(tcx, ty::Binder::bind(trait_ref, tcx)),
1697+
|| traits::supertraits(tcx, ty::Binder::dummy(trait_ref)),
16981698
|| "Self".to_string(),
16991699
assoc_ident,
17001700
span,

compiler/rustc_typeck/src/check/compare_method.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ fn compare_predicate_entailment<'tcx>(
225225
let (impl_m_own_bounds, _) = infcx.replace_bound_vars_with_fresh_vars(
226226
impl_m_span,
227227
infer::HigherRankedType,
228-
ty::Binder::bind(impl_m_own_bounds.predicates, tcx),
228+
ty::Binder::dummy(impl_m_own_bounds.predicates),
229229
);
230230
for predicate in impl_m_own_bounds {
231231
let traits::Normalized { value: predicate, obligations } =
@@ -258,14 +258,14 @@ fn compare_predicate_entailment<'tcx>(
258258
);
259259
let impl_sig =
260260
inh.normalize_associated_types_in(impl_m_span, impl_m_hir_id, param_env, impl_sig);
261-
let impl_fty = tcx.mk_fn_ptr(ty::Binder::bind(impl_sig, tcx));
261+
let impl_fty = tcx.mk_fn_ptr(ty::Binder::dummy(impl_sig));
262262
debug!("compare_impl_method: impl_fty={:?}", impl_fty);
263263

264264
let trait_sig = tcx.liberate_late_bound_regions(impl_m.def_id, tcx.fn_sig(trait_m.def_id));
265265
let trait_sig = trait_sig.subst(tcx, trait_to_placeholder_substs);
266266
let trait_sig =
267267
inh.normalize_associated_types_in(impl_m_span, impl_m_hir_id, param_env, trait_sig);
268-
let trait_fty = tcx.mk_fn_ptr(ty::Binder::bind(trait_sig, tcx));
268+
let trait_fty = tcx.mk_fn_ptr(ty::Binder::dummy(trait_sig));
269269

270270
debug!("compare_impl_method: trait_fty={:?}", trait_fty);
271271

compiler/rustc_typeck/src/check/method/confirm.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ impl<'a, 'tcx> ConfirmContext<'a, 'tcx> {
119119
// We won't add these if we encountered an illegal sized bound, so that we can use
120120
// a custom error in that case.
121121
if illegal_sized_bound.is_none() {
122-
let method_ty = self.tcx.mk_fn_ptr(ty::Binder::bind(method_sig, self.tcx));
122+
let method_ty = self.tcx.mk_fn_ptr(ty::Binder::dummy(method_sig));
123123
self.add_obligations(method_ty, all_substs, method_predicates);
124124
}
125125

compiler/rustc_typeck/src/check/method/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -404,7 +404,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
404404
obligations.extend(traits::predicates_for_generics(cause.clone(), self.param_env, bounds));
405405

406406
// Also add an obligation for the method type being well-formed.
407-
let method_ty = tcx.mk_fn_ptr(ty::Binder::bind(fn_sig, tcx));
407+
let method_ty = tcx.mk_fn_ptr(ty::Binder::dummy(fn_sig));
408408
debug!(
409409
"lookup_in_trait_adjusted: matched method method_ty={:?} obligation={:?}",
410410
method_ty, obligation

compiler/rustc_typeck/src/check/wfcheck.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -1087,14 +1087,13 @@ fn check_method_receiver<'fcx, 'tcx>(
10871087
debug!("check_method_receiver: sig={:?}", sig);
10881088

10891089
let self_ty = fcx.normalize_associated_types_in(span, self_ty);
1090-
let self_ty =
1091-
fcx.tcx.liberate_late_bound_regions(method.def_id, ty::Binder::bind(self_ty, fcx.tcx));
1090+
let self_ty = fcx.tcx.liberate_late_bound_regions(method.def_id, ty::Binder::dummy(self_ty));
10921091

10931092
let receiver_ty = sig.inputs()[0];
10941093

10951094
let receiver_ty = fcx.normalize_associated_types_in(span, receiver_ty);
10961095
let receiver_ty =
1097-
fcx.tcx.liberate_late_bound_regions(method.def_id, ty::Binder::bind(receiver_ty, fcx.tcx));
1096+
fcx.tcx.liberate_late_bound_regions(method.def_id, ty::Binder::dummy(receiver_ty));
10981097

10991098
if fcx.tcx.features().arbitrary_self_types {
11001099
if !receiver_is_valid(fcx, span, receiver_ty, self_ty, true) {

compiler/rustc_typeck/src/collect.rs

+9-6
Original file line numberDiff line numberDiff line change
@@ -1767,7 +1767,7 @@ fn fn_sig(tcx: TyCtxt<'_>, def_id: DefId) -> ty::PolyFnSig<'_> {
17671767
}
17681768
diag.emit();
17691769

1770-
ty::Binder::bind(fn_sig, tcx)
1770+
ty::Binder::dummy(fn_sig)
17711771
}
17721772
None => <dyn AstConv<'_>>::ty_of_fn(
17731773
&icx,
@@ -1811,10 +1811,13 @@ fn fn_sig(tcx: TyCtxt<'_>, def_id: DefId) -> ty::PolyFnSig<'_> {
18111811
let ty = tcx.type_of(tcx.hir().get_parent_did(hir_id).to_def_id());
18121812
let inputs =
18131813
data.fields().iter().map(|f| tcx.type_of(tcx.hir().local_def_id(f.hir_id)));
1814-
ty::Binder::bind(
1815-
tcx.mk_fn_sig(inputs, ty, false, hir::Unsafety::Normal, abi::Abi::Rust),
1816-
tcx,
1817-
)
1814+
ty::Binder::dummy(tcx.mk_fn_sig(
1815+
inputs,
1816+
ty,
1817+
false,
1818+
hir::Unsafety::Normal,
1819+
abi::Abi::Rust,
1820+
))
18181821
}
18191822

18201823
Expr(&hir::Expr { kind: hir::ExprKind::Closure(..), .. }) => {
@@ -2098,7 +2101,7 @@ fn gather_explicit_predicates_of(tcx: TyCtxt<'_>, def_id: DefId) -> ty::GenericP
20982101
param.bounds.iter().for_each(|bound| match bound {
20992102
hir::GenericBound::Outlives(lt) => {
21002103
let bound = <dyn AstConv<'_>>::ast_region_to_region(&icx, &lt, None);
2101-
let outlives = ty::Binder::bind(ty::OutlivesPredicate(region, bound), tcx);
2104+
let outlives = ty::Binder::dummy(ty::OutlivesPredicate(region, bound));
21022105
predicates.insert((outlives.to_predicate(tcx), lt.span));
21032106
}
21042107
_ => bug!(),

0 commit comments

Comments
 (0)