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

context pointer inside structs constness problems #19404

Open
dlangBugzillaToGithub opened this issue Mar 6, 2018 · 1 comment
Open

context pointer inside structs constness problems #19404

dlangBugzillaToGithub opened this issue Mar 6, 2018 · 1 comment

Comments

@dlangBugzillaToGithub
Copy link

Shachar Shemesh (@Shachar) reported this on 2018-03-06T14:47:23Z

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

CC List

  • Ketmar Dark
  • Timoses

Description

The following program does not compile:
void main() {
    struct S {
        uint value;

        ~this() {
        }
    }

    const S a = S(12);
    S b = a;
}

test.d(10): Error: cannot implicitly convert expression a of type const(S) to S 

The reason is that the context pointer stored in a is const, and thus implies that the context it points to is also const. This cannot be copied to the non-const context pointer in b.

This context pointer is not, however, actually treated as const. The following code compiles and passes:

unittest {
    int i = 0;
    struct S {
        int n;
        void fun() const {
            i++;
        }
    }
    const S s;
    assert(i == 0);
    s.fun();
    assert(i == 1);
} 

Full discussion thread at https://forum.dlang.org/thread/p7lp2b$1jod$1@digitalmars.com

I would argue that the correct solution is to allow the assignment.
@dlangBugzillaToGithub
Copy link
Author

timosesu commented on 2018-07-19T08:43:00Z

Related: https://forum.dlang.org/post/rxolioihwfrljpfjfmxw@forum.dlang.org

// dmd v2.081.1
unittest {
    struct S {
        uint value;
        ~this() pure {
        }
    }
    S makeS() pure
    {
     	S s = S();
        return s;
    }
    // Error: cannot implicitly convert expression makeS() of type S to immutable(S)
    immutable S s = makeS();
}

Without the destructor compiles fine.

Perhaps a better error message could be found.

Further related issue: https://issues.dlang.org/show_bug.cgi?id=18567

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant