dotnet / roslyn

The Roslyn .NET compiler provides C# and Visual Basic languages with rich code analysis APIs.
https://docs.microsoft.com/dotnet/csharp/roslyn-sdk/
MIT License
18.73k stars 3.99k forks source link

Test plan for "generic attributes" #36285

Closed jcouv closed 5 days ago

jcouv commented 5 years ago

Championed issue Latest LDM notes.

Specification

Compiler

Productivity

tmat commented 2 years ago
csaba-sagi-sonarsource commented 2 years ago

Hello,

I see that this feature has been merged, however it is still in "Preview". And also it has been added to the C# Next table instead of the C# 10 table. Will this feature make it to the C# 10 release or not?

PathogenDavid commented 2 years ago

@csaba-sagi-sonarsource It's waiting until C# 11 due to some issues with the tools and an unresolved runtime issue about how CustomAttributeData.GetCustomAttributes should handle them. (See https://github.com/dotnet/csharplang/issues/124#issuecomment-898066402 for more details and further discussion.)

RikkiGibson commented 2 years ago

Basically, if you install .NET 6 and use <LangVersion>preview</LangVersion> in your project you should be able to use the feature.

ThaDaVos commented 7 months ago

I don't know if this is fully related but the following logic fails when trying to resolve a Generic Attribute using the MetadataReader - it results in an exception of:

Unhandled exception. System.InvalidCastException: Specified cast is not valid.
     at System.Reflection.Throw.InvalidCast()
     at System.Reflection.Metadata.TypeReferenceHandle.op_Explicit(EntityHandle handle)
     at DNNE.Assembly.Attributors.Attributor.ParseCustomAttribute(MetadataReader reader, CustomAttribute attribute)
     at DNNE.Assembly.Attributors.Attributor.IsApplicable(MetadataReader reader, CustomAttribute attribute, Boolean isReturn)
     at DNNE.Assembly.AssemblyReader.<>c__DisplayClass12_0.<Read>b__0(IAttributor attributor)
     at System.Linq.Enumerable.WhereSelectListIterator`2.MoveNext()
     at System.Collections.Generic.List`1.AddRange(IEnumerable`1 collection)
     at DNNE.Assembly.AssemblyReader.Read()
     at DNNE.Program.Main(String[] args)

Maybe I am doing something wrong below - but I am not sure, can't really find much information about the MetadataReader and trying to add support for certain features to DNNE:

The following line throws the exception:

TypeReference refType = reader.GetTypeReference((TypeReferenceHandle)refConstructor.Parent);

which is in:

internal static (StringHandle? parsedNamespace, StringHandle? parsedName) ParseCustomAttribute(MetadataReader reader, CustomAttribute attribute)
{
    switch (attribute.Constructor.Kind)
    {
        case HandleKind.MemberReference:
            MemberReference refConstructor = reader.GetMemberReference((MemberReferenceHandle)attribute.Constructor);

            if (refConstructor.Parent.IsNil)
            {
                Debug.Assert(false, "Unknown parent reference kind");
                return (null, null);
            }

            switch (refConstructor.Parent.Kind)
            {
                case HandleKind.TypeReference:
                    TypeReference refType = reader.GetTypeReference((TypeReferenceHandle)refConstructor.Parent);
                    return (refType.Namespace, refType.Name);
                default:
                    Debug.Assert(false, "Unknown parent reference kind");
                    return (null, null);
            }
        case HandleKind.MethodDefinition:
            MethodDefinition defConstructor = reader.GetMethodDefinition((MethodDefinitionHandle)attribute.Constructor);
            TypeDefinition defType = reader.GetTypeDefinition(defConstructor.GetDeclaringType());
            return (defType.Namespace, defType.Name);

        default:
            Debug.Assert(false, "Unknown attribute constructor kind");
            return (null, null);
    }
}

I am using DotNet 8 and <LangVersion>preview</LangVersion> inside my project and inside DNNE

RikkiGibson commented 5 days ago

Closing out the test plan as the feature has shipped.

If you are still experiencing a problem related to the feature, please file a new issue.