@@ -278,6 +278,61 @@ bool merge
278278 return ProcessSingleTargetAssignment ( targetOperation , valueOperation : null , value , valueIsKnown : true , assignmentOperation , state , merge ) ;
279279 }
280280
281+ // Visits the side-effecting sub-expressions of an assignment target that identify *where*
282+ // the assignment writes (the receiver instance, and/or index/argument sub-expressions),
283+ // covering exactly the subset and order that ProcessSingleTargetAssignment visits before
284+ // evaluating the value being assigned. Note that IPropertyReferenceOperation's explicit
285+ // indexer Arguments are intentionally NOT covered here: today they are visited only after
286+ // the value, inside ProcessSingleTargetAssignment's IPropertyReferenceOperation case. This
287+ // method is shared by ProcessSingleTargetAssignment and VisitDeconstructionTargetSideEffects
288+ // so that ordinary and deconstruction assignment evaluate target locations identically,
289+ // including this known gap - fixing the evaluation order in this one place will fix it for
290+ // both assignment forms.
291+ private bool TryVisitAssignmentTargetInstance (
292+ IOperation targetOperation ,
293+ out TValue instanceValue ,
294+ out ImmutableArray < TValue > indexValues ,
295+ LocalDataFlowState < TValue , TContext , TValueLattice , TContextLattice > state )
296+ {
297+ switch ( targetOperation )
298+ {
299+ case IFieldReferenceOperation fieldRef :
300+ // Visit the instance to ensure that method calls or other operations
301+ // used as the instance are properly analyzed for diagnostics.
302+ instanceValue = Visit ( fieldRef . Instance , state ) ;
303+ indexValues = default ;
304+ return true ;
305+ case IEventReferenceOperation eventRef :
306+ instanceValue = Visit ( eventRef . Instance , state ) ;
307+ indexValues = default ;
308+ return true ;
309+ case IPropertyReferenceOperation propertyRef :
310+ // Avoid visiting the property reference because for captured properties, we can't
311+ // correctly detect whether it is used for reading or writing inside of VisitPropertyReference.
312+ // https://github.com/dotnet/roslyn/issues/25057
313+ instanceValue = Visit ( propertyRef . Instance , state ) ;
314+ indexValues = default ;
315+ return true ;
316+ case IImplicitIndexerReferenceOperation indexerRef :
317+ // An implicit reference to an indexer where the argument is a System.Index
318+ instanceValue = Visit ( indexerRef . Instance , state ) ;
319+ indexValues = ImmutableArray . Create ( Visit ( indexerRef . Argument , state ) ) ;
320+ return true ;
321+ case IArrayElementReferenceOperation arrayElementRef when arrayElementRef . Indices . Length == 1 :
322+ instanceValue = Visit ( arrayElementRef . ArrayReference , state ) ;
323+ indexValues = ImmutableArray . Create ( Visit ( arrayElementRef . Indices [ 0 ] , state ) ) ;
324+ return true ;
325+ case IInlineArrayAccessOperation inlineArrayAccess :
326+ instanceValue = Visit ( inlineArrayAccess . Instance , state ) ;
327+ indexValues = ImmutableArray . Create ( Visit ( inlineArrayAccess . Argument , state ) ) ;
328+ return true ;
329+ default :
330+ instanceValue = default ! ;
331+ indexValues = default ;
332+ return false ;
333+ }
334+ }
335+
281336 private TValue ProcessSingleTargetAssignment (
282337 IOperation targetOperation ,
283338 IOperation ? valueOperation ,
@@ -292,9 +347,7 @@ bool merge
292347 {
293348 case IFieldReferenceOperation fieldRef :
294349 {
295- // Visit the instance to ensure that method calls or other operations
296- // used as the instance are properly analyzed for diagnostics.
297- Visit ( fieldRef . Instance , state ) ;
350+ TryVisitAssignmentTargetInstance ( fieldRef , out _ , out _ , state ) ;
298351 var current = state . Current ;
299352 TValue targetValue = GetFieldTargetValue ( fieldRef , in current . Context ) ;
300353 value = GetAssignmentValue ( valueOperation , value , valueIsKnown , state ) ;
@@ -314,10 +367,7 @@ bool merge
314367 // to handle the LHS specially.
315368 case IPropertyReferenceOperation propertyRef :
316369 {
317- // Avoid visiting the property reference because for captured properties, we can't
318- // correctly detect whether it is used for reading or writing inside of VisitPropertyReference.
319- // https://github.com/dotnet/roslyn/issues/25057
320- TValue instanceValue = Visit ( propertyRef . Instance , state ) ;
370+ TryVisitAssignmentTargetInstance ( propertyRef , out TValue instanceValue , out _ , state ) ;
321371 value = GetAssignmentValue ( valueOperation , value , valueIsKnown , state ) ;
322372 IMethodSymbol ? setMethod = propertyRef . Property . GetSetMethod ( ) ;
323373
@@ -367,14 +417,13 @@ bool merge
367417 // Handles assignment to an event like 'Event = Handler;', which is a write to the underlying field,
368418 // not a call to an event accessor method. There is no Roslyn API to access the field,
369419 // so just visit the instance and the value. https://github.com/dotnet/roslyn/issues/40103
370- Visit ( eventRef . Instance , state ) ;
420+ TryVisitAssignmentTargetInstance ( eventRef , out _ , out _ , state ) ;
371421 return GetAssignmentValue ( valueOperation , value , valueIsKnown , state ) ;
372422 }
373423 case IImplicitIndexerReferenceOperation indexerRef :
374424 {
375- // An implicit reference to an indexer where the argument is a System.Index
376- TValue instanceValue = Visit ( indexerRef . Instance , state ) ;
377- TValue indexArgumentValue = Visit ( indexerRef . Argument , state ) ;
425+ TryVisitAssignmentTargetInstance ( indexerRef , out TValue instanceValue , out ImmutableArray < TValue > indexValues , state ) ;
426+ TValue indexArgumentValue = indexValues [ 0 ] ;
378427 value = GetAssignmentValue ( valueOperation , value , valueIsKnown , state ) ;
379428
380429 var property = ( IPropertySymbol ) indexerRef . IndexerSymbol ;
@@ -412,19 +461,18 @@ bool merge
412461 }
413462 case IArrayElementReferenceOperation arrayElementRef :
414463 {
415- if ( arrayElementRef . Indices . Length != 1 )
464+ if ( ! TryVisitAssignmentTargetInstance ( arrayElementRef , out TValue arrayRef , out ImmutableArray < TValue > indexValues , state ) )
416465 break ;
417466
418- TValue arrayRef = Visit ( arrayElementRef . ArrayReference , state ) ;
419- TValue index = Visit ( arrayElementRef . Indices [ 0 ] , state ) ;
467+ TValue index = indexValues [ 0 ] ;
420468 value = GetAssignmentValue ( valueOperation , value , valueIsKnown , state ) ;
421469 HandleArrayElementWrite ( arrayRef , index , value , assignmentOperation , merge : merge ) ;
422470 return value ;
423471 }
424472 case IInlineArrayAccessOperation inlineArrayAccess :
425473 {
426- TValue arrayRef = Visit ( inlineArrayAccess . Instance , state ) ;
427- TValue index = Visit ( inlineArrayAccess . Argument , state ) ;
474+ TryVisitAssignmentTargetInstance ( inlineArrayAccess , out TValue arrayRef , out ImmutableArray < TValue > indexValues , state ) ;
475+ TValue index = indexValues [ 0 ] ;
428476 value = GetAssignmentValue ( valueOperation , value , valueIsKnown , state ) ;
429477 HandleArrayElementWrite ( arrayRef , index , value , assignmentOperation , merge : merge ) ;
430478 return value ;
@@ -613,16 +661,16 @@ public override TValue VisitDeconstructionAssignment(
613661 return Visit ( operation . Value , state ) ;
614662 }
615663
616- // Like ordinary assignments (see ProcessSingleTargetAssignment), any side-effecting
617- // sub-expressions that identify a target location (a property/indexer receiver and its
618- // index arguments, or an array reference and its index ) must be evaluated before the
619- // source, matching left-to-right evaluation order. Unlike ordinary assignments, the
620- // actual write can't happen until every target location has been identified and every
621- // source value has been read, so these sub-expressions get visited again for effect
622- // when performing the write in AssignDeconstruction. Revisiting the same IOperation node
623- // for its side effect is safe: the pattern store used for intrinsic method calls merges
624- // patterns keyed by the same IOperation instance (see the analogous existing tolerance
625- // in ProcessAssignment for flow-capture targets with multiple captured references).
664+ // Like ordinary assignments (see ProcessSingleTargetAssignment), the side-effecting
665+ // sub-expression that identifies a target location (a property/indexer/array-element
666+ // receiver ) must be evaluated before the source, matching left-to-right evaluation
667+ // order. Unlike ordinary assignments, the actual write can't happen until every target
668+ // location has been identified and every source value has been read, so this
669+ // sub-expression gets visited again for effect when performing the write in
670+ // AssignDeconstruction. Revisiting the same IOperation node for its side effect is safe:
671+ // the pattern store used for intrinsic method calls merges patterns keyed by the same
672+ // IOperation instance (see the analogous existing tolerance in ProcessAssignment for
673+ // flow-capture targets with multiple captured references).
626674 VisitDeconstructionTargetSideEffects ( UnwrapDeconstructionTarget ( operation . Target ) , state ) ;
627675
628676 ITypeSymbol ? sourceType = operation . Value . Type ;
@@ -849,10 +897,14 @@ private TValue GetDeconstructionSourceValue(
849897 }
850898
851899 // Visits the side-effecting sub-expressions that identify a deconstruction target location
852- // (a property/indexer receiver and its index arguments, or an array reference and its index),
853- // without performing any write. This runs before the source is visited, so that expressions
854- // like arr[F()] in '(arr[F()], b) = (x, y)' evaluate 'arr' and 'F()' before 'x'/'y' are read,
855- // matching left-to-right evaluation order and Roslyn's own lowering (GetAssignmentTargetsAndSideEffects).
900+ // (a property/indexer/array-element receiver, and any index sub-expressions that
901+ // ProcessSingleTargetAssignment already visits before the value - see
902+ // TryVisitAssignmentTargetInstance), without performing any write. This runs before the
903+ // source is visited, so that e.g. 'arr' in '(arr[i], b) = (x, y)' is evaluated before 'x'/'y'
904+ // are read, matching left-to-right evaluation order and Roslyn's own lowering
905+ // (GetAssignmentTargetsAndSideEffects). Uses the same shared helper as
906+ // ProcessSingleTargetAssignment so both assignment forms evaluate target locations
907+ // identically, including any gaps in that shared logic (see TryVisitAssignmentTargetInstance).
856908 private void VisitDeconstructionTargetSideEffects (
857909 IOperation target ,
858910 LocalDataFlowState < TValue , TContext , TValueLattice , TContextLattice > state )
@@ -865,36 +917,7 @@ private void VisitDeconstructionTargetSideEffects(
865917 return ;
866918 }
867919
868- switch ( target )
869- {
870- case IFieldReferenceOperation fieldRef :
871- Visit ( fieldRef . Instance , state ) ;
872- break ;
873- case IPropertyReferenceOperation propertyRef :
874- // Avoid visiting the property reference itself; see the similar comment in
875- // ProcessSingleTargetAssignment about https://github.com/dotnet/roslyn/issues/25057.
876- Visit ( propertyRef . Instance , state ) ;
877- foreach ( var argument in propertyRef . Arguments )
878- Visit ( argument , state ) ;
879- break ;
880- case IArrayElementReferenceOperation arrayElementRef :
881- Visit ( arrayElementRef . ArrayReference , state ) ;
882- foreach ( var index in arrayElementRef . Indices )
883- Visit ( index , state ) ;
884- break ;
885- case IInlineArrayAccessOperation inlineArrayAccess :
886- Visit ( inlineArrayAccess . Instance , state ) ;
887- Visit ( inlineArrayAccess . Argument , state ) ;
888- break ;
889- case IImplicitIndexerReferenceOperation indexerRef :
890- Visit ( indexerRef . Instance , state ) ;
891- Visit ( indexerRef . Argument , state ) ;
892- break ;
893- default :
894- // Locals, parameters, discards, and declaration expressions have no
895- // side-effecting sub-expression to evaluate ahead of the source.
896- break ;
897- }
920+ TryVisitAssignmentTargetInstance ( target , out _ , out _ , state ) ;
898921 }
899922
900923 private static IOperation UnwrapDeconstructionTarget ( IOperation target )
0 commit comments