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

Fix dynamic type index calculation in ApplyAttributeTypeVisitor #2840

Merged

Conversation

ElektroKill
Copy link
Contributor

@ElektroKill ElektroKill commented Nov 18, 2022

Link to issue(s) this covers:
N/A

Problem

ILSpy did not correctly decompile volatile dynamic fields.

Example:

static volatile dynamic volatileField;

would decompile to:

static volatile object volatileField;

The issue lies in the implementation of ApplyAttributeTypeVisitor. This class did not properly handle modifier signatures (CModOpt and CModReqd) which are used to indicate that a field is volatile.

Roslyn implementation: https://github.com/dotnet/roslyn/blob/29cdbf9a70ae83e6b7b914182fc78a7ed122a0a0/src/Compilers/CSharp/Portable/Symbols/Compilation_WellKnownMembers.cs#L850

Solution

Modifier signatures are now correctly handled. The dynamicTypeIndex is incremented for every modifier type to match Roslyn's behavior when it generates the DynamicAttribute attributes. The "crazy special case" for readonly return types has been removed as those include a modifier signature of InAttribute.

The dynamic "pretty" test was extended to include a dynamic volatile field.

@@ -225,7 +225,6 @@ private IType DecodeTypeAndVolatileFlag()
if (ty is ModifiedType mod && mod.Modifier.Name == "IsVolatile" && mod.Modifier.Namespace == "System.Runtime.CompilerServices")
{
Volatile.Write(ref this.isVolatile, true);
ty = mod.ElementType;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to get rid of the ModifiedType, otherwise it might confuse other parts of the type system that don't expect it to be present (e.g. in overload resolution).
But I think you can re-order the ApplyAttributesToType call to come before this if.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't ApplyAttributesToType already remove modifier types? Or is that option not enabled by default? It seems that other places using ApplyAttributesToType where modifier types might appear do not have some special handling for them (eg. method return types).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah right, I forgot that ApplyAttributesToType usually removed modopts.
Pretty much only the ILSpy UI uses TypeSystemOptions.KeepModifiers to keep them around, and in that case it seems harmless to also keep IsVolatile around.

@dgrunwald dgrunwald merged commit 260f561 into icsharpcode:master Nov 26, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants