Skip to content
This repository was archived by the owner on Sep 25, 2026. It is now read-only.
This repository was archived by the owner on Sep 25, 2026. It is now read-only.

Attributes on async/iterator type parameters are lost by linker #2785

Closed

Description

@agocke

Basic problem is here: https://sharplab.io/#v2:EYLgxg9gTgpgtADwGwBYA+ABATARgLABQGADAAQY4oDchJ5OAdACICWAhgOYB2EAzgC4swvBgGEIAExgBBLmwA2AT14teNImQoMASjABm8mGEEQu62gGZyWUqNIBvQqWfkrGFKQCyACgCUDpxcgvWgYNjAAC1JvADc2KFIAW1IWLi8YfgjJXgAeCmIAPj9fQKDnRwIylwBfUtJaypc6jDccCxzPDKyJAEkuEIL0zOycgG0mRTlEoQUlaTAwGF5eGAlOxOAYKF5vCamZ+TmFpZW1mA2tgBVFAAclhgAFAFdgeSFO4YleXwBdUkuiiVGuU6sFQuEorF4kkUml+LcYBA9N5Lr4GABxDIfbo7XxAqog4EE5wUHDkADsSXUxIaQQa1SAA

If you have code that looks like this:

    public void M() {
        foreach (var m in Methods<string>())
        {
        }
    }
    
    public IEnumerable<MethodInfo> Methods<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods)] T>()
    {
        foreach (var m in typeof(T).GetMethods())
        {
            yield return m;
        }
    }

Then the code inside Methods gets compiled into a MoveNext method inside a state machine class. That code looks like

        private bool MoveNext()
        {
            int num = <>1__state;
            if (num != 0)
            {
                if (num != 1)
                {
                    return false;
                }
                <>1__state = -1;
                <>7__wrap2++;
            }
            else
            {
                <>1__state = -1;
                <>7__wrap1 = typeof(T).GetMethods();
                <>7__wrap2 = 0;
            }
            if (<>7__wrap2 < <>7__wrap1.Length)
            {
                MethodInfo methodInfo = (<>2__current = <>7__wrap1[<>7__wrap2]);
                <>1__state = 1;
                return true;
            }
            <>7__wrap1 = null;
            return false;
        }

Note the line

               <>7__wrap1 = typeof(T).GetMethods();

The T there is the T on the wrapper class, i.e.

private sealed class <Methods>d__1<T> : IEnumerable<MethodInfo>, IEnumerable, IEnumerator<MethodInfo>, IEnumerator, IDisposable

But the T that the user meant to use was the one on the method:

[IteratorStateMachine(typeof(<Methods>d__1<>))]
    public IEnumerable<MethodInfo> Methods<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods)] T>()
    {
        return new <Methods>d__1<T>(-2);
    }

To get rid of the warnings, the linker needs to use the attributes on the original method, instead of the ones on the wrapper type.

Activity

  1. added this to the .NET 7.0 milestone on May 6, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions