弃用:返回 this 会转义对参数 this 的引用
Deprecation: returning this escapes a reference to parameter this
在我们的代码库中将 DMD 更新到 2.094.1 时 运行 进入这个。它是关于什么的以及如何解决它?
Deprecation: returning this escapes a reference to parameter this
perhaps annotate the parameter with return
return this;
行发出了警告:
public ref EventBuilder typeOne()
{
this.type = 1;
return this;
}
此弃用警告与 DIP25 有关。在此处添加 return
:
public ref EventBuilder typeOne() return
{
this.type = 1;
return this;
}
引用 DMD 的更新日志 2.092.0:
DIP25 has been available since v2.067.0, first as its own switch, and more recently under the -preview=dip25 switch. The feature is now fully functional and has been built on, for example by DIP1000.
Starting from this release, code that would trigger errors when -preview=dip25 is passed to the compiler will also trigger a deprecation message without -preview=dip25. The behavior of the switch is unchanged (errors will still be issued).
DIP25 aims to make it impossible for @safe code to refer to destructed object. In practice, functions and methods returning a ref to their parameter might be required to qualify the method or the parameter as return, as hinted by the compiler.
struct Foo
{
int x;
// returning `this.x` escapes a reference to parameter `this`,
// perhaps annotate with `return`
ref int method() /* return */ { return this.x; }
}
// returning `v` escapes a reference to parameter `v`,
// perhaps annotate with `return`
ref int identity(/* return */ ref int v) { return v; }
In both cases, uncommenting the return annotation will appease the compiler.
运行 进入这个。它是关于什么的以及如何解决它?
Deprecation: returning this escapes a reference to parameter this
perhaps annotate the parameter with return
return this;
行发出了警告:
public ref EventBuilder typeOne()
{
this.type = 1;
return this;
}
此弃用警告与 DIP25 有关。在此处添加 return
:
public ref EventBuilder typeOne() return
{
this.type = 1;
return this;
}
引用 DMD 的更新日志 2.092.0:
DIP25 has been available since v2.067.0, first as its own switch, and more recently under the -preview=dip25 switch. The feature is now fully functional and has been built on, for example by DIP1000.
Starting from this release, code that would trigger errors when -preview=dip25 is passed to the compiler will also trigger a deprecation message without -preview=dip25. The behavior of the switch is unchanged (errors will still be issued).
DIP25 aims to make it impossible for @safe code to refer to destructed object. In practice, functions and methods returning a ref to their parameter might be required to qualify the method or the parameter as return, as hinted by the compiler.
struct Foo
{
int x;
// returning `this.x` escapes a reference to parameter `this`,
// perhaps annotate with `return`
ref int method() /* return */ { return this.x; }
}
// returning `v` escapes a reference to parameter `v`,
// perhaps annotate with `return`
ref int identity(/* return */ ref int v) { return v; }
In both cases, uncommenting the return annotation will appease the compiler.