Skip to content

Commit 01ccde5

Browse files
authored
Rollup merge of #100095 - jackh726:early-binder, r=lcnr
More EarlyBinder cleanups Each commit is independent r? types
2 parents 6b938c8 + 955fcad commit 01ccde5

File tree

14 files changed

+94
-68
lines changed

14 files changed

+94
-68
lines changed

compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs

+16-16
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,7 @@ use rustc_middle::mir::{
1616
FakeReadCause, LocalDecl, LocalInfo, LocalKind, Location, Operand, Place, PlaceRef,
1717
ProjectionElem, Rvalue, Statement, StatementKind, Terminator, TerminatorKind, VarBindingForm,
1818
};
19-
use rustc_middle::ty::{
20-
self, subst::Subst, suggest_constraining_type_params, EarlyBinder, PredicateKind, Ty,
21-
};
19+
use rustc_middle::ty::{self, subst::Subst, suggest_constraining_type_params, PredicateKind, Ty};
2220
use rustc_mir_dataflow::move_paths::{InitKind, MoveOutIndex, MovePathIndex};
2321
use rustc_span::def_id::LocalDefId;
2422
use rustc_span::hygiene::DesugaringKind;
@@ -461,35 +459,37 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
461459
let tcx = self.infcx.tcx;
462460

463461
// Find out if the predicates show that the type is a Fn or FnMut
464-
let find_fn_kind_from_did = |predicates: &[(ty::Predicate<'tcx>, Span)], substs| {
465-
predicates.iter().find_map(|(pred, _)| {
466-
let pred = if let Some(substs) = substs {
467-
EarlyBinder(*pred).subst(tcx, substs).kind().skip_binder()
468-
} else {
469-
pred.kind().skip_binder()
470-
};
471-
if let ty::PredicateKind::Trait(pred) = pred && pred.self_ty() == ty {
462+
let find_fn_kind_from_did =
463+
|predicates: ty::EarlyBinder<&[(ty::Predicate<'tcx>, Span)]>, substs| {
464+
predicates.0.iter().find_map(|(pred, _)| {
465+
let pred = if let Some(substs) = substs {
466+
predicates.rebind(*pred).subst(tcx, substs).kind().skip_binder()
467+
} else {
468+
pred.kind().skip_binder()
469+
};
470+
if let ty::PredicateKind::Trait(pred) = pred && pred.self_ty() == ty {
472471
if Some(pred.def_id()) == tcx.lang_items().fn_trait() {
473472
return Some(hir::Mutability::Not);
474473
} else if Some(pred.def_id()) == tcx.lang_items().fn_mut_trait() {
475474
return Some(hir::Mutability::Mut);
476475
}
477476
}
478-
None
479-
})
480-
};
477+
None
478+
})
479+
};
481480

482481
// If the type is opaque/param/closure, and it is Fn or FnMut, let's suggest (mutably)
483482
// borrowing the type, since `&mut F: FnMut` iff `F: FnMut` and similarly for `Fn`.
484483
// These types seem reasonably opaque enough that they could be substituted with their
485484
// borrowed variants in a function body when we see a move error.
486485
let borrow_level = match ty.kind() {
487486
ty::Param(_) => find_fn_kind_from_did(
488-
tcx.explicit_predicates_of(self.mir_def_id().to_def_id()).predicates,
487+
tcx.bound_explicit_predicates_of(self.mir_def_id().to_def_id())
488+
.map_bound(|p| p.predicates),
489489
None,
490490
),
491491
ty::Opaque(did, substs) => {
492-
find_fn_kind_from_did(tcx.explicit_item_bounds(*did), Some(*substs))
492+
find_fn_kind_from_did(tcx.bound_explicit_item_bounds(*did), Some(*substs))
493493
}
494494
ty::Closure(_, substs) => match substs.as_closure().kind() {
495495
ty::ClosureKind::Fn => Some(hir::Mutability::Not),

compiler/rustc_const_eval/src/const_eval/eval_queries.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use rustc_middle::mir::pretty::display_allocation;
1313
use rustc_middle::traits::Reveal;
1414
use rustc_middle::ty::layout::LayoutOf;
1515
use rustc_middle::ty::print::with_no_trimmed_paths;
16-
use rustc_middle::ty::{self, subst::Subst, EarlyBinder, TyCtxt};
16+
use rustc_middle::ty::{self, subst::Subst, TyCtxt};
1717
use rustc_span::source_map::Span;
1818
use rustc_target::abi::{self, Abi};
1919
use std::borrow::Cow;
@@ -45,7 +45,7 @@ fn eval_body_using_ecx<'mir, 'tcx>(
4545
"Unexpected DefKind: {:?}",
4646
ecx.tcx.def_kind(cid.instance.def_id())
4747
);
48-
let layout = ecx.layout_of(EarlyBinder(body.return_ty()).subst(tcx, cid.instance.substs))?;
48+
let layout = ecx.layout_of(body.bound_return_ty().subst(tcx, cid.instance.substs))?;
4949
assert!(!layout.is_unsized());
5050
let ret = ecx.allocate(layout, MemoryKind::Stack)?;
5151

compiler/rustc_middle/src/mir/mod.rs

+6
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,12 @@ impl<'tcx> Body<'tcx> {
431431
self.local_decls[RETURN_PLACE].ty
432432
}
433433

434+
/// Returns the return type; it always return first element from `local_decls` array.
435+
#[inline]
436+
pub fn bound_return_ty(&self) -> ty::EarlyBinder<Ty<'tcx>> {
437+
ty::EarlyBinder(self.local_decls[RETURN_PLACE].ty)
438+
}
439+
434440
/// Gets the location of the terminator for the given block.
435441
#[inline]
436442
pub fn terminator_loc(&self, bb: BasicBlock) -> Location {

compiler/rustc_middle/src/ty/adt.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -563,7 +563,7 @@ impl<'tcx> AdtDef<'tcx> {
563563
///
564564
/// Due to normalization being eager, this applies even if
565565
/// the associated type is behind a pointer (e.g., issue #31299).
566-
pub fn sized_constraint(self, tcx: TyCtxt<'tcx>) -> &'tcx [Ty<'tcx>] {
567-
tcx.adt_sized_constraint(self.did()).0
566+
pub fn sized_constraint(self, tcx: TyCtxt<'tcx>) -> ty::EarlyBinder<&'tcx [Ty<'tcx>]> {
567+
ty::EarlyBinder(tcx.adt_sized_constraint(self.did()).0)
568568
}
569569
}

compiler/rustc_middle/src/ty/sty.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2191,7 +2191,7 @@ impl<'tcx> Ty<'tcx> {
21912191

21922192
ty::Tuple(tys) => tys.iter().all(|ty| ty.is_trivially_sized(tcx)),
21932193

2194-
ty::Adt(def, _substs) => def.sized_constraint(tcx).is_empty(),
2194+
ty::Adt(def, _substs) => def.sized_constraint(tcx).0.is_empty(),
21952195

21962196
ty::Projection(_) | ty::Param(_) | ty::Opaque(..) => false,
21972197

compiler/rustc_middle/src/ty/util.rs

+18
Original file line numberDiff line numberDiff line change
@@ -680,6 +680,24 @@ impl<'tcx> TyCtxt<'tcx> {
680680
pub fn bound_const_param_default(self, def_id: DefId) -> ty::EarlyBinder<ty::Const<'tcx>> {
681681
ty::EarlyBinder(self.const_param_default(def_id))
682682
}
683+
684+
pub fn bound_predicates_of(
685+
self,
686+
def_id: DefId,
687+
) -> ty::EarlyBinder<ty::generics::GenericPredicates<'tcx>> {
688+
ty::EarlyBinder(self.predicates_of(def_id))
689+
}
690+
691+
pub fn bound_explicit_predicates_of(
692+
self,
693+
def_id: DefId,
694+
) -> ty::EarlyBinder<ty::generics::GenericPredicates<'tcx>> {
695+
ty::EarlyBinder(self.explicit_predicates_of(def_id))
696+
}
697+
698+
pub fn bound_impl_subject(self, def_id: DefId) -> ty::EarlyBinder<ty::ImplSubject<'tcx>> {
699+
ty::EarlyBinder(self.impl_subject(def_id))
700+
}
683701
}
684702

685703
struct OpaqueTypeExpander<'tcx> {

compiler/rustc_mir_transform/src/const_prop.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,7 @@ use rustc_middle::mir::{
1818
};
1919
use rustc_middle::ty::layout::{LayoutError, LayoutOf, LayoutOfHelpers, TyAndLayout};
2020
use rustc_middle::ty::subst::{InternalSubsts, Subst};
21-
use rustc_middle::ty::{
22-
self, ConstKind, EarlyBinder, Instance, ParamEnv, Ty, TyCtxt, TypeVisitable,
23-
};
21+
use rustc_middle::ty::{self, ConstKind, Instance, ParamEnv, Ty, TyCtxt, TypeVisitable};
2422
use rustc_span::{def_id::DefId, Span};
2523
use rustc_target::abi::{self, HasDataLayout, Size, TargetDataLayout};
2624
use rustc_target::spec::abi::Abi as CallAbi;
@@ -387,7 +385,7 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
387385
);
388386

389387
let ret_layout = ecx
390-
.layout_of(EarlyBinder(body.return_ty()).subst(tcx, substs))
388+
.layout_of(body.bound_return_ty().subst(tcx, substs))
391389
.ok()
392390
// Don't bother allocating memory for large values.
393391
// I don't know how return types can seem to be unsized but this happens in the

compiler/rustc_mir_transform/src/const_prop_lint.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,7 @@ use rustc_middle::mir::{
2323
use rustc_middle::ty::layout::{LayoutError, LayoutOf, LayoutOfHelpers, TyAndLayout};
2424
use rustc_middle::ty::subst::{InternalSubsts, Subst};
2525
use rustc_middle::ty::{
26-
self, ConstInt, ConstKind, EarlyBinder, Instance, ParamEnv, ScalarInt, Ty, TyCtxt,
27-
TypeVisitable,
26+
self, ConstInt, ConstKind, Instance, ParamEnv, ScalarInt, Ty, TyCtxt, TypeVisitable,
2827
};
2928
use rustc_session::lint;
3029
use rustc_span::Span;
@@ -196,7 +195,7 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
196195
);
197196

198197
let ret_layout = ecx
199-
.layout_of(EarlyBinder(body.return_ty()).subst(tcx, substs))
198+
.layout_of(body.bound_return_ty().subst(tcx, substs))
200199
.ok()
201200
// Don't bother allocating memory for large values.
202201
// I don't know how return types can seem to be unsized but this happens in the

compiler/rustc_trait_selection/src/traits/project.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ use rustc_middle::traits::select::OverflowError;
3232
use rustc_middle::ty::fold::{TypeFoldable, TypeFolder, TypeSuperFoldable};
3333
use rustc_middle::ty::subst::Subst;
3434
use rustc_middle::ty::visit::{MaxUniverse, TypeVisitable};
35-
use rustc_middle::ty::{self, EarlyBinder, Term, ToPredicate, Ty, TyCtxt};
35+
use rustc_middle::ty::{self, Term, ToPredicate, Ty, TyCtxt};
3636
use rustc_span::symbol::sym;
3737

3838
use std::collections::BTreeMap;
@@ -2005,16 +2005,16 @@ fn confirm_impl_candidate<'cx, 'tcx>(
20052005
let substs = obligation.predicate.substs.rebase_onto(tcx, trait_def_id, substs);
20062006
let substs =
20072007
translate_substs(selcx.infcx(), param_env, impl_def_id, substs, assoc_ty.defining_node);
2008-
let ty = tcx.type_of(assoc_ty.item.def_id);
2008+
let ty = tcx.bound_type_of(assoc_ty.item.def_id);
20092009
let is_const = matches!(tcx.def_kind(assoc_ty.item.def_id), DefKind::AssocConst);
2010-
let term: ty::Term<'tcx> = if is_const {
2010+
let term: ty::EarlyBinder<ty::Term<'tcx>> = if is_const {
20112011
let identity_substs =
20122012
crate::traits::InternalSubsts::identity_for_item(tcx, assoc_ty.item.def_id);
20132013
let did = ty::WithOptConstParam::unknown(assoc_ty.item.def_id);
20142014
let kind = ty::ConstKind::Unevaluated(ty::Unevaluated::new(did, identity_substs));
2015-
tcx.mk_const(ty::ConstS { ty, kind }).into()
2015+
ty.map_bound(|ty| tcx.mk_const(ty::ConstS { ty, kind }).into())
20162016
} else {
2017-
ty.into()
2017+
ty.map_bound(|ty| ty.into())
20182018
};
20192019
if substs.len() != tcx.generics_of(assoc_ty.item.def_id).count() {
20202020
let err = tcx.ty_error_with_message(
@@ -2024,7 +2024,7 @@ fn confirm_impl_candidate<'cx, 'tcx>(
20242024
Progress { term: err.into(), obligations: nested }
20252025
} else {
20262026
assoc_ty_own_obligations(selcx, obligation, &mut nested);
2027-
Progress { term: EarlyBinder(term).subst(tcx, substs), obligations: nested }
2027+
Progress { term: term.subst(tcx, substs), obligations: nested }
20282028
}
20292029
}
20302030

compiler/rustc_trait_selection/src/traits/select/mod.rs

+10-6
Original file line numberDiff line numberDiff line change
@@ -1886,7 +1886,11 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
18861886
let sized_crit = def.sized_constraint(self.tcx());
18871887
// (*) binder moved here
18881888
Where(obligation.predicate.rebind({
1889-
sized_crit.iter().map(|ty| EarlyBinder(*ty).subst(self.tcx(), substs)).collect()
1889+
sized_crit
1890+
.0
1891+
.iter()
1892+
.map(|ty| sized_crit.rebind(*ty).subst(self.tcx(), substs))
1893+
.collect()
18901894
}))
18911895
}
18921896

@@ -2357,11 +2361,11 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
23572361
// obligation will normalize to `<$0 as Iterator>::Item = $1` and
23582362
// `$1: Copy`, so we must ensure the obligations are emitted in
23592363
// that order.
2360-
let predicates = tcx.predicates_of(def_id);
2364+
let predicates = tcx.bound_predicates_of(def_id);
23612365
debug!(?predicates);
2362-
assert_eq!(predicates.parent, None);
2363-
let mut obligations = Vec::with_capacity(predicates.predicates.len());
2364-
for (predicate, span) in predicates.predicates {
2366+
assert_eq!(predicates.0.parent, None);
2367+
let mut obligations = Vec::with_capacity(predicates.0.predicates.len());
2368+
for (predicate, span) in predicates.0.predicates {
23652369
let span = *span;
23662370
let cause = cause.clone().derived_cause(parent_trait_pred, |derived| {
23672371
ImplDerivedObligation(Box::new(ImplDerivedObligationCause {
@@ -2375,7 +2379,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
23752379
param_env,
23762380
cause.clone(),
23772381
recursion_depth,
2378-
EarlyBinder(*predicate).subst(tcx, substs),
2382+
predicates.rebind(*predicate).subst(tcx, substs),
23792383
&mut obligations,
23802384
);
23812385
obligations.push(Obligation { cause, recursion_depth, param_env, predicate });

compiler/rustc_trait_selection/src/traits/util.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use smallvec::SmallVec;
66
use rustc_data_structures::fx::FxHashSet;
77
use rustc_hir::def_id::DefId;
88
use rustc_middle::ty::subst::{GenericArg, Subst, SubstsRef};
9-
use rustc_middle::ty::{self, EarlyBinder, ImplSubject, ToPredicate, Ty, TyCtxt, TypeVisitable};
9+
use rustc_middle::ty::{self, ImplSubject, ToPredicate, Ty, TyCtxt, TypeVisitable};
1010

1111
use super::{Normalized, Obligation, ObligationCause, PredicateObligation, SelectionContext};
1212
pub use rustc_infer::traits::{self, util::*};
@@ -200,8 +200,8 @@ pub fn impl_subject_and_oblig<'a, 'tcx>(
200200
impl_def_id: DefId,
201201
impl_substs: SubstsRef<'tcx>,
202202
) -> (ImplSubject<'tcx>, impl Iterator<Item = PredicateObligation<'tcx>>) {
203-
let subject = selcx.tcx().impl_subject(impl_def_id);
204-
let subject = EarlyBinder(subject).subst(selcx.tcx(), impl_substs);
203+
let subject = selcx.tcx().bound_impl_subject(impl_def_id);
204+
let subject = subject.subst(selcx.tcx(), impl_substs);
205205
let Normalized { value: subject, obligations: normalization_obligations1 } =
206206
super::normalize(selcx, param_env, ObligationCause::dummy(), subject);
207207

compiler/rustc_traits/src/chalk/db.rs

+16-15
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,11 @@ impl<'tcx> RustIrDatabase<'tcx> {
5151
where
5252
ty::Predicate<'tcx>: LowerInto<'tcx, std::option::Option<T>>,
5353
{
54-
self.interner
55-
.tcx
56-
.explicit_item_bounds(def_id)
54+
let bounds = self.interner.tcx.bound_explicit_item_bounds(def_id);
55+
bounds
56+
.0
5757
.iter()
58-
.map(|(bound, _)| EarlyBinder(*bound).subst(self.interner.tcx, &bound_vars))
58+
.map(|(bound, _)| bounds.rebind(*bound).subst(self.interner.tcx, &bound_vars))
5959
.filter_map(|bound| LowerInto::<Option<_>>::lower_into(bound, self.interner))
6060
.collect()
6161
}
@@ -268,21 +268,20 @@ impl<'tcx> chalk_solve::RustIrDatabase<RustInterner<'tcx>> for RustIrDatabase<'t
268268

269269
let where_clauses = self.where_clauses_for(def_id, bound_vars);
270270

271-
let sig = self.interner.tcx.fn_sig(def_id);
271+
let sig = self.interner.tcx.bound_fn_sig(def_id);
272272
let (inputs_and_output, iobinders, _) = crate::chalk::lowering::collect_bound_vars(
273273
self.interner,
274274
self.interner.tcx,
275-
EarlyBinder(sig.inputs_and_output()).subst(self.interner.tcx, bound_vars),
275+
sig.map_bound(|s| s.inputs_and_output()).subst(self.interner.tcx, bound_vars),
276276
);
277277

278278
let argument_types = inputs_and_output[..inputs_and_output.len() - 1]
279279
.iter()
280-
.map(|t| {
281-
EarlyBinder(*t).subst(self.interner.tcx, &bound_vars).lower_into(self.interner)
282-
})
280+
.map(|t| sig.rebind(*t).subst(self.interner.tcx, &bound_vars).lower_into(self.interner))
283281
.collect();
284282

285-
let return_type = EarlyBinder(inputs_and_output[inputs_and_output.len() - 1])
283+
let return_type = sig
284+
.rebind(inputs_and_output[inputs_and_output.len() - 1])
286285
.subst(self.interner.tcx, &bound_vars)
287286
.lower_into(self.interner);
288287

@@ -295,7 +294,7 @@ impl<'tcx> chalk_solve::RustIrDatabase<RustInterner<'tcx>> for RustIrDatabase<'t
295294
};
296295
Arc::new(chalk_solve::rust_ir::FnDefDatum {
297296
id: fn_def_id,
298-
sig: sig.lower_into(self.interner),
297+
sig: sig.0.lower_into(self.interner),
299298
binders: chalk_ir::Binders::new(binders, bound),
300299
})
301300
}
@@ -503,12 +502,14 @@ impl<'tcx> chalk_solve::RustIrDatabase<RustInterner<'tcx>> for RustIrDatabase<'t
503502

504503
let identity_substs = InternalSubsts::identity_for_item(self.interner.tcx, opaque_ty_id.0);
505504

505+
let explicit_item_bounds = self.interner.tcx.bound_explicit_item_bounds(opaque_ty_id.0);
506506
let bounds =
507-
self.interner
508-
.tcx
509-
.explicit_item_bounds(opaque_ty_id.0)
507+
explicit_item_bounds
508+
.0
510509
.iter()
511-
.map(|(bound, _)| EarlyBinder(*bound).subst(self.interner.tcx, &bound_vars))
510+
.map(|(bound, _)| {
511+
explicit_item_bounds.rebind(*bound).subst(self.interner.tcx, &bound_vars)
512+
})
512513
.map(|bound| {
513514
bound.fold_with(&mut ReplaceOpaqueTyFolder {
514515
tcx: self.interner.tcx,

compiler/rustc_ty_utils/src/ty.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@ use rustc_data_structures::fx::FxIndexSet;
22
use rustc_hir as hir;
33
use rustc_hir::def_id::DefId;
44
use rustc_middle::ty::subst::Subst;
5-
use rustc_middle::ty::{
6-
self, Binder, EarlyBinder, Predicate, PredicateKind, ToPredicate, Ty, TyCtxt,
7-
};
5+
use rustc_middle::ty::{self, Binder, Predicate, PredicateKind, ToPredicate, Ty, TyCtxt};
86
use rustc_trait_selection::traits;
97

108
fn sized_constraint_for_ty<'tcx>(
@@ -33,8 +31,9 @@ fn sized_constraint_for_ty<'tcx>(
3331
let adt_tys = adt.sized_constraint(tcx);
3432
debug!("sized_constraint_for_ty({:?}) intermediate = {:?}", ty, adt_tys);
3533
adt_tys
34+
.0
3635
.iter()
37-
.map(|ty| EarlyBinder(*ty).subst(tcx, substs))
36+
.map(|ty| adt_tys.rebind(*ty).subst(tcx, substs))
3837
.flat_map(|ty| sized_constraint_for_ty(tcx, adtdef, ty))
3938
.collect()
4039
}

0 commit comments

Comments
 (0)