C# 编译器错误?表达式中用于只写属性的对象初始值设定项语法导致 csc 崩溃

发布于 2024-11-16 23:04:04 字数 1296 浏览 4 评论 0原文

您可能会认为这是一个错误报告,但是我很好奇我在这里是否犯了严重错误,或者 Eric 或 Microsoft 的其他人是否有解释。

更新

现在是在 Microsoft Connect 上发布为错误

描述

考虑以下类:

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

这里,AB 是一个只写,但其他方面都很好。
现在,想象一下我们在表达式内部分配它

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

此代码使 C# 编译器(3.5.30729.4926 和 4.0.30319.1)吐出

内部编译器错误(地址 013E213F 处为 0xc0000005):罪魁祸首可能是“BIND”。

并崩溃。

但是,仅用构造函数 (( )) 替换对象初始值设定项语法 ({ }) 就可以很好地编译

用于复制的完整代码:(

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 { } }
}

是的,我在实际项目中确实做到了。)

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.

Update

This is now posted as a bug on Microsoft Connect.

Description

Consider the following class:

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

Here, A.B is a write-only but otherwise fine property.
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

Internal Compiler Error (0xc0000005 at address 013E213F): likely culprit is '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.)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

偏爱你一生 2024-11-23 23:04:04

恐怕我不是 Eric Lippert(哦,但我能这么潇洒吗……),但作为一名仍然可以搜索源代码的前 Visual Studio 语言人员,我可以说两点关于此的事情:

  1. 任何时候你看到以“内部编译器错误”开头的东西,你肯定发现了一个错误。这就是该错误存在的原因,无论是 C#、VB 还是 C++ 编译器。这是“哦,糟糕,刚刚发生了出乎意料的错误!” throw-up-our-hands-and-bail-out 错误。

  2. 除此之外,这绝对是 C# 编译器中应该报告的错误。崩溃的代码假设当你对一个属性进行初始化时,它可以查看一个 getter,然后,嘿,你猜怎么着?在这种情况下,没有。奇怪的是,如果我将构造的类型更改为某种类型“C”而不是“对象”,我不会崩溃,所以我猜测这是堆栈上层的失败(即代码永远不应该得到) 除此之外,这绝对是 C# 编译器中

希望这有帮助。

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:

  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. 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. 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? 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 (i.e. the code never should have gotten down to the point where it was looking for the property getter).

Hope this helps.

月亮邮递员 2024-11-23 23:04:04

这是我在网上找到的与该错误相关的内容,

由 Microsoft 于 2010 年 3 月 9 日上午 10:58 发布

感谢大家的报告。我
相信这个问题已经得到解决
RC 后。问题是 C#
编译器正在崩溃,因为它试图
报告错误或警告。在几个
我们看到警告的情况是
报道称LIB环境
变量包含无效路径。到
避免崩溃,检查你的 LIB
环境变量包含有效的
路径。

问候,

Ed Maurer 开发经理,VB 和
C# 编译器

This is what I found online related to the error,

Posted by Microsoft on 3/9/2010 at 10:58 AM

Thanks everyone for the reports. I
believe that this issue has been fixed
post-RC. The problem is that the C#
compiler is crashing as it is tries to
report an error or warning. In several
cases we have seen the warning being
reported is that the LIB environment
variable contains an invalid path. To
avoid the crash, check that your LIB
environment variable contains valid
paths.

Regards,

Ed Maurer Development Manager, VB and
C# compilers

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文