Nested Linq Min() crashes Visual Studio

本秂侑毒 提交于 2019-12-05 00:31:55

A while ago I submitted a bug report on MS Connect. This morning I got a response:

Thanks for the bug report for Visual Studio 2008!

As you point out in your linked post from Eric Lippert's blog, we have limits on our ability to do type inference on such nested lambda expressions in a reasonable amount of time. That said, we could certainly try to timebox such inference or put a hard limit on lambda nesting to prevent this type of issue. Unfortunately, we're starting to lock down on what we can fix in Visual Studio 2010 and we won't be able to enforce such limits in this release.

We'll definitely keep this issue in mind when planning for future releases!

Alex Turner

Program Manager

Visual C# Compiler

and

The following feedback item you submitted at Microsoft Connect has been updated: Product/Technology - Visual Studio and .NET Framework - Feedback ID – 476133 Feedback Title – Nested Linq Min() crashes Visual Studio 2008 IDE The following fields or values changed: Field Status changed from [Active] to [Resolved]

Field Resolution changed from [None] to [Won't Fix]

I was able to repro this on my Visual Studio 2008 install. It looks like the language service is hitting an infinite loop and eventually running out of memory. Can you please file a bug on the connect site?

Connect: http://connect.microsoft.com

If you do file the bug, please add a comment to my answer with the bug number.

Type inference for nested lambda expressions takes exponential time. So it's not surprising the compiler gets slow when you do too much nesting.

However, the IDE ideally would handle such cases and abort type inference if its taking too long.

Using basic recursion instead of trying to 'guess' the depth, increasing complexity at every level

public static class TestExt
{
    public static int Min(this Test test)
    {
        return Math.Min(test.val, test.Tests.Min(x => x.Min()));
    }
}
public class Test
{
    public int val;
    public List<Test> Tests;
}

public class LinqTest
{
    public void GetMin()
    {
        Test t = new Test();
        var min = t.Min();
    }
}

As pointed out by Ryan Versaw, you can also do this without extension methods like so:

public class Test
{
    public int val;
    public List<Test> Tests;

    public int Min()
    {
        return Math.Min(val,Tests.Min(x => x.Min()));
    }
}

You don't even have to go that far. VS froze for me when typing in the d part of the nested LINQ expression.

Thought i'd point out that it also freezes VS 2010, and what's more it nearly froze my whole system!

I think I just experienced something similar. I was going through my code and using var on lines where I was declaring and initializing variables to new SomeClass(). My code compiled. When I tried to run any Visual Studio 2008 unit test, Visual Studio would crash with CLR20r3 as the error name/type. Reverting all my var changes, the tests run fine.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!