From 8b4e0d6af260da953c1ad192b80b085d66361783 Mon Sep 17 00:00:00 2001 From: Sven Boemer Date: Fri, 18 Mar 2022 11:39:02 -0700 Subject: [PATCH] Fix empty/null handling for Type.BaseType intrinsic --- src/ILLink.Shared/TrimAnalysis/HandleCallAction.cs | 6 ++++++ .../DataFlow/TypeBaseTypeDataFlow.cs | 4 +--- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/src/ILLink.Shared/TrimAnalysis/HandleCallAction.cs b/src/ILLink.Shared/TrimAnalysis/HandleCallAction.cs index c20e3b6f0c7a..e4976fff53d3 100644 --- a/src/ILLink.Shared/TrimAnalysis/HandleCallAction.cs +++ b/src/ILLink.Shared/TrimAnalysis/HandleCallAction.cs @@ -544,6 +544,11 @@ public bool Invoke (MethodProxy calledMethod, MultiValue instanceValue, IReadOnl // 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; @@ -580,6 +585,7 @@ public bool Invoke (MethodProxy calledMethod, MultiValue instanceValue, IReadOnl 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 diff --git a/test/Mono.Linker.Tests.Cases/DataFlow/TypeBaseTypeDataFlow.cs b/test/Mono.Linker.Tests.Cases/DataFlow/TypeBaseTypeDataFlow.cs index 646994d112e1..270d436162d0 100644 --- a/test/Mono.Linker.Tests.Cases/DataFlow/TypeBaseTypeDataFlow.cs +++ b/test/Mono.Linker.Tests.Cases/DataFlow/TypeBaseTypeDataFlow.cs @@ -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 (); }