www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - safe function with __gshared as default parameter value

reply Anonymouse <zorael gmail.com> writes:
```
import std.stdio;

 safe:

__gshared int gshared = 42;

void foo(int i = gshared)
{
     writeln(i);
}

void main()
{
     foo();
}
```

This currently works; `foo` is ` safe` and prints the value of 
`gshared`. Changing the call in main to `foo(gshared)` errors.

Should it work, and can I expect it to keep working?
Apr 08 2020
parent reply data pulverizer <data.pulverizer gmail.com> writes:
On Wednesday, 8 April 2020 at 16:53:05 UTC, Anonymouse wrote:
 ```
 import std.stdio;

  safe:

 __gshared int gshared = 42;

 void foo(int i = gshared)
 {
     writeln(i);
 }

 void main()
 {
     foo();
 }
 ```

 This currently works; `foo` is ` safe` and prints the value of 
 `gshared`. Changing the call in main to `foo(gshared)` errors.

 Should it work, and can I expect it to keep working?
According to the manual it shouldn't work at all https://dlang.org/spec/function.html#function-safety where it says Safe Functions: "Cannot access __gshared variables.", I don't know why calling as `foo()` works.
Apr 08 2020
parent reply jmh530 <john.michael.hall gmail.com> writes:
On Wednesday, 8 April 2020 at 18:50:16 UTC, data pulverizer wrote:
 On Wednesday, 8 April 2020 at 16:53:05 UTC, Anonymouse wrote:
 ```
 import std.stdio;

  safe:

 __gshared int gshared = 42;

 void foo(int i = gshared)
 {
     writeln(i);
 }

 void main()
 {
     foo();
 }
 ```

 This currently works; `foo` is ` safe` and prints the value of 
 `gshared`. Changing the call in main to `foo(gshared)` errors.

 Should it work, and can I expect it to keep working?
According to the manual it shouldn't work at all https://dlang.org/spec/function.html#function-safety where it says Safe Functions: "Cannot access __gshared variables.", I don't know why calling as `foo()` works.
You still wouldn't be able to manipulate gshared within the function. Though it may still be a problem for safe... import std.stdio; __gshared int gshared = 42; safe void foo(int i = gshared) { i++; writeln(i); } void main() { writeln(gshared); foo(); writeln(gshared); gshared++; writeln(gshared); foo(); writeln(gshared); }
Apr 08 2020
parent reply Anonymouse <zorael gmail.com> writes:
On Wednesday, 8 April 2020 at 19:22:11 UTC, jmh530 wrote:
 On Wednesday, 8 April 2020 at 18:50:16 UTC, data pulverizer 
 wrote:
 On Wednesday, 8 April 2020 at 16:53:05 UTC, Anonymouse wrote:
 ```
 import std.stdio;

  safe:

 __gshared int gshared = 42;

 void foo(int i = gshared)
 {
     writeln(i);
 }

 void main()
 {
     foo();
 }
 ```

 This currently works; `foo` is ` safe` and prints the value 
 of `gshared`. Changing the call in main to `foo(gshared)` 
 errors.

 Should it work, and can I expect it to keep working?
According to the manual it shouldn't work at all https://dlang.org/spec/function.html#function-safety where it says Safe Functions: "Cannot access __gshared variables.", I don't know why calling as `foo()` works.
You still wouldn't be able to manipulate gshared within the function. Though it may still be a problem for safe...
It works with `ref int` too. ``` __gshared int gshared = 42; void foo(ref int i = gshared) safe { ++i; } void main() { assert(gshared == 42); foo(); assert(gshared == 43); } ```
Apr 08 2020
next sibling parent reply jmh530 <john.michael.hall gmail.com> writes:
On Wednesday, 8 April 2020 at 19:29:17 UTC, Anonymouse wrote:
 [snip]

 It works with `ref int` too.


 ```
 __gshared int gshared = 42;

 void foo(ref int i = gshared)  safe
 {
     ++i;
 }
 void main()
 {
     assert(gshared == 42);
     foo();
     assert(gshared == 43);
 }
 ```
Well that definitely shouldn't happen. I would file a bug report.
Apr 08 2020
parent Anonymouse <zorael gmail.com> writes:
On Wednesday, 8 April 2020 at 19:53:03 UTC, jmh530 wrote:
 Well that definitely shouldn't happen. I would file a bug 
 report.
Filed as https://issues.dlang.org/show_bug.cgi?id=20726
Apr 08 2020
prev sibling parent data pulverizer <data.pulverizer gmail.com> writes:
On Wednesday, 8 April 2020 at 19:29:17 UTC, Anonymouse wrote:
 ```
 __gshared int gshared = 42;

 void foo(ref int i = gshared)  safe
 {
     ++i;
 }
 void main()
 {
     assert(gshared == 42);
     foo();
     assert(gshared == 43);
 }
 ```
Dude, you just broke ` safe`! Lol!
Apr 08 2020