Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System;
using ILLink.Shared;
using ILLink.Shared.DataFlow;

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

public override bool Equals(object? obj) => obj is LocalStateAndContext<TValue, TContext> other && Equals(other);

// Local dataflow states are mutable and should never be used as dictionary keys.
public override int GetHashCode() => throw new NotImplementedException();
public override int GetHashCode() => HashUtils.Combine(LocalState, Context);
}

public readonly struct LocalStateAndContextLattice<TValue, TContext, TValueLattice, TContextLattice> : ILattice<LocalStateAndContext<TValue, TContext>>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System;
using System.Collections.Generic;
using ILLink.Shared.DataFlow;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.FlowAnalysis;
Expand Down Expand Up @@ -36,65 +35,6 @@ public override string ToString()
}
}

public readonly struct CapturedTargetKey : IEquatable<CapturedTargetKey>
{
private readonly IOperation Operation;

public CapturedTargetKey(IOperation operation) => Operation = operation;

public bool Equals(CapturedTargetKey other) => Operation == other.Operation;

public override bool Equals(object obj)
=> obj is CapturedTargetKey inst && Equals(inst);

public override int GetHashCode() => Operation.GetHashCode();
}

public readonly struct CapturedTargetValue<TValue> : IEquatable<CapturedTargetValue<TValue>>, IDeepCopyValue<CapturedTargetValue<TValue>>
where TValue : IEquatable<TValue>
{
public readonly bool HasValue;

public readonly TValue Value;

public CapturedTargetValue(TValue value) => (HasValue, Value) = (true, value);

public bool Equals(CapturedTargetValue<TValue> other) =>
HasValue == other.HasValue &&
(!HasValue || EqualityComparer<TValue>.Default.Equals(Value, other.Value));

public override bool Equals(object obj)
=> obj is CapturedTargetValue<TValue> inst && Equals(inst);

public override int GetHashCode() => HasValue ? EqualityComparer<TValue>.Default.GetHashCode(Value) : 0;

public CapturedTargetValue<TValue> DeepCopy() =>
HasValue
? new CapturedTargetValue<TValue>(
Value is IDeepCopyValue<TValue> copyValue ? copyValue.DeepCopy() : Value)
: default;
}

public readonly struct CapturedTargetValueLattice<TValue, TValueLattice> : ILattice<CapturedTargetValue<TValue>>
where TValue : IEquatable<TValue>
where TValueLattice : ILattice<TValue>
{
private readonly TValueLattice _valueLattice;

public CapturedTargetValueLattice(TValueLattice valueLattice) => _valueLattice = valueLattice;

public CapturedTargetValue<TValue> Top => default;

public CapturedTargetValue<TValue> Meet(CapturedTargetValue<TValue> left, CapturedTargetValue<TValue> right)
{
if (!left.HasValue)
return right.DeepCopy();
if (!right.HasValue)
return left.DeepCopy();
return new CapturedTargetValue<TValue>(_valueLattice.Meet(left.Value, right.Value));
}
}

