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

Attributes taking object as a parameter have unexpected behavior in AOT #100688

Closed
phil-scott-78 opened this issue Apr 5, 2024 · 2 comments · Fixed by #100763
Closed

Attributes taking object as a parameter have unexpected behavior in AOT #100688

phil-scott-78 opened this issue Apr 5, 2024 · 2 comments · Fixed by #100763

Comments

@phil-scott-78
Copy link

Description

Given an attribute that take an object as a parameter, when using an Enum it gets converted to an int before being passed to the constructor during AOT

Reproduction Steps

using System.ComponentModel;
using System.Reflection;

var defaultValue = typeof(MyTeam)
    .GetProperty("Quarterback")!
    .GetCustomAttribute<DefaultValueAttribute>()!.Value!;


var customValue = typeof(MyTeam)
    .GetProperty("Backup")!
    .GetCustomAttribute<MyCustomAttribute>()!.Value!;

Console.WriteLine($"{defaultValue.GetType()} {defaultValue}");
Console.WriteLine($"{customValue.GetType()} {customValue}");

internal enum Quarterbacks { Teddy, Lamar, Malik, Stefan }

internal class MyTeam
{
    [DefaultValue(Quarterbacks.Lamar)]
    public Quarterbacks Quarterback { get; set; }
    
    [MyCustom(Quarterbacks.Teddy)]
    public Quarterbacks Backup { get; set; }
}

internal class MyCustomAttribute(object value) : Attribute
{
    public object Value => value;
}

Expected behavior

Quarterbacks Lamar
Quarterbacks Teddy

Actual behavior

System.Int32 1
System.Int32 0

Regression?

Not sure

Known Workarounds

For custom attributes, using a generic attribute will now work. But that doesn't help when wanting to use an existing one like DefaultAttribute

Configuration

  • .net 8
  • Windows 11
  • x64

Not sure

Other information

No response

Copy link
Contributor

Tagging subscribers to this area: @dotnet/area-system-reflection
See info in area-owners.md if you want to be subscribed.

@vitek-karas
Copy link
Member

I was able to validate (on .NET 8) that on CoreCLR this behaves as expected. On NativeAOT it does reproduce the wrong behavior (converting the values to Int32).
@dotnet/ilc-contrib

phil-scott-78 added a commit to phil-scott-78/spectre.console that referenced this issue Apr 5, 2024
Enums are converted to integers with NativeAOT on attribute constructors. This work around is needed until that bug is fixed. See dotnet/runtime#100688
MichalStrehovsky added a commit that referenced this issue Apr 16, 2024
)

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
@dotnet-policy-service dotnet-policy-service bot removed the untriaged New issue has not been triaged by the area owner label Apr 16, 2024
phil-scott-78 added a commit to phil-scott-78/spectre.console that referenced this issue Apr 18, 2024
Enums are converted to integers with NativeAOT on attribute constructors. This work around is needed until that bug is fixed. See dotnet/runtime#100688
matouskozak pushed a commit to matouskozak/runtime that referenced this issue Apr 30, 2024
…et#100763)

Fixes dotnet#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
@github-actions github-actions bot locked and limited conversation to collaborators May 16, 2024
Ruihan-Yin pushed a commit to Ruihan-Yin/runtime that referenced this issue May 30, 2024
…et#100763)

Fixes dotnet#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
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

2 participants