Skip to content

Commit

Permalink
Fix empty/null handling for Type.BaseType intrinsic (#2694)
Browse files Browse the repository at this point in the history
  • Loading branch information
sbomer authored Mar 18, 2022
1 parent 1d77412 commit 947f66a
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
6 changes: 6 additions & 0 deletions src/ILLink.Shared/TrimAnalysis/HandleCallAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -614,6 +614,11 @@ GenericParameterValue genericParam
// Type.BaseType
//
case IntrinsicId.Type_get_BaseType: {
if (instanceValue.IsEmpty ()) {
returnValue = MultiValueLattice.Top;
break;
}

foreach (var value in instanceValue) {
if (value is ValueWithDynamicallyAccessedMembers valueWithDynamicallyAccessedMembers) {
DynamicallyAccessedMemberTypes propagatedMemberTypes = DynamicallyAccessedMemberTypes.None;
Expand Down Expand Up @@ -650,6 +655,7 @@ GenericParameterValue genericParam
AddReturnValue (GetMethodReturnValue (calledMethod, returnValueDynamicallyAccessedMemberTypes));
} else if (value == NullValue.Instance) {
// Ignore nulls - null.BaseType will fail at runtime, but it has no effect on static analysis
returnValue ??= MultiValueLattice.Top;
continue;
} else {
// Unknown input - propagate a return value without any annotation - we know it's a Type but we know nothing about it
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -229,19 +229,17 @@ static void TestAnnotatedAndUnannotated (
type.BaseType.RequiresPublicMethods ();
}

[ExpectedWarning ("IL2072", nameof (DataFlowTypeExtensions) + "." + nameof (DataFlowTypeExtensions.RequiresPublicMethods))]
static void TestNull ()
{
Type type = null;
type.BaseType.RequiresPublicMethods ();
}

[ExpectedWarning ("IL2072", nameof (DataFlowTypeExtensions) + "." + nameof (DataFlowTypeExtensions.RequiresPublicMethods))]
static void TestNoValue ()
{
Type t = null;
Type noValue = Type.GetTypeFromHandle (t.TypeHandle);
// Warns about the base type even though the above throws an exception at runtime.
// No warning because the above throws an exception at runtime.
noValue.BaseType.RequiresPublicMethods ();
}

Expand Down

0 comments on commit 947f66a

Please sign in to comment.