public struct LocalState<TValue> : IEquatable<LocalState<TValue>>
where TValue : IEquatable<TValue>
{
Expand All @@ -105,38 +45,24 @@ public struct LocalState<TValue> : IEquatable<LocalState<TValue>>
// are tracked as part of the dictionary of values, keyed by LocalKey.
public DefaultValueDictionary<CaptureId, ValueSet<CapturedReferenceValue>> CapturedReferences;

// Stores target receiver and index values evaluated by deconstruction l-value captures.
public DefaultValueDictionary<CapturedTargetKey, CapturedTargetValue<TValue>> CapturedTargetValues;

public LocalState(
DefaultValueDictionary<LocalKey, TValue> dictionary,
DefaultValueDictionary<CaptureId, ValueSet<CapturedReferenceValue>> capturedReferences,
DefaultValueDictionary<CapturedTargetKey, CapturedTargetValue<TValue>> capturedTargetValues)
public LocalState(DefaultValueDictionary<LocalKey, TValue> dictionary, DefaultValueDictionary<CaptureId, ValueSet<CapturedReferenceValue>> capturedReferences)
{
Dictionary = dictionary;
CapturedReferences = capturedReferences;
CapturedTargetValues = capturedTargetValues;
}

public LocalState(DefaultValueDictionary<LocalKey, TValue> dictionary)
: this(
dictionary,
new DefaultValueDictionary<CaptureId, ValueSet<CapturedReferenceValue>>(default(ValueSet<CapturedReferenceValue>)),
new DefaultValueDictionary<CapturedTargetKey, CapturedTargetValue<TValue>>(default(CapturedTargetValue<TValue>)))
: this(dictionary, new DefaultValueDictionary<CaptureId, ValueSet<CapturedReferenceValue>>(default(ValueSet<CapturedReferenceValue>)))
{
}

public bool Equals(LocalState<TValue> other) =>
Dictionary.Equals(other.Dictionary) &&
CapturedReferences.Equals(other.CapturedReferences) &&
CapturedTargetValues.Equals(other.CapturedTargetValues);
public bool Equals(LocalState<TValue> other) => Dictionary.Equals(other.Dictionary);

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

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

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

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

public LocalStateLattice(TValueLattice valueLattice)
{
Lattice = new DictionaryLattice<LocalKey, TValue, TValueLattice>(valueLattice);
CapturedReferenceLattice = new DictionaryLattice<CaptureId, ValueSet<CapturedReferenceValue>, ValueSetLattice<CapturedReferenceValue>>(default(ValueSetLattice<CapturedReferenceValue>));
CapturedTargetValueLattice = new DictionaryLattice<CapturedTargetKey, CapturedTargetValue<TValue>, CapturedTargetValueLattice<TValue, TValueLattice>>(
new CapturedTargetValueLattice<TValue, TValueLattice>(valueLattice));
Top = new(Lattice.Top);
}

Expand All @@ -169,8 +92,7 @@ public LocalState<TValue> Meet(LocalState<TValue> left, LocalState<TValue> right
{
var dictionary = Lattice.Meet(left.Dictionary, right.Dictionary);
var capturedProperties = CapturedReferenceLattice.Meet(left.CapturedReferences, right.CapturedReferences);
var capturedTargetValues = CapturedTargetValueLattice.Meet(left.CapturedTargetValues, right.CapturedTargetValues);
return new LocalState<TValue>(dictionary, capturedProperties, capturedTargetValues);
return new LocalState<TValue>(dictionary, capturedProperties);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ public override MultiValue VisitConversion(IConversionOperation operation, State
var value = base.VisitConversion(operation, state);

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

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

public override MultiValue GetTupleElementValue(IFieldSymbol tupleElement)
{
return new FieldValue(tupleElement);
}

public override MultiValue GetConversionValue(IMethodSymbol conversionOperator, MultiValue operandValue)
{
return conversionOperator.ReturnType.IsTypeInterestingForDataflow(isByRef: conversionOperator.ReturnsByRef)
? new MethodReturnValue(conversionOperator, isNewObj: false)
: operandValue;
}

public override MultiValue GetParameterTargetValue(IParameterSymbol parameter)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public static void Main()

class DeconstructedVariable
{
[ExpectedWarning("IL2077")]
[ExpectedWarning("IL2077", Tool.Trimmer | Tool.NativeAot, "https://github.com/dotnet/runtime/issues/123767")]
static void DeconstructVariableNoAnnotation((Type type, object instance) input)
{
var (type, instance) = input;
Expand All @@ -31,7 +31,7 @@ static void DeconstructVariableNoAnnotation((Type type, object instance) input)

static (Type type, object instance) GetInput(Type type, int unused) => (type, null);

[ExpectedWarning("IL2077")]
[ExpectedWarning("IL2077", Tool.Trimmer | Tool.NativeAot, "https://github.com/dotnet/runtime/issues/123767")]
static void DeconstructVariableFlowCapture(bool b = true)
{
// This creates a control-flow graph where the tuple elements assigned to
Expand Down Expand Up @@ -68,7 +68,7 @@ record TypeAndInstance(
// In IL based tools this is a behavior of the compiler. The attribute on the record declaration parameter
// is only propagated to the .ctor constructor parameter. The property and field attributes are applied to the
// generated property and field respectively. But none of the attributes is propagated to the Deconstruct method parameters.
[ExpectedWarning("IL2067")]
[ExpectedWarning("IL2067", Tool.Trimmer | Tool.NativeAot, "https://github.com/dotnet/runtime/issues/123767")]
static void DeconstructRecordWithAnnotation(TypeAndInstance value)
{
var (type, instance) = value;
Expand Down Expand Up @@ -102,7 +102,7 @@ static void DeconstructClassWithAnnotation(TypeAndInstanceManual value)
// test cases above. The source call and the synthesized Deconstruct() call must be
// tracked as two independent calls, not merged together.
[ExpectedWarning("IL2026", nameof(GetTypeAndInstance))]
[ExpectedWarning("IL2067")]
[ExpectedWarning("IL2067", Tool.Trimmer | Tool.NativeAot, "https://github.com/dotnet/runtime/issues/123767")]
static void DeconstructMethodCallSource()
{
var (type, instance) = GetTypeAndInstance();
Expand All @@ -129,7 +129,7 @@ static void DeconstructRecordManualWithAnnotation(TypeAndInstanceRecordManual va
type.RequiresPublicMethods();
}

[ExpectedWarning("IL2067")]
[ExpectedWarning("IL2067", Tool.Trimmer | Tool.NativeAot, "https://github.com/dotnet/runtime/issues/123767")]
static void DeconstructRecordManualWithMismatchAnnotation(TypeAndInstanceRecordManual value)
{
var (type, instance) = value;
Expand All @@ -142,14 +142,14 @@ static void DeconstructExtensionWithAnnotation(TypeAndInstanceExtension value)
type.RequiresPublicMethods();
}

[ExpectedWarning("IL2067")]
[ExpectedWarning("IL2067", Tool.Trimmer | Tool.NativeAot, "https://github.com/dotnet/runtime/issues/123767")]
static void DeconstructExtensionWithMismatchAnnotation(TypeAndInstanceExtension value)
{
var (type, instance) = value;
type.RequiresPublicFields();
}

[ExpectedWarning("IL2077")]
[ExpectedWarning("IL2077", Tool.Trimmer | Tool.NativeAot, "https://github.com/dotnet/runtime/issues/123767")]
static void DeconstructNestedTuple(((Type type, object instance) nested, object instance) input)
{
var ((type, instance), outerInstance) = input;
Expand All @@ -163,8 +163,6 @@ static void DeconstructTupleLiteral(
type.RequiresPublicMethods();
}

// The swap correctly propagates the annotation from typeWithMethods to first (via second),
// so no warning is produced here.
static void DeconstructTupleSwapSuccess(
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods)] Type typeWithMethods,
Type typeWithoutMethods)
Expand All @@ -175,7 +173,7 @@ static void DeconstructTupleSwapSuccess(
first.RequiresPublicMethods();
}

[ExpectedWarning("IL2067", nameof(DataFlowTypeExtensions.RequiresPublicMethods))]
[ExpectedWarning("IL2067", nameof(DataFlowTypeExtensions.RequiresPublicMethods), Tool.Trimmer | Tool.NativeAot, "https://github.com/dotnet/runtime/issues/123767")]
static void DeconstructTupleSwap(
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods)] Type typeWithMethods,
Type typeWithoutMethods)
Expand Down Expand Up @@ -268,8 +266,8 @@ public Type this[int index]
// different places: the receiver ahead of the source (VisitDeconstructionTargetSideEffects),
// and the index argument as part of performing the write (ProcessSingleTargetAssignment),
// matching the same (pre-existing) evaluation order used for an ordinary indexer assignment.
[ExpectedWarning("IL2026", nameof(GetIndexerHolder))]
[ExpectedWarning("IL2026", nameof(GetIndex))]
[ExpectedWarning("IL2026", nameof(GetIndexerHolder), Tool.Trimmer | Tool.NativeAot, "https://github.com/dotnet/runtime/issues/123767")]
[ExpectedWarning("IL2026", nameof(GetIndex), Tool.Trimmer | Tool.NativeAot, "https://github.com/dotnet/runtime/issues/123767")]
Comment on lines +269 to +270
static void DeconstructIndexerTargetSideEffect(Type first, Type second)
{
object other;
Expand All @@ -290,7 +288,7 @@ static void DeconstructParameterTarget(Type type, object instance)
// A discard target (IDiscardOperation) drops the corresponding source value entirely -
// there's nothing to check dataflow-wise, and it must not affect tracking of the other
// target in the same deconstruction.
[ExpectedWarning("IL2072")]
[ExpectedWarning("IL2072", Tool.Trimmer | Tool.NativeAot, "https://github.com/dotnet/runtime/issues/123767")]
static void DeconstructDiscardTarget()
{
(_, Type type) = (new object(), GetUnannotatedType());
Expand All @@ -309,8 +307,8 @@ static void DeconstructDiscardTarget()
// no equivalent to the indexer-Arguments ordering quirk here), so both are visited twice
// (once ahead of the source, once again performing the write) - verifying each still
// produces exactly one warning despite the double-visit.
[ExpectedWarning("IL2026", nameof(GetArrayForElementTarget))]
[ExpectedWarning("IL2026", nameof(GetArrayIndex))]
[ExpectedWarning("IL2026", nameof(GetArrayForElementTarget), Tool.Trimmer | Tool.NativeAot, "https://github.com/dotnet/runtime/issues/123767")]
[ExpectedWarning("IL2026", nameof(GetArrayIndex), Tool.Trimmer | Tool.NativeAot, "https://github.com/dotnet/runtime/issues/123767")]
static void DeconstructArrayElementTargetSideEffect(Type first, Type second)
{
object other;
Expand All @@ -323,7 +321,7 @@ static void DeconstructArrayElementTargetSideEffect(Type first, Type second)
// An implicit System.Index-based indexer target (IImplicitIndexerReferenceOperation),
// e.g. 'arr[^1]'. The receiver is a side-effecting expression visited ahead of the source,
// same as the explicit indexer and array element cases above.
[ExpectedWarning("IL2026", nameof(GetArrayForImplicitIndexerTarget))]
[ExpectedWarning("IL2026", nameof(GetArrayForImplicitIndexerTarget), Tool.Trimmer | Tool.NativeAot, "https://github.com/dotnet/runtime/issues/123767")]
static void DeconstructImplicitIndexerTargetSideEffect(Type first, Type second)
{
object other;
Expand All @@ -344,7 +342,7 @@ static void DeconstructWithUserDefinedConversion(
(ConversionTarget converted, object instance) = (typeWithMethods, new object());
}

[ExpectedWarning("IL2077")]
[ExpectedWarning("IL2077", Tool.Trimmer | Tool.NativeAot, "https://github.com/dotnet/runtime/issues/123767")]
static void DeconstructForeach((Type type, object instance)[] inputs)
{
foreach (var (type, instance) in inputs)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class DeconstructFieldTarget
static Type GetUnannotatedType() => null;

// Verify that assigning a deconstructed value to an annotated static field is validated.
[ExpectedWarning("IL2074", nameof(GetUnannotatedType))]
[ExpectedWarning("IL2074", nameof(GetUnannotatedType), Tool.Trimmer | Tool.NativeAot, "https://github.com/dotnet/runtime/issues/123767")]
public static void Main()
{
object other;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ struct ConversionSource
}

// The converted value is the conversion operator's return value, whose type is unannotated.
[ExpectedWarning("IL2074", nameof(ConversionSource))]
[ExpectedWarning("IL2074", nameof(ConversionSource), Tool.Trimmer | Tool.NativeAot, "https://github.com/dotnet/runtime/issues/123767")]
static void Test((ConversionSource value, object instance) input)
{
object instance;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,13 @@ static void TestExtensionMethodWithParamsMismatch()
GetWithMethods().ExtensionMembersMethodWithParamsMismatch(GetWithFields());
}

[ExpectedWarning("IL2072", nameof(GetWithMethods), nameof(ExtensionMembers.Deconstruct))]
[ExpectedWarning("IL2072", nameof(GetWithMethods), nameof(ExtensionMembers.Deconstruct), Tool.Trimmer | Tool.NativeAot, "https://github.com/dotnet/runtime/issues/123767")]
static void TestExtensionDeconstructMismatch()
{
var (first, second) = GetWithMethods();
}

[ExpectedWarning("IL3050", nameof(RequiresDynamicCode), Tool.NativeAot, "NativeAOT doesn't model DoesNotReturnIf on Deconstruct methods")]
[UnexpectedWarning("IL3050", nameof(RequiresDynamicCode), Tool.Analyzer | Tool.NativeAot, "Analyzer and NativeAOT don't model DoesNotReturnIf on Deconstruct methods")]
static void TestExtensionDeconstructDoesNotReturnIf()
{
(_, _) = !RuntimeFeature.IsDynamicCodeSupported;
Expand Down
Loading