Skip to content

Commit 6eb4f1d

Browse files
committed
Auto merge of #50016 - tmandry:cleanup-binder, r=nikomatsakis
Make Binder's field private and clean up its usage AKA "tour de rustc" Closes #49814.
2 parents 25749ad + 9ffe9be commit 6eb4f1d

Some content is hidden

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

48 files changed

+269
-252
lines changed

src/librustc/ich/impls_ty.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -262,8 +262,7 @@ impl<'a, 'gcx, T> HashStable<StableHashingContext<'a>> for ty::Binder<T>
262262
fn hash_stable<W: StableHasherResult>(&self,
263263
hcx: &mut StableHashingContext<'a>,
264264
hasher: &mut StableHasher<W>) {
265-
let ty::Binder(ref inner) = *self;
266-
inner.hash_stable(hcx, hasher);
265+
self.skip_binder().hash_stable(hcx, hasher);
267266
}
268267
}
269268

src/librustc/infer/canonical.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -388,14 +388,16 @@ impl<'cx, 'gcx, 'tcx> InferCtxt<'cx, 'gcx, 'tcx> {
388388
Obligation::new(
389389
cause.clone(),
390390
param_env,
391-
ty::Predicate::RegionOutlives(ty::Binder(ty::OutlivesPredicate(r1, r2))),
391+
ty::Predicate::RegionOutlives(
392+
ty::Binder::dummy(ty::OutlivesPredicate(r1, r2))),
392393
),
393394

394395
UnpackedKind::Type(t1) =>
395396
Obligation::new(
396397
cause.clone(),
397398
param_env,
398-
ty::Predicate::TypeOutlives(ty::Binder(ty::OutlivesPredicate(t1, r2))),
399+
ty::Predicate::TypeOutlives(
400+
ty::Binder::dummy(ty::OutlivesPredicate(t1, r2))),
399401
),
400402
}
401403
})) as Box<dyn Iterator<Item = _>>

src/librustc/infer/combine.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,7 @@ struct Generalizer<'cx, 'gcx: 'cx+'tcx, 'tcx: 'cx> {
302302

303303
/// Result from a generalization operation. This includes
304304
/// not only the generalized type, but also a bool flag
305-
/// indicating whether further WF checks are needed.q
305+
/// indicating whether further WF checks are needed.
306306
struct Generalization<'tcx> {
307307
ty: Ty<'tcx>,
308308

@@ -351,7 +351,7 @@ impl<'cx, 'gcx, 'tcx> TypeRelation<'cx, 'gcx, 'tcx> for Generalizer<'cx, 'gcx, '
351351
-> RelateResult<'tcx, ty::Binder<T>>
352352
where T: Relate<'tcx>
353353
{
354-
Ok(ty::Binder(self.relate(a.skip_binder(), b.skip_binder())?))
354+
Ok(ty::Binder::bind(self.relate(a.skip_binder(), b.skip_binder())?))
355355
}
356356

357357
fn relate_item_substs(&mut self,

src/librustc/infer/error_reporting/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -916,7 +916,7 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
916916
};
917917

918918
if let (Some(def_id), Some(ret_ty)) = (def_id, ret_ty) {
919-
if exp_is_struct && exp_found.expected == ret_ty.0 {
919+
if exp_is_struct && &exp_found.expected == ret_ty.skip_binder() {
920920
let message = format!(
921921
"did you mean `{}(/* fields */)`?",
922922
self.tcx.item_path_str(def_id)

src/librustc/infer/higher_ranked/mod.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ impl<'a, 'gcx, 'tcx> CombineFields<'a, 'gcx, 'tcx> {
8080

8181
debug!("higher_ranked_sub: OK result={:?}", result);
8282

83-
Ok(ty::Binder(result))
83+
Ok(ty::Binder::bind(result))
8484
});
8585
}
8686

@@ -239,7 +239,7 @@ impl<'a, 'gcx, 'tcx> CombineFields<'a, 'gcx, 'tcx> {
239239
b,
240240
result1);
241241

242-
Ok(ty::Binder(result1))
242+
Ok(ty::Binder::bind(result1))
243243
});
244244

245245
fn generalize_region<'a, 'gcx, 'tcx>(infcx: &InferCtxt<'a, 'gcx, 'tcx>,
@@ -335,7 +335,7 @@ impl<'a, 'gcx, 'tcx> CombineFields<'a, 'gcx, 'tcx> {
335335
b,
336336
result1);
337337

338-
Ok(ty::Binder(result1))
338+
Ok(ty::Binder::bind(result1))
339339
});
340340

