Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

std.getopt uses deprecated rwm operations for shared variables #19379

Open
dlangBugzillaToGithub opened this issue Feb 5, 2018 · 3 comments
Open
Labels
Arch:x86_64 Issues specific to x86_64 P3 Severity:normal

Comments

@dlangBugzillaToGithub
Copy link

Seb reported this on 2018-02-05T12:57:36Z

Transferred from https://issues.dlang.org/show_bug.cgi?id=18375

CC List

  • ZombineDev

Description

---
shared real v;
shared string s;
auto args = ["program.name", "-v=2", "-s=bar"];
---
@dlangBugzillaToGithub
Copy link
Author

greensunny12 commented on 2018-02-05T13:33:24Z

std.getopt test cases:

1) Passing and "just" triggering deprecation warnings


---
@system unittest
{
    shared real v;
    shared uint i;
    shared string[] arrString;
    shared int[] arrInt;

    auto args = ["program.name",
        "-v=2",
        "-i=10",
        "--arrString=a", "--arrString=b",
        "--arrInt=1", "--arrInt=2",
    ];
    getopt(args,
        "v", &v,
        "i", &i,
        "arrString", &arrString,
        "arrInt", &arrInt,
    );
    assert(v == 2);
    assert(i == 10);

    shared expectedArrString = ["a", "b"];
    assert(arrString == expectedArrString);
    shared expectedArrInt = [1, 2];
    assert(arrInt == expectedArrInt);
}
---

2) Completely failing

---
@system unittest
{
    shared string s;
    shared bool b;
    shared string[string] assocArrayString;
    shared double[string] assocArrayDouble;

    auto args = ["program.name",
        "-s=bar",
        "-b",
        "--assocArrayString=foo=bar,bar=1",
        "--assocArrayDouble=foo=1,bar=2",
    ];
    getopt(args,
        "s", &s,
        "b", &b,
        "assocArrayString", &assocArrayString,
        "assocArrayDouble", &assocArrayDouble,
    );

    assert(s == "bar");
    assert(b == 1);

    shared expectedAssocArrayString = ["foo": "bar", "bar": "1"];
    assert(assocArrayString == expectedAssocArrayString);
    shared expectedAssocArrayDouble = ["foo":1.0, "bar": 2];
    assert(assocArrayDouble == expectedAssocArrayDouble);
}
---

@dlangBugzillaToGithub
Copy link
Author

petar.p.kirov commented on 2018-02-05T17:23:25Z

All of these examples should *never* compile. The whole point of `shared` is to statically disallow accidental access shared mutable state. > 90% of all functions in phobos have no business touching `shared`/`__gshared` variables, especially `std.getopt`.

`std.getopt` can be safely used to set:
* function local variables
* (static) thread-local variables
* immutable global variables from shared static constructors (not sure if this currently works, but it should be ok from memory model perspective)

I consider everything else to potentially trigger undefined behavior.

@dlangBugzillaToGithub
Copy link
Author

greensunny12 commented on 2018-02-05T17:56:47Z

>  immutable global variables from shared static constructors (not sure if this currently works, but it should be ok from memory model perspective)

No this doesn't work at the moment.


For future readers: it has been used by people before (https://github.com/vibe-d/vibe.d/pull/2060).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Arch:x86_64 Issues specific to x86_64 P3 Severity:normal
Projects
None yet
Development

No branches or pull requests

1 participant