Skip to content

Commit

Permalink
Fix decompilation of record with missing base type
Browse files Browse the repository at this point in the history
This commit updates `RecordDecompiler` to avoid a null ref when the
decompiler is unable to determine the base type of a record (e.g.
because the base type is defined in another assembly that is not
loaded).
  • Loading branch information
andrewcrawley committed Jun 22, 2023
1 parent d57b08c commit 7451b21
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions ICSharpCode.Decompiler/CSharp/RecordDecompiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ public RecordDecompiler(IDecompilerTypeSystem dts, ITypeDefinition recordTypeDef
this.settings = settings;
this.cancellationToken = cancellationToken;
this.baseClass = recordTypeDef.DirectBaseTypes.FirstOrDefault(b => b.Kind == TypeKind.Class);
this.isStruct = baseClass.IsKnownType(KnownTypeCode.ValueType);
this.isInheritedRecord = !isStruct && !baseClass.IsKnownType(KnownTypeCode.Object);
this.isStruct = baseClass?.IsKnownType(KnownTypeCode.ValueType) ?? false;
this.isInheritedRecord = !isStruct && !(baseClass?.IsKnownType(KnownTypeCode.Object) ?? false);
this.isSealed = recordTypeDef.IsSealed;
DetectAutomaticProperties();
this.orderedMembers = DetectMemberOrder(recordTypeDef, backingFieldToAutoProperty);
Expand Down Expand Up @@ -292,7 +292,7 @@ public bool MethodIsGenerated(IMethod method)
// virtual bool Equals(R? other): generated unless user-declared
return IsGeneratedEquals(method);
}
else if (isInheritedRecord && NormalizeTypeVisitor.TypeErasure.EquivalentTypes(paramType, baseClass) && method.IsOverride)
else if (isInheritedRecord && baseClass != null && NormalizeTypeVisitor.TypeErasure.EquivalentTypes(paramType, baseClass) && method.IsOverride)
{
// override bool Equals(BaseClass? obj): always generated
return true;
Expand Down Expand Up @@ -772,7 +772,7 @@ private bool IsGeneratedEquals(IMethod method)
return false;
if (!(conditions[pos] is Call { Method: { Name: "Equals" } } call))
return false;
if (!NormalizeTypeVisitor.TypeErasure.EquivalentTypes(call.Method.DeclaringType, baseClass))
if (baseClass != null && !NormalizeTypeVisitor.TypeErasure.EquivalentTypes(call.Method.DeclaringType, baseClass))
return false;
if (call.Arguments.Count != 2)
return false;
Expand Down

0 comments on commit 7451b21

Please sign in to comment.