Skip to content

Commit 0de0ba8

Browse files
sbomerCopilot
authored andcommitted
Add dataflow support for deconstruction assignments (dotnet#131624)
Fixes dotnet#123767. The ILLink Roslyn analyzer's dataflow visitor did not handle `IDeconstructionAssignmentOperation`, so `RequiresUnreferencedCode` and `DynamicallyAccessedMembers` diagnostics were silently skipped for values flowing through deconstruction, including assignments and deconstruction in `foreach`. ## Summary of changes - Add dataflow support for tuple expressions, tuple-typed values, instance and extension `Deconstruct` methods, nested deconstruction, and user-defined conversions. - Support local, field, property, parameter, discard, array-element, explicit-indexer, implicit-indexer, and nested tuple targets. - Preserve C# evaluation order by evaluating target locations before source values and performing target writes only after all source values and nested `Deconstruct` calls have been evaluated. - Preserve flow-captured target receivers and index arguments so target locations are evaluated once and retain the values selected before source-side mutations. - Correctly map receivers for classic extension methods and C# 14 extension-block `Deconstruct` methods. - Treat outer or nested `[DoesNotReturn] Deconstruct` calls as unreachable and suppress every target assignment on that path. ## Testing Added regression coverage for deconstruction sources, targets, evaluation ordering, flow captures, extension receivers, user-defined conversions, and direct and nested `[DoesNotReturn]` methods. The DataFlow suites pass across: - `ILLink.RoslynAnalyzer.Tests` - `Mono.Linker.Tests` - `ILCompiler.Trimming.Tests` - `ILTrim.Tests` Two unsupported ILTrim scenarios are isolated as standalone expected failures instead of excluding the shared deconstruction coverage: - `DeconstructFieldTarget` exposes an existing ILTrim.Core gap in static-field write dataflow. - `DeconstructUserDefinedConversion` exposes an existing ILTrim.Core gap in tracking a user-defined conversion operator's return value. The Roslyn analyzer, linker, and NativeAOT tests retain coverage for both cases. ILLink/ILTrim and NativeAOT also conservatively report the expected warning after `[DoesNotReturn]` calls because the IL-based tools do not treat that attribute as a reachability contract; those expectations are scoped to the IL-based tools while the Roslyn analyzer verifies the path is unreachable. > [!NOTE] > This content was created with assistance from AI. --------- Co-authored-by: Copilot <[email protected]> Copilot-Session: b4dc4554-6607-4109-ab22-56a5815b294d
1 parent b1864fa commit 0de0ba8

10 files changed

Lines changed: 1119 additions & 66 deletions

File tree

‎src/coreclr/tools/ILTrim.Tests/ILTrimExpectedFailures.txt‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,8 @@ DataFlow.CompilerGeneratedTypesNet90
8383
DataFlow.CompilerGeneratedTypesRelease
8484
DataFlow.CompilerGeneratedTypesReleaseNet90
8585
DataFlow.ConstructorDataFlow
86+
DataFlow.DeconstructFieldTarget
87+
DataFlow.DeconstructUserDefinedConversion
8688
DataFlow.DependencyInjectionPattern
8789
DataFlow.DynamicDependencyDataflow
8890
DataFlow.ExponentialDataFlow

‎src/tools/illink/src/ILLink.RoslynAnalyzer/DataFlow/LocalDataFlowVisitor.cs‎

Lines changed: 600 additions & 50 deletions
Large diffs are not rendered by default.

‎src/tools/illink/src/ILLink.RoslynAnalyzer/DataFlow/LocalStateAndContextLattice.cs‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
33

44
using System;
5-
using ILLink.Shared;
65
using ILLink.Shared.DataFlow;
76

87
namespace ILLink.RoslynAnalyzer.DataFlow
@@ -25,7 +24,9 @@ public bool Equals(LocalStateAndContext<TValue, TContext> other) =>
2524
LocalState.Equals(other.LocalState) && Context.Equals(other.Context);
2625

2726
public override bool Equals(object? obj) => obj is LocalStateAndContext<TValue, TContext> other && Equals(other);
28-
public override int GetHashCode() => HashUtils.Combine(LocalState, Context);
27+
28+
// Local dataflow states are mutable and should never be used as dictionary keys.
29+
public override int GetHashCode() => throw new NotImplementedException();
2930
}
3031

3132
public readonly struct LocalStateAndContextLattice<TValue, TContext, TValueLattice, TContextLattice> : ILattice<LocalStateAndContext<TValue, TContext>>

‎src/tools/illink/src/ILLink.RoslynAnalyzer/DataFlow/LocalStateLattice.cs‎

Lines changed: 82 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
33

44
using System;
5+
using System.Collections.Generic;
56
using ILLink.Shared.DataFlow;
67
using Microsoft.CodeAnalysis;
78
using Microsoft.CodeAnalysis.FlowAnalysis;
@@ -35,6 +36,65 @@ public override string ToString()
3536
}
3637
}
3738

