Skip to content

Commit 947f66a

Browse files
authored
Fix empty/null handling for Type.BaseType intrinsic (#2694)
1 parent 1d77412 commit 947f66a

File tree

2 files changed

+7
-3
lines changed

2 files changed

+7
-3
lines changed

src/ILLink.Shared/TrimAnalysis/HandleCallAction.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -614,6 +614,11 @@ GenericParameterValue genericParam
614614
// Type.BaseType
615615
//
616616
case IntrinsicId.Type_get_BaseType: {
617+
if (instanceValue.IsEmpty ()) {
618+
returnValue = MultiValueLattice.Top;
619+
break;
620+
}
621+
617622
foreach (var value in instanceValue) {
618623
if (value is ValueWithDynamicallyAccessedMembers valueWithDynamicallyAccessedMembers) {
619624
DynamicallyAccessedMemberTypes propagatedMemberTypes = DynamicallyAccessedMemberTypes.None;
@@ -650,6 +655,7 @@ GenericParameterValue genericParam
650655
AddReturnValue (GetMethodReturnValue (calledMethod, returnValueDynamicallyAccessedMemberTypes));
651656
} else if (value == NullValue.Instance) {
652657
// Ignore nulls - null.BaseType will fail at runtime, but it has no effect on static analysis
658+
returnValue ??= MultiValueLattice.Top;
653659
continue;
654660
} else {
655661
// Unknown input - propagate a return value without any annotation - we know it's a Type but we know nothing about it

test/Mono.Linker.Tests.Cases/DataFlow/TypeBaseTypeDataFlow.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -229,19 +229,17 @@ static void TestAnnotatedAndUnannotated (
229229
type.BaseType.RequiresPublicMethods ();
230230
}
231231

232-
[ExpectedWarning ("IL2072", nameof (DataFlowTypeExtensions) + "." + nameof (DataFlowTypeExtensions.RequiresPublicMethods))]
233232
static void TestNull ()
234233
{
235234
Type type = null;
236235
type.BaseType.RequiresPublicMethods ();
237236
}
238237

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

0 commit comments

Comments
 (0)