Beef icon indicating copy to clipboard operation
Beef copied to clipboard

GetCustomAttribute fails on attributes with char8* constructor parameters

Open Moneyl opened this issue 2 years ago • 5 comments

Example code:

using System;
using System.Reflection;

namespace BeefAttributeReflectionBug;

class Program
{
    [AttributeUsage(.Field | .Property, .AlwaysIncludeTarget | .ReflectAttribute, ReflectUser = .AllMembers)]
    public struct NoParametersAttribute : Attribute
    {

    }

    [AttributeUsage(.Field | .Property, .AlwaysIncludeTarget | .ReflectAttribute, ReflectUser = .AllMembers)]
    public struct Char8ParametersAttribute : Attribute
    {
        public char8* Xtbl;
        public char8* FieldPath;

        public this(char8* xtbl, char8* fieldPath)
        {
            Xtbl = xtbl;
            FieldPath = fieldPath;
        }
    }

    [Reflect(.All)]
    public class TestClass
    {
        [NoParameters, Char8Parameters("ambient_spawn_info.xtbl", "table;ambient_spawn_info;Name")]
        public int a;
        public float b;
    }

	public static int Main(String[] args)
	{
        GetAttributeTest();
		return 0;
	}

    public static void GetAttributeTest()
    {
        Type zoneObjType = typeof(TestClass);

        var getFieldResult = zoneObjType.GetField("a");
        if (getFieldResult case .Err)
        {
            return;
        }
        FieldInfo fieldInfo = getFieldResult.Get();

        var getGoodAttribute = fieldInfo.GetCustomAttribute<NoParametersAttribute>();
        var getBadAttribute = fieldInfo.GetCustomAttribute<Char8ParametersAttribute>();

        var breakpointHere = 0;
    }
}

In GetAttributeTest() it manages to find NoParametersAttribute but fails to find Char8ParametersAttribute. Running in Win64 debug configuration for the default beef project. Tested on 57c50635a0faa1615846851a904b652ba087cd0e.

Attached the project: BeefAttributeReflectionBug2.zip

Moneyl avatar Aug 19 '23 17:08 Moneyl

And String instead of char8* works fine?

bfiete avatar Aug 19 '23 17:08 bfiete

Ah. I didn't consider using String since I always forget it can hold string literals like these without needing a manual allocation. String works.

StringView also doesn't work but I wanted to make another issue for that since it has a compile time error instead:

ERROR: Unhandled attribute constant data in 'BeefAttributeReflectionBug.Program.TestClass'
  while generating vdata for project 'BeefAttributeReflectionBug'
  while compiling project 'BeefAttributeReflectionBug'
ERROR: Unhandled attribute constant data in 'BeefAttributeReflectionBug.Program.TestClass'
  while generating vdata for project 'BeefAttributeReflectionBug'
  while compiling project 'BeefAttributeReflectionBug'

Moneyl avatar Aug 19 '23 17:08 Moneyl

There's no benefit to having your attribute take a StringView instead of a String...

bfiete avatar Aug 19 '23 19:08 bfiete

There's no benefit to having a constant StringView over a constant String in general...

bfiete avatar Aug 19 '23 19:08 bfiete

Yeah. Like I said I was working under the assumption that using String meant a manual allocation was necessary. Knowing that's wrong I agree. I can't think of a situation where using StringView or char8* over String would be necessary here either.

Moneyl avatar Aug 20 '23 00:08 Moneyl