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.92k stars 4.02k forks source link

Typeless expressions in GetEnumeratorInfo #31804

Open gafter opened 5 years ago

gafter commented 5 years ago

The method ForEachLoopBinder.GetEnumeratorInfo has the following in it:

                    // The null and default literals were caught above, so anything else with a null type is a method group or anonymous function
                    diagnostics.Add(ErrorCode.ERR_AnonMethGrpInForEach, _syntax.Expression.Location, collectionExpr.Display);

But this is just wrong (e.g. (1, null) has no type), and in any case is incredibly fragile to future compiler changes. It should be made robust to new expression kinds that can be null.

gafter commented 5 years ago

Compiling

            foreach (var x in (1, null))
            {
            }

gives

CS0446  Foreach cannot operate on a '(int, <null>)'. Did you intend to invoke the '(int, <null>)'?