当前位置:网站首页>Dip1000 runaway
Dip1000 runaway
2022-07-02 08:35:00 【fqbqrr】
import std.stdio;
@safe:
struct ScopeBuffer(T, size_t Len)
{
this(T[Len] buf, size_t len = 0)
{
this.buf = buf;
this.len = len;
}
// use `return` Issue compilation errors correctly , nothing , Compiler error .
inout(T)[] opSlice(size_t lower, size_t upper) inout /*return*/
in
{
assert(lower <= len, "");
assert(upper <= len, "");
assert(lower <= upper,"");
}
do
{
return buf[lower .. upper];
// Loophole , If opSlice nothing ` in `, Should be sent here ` compile ` error .
}
T[Len] buf;
size_t len;
}
char[] fun()
{
char[4] buf = "abcd";
auto sb = ScopeBuffer!(char, 4)(buf, 4);
return sb[0..2];
// Loophole , Unless there is ` in `, Compiler allows `ScopeBuffer` Internal data escape .
}
void main()
{
auto s = fun();
writeln(s);
}
use dmd -preview=dip1000 source.d compile .
expect ,ScopeBuffer in buffer Of section Escape time , A compilation error should be issued . Yes in Send errors correctly , nothing in Time should be but No, Issue an error .
Interestingly , Delete inout Sent an error . Delete template , Then the behavior is correct .
A more concise example :
import std.stdio;
@safe:
struct ScopeBuffer
{
this(char[4] init)
{
this.buf = init;
}
// The loophole is `inout` stay `this` Upper derivation `return`.
inout(char)[] opSlice(size_t lower, size_t upper) inout
do
{
return buf[lower .. upper];
// Loophole , An error should be issued .
}
char[4] buf;
}
char[] fun()
{
char[4] buf = "abcd";
auto sb = ScopeBuffer(buf);
return sb[0..2];
}
void main()
{
auto s = fun();
writeln(s);
}
Not yet determined problem , But not here. inout. A simple example :
@safe:
struct S {
this(int) {
}
char[] opSlice() return {
return buf[]; }
char[4] buf;
}
S bar();
char[] fun() {
return S()[];//S('\xff') The returned stack value reference escaped
return S(1)[]; // error , It should be the same
return bar()[]; //bar The returned stack value reference escaped
}
The problem is that they should be treated the same Constructor call And Literally / The function returns Temporary value of .1 A question ,1 Repairs .
problem There is still , The following is Minimum example .
@safe:
struct ScopeBuffer
{
this(char[4] init)
{
this.buf = init;
}
inout(char)[] opSlice(size_t lower, size_t upper) inout
{
return buf[lower .. upper];
}
char[4] buf;
}
//... The rest are the same as above .
It should produce The stack allocated memory pointer escaped Error of .
A more simplified example :
@safe:
struct S {
//inout(char)* slice() inout return
// The correct error is given above
inout(char)* slice() inout/* No mistake */
{
return &buf;
}
char buf;
}
char* fun() {
S sb;
return sb.slice();
}
If , section Is a static function , And press quote Pass on sb As a first 1 ginseng . The compiler issues correct error , thus , And Ben It's a special case of .
边栏推荐
- 旋转链表(图解说明)
- Jumping | Blue Bridge Cup
- Introduction to parameters of CarSim pavement 3D shape file
- Carsim-路面3D形状文件参数介绍
- Force deduction method summary: double pointer
- Common shortcut keys of Jupiter notebook (you can also view it by pressing h in command mode)
- Chrome debugging
- Gateway 简单使用
- St-link connection error invalid ROM table of STM32 difficult and miscellaneous diseases
- Use Matplotlib to draw a preliminary chart
猜你喜欢

链表经典面试题(反转链表,中间节点,倒数第k个节点,合并分割链表,删除重复节点)

c语言将字符串中的空格替换成%20

使用wireshark抓取Tcp三次握手

16: 00 interview, came out at 16:08, the question is really too

Detailed explanation of NIN network

Carsim-问题Failed to start Solver: PATH_ID_OBJ(X) was set to Y; no corresponding value of XXXXX?

OpenFeign 簡單使用

Carsim 学习心得-粗略翻译1

2022 Heilongjiang's latest eight member (Safety Officer) simulated test question bank and answers

On November 24, we celebrate the "full moon"
随机推荐
Carsim 学习心得-粗略翻译1
Honeypot attack and defense drill landing application scheme
OpenFeign 簡單使用
双向链表的实现(双向链表与单向链表的简单区别联系和实现)
idea中注释代码取消代码的快捷键
The best blog to explain the basics of compilation (share)
Matlab - autres
【无标题】
Global and Chinese market of electric cheese grinder 2022-2028: Research Report on technology, participants, trends, market size and share
Opencv common method source link (continuous update)
旋转链表(图解说明)
Deep understanding of JVM
Use Wireshark to grab TCP three handshakes
文件上传-upload-labs
Matlab-其它
Openfeign is easy to use
Gateway 简单使用
Data asset management function
One of the reasons for WCF update service reference error
Force deduction method summary: find classes