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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -47,6 +47,7 @@ public interface I
}

private static dynamic field;
private static volatile dynamic volatileField;
private static object objectField;
public dynamic Property { get; set; }

Expand Down
15 changes: 4 additions & 11 deletions ICSharpCode.Decompiler/TypeSystem/ApplyAttributeTypeVisitor.cs
@@ -1,4 +1,4 @@
// Copyright (c) 2018 Daniel Grunwald
// Copyright (c) 2018 Daniel Grunwald
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
// software and associated documentation files (the "Software"), to deal in the Software
Expand Down Expand Up @@ -40,8 +40,7 @@ sealed class ApplyAttributeTypeVisitor : TypeVisitor
SRM.MetadataReader metadata,
TypeSystemOptions options,
Nullability nullableContext,
bool typeChildrenOnly = false,
bool isSignatureReturnType = false)
bool typeChildrenOnly = false)
{
bool hasDynamicAttribute = false;
bool[] dynamicAttributeData = null;
Expand Down Expand Up @@ -134,14 +133,6 @@ sealed class ApplyAttributeTypeVisitor : TypeVisitor
options, tupleElementNames,
nullability, nullableAttributeData
);
if (isSignatureReturnType && hasDynamicAttribute
&& inputType.SkipModifiers().Kind == TypeKind.ByReference
&& attributes.Value.HasKnownAttribute(metadata, KnownAttribute.IsReadOnly))
{
// crazy special case: `ref readonly` return takes one dynamic index more than
// a non-readonly `ref` return.
visitor.dynamicTypeIndex++;
}
if (typeChildrenOnly)
{
return inputType.VisitChildren(visitor);
Expand Down Expand Up @@ -190,6 +181,7 @@ sealed class ApplyAttributeTypeVisitor : TypeVisitor

public override IType VisitModOpt(ModifiedType type)
{
dynamicTypeIndex++;
if ((options & TypeSystemOptions.KeepModifiers) != 0)
return base.VisitModOpt(type);
else
Expand All @@ -198,6 +190,7 @@ public override IType VisitModOpt(ModifiedType type)

public override IType VisitModReq(ModifiedType type)
{
dynamicTypeIndex++;
if ((options & TypeSystemOptions.KeepModifiers) != 0)
return base.VisitModReq(type);
else
Expand Down
@@ -1,4 +1,4 @@
// Copyright (c) 2018 Daniel Grunwald
// Copyright (c) 2018 Daniel Grunwald
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
// software and associated documentation files (the "Software"), to deal in the Software
Expand Down Expand Up @@ -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.

}
ty = ApplyAttributeTypeVisitor.ApplyAttributesToType(ty, Compilation,
fieldDef.GetCustomAttributes(), metadata, module.OptionsForEntity(this),
Expand Down
@@ -1,4 +1,4 @@
// Copyright (c) 2018 Daniel Grunwald
// Copyright (c) 2018 Daniel Grunwald
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
// software and associated documentation files (the "Software"), to deal in the Software
Expand Down Expand Up @@ -271,8 +271,7 @@ private void DecodeSignature()
}
Debug.Assert(i == parameters.Length);
var returnType = ApplyAttributeTypeVisitor.ApplyAttributesToType(signature.ReturnType,
module.Compilation, returnTypeAttributes, metadata, typeSystemOptions, nullableContext,
isSignatureReturnType: true);
module.Compilation, returnTypeAttributes, metadata, typeSystemOptions, nullableContext);
return (returnType, parameters, signature.ReturnType as ModifiedType);
}
#endregion
Expand Down