Skip to content

Commit 2a8a0fc

Browse files
committed
Auto merge of #96883 - jackh726:early-binder-2, r=oli-obk
Add EarlyBinder Chalk has no concept of `Param` (https://github.com/rust-lang/chalk/blob/e0ade19d139bc784384acc6736cd960c91dd55a1/chalk-ir/src/lib.rs#L579) or `ReEarlyBound` (https://github.com/rust-lang/chalk/blob/e0ade19d139bc784384acc6736cd960c91dd55a1/chalk-ir/src/lib.rs#L1308). Everything is just "bound" - the equivalent of rustc's late-bound. It's not completely clear yet whether to move everything to the same time of binder in rustc or add `Param` and `ReEarlyBound` in Chalk. Either way, tracking when we have or haven't already substituted out these in rustc can be helpful. As a first step, I'm just adding a `EarlyBinder` newtype that is required to call `subst`. I also add a couple "transparent" `bound_*` wrappers around a couple query that are often immediately substituted. r? `@nikomatsakis`
2 parents 70b3681 + 06a1e88 commit 2a8a0fc

File tree

67 files changed

+400
-217
lines changed

Some content is hidden

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

67 files changed

+400
-217
lines changed

compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ use rustc_middle::mir::{
1313
FakeReadCause, LocalDecl, LocalInfo, LocalKind, Location, Operand, Place, PlaceRef,
1414
ProjectionElem, Rvalue, Statement, StatementKind, Terminator, TerminatorKind, VarBindingForm,
1515
};
16-
use rustc_middle::ty::{self, subst::Subst, suggest_constraining_type_params, PredicateKind, Ty};
16+
use rustc_middle::ty::{
17+
self, subst::Subst, suggest_constraining_type_params, EarlyBinder, PredicateKind, Ty,
18+
};
1719
use rustc_mir_dataflow::move_paths::{InitKind, MoveOutIndex, MovePathIndex};
1820
use rustc_span::symbol::sym;
1921
use rustc_span::{BytePos, Span};
@@ -336,7 +338,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
336338
let find_fn_kind_from_did = |predicates: &[(ty::Predicate<'tcx>, Span)], substs| {
337339
predicates.iter().find_map(|(pred, _)| {
338340
let pred = if let Some(substs) = substs {
339-
pred.subst(tcx, substs).kind().skip_binder()
341+
EarlyBinder(*pred).subst(tcx, substs).kind().skip_binder()
340342
} else {
341343
pred.kind().skip_binder()
342344
};

compiler/rustc_borrowck/src/universal_regions.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -477,8 +477,11 @@ impl<'cx, 'tcx> UniversalRegionsBuilder<'cx, 'tcx> {
477477
.infcx
478478
.tcx
479479
.mk_region(ty::ReVar(self.infcx.next_nll_region_var(FR).to_region_vid()));
480-
let va_list_ty =
481-
self.infcx.tcx.type_of(va_list_did).subst(self.infcx.tcx, &[region.into()]);
480+
let va_list_ty = self
481+
.infcx
482+
.tcx
483+
.bound_type_of(va_list_did)
484+
.subst(self.infcx.tcx, &[region.into()]);
482485

483486
unnormalized_input_tys = self.infcx.tcx.mk_type_list(
484487
unnormalized_input_tys.iter().copied().chain(iter::once(va_list_ty)),

compiler/rustc_const_eval/src/const_eval/eval_queries.rs

Lines changed: 2 additions & 2 deletions
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, TyCtxt};
16+
use rustc_middle::ty::{self, subst::Subst, EarlyBinder, TyCtxt};
1717
use rustc_span::source_map::Span;
1818
use rustc_target::abi::{self, Abi};
1919
use std::borrow::Cow;
@@ -47,7 +47,7 @@ fn eval_body_using_ecx<'mir, 'tcx>(
4747
"Unexpected DefKind: {:?}",
4848
ecx.tcx.def_kind(cid.instance.def_id())
4949
);
50-
let layout = ecx.layout_of(body.return_ty().subst(tcx, cid.instance.substs))?;
50+
let layout = ecx.layout_of(EarlyBinder(body.return_ty()).subst(tcx, cid.instance.substs))?;
5151
assert!(!layout.is_unsized());
5252
let ret = ecx.allocate(layout, MemoryKind::Stack)?;
5353

compiler/rustc_const_eval/src/interpret/intrinsics/caller_location.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
9595
// Allocate memory for `CallerLocation` struct.
9696
let loc_ty = self
9797
.tcx
98-
.type_of(self.tcx.require_lang_item(LangItem::PanicLocation, None))
98+
.bound_type_of(self.tcx.require_lang_item(LangItem::PanicLocation, None))
9999
.subst(*self.tcx, self.tcx.mk_substs([self.tcx.lifetimes.re_erased.into()].iter()));
100100
let loc_layout = self.layout_of(loc_ty).unwrap();
101101
// This can fail if rustc runs out of memory right here. Trying to emit an error would be

compiler/rustc_infer/src/infer/error_reporting/mod.rs

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ use rustc_middle::ty::{
7070
self,
7171
error::TypeError,
7272
subst::{GenericArgKind, Subst, SubstsRef},
73-
Binder, List, Region, Ty, TyCtxt, TypeFoldable,
73+
Binder, EarlyBinder, List, Region, Ty, TyCtxt, TypeFoldable,
7474
};
7575
use rustc_span::{sym, BytePos, DesugaringKind, Pos, Span};
7676
use rustc_target::spec::abi;
@@ -961,12 +961,14 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
961961
for (def_id, actual) in iter::zip(default_params, substs.iter().rev()) {
962962
match actual.unpack() {
963963
GenericArgKind::Const(c) => {
964-
if self.tcx.const_param_default(def_id).subst(self.tcx, substs) != c {
964+
if EarlyBinder(self.tcx.const_param_default(def_id)).subst(self.tcx, substs)
965+
!= c
966+
{
965967
break;
966968
}
967969
}
968970
GenericArgKind::Type(ty) => {
969-
if self.tcx.type_of(def_id).subst(self.tcx, substs) != ty {
971+
if self.tcx.bound_type_of(def_id).subst(self.tcx, substs) != ty {
970972
break;
971973
}
972974
}
@@ -1383,8 +1385,8 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
13831385
}
13841386

13851387
(ty::FnDef(did1, substs1), ty::FnDef(did2, substs2)) => {
1386-
let sig1 = self.tcx.fn_sig(*did1).subst(self.tcx, substs1);
1387-
let sig2 = self.tcx.fn_sig(*did2).subst(self.tcx, substs2);
1388+
let sig1 = self.tcx.bound_fn_sig(*did1).subst(self.tcx, substs1);
1389+
let sig2 = self.tcx.bound_fn_sig(*did2).subst(self.tcx, substs2);
13881390
let mut values = self.cmp_fn_sig(&sig1, &sig2);
13891391
let path1 = format!(" {{{}}}", self.tcx.def_path_str_with_substs(*did1, substs1));
13901392
let path2 = format!(" {{{}}}", self.tcx.def_path_str_with_substs(*did2, substs2));
@@ -1395,7 +1397,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
13951397
}
13961398

13971399
(ty::FnDef(did1, substs1), ty::FnPtr(sig2)) => {
1398-
let sig1 = self.tcx.fn_sig(*did1).subst(self.tcx, substs1);
1400+
let sig1 = self.tcx.bound_fn_sig(*did1).subst(self.tcx, substs1);
13991401
let mut values = self.cmp_fn_sig(&sig1, sig2);
14001402
values.0.push_highlighted(format!(
14011403
" {{{}}}",
@@ -1405,7 +1407,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
14051407
}
14061408

14071409
(ty::FnPtr(sig1), ty::FnDef(did2, substs2)) => {
1408-
let sig2 = self.tcx.fn_sig(*did2).subst(self.tcx, substs2);
1410+
let sig2 = self.tcx.bound_fn_sig(*did2).subst(self.tcx, substs2);
14091411
let mut values = self.cmp_fn_sig(sig1, &sig2);
14101412
values.1.push_normal(format!(
14111413
" {{{}}}",
@@ -1847,9 +1849,9 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
18471849
// Future::Output
18481850
let item_def_id = self.tcx.associated_item_def_ids(future_trait)[0];
18491851

1850-
let bounds = self.tcx.explicit_item_bounds(*def_id);
1852+
let bounds = self.tcx.bound_explicit_item_bounds(*def_id);
18511853

1852-
for (predicate, _) in bounds {
1854+
for predicate in bounds.transpose_iter().map(|e| e.map_bound(|(p, _)| *p)) {
18531855
let predicate = predicate.subst(self.tcx, substs);
18541856
let output = predicate
18551857
.kind()

compiler/rustc_infer/src/infer/opaque_types.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -561,9 +561,9 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
561561
obligations = self.at(&cause, param_env).eq(prev, hidden_ty)?.obligations;
562562
}
563563

564-
let item_bounds = tcx.explicit_item_bounds(def_id);
564+
let item_bounds = tcx.bound_explicit_item_bounds(def_id);
565565

566-
for (predicate, _) in item_bounds {
566+
for predicate in item_bounds.transpose_iter().map(|e| e.map_bound(|(p, _)| *p)) {
567567
debug!(?predicate);
568568
let predicate = predicate.subst(tcx, substs);
569569

compiler/rustc_infer/src/infer/outlives/verify.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use rustc_data_structures::captures::Captures;
44
use rustc_data_structures::sso::SsoHashSet;
55
use rustc_hir::def_id::DefId;
66
use rustc_middle::ty::subst::{GenericArg, GenericArgKind, Subst};
7-
use rustc_middle::ty::{self, Ty, TyCtxt};
7+
use rustc_middle::ty::{self, EarlyBinder, Ty, TyCtxt};
88

99
/// The `TypeOutlives` struct has the job of "lowering" a `T: 'a`
1010
/// obligation into a series of `'a: 'b` constraints and "verifys", as
@@ -290,7 +290,7 @@ impl<'cx, 'tcx> VerifyBoundCx<'cx, 'tcx> {
290290
debug!("projection_bounds(projection_ty={:?})", projection_ty);
291291
let tcx = self.tcx;
292292
self.region_bounds_declared_on_associated_item(projection_ty.item_def_id)
293-
.map(move |r| r.subst(tcx, projection_ty.substs))
293+
.map(move |r| EarlyBinder(r).subst(tcx, projection_ty.substs))
294294
}
295295

296296
/// Given the `DefId` of an associated item, returns any region

compiler/rustc_middle/src/mir/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2393,7 +2393,7 @@ impl<'tcx> Operand<'tcx> {
23932393
substs: SubstsRef<'tcx>,
23942394
span: Span,
23952395
) -> Self {
2396-
let ty = tcx.type_of(def_id).subst(tcx, substs);
2396+
let ty = tcx.bound_type_of(def_id).subst(tcx, substs);
23972397
Operand::Constant(Box::new(Constant {
23982398
span,
23992399
user_ty: None,

compiler/rustc_middle/src/mir/tcx.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,9 @@ impl<'tcx> Rvalue<'tcx> {
202202
Rvalue::Aggregate(ref ak, ref ops) => match **ak {
203203
AggregateKind::Array(ty) => tcx.mk_array(ty, ops.len() as u64),
204204
AggregateKind::Tuple => tcx.mk_tup(ops.iter().map(|op| op.ty(local_decls, tcx))),
205-
AggregateKind::Adt(did, _, substs, _, _) => tcx.type_of(did).subst(tcx, substs),
205+
AggregateKind::Adt(did, _, substs, _, _) => {
206+
tcx.bound_type_of(did).subst(tcx, substs)
207+
}
206208
AggregateKind::Closure(did, substs) => tcx.mk_closure(did, substs),
207209
AggregateKind::Generator(did, substs, movability) => {
208210
tcx.mk_generator(did, substs, movability)

compiler/rustc_middle/src/ty/context.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1603,7 +1603,7 @@ impl<'tcx> TyCtxt<'tcx> {
16031603
pub fn caller_location_ty(self) -> Ty<'tcx> {
16041604
self.mk_imm_ref(
16051605
self.lifetimes.re_static,
1606-
self.type_of(self.require_lang_item(LangItem::PanicLocation, None))
1606+
self.bound_type_of(self.require_lang_item(LangItem::PanicLocation, None))
16071607
.subst(self, self.mk_substs([self.lifetimes.re_static.into()].iter())),
16081608
)
16091609
}
@@ -2332,7 +2332,7 @@ impl<'tcx> TyCtxt<'tcx> {
23322332
ty_param.into()
23332333
} else {
23342334
assert!(has_default);
2335-
self.type_of(param.def_id).subst(self, substs).into()
2335+
self.bound_type_of(param.def_id).subst(self, substs).into()
23362336
}
23372337
}
23382338
});

0 commit comments

Comments
 (0)