[Issue 23747] New: 'auto ref' function return signature does not flag escaping a reference to local variable
d-bugmail at puremagic.com
d-bugmail at puremagic.com
Mon Feb 27 00:00:28 UTC 2023
https://issues.dlang.org/show_bug.cgi?id=23747
Issue ID: 23747
Summary: 'auto ref' function return signature does not flag
escaping a reference to local variable
Product: D
Version: D2
Hardware: x86_64
OS: Linux
Status: NEW
Severity: minor
Priority: P1
Component: dmd
Assignee: nobody at puremagic.com
Reporter: Michael.Shah at tufts.edu
Per the spec on 'auto ref'
https://dlang.org/spec/function.html#auto-ref-functions, I am not seeing the
same behavior with 'auto ref' for flagging escaped local references as shown in
the section on ref-functions:
https://dlang.org/spec/function.html#ref-functions
```
// Tested on:
// DMD64 D Compiler v2.102.1
import std.stdio;
// The compiler will correctly
// flag this as a local variable 'i' escaping.
//ref int Func1(){
// int i=1;
// return i;
//}
// BUGGY Behavior
// 'i' should be inferred as an 'int, thus a
// 'ref int' return value same as above.
ref auto Func2(){
int i=2;
return i;
}
// BUGGY Behavior
// Same as above, with explicit return type
auto ref int Func3(){
int i=3;
return i;
}
// Correct behavior
// Compiler flags `p` escaping a reference to local variable
//ref int* Func4(){
// int i=4;
// int* p = &i;
// return p;
//}
// BUGGY behavior
ref auto Func5(){
int i=4;
int* p = &i;
return p;
}
void main(){
auto two = Func2();
writeln(two, ":", typeid(two));
auto three = Func3();
writeln(three, ":", typeid(three));
auto five = Func5();
writeln(five, ":", typeid(five));
}
```
--
More information about the Digitalmars-d-bugs
mailing list