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

Incompatability between desktop and coreclr for Generic Array Enumeration #11488

Closed
KevinRansom opened this issue Nov 16, 2018 · 3 comments
Closed

Comments

@KevinRansom
Copy link
Member

Enumerator fails to throw when calling current after MoveNext returns false

repro:
This code throws on the NETFramework and the coreclr doesn't

namespace ConsoleApp23
{
    class Program
    {
        static void Main(string[] args)
        {
            var keyValuePairs =
                new KeyValuePair<int, int>[3]
                {
                    new KeyValuePair<int, int>(1,1),
                    new KeyValuePair<int, int>(2,2),
                    new KeyValuePair<int, int>(3,3),
                };

                IEnumerable<KeyValuePair<int, int>> collection = keyValuePairs;

            var e = collection.GetEnumerator();

            var step2 = e.MoveNext();
            var step3 = e.Current;
            Console.WriteLine("{0} {1}", step2, step3);

            var step4 = e.MoveNext();
            var step5 = e.Current;
            Console.WriteLine("{0} {1}", step4, step5);

            var step6 = e.MoveNext();
            var step7 = e.Current;
            Console.WriteLine("{0} {1}", step6, step7);

            var step8 = e.MoveNext();
            var step9 = e.Current;
            Console.WriteLine("{0} {1}", step8, step9);
       }
    }
}

Produces this output on NET Framework: (Note the exception is thrown)

C:\Users\kevinr\source\repos\ConsoleApp23\ConsoleApp23\bin\Debug>net472\ConsoleApp23.exe
True [1, 1]
True [2, 2]
True [3, 3]

Unhandled Exception: System.InvalidOperationException: Enumeration already finished.
   at System.SZArrayHelper.SZGenericArrayEnumerator`1.get_Current()
   at ConsoleApp23.Program.Main(String[] args) in C:\Users\kevinr\source\repos\ConsoleApp23\ConsoleApp23\Program.cs:line 35

C:\Users\kevinr\source\repos\ConsoleApp23\ConsoleApp23\bin\Debug>

and this output on the Coreclr: Note it produces [3,3] when MoveNext returns false

True [1, 1]
True [2, 2]
True [3, 3]
False [3, 3]
@KevinRansom
Copy link
Member Author

cc/ @jkotas

@jkotas jkotas transferred this issue from dotnet/core Nov 16, 2018
@jkotas
Copy link
Member

jkotas commented Nov 16, 2018

Regression from dotnet/coreclr#13157

@jkotas
Copy link
Member

jkotas commented Dec 3, 2018

The behavior of IEnumerator<T>.Current in this situation is documented as undefined:

https://docs.microsoft.com/en-us/dotnet/api/system.collections.generic.ienumerator-1.current?view=netcore-2.1#remarks

The behavior of enumerators throughout the framework is not consistent in this situation, exactly as the documentation says. Some throw exception, some do not.

While we may still want to consider fixing this for array enumerator, good code should not be depend on undefined behavior.

jkotas referenced this issue in jkotas/corefx Dec 5, 2018
Contributes to dotnet/coreclr#21046
jkotas referenced this issue in jkotas/coreclr Dec 5, 2018
stephentoub referenced this issue in dotnet/coreclr Dec 5, 2018
stephentoub referenced this issue in dotnet/corefx Dec 7, 2018
Contributes to dotnet/coreclr#21046
jlennox referenced this issue in jlennox/corefx Dec 16, 2018
Contributes to dotnet/coreclr#21046
@msftgits msftgits transferred this issue from dotnet/coreclr Jan 31, 2020
@ghost ghost locked as resolved and limited conversation to collaborators Dec 15, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants