Skip to content

Commit 4512721

Browse files
committed
Auto merge of #72494 - lcnr:predicate-cleanup, r=nikomatsakis
Pass more `Copy` types by value. There are a lot of locations where we pass `&T where T: Copy` by reference, which should both be slightly less performant and less readable IMO. This PR currently consists of three fairly self contained commits: - passes `ty::Predicate` by value and stops depending on `AsRef<ty::Predicate>`. - changes `<&List<_>>::into_iter` to iterate over the elements by value. This would break `List`s of non copy types. But as the only list constructor requires `T` to be copy anyways, I think the improved readability is worth this potential future restriction. - passes `mir::PlaceElem` by value. Mir currently has quite a few copy types which are passed by reference, e.g. `Local`. As I don't have a lot of experience working with MIR, I mostly did this to get some feedback from people who use MIR more frequently - tries to reuse `ty::Predicate` in case it did not change in some places, which should hopefully fix the regression caused by #72055 r? @nikomatsakis for the first commit, which continues the work of #72055 and makes adding `PredicateKind::ForAll` slightly more pleasant. Feel free to reassign though
2 parents 664fcd3 + f15e4b3 commit 4512721

File tree

59 files changed

+194
-196
lines changed

Some content is hidden

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

59 files changed

+194
-196
lines changed