341341
fn generalize_region<'a, 'gcx, 'tcx>(infcx: &InferCtxt<'a, 'gcx, 'tcx>,

src/librustc/infer/sub.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ impl<'combine, 'infcx, 'gcx, 'tcx> TypeRelation<'infcx, 'gcx, 'tcx>
9898
self.fields.trace.cause.clone(),
9999
self.fields.param_env,
100100
ty::Predicate::Subtype(
101-
ty::Binder(ty::SubtypePredicate {
101+
ty::Binder::dummy(ty::SubtypePredicate {
102102
a_is_expected: self.a_is_expected,
103103
a,
104104
b,

src/librustc/traits/error_reporting.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -537,7 +537,7 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
537537
&data.parent_trait_ref);
538538
match self.get_parent_trait_ref(&data.parent_code) {
539539
Some(t) => Some(t),
540-
None => Some(format!("{}", parent_trait_ref.0.self_ty())),
540+
None => Some(format!("{}", parent_trait_ref.skip_binder().self_ty())),
541541
}
542542
}
543543
_ => None,
@@ -862,7 +862,7 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
862862
obligation: &PredicateObligation<'tcx>,
863863
err: &mut DiagnosticBuilder<'tcx>,
864864
trait_ref: &ty::Binder<ty::TraitRef<'tcx>>) {
865-
let ty::Binder(trait_ref) = trait_ref;
865+
let trait_ref = trait_ref.skip_binder();
866866
let span = obligation.cause.span;
867867

868868
if let Ok(snippet) = self.tcx.sess.codemap().span_to_snippet(span) {
@@ -1102,7 +1102,7 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
11021102
::syntax::abi::Abi::Rust
11031103
)
11041104
};
1105-
format!("{}", ty::Binder(sig))
1105+
format!("{}", ty::Binder::bind(sig))
11061106
}
11071107

11081108
let argument_is_closure = expected_ref.skip_binder().substs.type_at(0).is_closure();
@@ -1436,7 +1436,7 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
14361436
}
14371437
ObligationCauseCode::BuiltinDerivedObligation(ref data) => {
14381438
let parent_trait_ref = self.resolve_type_vars_if_possible(&data.parent_trait_ref);
1439-
let ty = parent_trait_ref.0.self_ty();
1439+
let ty = parent_trait_ref.skip_binder().self_ty();
14401440
err.note(&format!("required because it appears within the type `{}`", ty));
14411441
obligated_types.push(ty);
14421442

@@ -1453,7 +1453,7 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
14531453
err.note(
14541454
&format!("required because of the requirements on the impl of `{}` for `{}`",
14551455
parent_trait_ref,
1456-
parent_trait_ref.0.self_ty()));
1456+
parent_trait_ref.skip_binder().self_ty()));
14571457
let parent_predicate = parent_trait_ref.to_predicate();
14581458
self.note_obligation_cause_code(err,
14591459
&parent_predicate,
@@ -1484,7 +1484,7 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
14841484
if let ObligationCauseCode::BuiltinDerivedObligation(ref data) = cause_code {
14851485
let parent_trait_ref = self.resolve_type_vars_if_possible(&data.parent_trait_ref);
14861486
for obligated_type in obligated_types {
1487-
if obligated_type == &parent_trait_ref.0.self_ty() {
1487+
if obligated_type == &parent_trait_ref.skip_binder().self_ty() {
14881488
return true;
14891489
}
14901490
}

src/librustc/traits/mod.rs

+8-5
Original file line numberDiff line numberDiff line change
@@ -855,16 +855,19 @@ fn vtable_methods<'a, 'tcx>(
855855

856856
// the method may have some early-bound lifetimes, add
857857
// regions for those
858-
let substs = Substs::for_item(tcx, def_id,
859-
|_, _| tcx.types.re_erased,
860-
|def, _| trait_ref.substs().type_for_def(def));
858+
let substs = trait_ref.map_bound(|trait_ref| {
859+
Substs::for_item(
860+
tcx, def_id,
861+
|_, _| tcx.types.re_erased,
862+
|def, _| trait_ref.substs.type_for_def(def))
863+
});
861864

862865
// the trait type may have higher-ranked lifetimes in it;
863866
// so erase them if they appear, so that we get the type
864867
// at some particular call site
865868
let substs = tcx.normalize_erasing_late_bound_regions(
866869
ty::ParamEnv::reveal_all(),
867-
&ty::Binder(substs),
870+
&substs
868871
);
869872

870873
// It's possible that the method relies on where clauses that
@@ -997,7 +1000,7 @@ impl<'tcx> FulfillmentError<'tcx> {
9971000

9981001
impl<'tcx> TraitObligation<'tcx> {
9991002
fn self_ty(&self) -> ty::Binder<Ty<'tcx>> {
1000-
ty::Binder(self.predicate.skip_binder().self_ty())
1003+
self.predicate.map_bound(|p| p.self_ty())
10011004
}
10021005
}
10031006

src/librustc/traits/object_safety.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
149149
trait_def_id: DefId,
150150
supertraits_only: bool) -> bool
151151
{
152-
let trait_ref = ty::Binder(ty::TraitRef {
152+
let trait_ref = ty::Binder::dummy(ty::TraitRef {
153153
def_id: trait_def_id,
154154
substs: Substs::identity_for_item(self, trait_def_id)
155155
});
@@ -199,7 +199,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
199199
.any(|predicate| {
200200
match predicate {
201201
ty::Predicate::Trait(ref trait_pred) if trait_pred.def_id() == sized_def_id => {
202-
trait_pred.0.self_ty().is_self()
202+
trait_pred.skip_binder().self_ty().is_self()
203203
}
204204
ty::Predicate::Projection(..) |
205205
ty::Predicate::Trait(..) |
@@ -352,7 +352,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
352352

353353
// Compute supertraits of current trait lazily.
354354
if supertraits.is_none() {
355-
let trait_ref = ty::Binder(ty::TraitRef {
355+
let trait_ref = ty::Binder::bind(ty::TraitRef {
356356
def_id: trait_def_id,
357357
substs: Substs::identity_for_item(self, trait_def_id)
358358
});
@@ -367,7 +367,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
367367
// direct equality here because all of these types
368368
// are part of the formal parameter listing, and
369369
// hence there should be no inference variables.
370-
let projection_trait_ref = ty::Binder(data.trait_ref(self));
370+
let projection_trait_ref = ty::Binder::bind(data.trait_ref(self));
371371
let is_supertrait_of_current_trait =
372372
supertraits.as_ref().unwrap().contains(&projection_trait_ref);
373373

src/librustc/traits/project.rs

+34-35
Original file line numberDiff line numberDiff line change
@@ -478,7 +478,7 @@ pub fn normalize_projection_type<'a, 'b, 'gcx, 'tcx>(
478478
let def_id = projection_ty.item_def_id;
479479
let ty_var = selcx.infcx().next_ty_var(
480480
TypeVariableOrigin::NormalizeProjectionType(tcx.def_span(def_id)));
481-
let projection = ty::Binder(ty::ProjectionPredicate {
481+
let projection = ty::Binder::dummy(ty::ProjectionPredicate {
482482
projection_ty,
483483
ty: ty_var
484484
});
@@ -982,8 +982,7 @@ fn assemble_candidates_from_predicates<'cx, 'gcx, 'tcx, I>(
982982
predicate);
983983
match predicate {
984984
ty::Predicate::Projection(data) => {
985-
let same_def_id =
986-
data.0.projection_ty.item_def_id == obligation.predicate.item_def_id;
985+
let same_def_id = data.projection_def_id() == obligation.predicate.item_def_id;
987986

988987
let is_match = same_def_id && infcx.probe(|_| {
989988
let data_poly_trait_ref =
@@ -1241,7 +1240,7 @@ fn confirm_object_candidate<'cx, 'gcx, 'tcx>(
12411240
// item with the correct name
12421241
let env_predicates = env_predicates.filter_map(|p| match p {
12431242
ty::Predicate::Projection(data) =>
1244-
if data.0.projection_ty.item_def_id == obligation.predicate.item_def_id {
1243+
if data.projection_def_id() == obligation.predicate.item_def_id {
12451244
Some(data)
12461245
} else {
12471246
None
@@ -1302,28 +1301,28 @@ fn confirm_generator_candidate<'cx, 'gcx, 'tcx>(
13021301

13031302
let gen_def_id = tcx.lang_items().gen_trait().unwrap();
13041303

1305-
// Note: we unwrap the binder here but re-create it below (1)
1306-
let ty::Binder((trait_ref, yield_ty, return_ty)) =
1304+
let predicate =
13071305
tcx.generator_trait_ref_and_outputs(gen_def_id,
13081306
obligation.predicate.self_ty(),
1309-
gen_sig);
1310-
1311-
let name = tcx.associated_item(obligation.predicate.item_def_id).name;
1312-
let ty = if name == Symbol::intern("Return") {
1313-
return_ty
1314-
} else if name == Symbol::intern("Yield") {
1315-
yield_ty
1316-
} else {
1317-
bug!()
1318-
};
1307+
gen_sig)
1308+
.map_bound(|(trait_ref, yield_ty, return_ty)| {
1309+
let name = tcx.associated_item(obligation.predicate.item_def_id).name;
1310+
let ty = if name == Symbol::intern("Return") {
1311+
return_ty
1312+
} else if name == Symbol::intern("Yield") {
1313+
yield_ty
1314+
} else {
1315+
bug!()
1316+
};
13191317

1320-
let predicate = ty::Binder(ty::ProjectionPredicate { // (1) recreate binder here
1321-
projection_ty: ty::ProjectionTy {
1322-
substs: trait_ref.substs,
1323-
item_def_id: obligation.predicate.item_def_id,
1324-
},
1325-
ty: ty
1326-
});
1318+
ty::ProjectionPredicate {
1319+
projection_ty: ty::ProjectionTy {
1320+
substs: trait_ref.substs,
1321+
item_def_id: obligation.predicate.item_def_id,
1322+
},
1323+
ty: ty
1324+
}
1325+
});
13271326

13281327
confirm_param_env_candidate(selcx, obligation, predicate)
13291328
.with_addl_obligations(vtable.nested)
@@ -1400,21 +1399,21 @@ fn confirm_callable_candidate<'cx, 'gcx, 'tcx>(
14001399
// the `Output` associated type is declared on `FnOnce`
14011400
let fn_once_def_id = tcx.lang_items().fn_once_trait().unwrap();
14021401

1403-
// Note: we unwrap the binder here but re-create it below (1)
1404-
let ty::Binder((trait_ref, ret_type)) =
1402+
let predicate =
14051403
tcx.closure_trait_ref_and_return_type(fn_once_def_id,
14061404
obligation.predicate.self_ty(),
14071405
fn_sig,
1408-
flag);
1409-
1410-
let predicate = ty::Binder(ty::ProjectionPredicate { // (1) recreate binder here
1411-
projection_ty: ty::ProjectionTy::from_ref_and_name(
1412-
tcx,
1413-
trait_ref,
1414-
Symbol::intern(FN_OUTPUT_NAME),
1415-
),
1416-
ty: ret_type
1417-
});
1406+
flag)
1407+
.map_bound(|(trait_ref, ret_type)| {
1408+
ty::ProjectionPredicate {
1409+
projection_ty: ty::ProjectionTy::from_ref_and_name(
1410+
tcx,
1411+
trait_ref,
1412+
Symbol::intern(FN_OUTPUT_NAME),
1413+
),
1414+
ty: ret_type
1415+
}
1416+
});
14181417

14191418
confirm_param_env_candidate(selcx, obligation, predicate)
14201419
}

0 commit comments

Comments
 (0)