ILSpy icon indicating copy to clipboard operation
ILSpy copied to clipboard

Decompilation of record type produces an error if base type is unavailable

Open andrewcrawley opened this issue 3 years ago • 0 comments

Steps to reproduce

Use ILSpy to decompile a record type that derives from a record defined in another assembly that is not loaded.

I ran into this while testing against Visual Studio 2022 version 17.7 preview 1 assemblies:

  1. Remove all loaded assemblies in ILSpy
  2. Open <VS install folder>\Common7\IDE\Microsoft.VisualStudio.Shell.UI.Internal.dll
  3. Browse to Microsoft.VisualStudio.Services.UnifiedSettings.DataModel.RemovedSettingRegistration
  4. Observe the error message described below
  5. Add <VS install folder>\Common7\IDE\PublicAssemblies\Microsoft.VisualStudio.Settings.Registration.dll to the set of loaded assemblies
  6. Attempt the decompilation again
  7. Observe that it succeeds

Error message shown

Error decompiling @0200020F Microsoft.VisualStudio.Services.UnifiedSettings.DataModel.RemovedSettingRegistration
in assembly "C:\Program Files\Microsoft Visual Studio\2022\main\Common7\IDE\Microsoft.VisualStudio.Shell.UI.Internal.dll"
 ---> System.NullReferenceException: Object reference not set to an instance of an object.
   at ICSharpCode.Decompiler.TypeSystem.TypeSystemExtensions.IsKnownType(IType type, KnownTypeCode knownType) in TypeSystemExtensions.cs:line 316
   at ICSharpCode.Decompiler.CSharp.RecordDecompiler..ctor(IDecompilerTypeSystem dts, ITypeDefinition recordTypeDef, DecompilerSettings settings, CancellationToken cancellationToken) in RecordDecompiler.cs:line 56
   at ICSharpCode.Decompiler.CSharp.CSharpDecompiler.DoDecompile(ITypeDefinition typeDef, DecompileRun decompileRun, ITypeResolveContext decompilationContext) in CSharpDecompiler.cs:line 1307
-- continuing with outer exception (ICSharpCode.Decompiler.DecompilerException) --
   at ICSharpCode.Decompiler.CSharp.CSharpDecompiler.DoDecompile(ITypeDefinition typeDef, DecompileRun decompileRun, ITypeResolveContext decompilationContext) in CSharpDecompiler.cs:line 1447
   at ICSharpCode.Decompiler.CSharp.CSharpDecompiler.Decompile(IEnumerable`1 definitions) in CSharpDecompiler.cs:line 1032
   at ICSharpCode.Decompiler.CSharp.CSharpDecompiler.Decompile(EntityHandle[] definitions) in CSharpDecompiler.cs:line 1004
   at ICSharpCode.ILSpy.CSharpLanguage.DecompileType(ITypeDefinition type, ITextOutput output, DecompilationOptions options) in CSharpLanguage.cs:line 334
   at ICSharpCode.ILSpy.TreeNodes.TypeTreeNode.Decompile(Language language, ITextOutput output, DecompilationOptions options) in TypeTreeNode.cs:line 132
   at ICSharpCode.ILSpy.TextView.DecompilerTextView.DecompileNodes(DecompilationContext context, ITextOutput textOutput) in DecompilerTextView.cs:line 914
   at ICSharpCode.ILSpy.TextView.DecompilerTextView.<>c__DisplayClass52_0.<DecompileAsync>b__0() in DecompilerTextView.cs:line 884

Details

ILSpy version 8.0.0.7345 .NET version 6.0.5-servicing.22213.9+70ae3df4a6f3c92fb6b315afc405edd10ff38579

I have a PR to fix this by adding null checks in RecordDecompiler - with these checks, the decompiled code includes unnecessary [CompilerGenerated] methods, but the crash is avoided.

andrewcrawley avatar Jun 22 '23 02:06 andrewcrawley