src/librustc_codegen_ssa/debuginfo/type_names.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ pub fn push_debuginfo_type_name<'tcx>(
4848
}
4949
ty::Tuple(component_types) => {
5050
output.push('(');
51-
for &component_type in component_types {
51+
for component_type in component_types {
5252
push_debuginfo_type_name(tcx, component_type.expect_ty(), true, output, visited);
5353
output.push_str(", ");
5454
}

src/librustc_codegen_ssa/mir/analyze.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ impl<Bx: BuilderMethods<'a, 'tcx>> LocalAnalyzer<'mir, 'a, 'tcx, Bx> {
104104
) {
105105
let cx = self.fx.cx;
106106

107-
if let [proj_base @ .., elem] = place_ref.projection {
107+
if let &[ref proj_base @ .., elem] = place_ref.projection {
108108
let mut base_context = if context.is_mutating_use() {
109109
PlaceContext::MutatingUse(MutatingUseContext::Projection)
110110
} else {
@@ -186,7 +186,7 @@ impl<Bx: BuilderMethods<'a, 'tcx>> LocalAnalyzer<'mir, 'a, 'tcx, Bx> {
186186
// now that we have moved to the "slice of projections" representation.
187187
if let mir::ProjectionElem::Index(local) = elem {
188188
self.visit_local(
189-
local,
189+
&local,
190190
PlaceContext::NonMutatingUse(NonMutatingUseContext::Copy),
191191
location,
192192
);

src/librustc_codegen_ssa/mir/place.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -429,7 +429,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
429429
self.codegen_consume(bx, mir::PlaceRef { local, projection: proj_base })
430430
.deref(bx.cx())
431431
}
432-
mir::PlaceRef { local, projection: [proj_base @ .., elem] } => {
432+
mir::PlaceRef { local, projection: &[ref proj_base @ .., elem] } => {
433433
// FIXME turn this recursion into iteration
434434
let cg_base =
435435
self.codegen_place(bx, mir::PlaceRef { local, projection: proj_base });
@@ -440,7 +440,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
440440
cg_base.project_field(bx, field.index())
441441
}
442442
mir::ProjectionElem::Index(index) => {
443-
let index = &mir::Operand::Copy(mir::Place::from(*index));
443+
let index = &mir::Operand::Copy(mir::Place::from(index));
444444
let index = self.codegen_operand(bx, index);
445445
let llindex = index.immediate();
446446
cg_base.project_index(bx, llindex)
@@ -450,22 +450,22 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
450450
from_end: false,
451451
min_length: _,
452452
} => {
453-
let lloffset = bx.cx().const_usize(*offset as u64);
453+
let lloffset = bx.cx().const_usize(offset as u64);
454454
cg_base.project_index(bx, lloffset)
455455
}
456456
mir::ProjectionElem::ConstantIndex {
457457
offset,
458458
from_end: true,
459459
min_length: _,
460460
} => {
461-
let lloffset = bx.cx().const_usize(*offset as u64);
461+
let lloffset = bx.cx().const_usize(offset as u64);
462462
let lllen = cg_base.len(bx.cx());
463463
let llindex = bx.sub(lllen, lloffset);
464464
cg_base.project_index(bx, llindex)
465465
}
466466
mir::ProjectionElem::Subslice { from, to, from_end } => {
467467
let mut subslice =
468-
cg_base.project_index(bx, bx.cx().const_usize(*from as u64));
468+
cg_base.project_index(bx, bx.cx().const_usize(from as u64));
469469
let projected_ty =
470470
PlaceTy::from_ty(cg_base.layout.ty).projection_ty(tcx, elem).ty;
471471
subslice.layout = bx.cx().layout_of(self.monomorphize(&projected_ty));
@@ -474,7 +474,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
474474
assert!(from_end, "slice subslices should be `from_end`");
475475
subslice.llextra = Some(bx.sub(
476476
cg_base.llextra.unwrap(),
477-
bx.cx().const_usize((*from as u64) + (*to as u64)),
477+
bx.cx().const_usize((from as u64) + (to as u64)),
478478
));
479479
}
480480

@@ -487,7 +487,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
487487

488488
subslice
489489
}
490-
mir::ProjectionElem::Downcast(_, v) => cg_base.project_downcast(bx, *v),
490+
mir::ProjectionElem::Downcast(_, v) => cg_base.project_downcast(bx, v),
491491
}
492492
}
493493
};

src/librustc_infer/infer/canonical/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ impl<'cx, 'tcx> InferCtxt<'cx, 'tcx> {
8787
) -> CanonicalVarValues<'tcx> {
8888
let var_values: IndexVec<BoundVar, GenericArg<'tcx>> = variables
8989
.iter()
90-
.map(|info| self.instantiate_canonical_var(span, *info, &universe_map))
90+
.map(|info| self.instantiate_canonical_var(span, info, &universe_map))
9191
.collect();
9292

9393
CanonicalVarValues { var_values }

src/librustc_infer/infer/canonical/query_response.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -464,12 +464,12 @@ impl<'cx, 'tcx> InferCtxt<'cx, 'tcx> {
464464
if info.is_existential() {
465465
match opt_values[BoundVar::new(index)] {
466466
Some(k) => k,
467-
None => self.instantiate_canonical_var(cause.span, *info, |u| {
467+
None => self.instantiate_canonical_var(cause.span, info, |u| {
468468
universe_map[u.as_usize()]
469469
}),
470470
}
471471
} else {
472-
self.instantiate_canonical_var(cause.span, *info, |u| {
472+
self.instantiate_canonical_var(cause.span, info, |u| {
473473
universe_map[u.as_usize()]
474474
})
475475
}

src/librustc_infer/infer/mod.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -970,7 +970,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
970970
&self,
971971
cause: &ObligationCause<'tcx>,
972972
param_env: ty::ParamEnv<'tcx>,
973-
predicate: &ty::PolySubtypePredicate<'tcx>,
973+
predicate: ty::PolySubtypePredicate<'tcx>,
974974
) -> Option<InferResult<'tcx, ()>> {
975975
// Subtle: it's ok to skip the binder here and resolve because
976976
// `shallow_resolve` just ignores anything that is not a type
@@ -993,7 +993,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
993993

994994
Some(self.commit_if_ok(|snapshot| {
995995
let (ty::SubtypePredicate { a_is_expected, a, b }, placeholder_map) =
996-
self.replace_bound_vars_with_placeholders(predicate);
996+
self.replace_bound_vars_with_placeholders(&predicate);
997997

998998
let ok = self.at(cause, param_env).sub_exp(a_is_expected, a, b)?;
999999

@@ -1006,11 +1006,11 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
10061006
pub fn region_outlives_predicate(
10071007
&self,
10081008
cause: &traits::ObligationCause<'tcx>,
1009-
predicate: &ty::PolyRegionOutlivesPredicate<'tcx>,
1009+
predicate: ty::PolyRegionOutlivesPredicate<'tcx>,
10101010
) -> UnitResult<'tcx> {
10111011
self.commit_if_ok(|snapshot| {
10121012
let (ty::OutlivesPredicate(r_a, r_b), placeholder_map) =
1013-
self.replace_bound_vars_with_placeholders(predicate);
1013+
self.replace_bound_vars_with_placeholders(&predicate);
10141014
let origin = SubregionOrigin::from_obligation_cause(cause, || {
10151015
RelateRegionParamBound(cause.span)
10161016
});

src/librustc_infer/infer/outlives/verify.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ impl<'cx, 'tcx> VerifyBoundCx<'cx, 'tcx> {
5050
// for further background and discussion.
5151
let mut bounds = substs
5252
.iter()
53-
.filter_map(|&child| match child.unpack() {
53+
.filter_map(|child| match child.unpack() {
5454
GenericArgKind::Type(ty) => Some(self.type_bound(ty)),
5555
GenericArgKind::Lifetime(_) => None,
5656
GenericArgKind::Const(_) => Some(self.recursive_bound(child)),
@@ -334,10 +334,10 @@ impl<'cx, 'tcx> VerifyBoundCx<'cx, 'tcx> {
334334
fn collect_outlives_from_predicate_list(
335335
&self,
336336
compare_ty: impl Fn(Ty<'tcx>) -> bool,
337-
predicates: impl Iterator<Item = impl AsRef<ty::Predicate<'tcx>>>,
337+
predicates: impl Iterator<Item = ty::Predicate<'tcx>>,
338338
) -> impl Iterator<Item = ty::OutlivesPredicate<Ty<'tcx>, ty::Region<'tcx>>> {
339339
predicates
340-
.filter_map(|p| p.as_ref().to_opt_type_outlives())
340+
.filter_map(|p| p.to_opt_type_outlives())
341341
.filter_map(|p| p.no_bound_vars())
342342
.filter(move |p| compare_ty(p.0))
343343
}

src/librustc_infer/traits/util.rs

+20-26
Original file line numberDiff line numberDiff line change
@@ -8,52 +8,46 @@ use rustc_span::Span;
88

99
pub fn anonymize_predicate<'tcx>(
1010
tcx: TyCtxt<'tcx>,
11-
pred: &ty::Predicate<'tcx>,
11+
pred: ty::Predicate<'tcx>,
1212
) -> ty::Predicate<'tcx> {
13-
match pred.kind() {
13+
let kind = pred.kind();
14+
let new = match kind {
1415
&ty::PredicateKind::Trait(ref data, constness) => {
1516
ty::PredicateKind::Trait(tcx.anonymize_late_bound_regions(data), constness)
16-
.to_predicate(tcx)
1717
}
1818

1919
ty::PredicateKind::RegionOutlives(data) => {
2020
ty::PredicateKind::RegionOutlives(tcx.anonymize_late_bound_regions(data))
21-
.to_predicate(tcx)
2221
}
2322

2423
ty::PredicateKind::TypeOutlives(data) => {
2524
ty::PredicateKind::TypeOutlives(tcx.anonymize_late_bound_regions(data))
26-
.to_predicate(tcx)
2725
}
2826

2927
ty::PredicateKind::Projection(data) => {
30-
ty::PredicateKind::Projection(tcx.anonymize_late_bound_regions(data)).to_predicate(tcx)
28+
ty::PredicateKind::Projection(tcx.anonymize_late_bound_regions(data))
3129
}
3230

33-
&ty::PredicateKind::WellFormed(data) => {
34-
ty::PredicateKind::WellFormed(data).to_predicate(tcx)
35-
}
31+
&ty::PredicateKind::WellFormed(data) => ty::PredicateKind::WellFormed(data),
3632

37-
&ty::PredicateKind::ObjectSafe(data) => {
38-
ty::PredicateKind::ObjectSafe(data).to_predicate(tcx)
39-
}
33+
&ty::PredicateKind::ObjectSafe(data) => ty::PredicateKind::ObjectSafe(data),
4034

4135
&ty::PredicateKind::ClosureKind(closure_def_id, closure_substs, kind) => {
42-
ty::PredicateKind::ClosureKind(closure_def_id, closure_substs, kind).to_predicate(tcx)
36+
ty::PredicateKind::ClosureKind(closure_def_id, closure_substs, kind)
4337
}
4438

4539
ty::PredicateKind::Subtype(data) => {
46-
ty::PredicateKind::Subtype(tcx.anonymize_late_bound_regions(data)).to_predicate(tcx)
40+
ty::PredicateKind::Subtype(tcx.anonymize_late_bound_regions(data))
4741
}
4842

4943
&ty::PredicateKind::ConstEvaluatable(def_id, substs) => {
50-
ty::PredicateKind::ConstEvaluatable(def_id, substs).to_predicate(tcx)
44+
ty::PredicateKind::ConstEvaluatable(def_id, substs)
5145
}
5246

53-
ty::PredicateKind::ConstEquate(c1, c2) => {
54-
ty::PredicateKind::ConstEquate(c1, c2).to_predicate(tcx)
55-
}
56-
}
47+
ty::PredicateKind::ConstEquate(c1, c2) => ty::PredicateKind::ConstEquate(c1, c2),
48+
};
49+
50+
if new != *kind { new.to_predicate(tcx) } else { pred }
5751
}
5852

5953
struct PredicateSet<'tcx> {
@@ -66,7 +60,7 @@ impl PredicateSet<'tcx> {
6660
Self { tcx, set: Default::default() }
6761
}
6862

69-
fn insert(&mut self, pred: &ty::Predicate<'tcx>) -> bool {
63+
fn insert(&mut self, pred: ty::Predicate<'tcx>) -> bool {
7064
// We have to be careful here because we want
7165
//
7266
// for<'a> Foo<&'a int>
@@ -81,10 +75,10 @@ impl PredicateSet<'tcx> {
8175
}
8276
}
8377

84-
impl<T: AsRef<ty::Predicate<'tcx>>> Extend<T> for PredicateSet<'tcx> {
85-
fn extend<I: IntoIterator<Item = T>>(&mut self, iter: I) {
78+
impl Extend<ty::Predicate<'tcx>> for PredicateSet<'tcx> {
79+
fn extend<I: IntoIterator<Item = ty::Predicate<'tcx>>>(&mut self, iter: I) {
8680
for pred in iter {
87-
self.insert(pred.as_ref());
81+
self.insert(pred);
8882
}
8983
}
9084
}
@@ -132,7 +126,7 @@ pub fn elaborate_obligations<'tcx>(
132126
mut obligations: Vec<PredicateObligation<'tcx>>,
133127
) -> Elaborator<'tcx> {
134128
let mut visited = PredicateSet::new(tcx);
135-
obligations.retain(|obligation| visited.insert(&obligation.predicate));
129+
obligations.retain(|obligation| visited.insert(obligation.predicate));
136130
Elaborator { stack: obligations, visited }
137131
}
138132

@@ -172,7 +166,7 @@ impl Elaborator<'tcx> {
172166
// cases. One common case is when people define
173167
// `trait Sized: Sized { }` rather than `trait Sized { }`.
174168
let visited = &mut self.visited;
175-
let obligations = obligations.filter(|o| visited.insert(&o.predicate));
169+
let obligations = obligations.filter(|o| visited.insert(o.predicate));
176170

177171
self.stack.extend(obligations);
178172
}
@@ -260,7 +254,7 @@ impl Elaborator<'tcx> {
260254
}
261255
})
262256
.map(|predicate_kind| predicate_kind.to_predicate(tcx))
263-
.filter(|predicate| visited.insert(predicate))
257+
.filter(|&predicate| visited.insert(predicate))
264258
.map(|predicate| predicate_obligation(predicate, None)),
265259
);
266260
}

src/librustc_middle/mir/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -2077,10 +2077,10 @@ impl Debug for Place<'_> {
20772077
ProjectionElem::ConstantIndex { offset, min_length, from_end: true } => {
20782078
write!(fmt, "[-{:?} of {:?}]", offset, min_length)?;
20792079
}
2080-
ProjectionElem::Subslice { from, to, from_end: true } if *to == 0 => {
2080+
ProjectionElem::Subslice { from, to, from_end: true } if to == 0 => {
20812081
write!(fmt, "[{:?}:]", from)?;
20822082
}
2083-
ProjectionElem::Subslice { from, to, from_end: true } if *from == 0 => {
2083+
ProjectionElem::Subslice { from, to, from_end: true } if from == 0 => {
20842084
write!(fmt, "[:-{:?}]", to)?;
20852085
}
20862086
ProjectionElem::Subslice { from, to, from_end: true } => {

src/librustc_middle/mir/tcx.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,8 @@ impl<'tcx> PlaceTy<'tcx> {
5656
/// Convenience wrapper around `projection_ty_core` for
5757
/// `PlaceElem`, where we can just use the `Ty` that is already
5858
/// stored inline on field projection elems.
59-
pub fn projection_ty(self, tcx: TyCtxt<'tcx>, elem: &PlaceElem<'tcx>) -> PlaceTy<'tcx> {
60-
self.projection_ty_core(tcx, ty::ParamEnv::empty(), elem, |_, _, ty| ty)
59+
pub fn projection_ty(self, tcx: TyCtxt<'tcx>, elem: PlaceElem<'tcx>) -> PlaceTy<'tcx> {
60+
self.projection_ty_core(tcx, ty::ParamEnv::empty(), &elem, |_, _, ty| ty)
6161
}
6262

6363
/// `place_ty.projection_ty_core(tcx, elem, |...| { ... })`
@@ -124,7 +124,7 @@ impl<'tcx> Place<'tcx> {
124124
{
125125
projection
126126
.iter()
127-
.fold(PlaceTy::from_ty(local_decls.local_decls()[local].ty), |place_ty, elem| {
127+
.fold(PlaceTy::from_ty(local_decls.local_decls()[local].ty), |place_ty, &elem| {
128128
place_ty.projection_ty(tcx, elem)
129129
})
130130
}

src/librustc_middle/mir/visit.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -903,7 +903,7 @@ macro_rules! visit_place_fns {
903903
let mut projection = Cow::Borrowed(projection);
904904

905905
for i in 0..projection.len() {
906-
if let Some(elem) = projection.get(i) {
906+
if let Some(&elem) = projection.get(i) {
907907
if let Some(elem) = self.process_projection_elem(elem, location) {
908908
// This converts the borrowed projection into `Cow::Owned(_)` and returns a
909909
// clone of the projection so we can mutate and reintern later.
@@ -921,19 +921,19 @@ macro_rules! visit_place_fns {
921921

922922
fn process_projection_elem(
923923
&mut self,
924-
elem: &PlaceElem<'tcx>,
924+
elem: PlaceElem<'tcx>,
925925
location: Location,
926926
) -> Option<PlaceElem<'tcx>> {
927927
match elem {
928928
PlaceElem::Index(local) => {
929-
let mut new_local = *local;
929+
let mut new_local = local;
930930
self.visit_local(
931931
&mut new_local,
932932
PlaceContext::NonMutatingUse(NonMutatingUseContext::Copy),
933933
location,
934934
);
935935

936-
if new_local == *local { None } else { Some(PlaceElem::Index(new_local)) }
936+
if new_local == local { None } else { Some(PlaceElem::Index(new_local)) }
937937
}
938938
PlaceElem::Deref
939939
| PlaceElem::Field(..)
@@ -959,7 +959,7 @@ macro_rules! visit_place_fns {
959959
&mut self,
960960
local: Local,
961961
proj_base: &[PlaceElem<'tcx>],
962-
elem: &PlaceElem<'tcx>,
962+
elem: PlaceElem<'tcx>,
963963
context: PlaceContext,
964964
location: Location,
965965
) {
@@ -990,7 +990,7 @@ macro_rules! visit_place_fns {
990990
location: Location,
991991
) {
992992
let mut cursor = projection;
993-
while let [proj_base @ .., elem] = cursor {
993+
while let &[ref proj_base @ .., elem] = cursor {
994994
cursor = proj_base;
995995
self.visit_projection_elem(local, cursor, elem, context, location);
996996
}
@@ -1000,7 +1000,7 @@ macro_rules! visit_place_fns {
10001000
&mut self,
10011001
_local: Local,
10021002
_proj_base: &[PlaceElem<'tcx>],
1003-
elem: &PlaceElem<'tcx>,
1003+
elem: PlaceElem<'tcx>,
10041004
_context: PlaceContext,
10051005
location: Location,
10061006
) {
@@ -1010,7 +1010,7 @@ macro_rules! visit_place_fns {
10101010
}
10111011
ProjectionElem::Index(local) => {
10121012
self.visit_local(
1013-
local,
1013+
&local,
10141014
PlaceContext::NonMutatingUse(NonMutatingUseContext::Copy),
10151015
location,
10161016
);

src/librustc_middle/ty/flags.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ impl FlagComputation {
129129
&ty::Dynamic(ref obj, r) => {
130130
let mut computation = FlagComputation::new();
131131
for predicate in obj.skip_binder().iter() {
132-
match *predicate {
132+
match predicate {
133133
ty::ExistentialPredicate::Trait(tr) => computation.add_substs(tr.substs),
134134
ty::ExistentialPredicate::Projection(p) => {
135135
let mut proj_computation = FlagComputation::new();

0 commit comments

Comments
 (0)