Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug with new C# 11 nint/nuint handling for IntPtr/UIntPtr #3004

Closed
ElektroKill opened this issue Jun 11, 2023 · 0 comments
Closed

Bug with new C# 11 nint/nuint handling for IntPtr/UIntPtr #3004

ElektroKill opened this issue Jun 11, 2023 · 0 comments
Labels
Bug Decompiler The decompiler engine itself

Comments

@ElektroKill
Copy link
Contributor

Input code

System.IntPtr from .NET 7 System.Private.CoreLib

Erroneous output

An assertion is hit regarding using declarations:

ICSharpCode.ILSpy.AssertionFailedException: Should not insert using declaration for namespace that is missing from the superset: System.Runtime.Versioning
   at ICSharpCode.ILSpy.ILSpyTraceListener.Fail(String message, String detailMessage) in E:\Programming\RiderProjects\ILSpy\ILSpy\ILSpyTraceListener.cs:line 86
   at System.Diagnostics.TraceInternal.Fail(String message, String detailMessage)
   at System.Diagnostics.TraceInternal.TraceProvider.Fail(String message, String detailMessage)
   at System.Diagnostics.Debug.Fail(String message, String detailMessage)
   at System.Diagnostics.Debug.Assert(Boolean condition, String message, String detailMessage)
   at System.Diagnostics.Debug.Assert(Boolean condition, String message)
   at ICSharpCode.Decompiler.CSharp.Transforms.IntroduceUsingDeclarations.Run(AstNode rootNode, TransformContext context) in E:\Programming\RiderProjects\ILSpy\ICSharpCode.Decompiler\CSharp\Transforms\IntroduceUsingDeclarations.cs:line 57
   at ICSharpCode.Decompiler.CSharp.CSharpDecompiler.RunTransforms(AstNode rootNode, DecompileRun decompileRun, ITypeResolveContext decompilationContext) in E:\Programming\RiderProjects\ILSpy\ICSharpCode.Decompiler\CSharp\CSharpDecompiler.cs:line 564
   at ICSharpCode.Decompiler.CSharp.CSharpDecompiler.Decompile(IEnumerable`1 definitions) in E:\Programming\RiderProjects\ILSpy\ICSharpCode.Decompiler\CSharp\CSharpDecompiler.cs:line 1088
   at ICSharpCode.Decompiler.CSharp.CSharpDecompiler.Decompile(EntityHandle[] definitions) in E:\Programming\RiderProjects\ILSpy\ICSharpCode.Decompiler\CSharp\CSharpDecompiler.cs:line 1004
   at ICSharpCode.ILSpy.CSharpLanguage.DecompileType(ITypeDefinition type, ITextOutput output, DecompilationOptions options) in E:\Programming\RiderProjects\ILSpy\ILSpy\Languages\CSharpLanguage.cs:line 334
   at ICSharpCode.ILSpy.TreeNodes.TypeTreeNode.Decompile(Language language, ITextOutput output, DecompilationOptions options) in E:\Programming\RiderProjects\ILSpy\ILSpy\TreeNodes\TypeTreeNode.cs:line 132
   at ICSharpCode.ILSpy.TextView.DecompilerTextView.DecompileNodes(DecompilationContext context, ITextOutput textOutput) in E:\Programming\RiderProjects\ILSpy\ILSpy\TextView\DecompilerTextView.cs:line 914
   at ICSharpCode.ILSpy.TextView.DecompilerTextView.<>c__DisplayClass52_0.<DecompileAsync>b__0() in E:\Programming\RiderProjects\ILSpy\ILSpy\TextView\DecompilerTextView.cs:line 884

After some debugging the issue seems to be caused to the type replacement from a MetadataTypeDefinition to a SpecialType occurring here:

ty = ApplyAttributeTypeVisitor.ApplyAttributesToType(ty, Compilation, typeAttributes, metadata, customOptions, nullableContext);

This means that the call to GetDefinition in ResolveEntity will return null when asked to resolve the IntPtr definition.
return ResolveType(entityHandle, context).GetDefinition();

ResolveEntity is called by RequiredNamespaceCollector but due to the null return value, RequiredNamespaceCollector is unable to collect the true required namespace set.
CollectNamespaces(module.ResolveEntity(entity, genericContext), module, namespaces);

Details

  • ILSpy compiled from source code (bdb3235)
@ElektroKill ElektroKill added Bug Decompiler The decompiler engine itself labels Jun 11, 2023
@ElektroKill ElektroKill changed the title Bug with new C# 11 Bug with new C# 11 nint/nuint handling for IntPtr/UIntPtr Jun 11, 2023
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Dec 31, 2023
mattsh247 pushed a commit to mattsh247/ILSpy that referenced this issue Jul 30, 2024
…or System.IntPtr when C# 11 native integers are enabled.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Bug Decompiler The decompiler engine itself
Projects
None yet
Development

No branches or pull requests

1 participant