d禁止赋值引用至非域参数
struct DontDoThis
{ immutable char[12] content;
@safe this(char ch)
{ content[] = ch;
throw new Exception(content[]);
// assert(0, content[]);//也能触发
}
}
void main() @safe
{ import std.stdio;
DontDoThis('a');
}
用-preview=dip1000打印出垃圾错误信息.
与构造器/异常无关,可化简为:
@safe:
void fun(ref int x)
{
int* scopePtr = &x; // 正确推导域
escape(scopePtr); // 正确拒绝
escape(&x); // 接收`无效的`
}
int* gPtr;
void escape(int* ptr) {
gPtr = ptr;
}
修复时,未充分测试.
浙公网安备 33010602011771号