Skip to content

Commit d42a3c6

Browse files
committed
arg_index
1 parent 9591942 commit d42a3c6

File tree

1 file changed

+18
-14
lines changed

1 file changed

+18
-14
lines changed

compiler/rustc_mir_transform/src/deduce_param_attrs.rs

+18-14
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@
88
use rustc_hir::def_id::LocalDefId;
99
use rustc_index::bit_set::DenseBitSet;
1010
use rustc_middle::mir::visit::{NonMutatingUseContext, PlaceContext, Visitor};
11-
use rustc_middle::mir::{Body, Location, Operand, Place, RETURN_PLACE, Terminator, TerminatorKind};
11+
use rustc_middle::mir::{
12+
Body, Local, Location, Operand, Place, RETURN_PLACE, Terminator, TerminatorKind,
13+
};
1214
use rustc_middle::ty::{self, DeducedParamAttrs, Ty, TyCtxt};
1315
use rustc_session::config::OptLevel;
1416
use tracing::instrument;
@@ -31,18 +33,23 @@ impl DeduceReadOnly {
3133
read_only_when_freeze: DenseBitSet::new_filled(arg_count),
3234
}
3335
}
36+
37+
fn arg_index(&self, local: Local) -> Option<usize> {
38+
if local == RETURN_PLACE || local.index() > self.read_only.domain_size() {
39+
None
40+
} else {
41+
Some(local.index() - 1)
42+
}
43+
}
3444
}
3545

3646
impl<'tcx> Visitor<'tcx> for DeduceReadOnly {
3747
fn visit_place(&mut self, place: &Place<'tcx>, context: PlaceContext, _location: Location) {
38-
// We're only interested in arguments.
39-
if place.local == RETURN_PLACE || place.local.index() > self.read_only.domain_size() {
40-
return;
41-
}
42-
let arg_index = place.local.index() - 1;
4348
if place.is_indirect() {
4449
return;
4550
}
51+
// We're only interested in arguments.
52+
let Some(arg_index) = self.arg_index(place.local) else { return };
4653
match context {
4754
PlaceContext::MutatingUse(..) => {
4855
// This is a mutation, so mark it as such.
@@ -87,16 +94,13 @@ impl<'tcx> Visitor<'tcx> for DeduceReadOnly {
8794
if let TerminatorKind::Call { ref args, .. } = terminator.kind {
8895
for arg in args {
8996
if let Operand::Move(place) = arg.node {
90-
let local = place.local;
91-
if place.is_indirect()
92-
|| local == RETURN_PLACE
93-
|| local.index() > self.read_only.domain_size()
94-
{
97+
if place.is_indirect() {
9598
continue;
9699
}
97-
let arg_index = local.index() - 1;
98-
self.read_only.remove(arg_index);
99-
self.read_only_when_freeze.remove(arg_index);
100+
if let Some(arg_index) = self.arg_index(place.local) {
101+
self.read_only.remove(arg_index);
102+
self.read_only_when_freeze.remove(arg_index);
103+
}
100104
}
101105
}
102106
};

0 commit comments

Comments
 (0)