简体   繁体   English

C#编译器错误? 用于Expression中的只写属性的对象初始化程序语法使csc崩溃

[英]C# compiler bug? Object initializer syntax used for write-only property in Expression makes csc crash

You may consider this a bug report, however I'm curious if I am terribly wrong here, or if there is an explanation from Eric or someone else at Microsoft. 您可能会认为这是一个错误报告,但是我很好奇我是否在这里非常错误,或者是否有来自Eric或Microsoft的其他人的解释。

Update 更新

This is now posted as a bug on Microsoft Connect. 现在,这是作为 Microsoft Connect上的错误发布的

Description 描述

Consider the following class: 考虑以下课程:

class A 
{
    public object B {
        set { }
    }
}

Here, AB is a write-only but otherwise fine property. 这里, AB是一个只写,但在其他方面很好的属性。
Now, imagine we assign it inside of expression : 现在,想象一下我们在表达式中分配它

Expression<Func<A>> expr = 
    () => new A {
        B = new object { }
    };

This code makes C# compiler (both 3.5 .30729.4926 and 4.0 .30319.1) spit out 此代码使C#编译器( 3.5 .30729.4926和4.0 .30319.1)吐出

Internal Compiler Error (0xc0000005 at address 013E213F): likely culprit is 'BIND'. 内部编译器错误(地址013E213F处的0xc0000005):可能的罪魁祸首是“BIND”。

and crash. 和崩溃。

However, merely replacing object initializer syntax ( { } ) with a constructor ( ( ) ) compiles just fine . 但是, 仅使用构造函数( ( ) )替换对象初始化程序语法( { } )就可以了

Full code for reproduction: 完整的复制代码:

using System;
using System.Linq.Expressions;

class Test {
    public static void Main()
    {
        Expression<Func<A>> expr = 
            () => new A {
                B = new object { }
            };
    }
}

class A {
    public object B { set { } }
}

(And yes, I did hit it working on a real project.) (是的,我确实在真正的项目上工作了。)

I'm afraid I'm not Eric Lippert (Oh, but could I be so dashing...), but as a former Visual Studio languages guy who can still search the sources, I can say two things about this: 我担心我不是Eric Lippert(哦,但我可能会如此潇洒......),但作为一名仍然可以搜索来源的Visual Studio语言人,我可以说两件事:

  1. Any time you see something that starts with "Internal Compiler Error" you have most definitely found a bug. 每当你看到以“内部编译器错误”开头的内容时,你肯定发现了一个错误。 That's what that error exists for, whether it's the C#, VB or C++ compiler. 这就是错误的存在,无论是C#,VB还是C ++编译器。 It's the "Oh, s**t, something just went really unexpectedly wrong!" 这就是“噢,不管怎样,事情出乎意料地错了!” throw-up-our-hands-and-bail-out error. 抛出我们的手和纾困错误。

  2. Beyond that, this is definitely a bug in the C# compiler that should be reported. 除此之外,这绝对是应该报告的C#编译器中的错误。 The code that's crashing is assuming that when you're doing an initializer on a property that there's a getter it can look at and, hey, guess what? 正在崩溃的代码假设当你在一个属性上做一个初始化器时,它可以看到一个getter,嘿,猜猜是什么? In this case, there isn't. 在这种情况下,没有。 Oddly enough, if I change the type being constructed to some type "C" instead of "object", I don't get the crash, so I'm guessing it's a failure further up the stack (ie the code never should have gotten down to the point where it was looking for the property getter). 奇怪的是,如果我将构造的类型更改为某种类型的“C”而不是“object”,我不会得到崩溃,所以我猜测它是堆栈中的进一步失败(即代码永远不应该得到)到了寻找物业吸气剂的地步)。

Hope this helps. 希望这可以帮助。

This is what I found online related to the error, 这是我在网上发现的与错误有关的内容,

Posted by Microsoft on 3/9/2010 at 10:58 AM 微软于2010年3月9日上午10:58发布

Thanks everyone for the reports. 谢谢大家的报道。 I believe that this issue has been fixed post-RC. 我相信此问题已在RC后修复。 The problem is that the C# compiler is crashing as it is tries to report an error or warning. 问题是C#编译器在尝试报告错误或警告时崩溃。 In several cases we have seen the warning being reported is that the LIB environment variable contains an invalid path. 在一些情况下,我们已经看到报告的警告是LIB环境变量包含无效路径。 To avoid the crash, check that your LIB environment variable contains valid paths. 要避免崩溃,请检查LIB环境变量是否包含有效路径。

Regards, 问候,

Ed Maurer Development Manager, VB and C# compilers Ed Maurer开发经理,VB和C#编译器

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM