简体   繁体   中英

DMD doesn't force restrictions on foreach

According to the section "Foreach Restrictions" at http://dlang.org/statement.html the following code

int[] a;
int[] b;
foreach (int i; a) { 
    a = null; // error
    a.length += 10; // error 
    a = b; // error 
}
a = null; // ok

should error.

But it doesn't for me when using 2.063.2 nor master.

Is this a regression bug?

It was probably never implemented as an actual error. I don't see an open bug, but I'm bad at searching bugzilla... regardless though, I'm sure the spec simply was never implemented to actually show the error, leaving it as undefined behavior if you actually do it.

In several places, the D documentation says something is an error, without the compiler actually checking for this. C and C++ would probably use the term 'undefined behavior' instead of 'error'. In C and C++, such a program is valid, you just have no idea what it does. In D, it is simply not considered a valid program.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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