39+
public readonly struct CapturedTargetKey : IEquatable<CapturedTargetKey>
40+
{
41+
private readonly IOperation Operation;
42+
43+
public CapturedTargetKey(IOperation operation) => Operation = operation;
44+
45+
public bool Equals(CapturedTargetKey other) => Operation == other.Operation;
46+
47+
public override bool Equals(object obj)
48+
=> obj is CapturedTargetKey inst && Equals(inst);
49+
50+
public override int GetHashCode() => Operation.GetHashCode();
51+
}
52+
53+
public readonly struct CapturedTargetValue<TValue> : IEquatable<CapturedTargetValue<TValue>>, IDeepCopyValue<CapturedTargetValue<TValue>>
54+
where TValue : IEquatable<TValue>
55+
{
56+
public readonly bool HasValue;
57+
58+
public readonly TValue Value;
59+
60+
public CapturedTargetValue(TValue value) => (HasValue, Value) = (true, value);
61+
62+
public bool Equals(CapturedTargetValue<TValue> other) =>
63+
HasValue == other.HasValue &&
64+
(!HasValue || EqualityComparer<TValue>.Default.Equals(Value, other.Value));
65+
66+
public override bool Equals(object obj)
67+
=> obj is CapturedTargetValue<TValue> inst && Equals(inst);
68+
69+
public override int GetHashCode() => HasValue ? EqualityComparer<TValue>.Default.GetHashCode(Value) : 0;
70+
71+
public CapturedTargetValue<TValue> DeepCopy() =>
72+
HasValue
73+
? new CapturedTargetValue<TValue>(
74+
Value is IDeepCopyValue<TValue> copyValue ? copyValue.DeepCopy() : Value)
75+
: default;
76+
}
77+
78+
public readonly struct CapturedTargetValueLattice<TValue, TValueLattice> : ILattice<CapturedTargetValue<TValue>>
79+
where TValue : IEquatable<TValue>
80+
where TValueLattice : ILattice<TValue>
81+
{
82+
private readonly TValueLattice _valueLattice;
83+
84+
public CapturedTargetValueLattice(TValueLattice valueLattice) => _valueLattice = valueLattice;
85+
86+
public CapturedTargetValue<TValue> Top => default;
87+
88+
public CapturedTargetValue<TValue> Meet(CapturedTargetValue<TValue> left, CapturedTargetValue<TValue> right)
89+
{
90+
if (!left.HasValue)
91+
return right.DeepCopy();
92+
if (!right.HasValue)
93+
return left.DeepCopy();
94+
return new CapturedTargetValue<TValue>(_valueLattice.Meet(left.Value, right.Value));
95+
}
96+
}
97+
3898
public struct LocalState<TValue> : IEquatable<LocalState<TValue>>
3999
where TValue : IEquatable<TValue>
40100
{
@@ -45,24 +105,38 @@ public struct LocalState<TValue> : IEquatable<LocalState<TValue>>
45105
// are tracked as part of the dictionary of values, keyed by LocalKey.
46106
public DefaultValueDictionary<CaptureId, ValueSet<CapturedReferenceValue>> CapturedReferences;
47107

48-
public LocalState(DefaultValueDictionary<LocalKey, TValue> dictionary, DefaultValueDictionary<CaptureId, ValueSet<CapturedReferenceValue>> capturedReferences)
108+
// Stores target receiver and index values evaluated by deconstruction l-value captures.
109+
public DefaultValueDictionary<CapturedTargetKey, CapturedTargetValue<TValue>> CapturedTargetValues;
110+
111+
public LocalState(
112+
DefaultValueDictionary<LocalKey, TValue> dictionary,
113+
DefaultValueDictionary<CaptureId, ValueSet<CapturedReferenceValue>> capturedReferences,
114+
DefaultValueDictionary<CapturedTargetKey, CapturedTargetValue<TValue>> capturedTargetValues)
49115
{
50116
Dictionary = dictionary;
51117
CapturedReferences = capturedReferences;
118+
CapturedTargetValues = capturedTargetValues;
52119
}
53120

54121
public LocalState(DefaultValueDictionary<LocalKey, TValue> dictionary)
55-
: this(dictionary, new DefaultValueDictionary<CaptureId, ValueSet<CapturedReferenceValue>>(default(ValueSet<CapturedReferenceValue>)))
122+
: this(
123+
dictionary,
124+
new DefaultValueDictionary<CaptureId, ValueSet<CapturedReferenceValue>>(default(ValueSet<CapturedReferenceValue>)),
125+
new DefaultValueDictionary<CapturedTargetKey, CapturedTargetValue<TValue>>(default(CapturedTargetValue<TValue>)))
56126
{
57127
}
58128

59-
public bool Equals(LocalState<TValue> other) => Dictionary.Equals(other.Dictionary);
129+
public bool Equals(LocalState<TValue> other) =>
130+
Dictionary.Equals(other.Dictionary) &&
131+
CapturedReferences.Equals(other.CapturedReferences) &&
132+
CapturedTargetValues.Equals(other.CapturedTargetValues);
60133

61134
public override bool Equals(object obj)
62135
=> obj is LocalState<TValue> inst && Equals(inst);
63136

64137
public TValue Get(LocalKey key) => Dictionary.Get(key);
65138

139+
// Local dataflow states are mutable and should never be used as dictionary keys.
66140
public override int GetHashCode()
67141
=> throw new NotImplementedException();
68142

@@ -78,11 +152,14 @@ public override int GetHashCode()
78152
{
79153
public readonly DictionaryLattice<LocalKey, TValue, TValueLattice> Lattice;
80154
public readonly DictionaryLattice<CaptureId, ValueSet<CapturedReferenceValue>, ValueSetLattice<CapturedReferenceValue>> CapturedReferenceLattice;
155+
public readonly DictionaryLattice<CapturedTargetKey, CapturedTargetValue<TValue>, CapturedTargetValueLattice<TValue, TValueLattice>> CapturedTargetValueLattice;
81156

82157
public LocalStateLattice(TValueLattice valueLattice)
83158
{
84159
Lattice = new DictionaryLattice<LocalKey, TValue, TValueLattice>(valueLattice);
85160
CapturedReferenceLattice = new DictionaryLattice<CaptureId, ValueSet<CapturedReferenceValue>, ValueSetLattice<CapturedReferenceValue>>(default(ValueSetLattice<CapturedReferenceValue>));
161+
CapturedTargetValueLattice = new DictionaryLattice<CapturedTargetKey, CapturedTargetValue<TValue>, CapturedTargetValueLattice<TValue, TValueLattice>>(
162+
new CapturedTargetValueLattice<TValue, TValueLattice>(valueLattice));
86163
Top = new(Lattice.Top);
87164
}
88165

@@ -92,7 +169,8 @@ public LocalState<TValue> Meet(LocalState<TValue> left, LocalState<TValue> right
92169
{
93170
var dictionary = Lattice.Meet(left.Dictionary, right.Dictionary);
94171
var capturedProperties = CapturedReferenceLattice.Meet(left.CapturedReferences, right.CapturedReferences);
95-
return new LocalState<TValue>(dictionary, capturedProperties);
172+
var capturedTargetValues = CapturedTargetValueLattice.Meet(left.CapturedTargetValues, right.CapturedTargetValues);
173+
return new LocalState<TValue>(dictionary, capturedProperties, capturedTargetValues);
96174
}
97175
}
98176
}

‎src/tools/illink/src/ILLink.RoslynAnalyzer/TrimAnalysis/TrimAnalysisVisitor.cs‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ public override MultiValue VisitConversion(IConversionOperation operation, State
129129
var value = base.VisitConversion(operation, state);
130130

131131
if (operation.OperatorMethod is IMethodSymbol method)
132-
return method.ReturnType.IsTypeInterestingForDataflow(isByRef: method.ReturnsByRef) ? new MethodReturnValue(method, isNewObj: false) : value;
132+
return GetConversionValue(method, value);
133133

134134
// TODO - is it possible to have annotation on the operator method parameters?
135135
// if so, will these be checked here?
@@ -260,6 +260,17 @@ public override MultiValue GetBackingFieldTargetValue(IPropertyReferenceOperatio
260260
return new FieldValue(property);
261261
}
262262

263+
public override MultiValue GetTupleElementValue(IFieldSymbol tupleElement)
264+
{
265+
return new FieldValue(tupleElement);
266+
}
267+
268+
public override MultiValue GetConversionValue(IMethodSymbol conversionOperator, MultiValue operandValue)
269+
{
270+
return conversionOperator.ReturnType.IsTypeInterestingForDataflow(isByRef: conversionOperator.ReturnsByRef)
271+
? new MethodReturnValue(conversionOperator, isNewObj: false)
272+
: operandValue;
273+
}
263274

264275
public override MultiValue GetParameterTargetValue(IParameterSymbol parameter)
265276
{

‎src/tools/illink/test/ILLink.RoslynAnalyzer.Tests/DataFlowTests.cs‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,18 @@ public Task DataflowInLocalMethodGroupArgument()
148148
return RunTest();
149149
}
150150

151+
[Fact]
152+
public Task DeconstructFieldTarget()
153+
{
154+
return RunTest();
155+
}
156+
157+
[Fact]
158+
public Task DeconstructUserDefinedConversion()
159+
{
160+
return RunTest();
161+
}
162+
151163
[Fact]
152164
public Task DependencyInjectionPattern()
153165
{

0 commit comments

Comments
 (0)