Skip to content

Commit

Permalink
Generate custom attributes from metadata instead of type system (#100763
Browse files Browse the repository at this point in the history
)

Fixes #100688

Fixes a bit more than just what was reported (see the test). We were losing type information in `attribute.DecodeValue` and could no longer distinguish between `new object[] { SomeEnum.Val }` and `new SomeEnum[] { SomeEnum.Val }`.

The fix required a complete rewrite of attribute emission using the more low level API.

Cc @dotnet/ilc-contrib
  • Loading branch information
MichalStrehovsky authored Apr 16, 2024
1 parent 5a6560a commit 1a820cb
Show file tree
Hide file tree
Showing 10 changed files with 492 additions and 412 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,11 @@
</Suppression>
<Suppression>
<DiagnosticId>CP0001</DiagnosticId>
<Target>T:Internal.Metadata.NativeFormat.ConstantBoxedEnumValue</Target>
<Target>T:Internal.Metadata.NativeFormat.ConstantEnumValue</Target>
</Suppression>
<Suppression>
<DiagnosticId>CP0001</DiagnosticId>
<Target>T:Internal.Metadata.NativeFormat.ConstantBoxedEnumValueHandle</Target>
<Target>T:Internal.Metadata.NativeFormat.ConstantEnumValueHandle</Target>
</Suppression>
<Suppression>
<DiagnosticId>CP0001</DiagnosticId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,9 +141,9 @@ public static bool IsConstructor(ref Method method, MetadataReader reader)
return nameHandle.StringEquals(ConstructorInfo.ConstructorName, reader) || nameHandle.StringEquals(ConstructorInfo.TypeConstructorName, reader);
}

private static Exception ParseBoxedEnumConstantValue(this ConstantBoxedEnumValueHandle handle, MetadataReader reader, out object value)
private static Exception ParseEnumConstantValue(this ConstantEnumValueHandle handle, MetadataReader reader, out object value)
{
ConstantBoxedEnumValue record = handle.GetConstantBoxedEnumValue(reader);
ConstantEnumValue record = handle.GetConstantEnumValue(reader);

Exception? exception = null;
Type? enumType = record.Type.TryResolve(reader, new TypeContext(null, null), ref exception)?.ToType();
Expand Down Expand Up @@ -317,9 +317,9 @@ public static Exception TryParseConstantValue(this Handle handle, MetadataReader
case HandleType.ConstantReferenceValue:
value = null;
return null;
case HandleType.ConstantBoxedEnumValue:
case HandleType.ConstantEnumValue:
{
return handle.ToConstantBoxedEnumValueHandle(reader).ParseBoxedEnumConstantValue(reader, out value);
return handle.ToConstantEnumValueHandle(reader).ParseEnumConstantValue(reader, out value);
}
default:
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,13 @@ from primitiveType in PrimitiveTypes
members: new MemberDef[] {
new MemberDef(name: "Value", typeName: "string")
}
),
new RecordDef(
name: "ConstantEnumValue",
members: new MemberDef[] {
new MemberDef("Value", EnumConstantValue, MemberDefFlags.RecordRef | MemberDefFlags.Child),
new MemberDef("Type", TypeDefOrRefOrSpec, MemberDefFlags.RecordRef)
}
)
}
)
Expand Down Expand Up @@ -630,13 +637,6 @@ from primitiveType in PrimitiveTypes
new MemberDef("Value", TypeDefOrRefOrSpecOrConstant, MemberDefFlags.RecordRef),
}
),
new RecordDef(
name: "ConstantBoxedEnumValue",
members: new MemberDef[] {
new MemberDef("Value", EnumConstantValue, MemberDefFlags.RecordRef | MemberDefFlags.Child),
new MemberDef("Type", TypeDefOrRefOrSpec, MemberDefFlags.RecordRef)
}
),
new RecordDef(
name: "GenericParameter",
members: new MemberDef[] {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -300,15 +300,6 @@ public static uint Read(this NativeReader reader, uint offset, out ConstantBoole
return offset;
} // Read

public static uint Read(this NativeReader reader, uint offset, out ConstantBoxedEnumValueHandle handle)
{
uint value;
offset = reader.DecodeUnsigned(offset, out value);
handle = new ConstantBoxedEnumValueHandle((int)value);
handle._Validate();
return offset;
} // Read

public static uint Read(this NativeReader reader, uint offset, out ConstantByteArrayHandle handle)
{
uint value;
Expand Down Expand Up @@ -372,6 +363,15 @@ public static uint Read(this NativeReader reader, uint offset, out ConstantEnumA
return offset;
} // Read

public static uint Read(this NativeReader reader, uint offset, out ConstantEnumValueHandle handle)
{
uint value;
offset = reader.DecodeUnsigned(offset, out value);
handle = new ConstantEnumValueHandle((int)value);
handle._Validate();
return offset;
} // Read

public static uint Read(this NativeReader reader, uint offset, out ConstantHandleArrayHandle handle)
{
uint value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,14 +94,14 @@ public enum HandleType : byte
ByReferenceSignature = 0x2,
ConstantBooleanArray = 0x3,
ConstantBooleanValue = 0x4,
ConstantBoxedEnumValue = 0x5,
ConstantByteArray = 0x6,
ConstantByteValue = 0x7,
ConstantCharArray = 0x8,
ConstantCharValue = 0x9,
ConstantDoubleArray = 0xa,
ConstantDoubleValue = 0xb,
ConstantEnumArray = 0xc,
ConstantByteArray = 0x5,
ConstantByteValue = 0x6,
ConstantCharArray = 0x7,
ConstantCharValue = 0x8,
ConstantDoubleArray = 0x9,
ConstantDoubleValue = 0xa,
ConstantEnumArray = 0xb,
ConstantEnumValue = 0xc,
ConstantHandleArray = 0xd,
ConstantInt16Array = 0xe,
ConstantInt16Value = 0xf,
Expand Down
Loading

0 comments on commit 1a820cb

Please sign in to comment.