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

修复时,未充分测试.

posted @ 2022-02-17 09:28  zjh6  阅读(16)  评论(0)    收藏  举报  来源