88use rustc_hir:: def_id:: LocalDefId ;
99use rustc_index:: bit_set:: DenseBitSet ;
1010use 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+ } ;
1214use rustc_middle:: ty:: { self , DeducedParamAttrs , Ty , TyCtxt } ;
1315use rustc_session:: config:: OptLevel ;
1416use 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
3646impl < ' 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