Skip to content
This repository was archived by the owner on Sep 25, 2026. It is now read-only.

Support array types in generic parameter - #2576

Merged
LakshanF merged 3 commits into
dotnet:feature/damAnalyzerfrom
LakshanF:GenericArrayFix
Feb 2, 2022
Merged

LakshanF merged 3 commits into
dotnet:feature/damAnalyzerfrom
LakshanF:GenericArrayFix

Conversation

@LakshanF

@LakshanF LakshanF commented Feb 2, 2022 •

Copy link
Copy Markdown
Contributor

Fix for issue #2574,

We create a SystemTypeValue of System.Array for Generic arguments that are of SymbolKind.ArrayType

@sbomer

sbomer commented Feb 2, 2022

Copy link
Copy Markdown
Member

Looks like the linker will resolve array type arguments to System.Array:

// Array types that are dynamically accessed should resolve to System.Array instead of its element type - which is what Cecil resolves to.
// Any data flow annotations placed on a type parameter which receives an array type apply to the array itself. None of the members in its
// element type should be marked.
public TypeDefinition? ResolveToTypeDefinition (TypeReference typeReference)
{
if (typeReference is ArrayType)
return BCL.FindPredefinedType ("System", "Array", _context);
return _context.TryResolve (typeReference);
}

(I don't know if we can get there with an array type argument since I think this code might resolve array types to their element type first:

TypeDefinition? argumentTypeDef = MarkType (argument, new DependencyInfo (DependencyKind.GenericArgumentType, instance));
- it would be good to add tests to check that the required System.Array members are actually kept.)

It probably won't make a difference for the analyzer since I don't think System.Array has or will have annotated members, but I think the correct thing to do here to model reflection is to have the array case hit this logic for System.Array:

} else if (uniqueValue is ValueWithDynamicallyAccessedMembers valueWithDynamicallyAccessedMembers) {
var availableMemberTypes = valueWithDynamicallyAccessedMembers.DynamicallyAccessedMemberTypes;
if (!Annotations.SourceHasRequiredAnnotations (availableMemberTypes, targetValue.DynamicallyAccessedMemberTypes, out var missingMemberTypes)) {
(var diagnosticId, var diagnosticArguments) = Annotations.GetDiagnosticForAnnotationMismatch (valueWithDynamicallyAccessedMembers, targetValue, missingMemberTypes);
diagnosticContext.AddDiagnostic (diagnosticId, diagnosticArguments);
}

SymbolKind.ErrorType => UnknownValue.Instance,
// What about things like ArrayType or PointerType and so on. Linker treats these as "named types" since it can resolve them to concrete type
SymbolKind.ArrayType => new ArrayValue (GetTypeValueNodeFromGenericArgument (((IArrayTypeSymbol) type).ElementType)),
// What about things like PointerType and so on. Linker treats these as "named types" since it can resolve them to concrete type

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we're here, we should handle these as well - linker resolves pointers and alike to their base element type - so for example "foo *" is resolved to "foo" and then tracked like that. I don't actually know if that is correct from semantics point of view (as in, what will reflection do). So it would be good to figure out the reflection behavior and potentially file an issue against linker as well.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think pointers can be used as generic arguments, at least in C# - do we even need to handle these?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's true, at least for now - I vaguely remember that there's some discussion to allow refs there (reflection could really use this to avoid boxing I think), but I could be wrong.

@LakshanF
LakshanF merged commit 97cb7eb into dotnet:feature/damAnalyzer Feb 2, 2022
@LakshanF
LakshanF deleted the GenericArrayFix branch February 2, 2022 19:39
agocke pushed a commit to dotnet/runtime that referenced this pull request Nov 16, 2022
* First mechanical fix for issue dotnet/linker#2574

* FB

* Reverting to the original

Commit migrated from dotnet/linker@97cb7eb
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants